Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,26 @@ where
})
}

// Like URI_CHARS, but bytes that have no special meaning in an authority
// map to PLAIN.
const PLAIN: u8 = 1;

const fn build_authority_map() -> [u8; 256] {
let mut t = [0; 256];
let mut i = 0;
while i < 256 {
t[i] = match URI_CHARS[i] {
0 => 0,
ch @ (b'/' | b'?' | b'#' | b':' | b'[' | b']' | b'@') => ch,
_ => PLAIN,
};
i += 1;
}
t
}

const AUTHORITY_MAP: [u8; 256] = build_authority_map();

/// Shared validation logic for authority bytes.
/// Returns the end position of valid authority bytes, or an error.
const fn validate_authority_bytes(s: &[u8]) -> Result<usize, AuthorityError> {
Expand All @@ -491,7 +511,13 @@ const fn validate_authority_bytes(s: &[u8]) -> Result<usize, AuthorityError> {
// code point.
while i < s.len() {
let b = s[i];
let ch = URI_CHARS[b as usize];
let ch = AUTHORITY_MAP[b as usize];

// Most bytes are plain host or userinfo characters.
if ch == PLAIN {
i += 1;
continue;
}

if ch == b'/' || ch == b'?' || ch == b'#' {
end = i;
Expand Down
9 changes: 4 additions & 5 deletions src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use crate::byte_str::ByteStr;
use std::convert::TryFrom;

use bytes::Bytes;
use bytes::{Buf, Bytes};

use std::error::Error;
use std::fmt;
Expand Down Expand Up @@ -825,16 +825,15 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
let scheme = match Scheme2::parse(&s[..])? {
Scheme2::None => Scheme2::None,
Scheme2::Standard(p) => {
// TODO: use truncate
let _ = s.split_to(p.len() + 3);
s.advance(p.len() + 3);
Scheme2::Standard(p)
}
Scheme2::Other(n) => {
// Grab the protocol
let mut scheme = s.split_to(n + 3);

// Strip ://, TODO: truncate
let _ = scheme.split_off(n);
// Strip ://
scheme.truncate(n);

// Allocate the ByteStr
let val = unsafe { ByteStr::from_utf8_unchecked(scheme) };
Expand Down
Loading