Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of long host with no scheme #351

Merged
merged 4 commits into from Nov 26, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/uri/scheme.rs
Expand Up @@ -336,10 +336,6 @@ impl Scheme2<usize> {
for i in 0..s.len() {
let b = s[i];

if i == MAX_SCHEME_LEN {
return Err(ErrorKind::SchemeTooLong.into());
}

match SCHEME_CHARS[b as usize] {
b':' => {
// Not enough data remaining
Expand All @@ -352,6 +348,10 @@ impl Scheme2<usize> {
break;
}

if i > MAX_SCHEME_LEN {
return Err(ErrorKind::SchemeTooLong.into());
}

// Return scheme
return Ok(Scheme2::Other(i));
}
Expand Down
38 changes: 37 additions & 1 deletion src/uri/tests.rs
Expand Up @@ -231,6 +231,30 @@ test_parse! {
port_part = None,
}

test_parse! {
test_uri_parse_long_host_with_no_scheme,
"thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost",
[],

scheme_part = None,
authority_part = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost"),
path = "",
query = None,
port_part = None,
}

test_parse! {
test_uri_parse_long_host_with_port_and_no_scheme,
"thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234",
[],

scheme_part = None,
authority_part = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234"),
path = "",
query = None,
port_part = Port::from_str("1234").ok(),
}

test_parse! {
test_userinfo1,
"http://a:b@127.0.0.1:1234/",
Expand Down Expand Up @@ -430,7 +454,7 @@ fn test_max_uri_len() {
}

#[test]
fn test_long_scheme() {
fn test_overflowing_scheme() {
let mut uri = vec![];
uri.extend(vec![b'a'; 256]);
uri.extend(b"://localhost/");
Expand All @@ -441,6 +465,18 @@ fn test_long_scheme() {
assert_eq!(res.unwrap_err().0, ErrorKind::SchemeTooLong);
}

#[test]
fn test_max_length_scheme() {
let mut uri = vec![];
uri.extend(vec![b'a'; 64]);
uri.extend(b"://localhost/");

let uri = String::from_utf8(uri).unwrap();
let uri: Uri = uri.parse().unwrap();

assert_eq!(uri.scheme_str().unwrap().len(), 64);
}

#[test]
fn test_uri_to_path_and_query() {
let cases = vec![
Expand Down