diff --git a/src/uri/authority.rs b/src/uri/authority.rs index 79bcbff9..c41515a6 100644 --- a/src/uri/authority.rs +++ b/src/uri/authority.rs @@ -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 { @@ -491,7 +511,13 @@ const fn validate_authority_bytes(s: &[u8]) -> Result { // 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; diff --git a/src/uri/mod.rs b/src/uri/mod.rs index 0f0b0156..c37f66b2 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -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; @@ -825,16 +825,15 @@ fn parse_full(mut s: Bytes) -> Result { 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) };