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
92 changes: 89 additions & 3 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,95 @@ impl Authority {
/// let authority = Authority::from_static("example.com");
/// assert_eq!(authority.host(), "example.com");
/// ```
pub fn from_static(src: &'static str) -> Self {
Authority::from_shared(Bytes::from_static(src.as_bytes()))
.expect("static str is not valid authority")
#[inline]
#[allow(unconditional_panic)]
pub const fn from_static(src: &'static str) -> Self {
let bytes = src.as_bytes();

if bytes.len() == 0 {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

let mut colon_cnt: u32 = 0;
let mut start_bracket: bool = false;
let mut end_bracket: bool = false;
let mut has_percent: bool = false;
let mut at_sign_pos: usize = bytes.len();
const MAX_COLONS: u32 = 8;

let mut i: usize = 0;
while i < bytes.len() {
let b = bytes[i];

if b == b'/' || b == b'?' || b == b'#' {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

let ch = URI_CHARS[b as usize];
if ch == 0 {
if b == b'%' {
has_percent = true;
} else {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}
} else if ch == b':' {
if colon_cnt >= MAX_COLONS {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}
colon_cnt += 1;
} else if ch == b'[' {
if has_percent || start_bracket {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}
start_bracket = true;
} else if ch == b']' {
if !start_bracket || end_bracket {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}
end_bracket = true;

// Forget IPv6 internals
colon_cnt = 0;
has_percent = false;
} else if ch == b'@' {
at_sign_pos = i;

colon_cnt = 0;
has_percent = false;
}

i += 1;
}

if (start_bracket && !end_bracket) || (!start_bracket && end_bracket) {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

if colon_cnt > 1 {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

if bytes.len() > 0 && at_sign_pos == bytes.len() - 1 {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

if has_percent {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

Authority {
data: ByteStr::from_static(src),
}
}

/// Attempt to convert a `Bytes` buffer to a `Authority`.
Expand Down
67 changes: 64 additions & 3 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,71 @@ impl PathAndQuery {
/// assert_eq!(v.query(), Some("world"));
/// ```
#[inline]
pub fn from_static(src: &'static str) -> Self {
let src = Bytes::from_static(src.as_bytes());
#[allow(unconditional_panic)]
pub const fn from_static(src: &'static str) -> Self {
let bytes = src.as_bytes();
let mut query: u16 = NONE;

// path ...
let mut i: usize = 0;
while i < bytes.len() {
let b = bytes[i];
if b == b'?' {
query = i as u16;
i += 1;
break;
} else if b == b'#' {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
} else {
let allowed = b == 0x21
|| (b >= 0x24 && b <= 0x3B)
|| b == 0x3D
|| (b >= 0x40 && b <= 0x5F)
|| (b >= 0x61 && b <= 0x7A)
|| b == 0x7C
|| b == 0x7E
|| b == b'"'
|| b == b'{'
|| b == b'}'
|| (b >= 0x7F);

if !allowed {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}
}
i += 1;
}

PathAndQuery::from_shared(src).unwrap()
// query ...
if query != NONE {
while i < bytes.len() {
let b = bytes[i];
if b == b'#' {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

let allowed = b == 0x21
|| (b >= 0x24 && b <= 0x3B)
|| b == 0x3D
|| (b >= 0x3F && b <= 0x7E)
|| (b >= 0x7F);

if !allowed {
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
([] as [u8; 0])[0];
}

i += 1;
}
}

PathAndQuery {
data: ByteStr::from_static(src),
query,
}
}

/// Attempt to convert a `Bytes` buffer to a `PathAndQuery`.
Expand Down