Skip to content
Merged
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
11 changes: 10 additions & 1 deletion src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ enum ErrorKind {
SchemeMissing,
AuthorityMissing,
PathAndQueryMissing,
PathDoesNotStartWithSlash,
TooLong,
Empty,
SchemeTooLong,
Expand Down Expand Up @@ -872,10 +873,17 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
data: unsafe { ByteStr::from_utf8_unchecked(authority) },
};

// When absolute, path is coered to / if empty.
let path_and_query = if s.is_empty() {
PathAndQuery::slash()
} else {
PathAndQuery::from_shared(s)?
};

Ok(Uri {
scheme: scheme.into(),
authority,
path_and_query: PathAndQuery::from_shared(s)?,
path_and_query,
})
}

Expand Down Expand Up @@ -1070,6 +1078,7 @@ impl InvalidUri {
ErrorKind::SchemeMissing => "scheme missing",
ErrorKind::AuthorityMissing => "authority missing",
ErrorKind::PathAndQueryMissing => "path missing",
ErrorKind::PathDoesNotStartWithSlash => "path does not start with slash",
ErrorKind::TooLong => "uri too long",
ErrorKind::Empty => "empty string",
ErrorKind::SchemeTooLong => "scheme too long",
Expand Down
18 changes: 18 additions & 0 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ const fn scan_path_and_query(bytes: &[u8]) -> Result<Scanned, ErrorKind> {

let mut is_maybe_not_utf8 = false;

if bytes.is_empty() {
return Err(ErrorKind::Empty);
}

if !matches!(bytes[0], b'/' | b'?' | b'#') {
return Err(ErrorKind::PathDoesNotStartWithSlash);
}

while i < bytes.len() {
// See https://url.spec.whatwg.org/#path-state
match bytes[i] {
Expand Down Expand Up @@ -621,6 +629,16 @@ mod tests {
PathAndQuery::try_from(&[b'/', b'a', b'?', 0xFF][..]).expect_err("reject invalid utf8");
}

#[test]
fn rejects_empty_string() {
PathAndQuery::try_from("").expect_err("reject empty str");
}

#[test]
fn requires_starting_with_slash() {
PathAndQuery::try_from("sneaky").expect_err("reject missing slash");
}

#[test]
fn json_is_fine() {
assert_eq!(
Expand Down
Loading