Skip to content

Commit

Permalink
Merge pull request #22 from timothee-haudebourg:timothee-haudebourg/i…
Browse files Browse the repository at this point in the history
…ssue21

Fix incorrect extraction of URI components.
  • Loading branch information
timothee-haudebourg authored Feb 20, 2024
2 parents 2e23b75 + d545415 commit 7fb32b3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ignore-grammars = []
pct-str = "2.0"
smallvec = "1.2"
thiserror = "1.0.40"
static-regular-grammar = "1.1"
static-regular-grammar = "1.1.1"
serde = { version = "1.0", optional = true }
hashbrown = { version = "0.14.0", optional = true }

Expand Down
22 changes: 19 additions & 3 deletions src/common/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn scheme_authority_or_path(bytes: &[u8], mut i: usize) -> (SchemeAuthorityO
State::SchemeOrPath => match c {
b':' => break SchemeAuthorityOrPath::Scheme,
b'?' | b'#' => break SchemeAuthorityOrPath::Path,
b'/' => State::Path,
_ => State::SchemeOrPath,
},
State::Path => match c {
Expand All @@ -74,8 +75,7 @@ pub fn scheme_authority_or_path(bytes: &[u8], mut i: usize) -> (SchemeAuthorityO
_ => State::Path,
},
State::Authority => match c {
b'/' => break SchemeAuthorityOrPath::Authority,
b'?' | b'#' => break SchemeAuthorityOrPath::Authority,
b'/' | b'?' | b'#' => break SchemeAuthorityOrPath::Authority,
_ => State::Authority,
},
};
Expand Down Expand Up @@ -199,6 +199,7 @@ pub enum UserInfoOrHost {
pub fn user_info_or_host(bytes: &[u8], mut i: usize) -> (UserInfoOrHost, usize) {
while i < bytes.len() {
match bytes[i] {
b'[' => return (UserInfoOrHost::Host, bytes.len()),
b'@' => return (UserInfoOrHost::UserInfo, i),
b':' => {
// end of the host, or still in the user-info.
Expand Down Expand Up @@ -238,6 +239,14 @@ pub fn find_user_info(bytes: &[u8], mut i: usize) -> Option<Range<usize>> {
}

pub fn host(bytes: &[u8], mut i: usize) -> usize {
if !bytes.is_empty() && bytes[0] == b'[' {
// IP-literal.
i += 1;
while i < bytes.len() && bytes[i] != b']' {
i += 1
}
}

while i < bytes.len() && bytes[i] != b':' {
i += 1
}
Expand Down Expand Up @@ -577,7 +586,12 @@ mod tests {

#[test]
fn host() {
let vectors = [("example.org:12", 11), ("example.org", 11), ("", 0)];
let vectors = [
("example.org:12", 11),
("example.org", 11),
("[::]", 4),
("", 0),
];

for (input, expected) in vectors {
let output = super::host(input.as_bytes(), 0);
Expand All @@ -593,6 +607,7 @@ mod tests {
("example.org", "example.org"),
("user:[email protected]:12", "example.org"),
("user:[email protected]", "example.org"),
("[::]", "[::]"),
];

for (input, expected) in vectors {
Expand Down Expand Up @@ -650,6 +665,7 @@ mod tests {
("foo/bar?query", "foo/bar"),
("foo/bar#fragment", "foo/bar"),
("foo/bar", "foo/bar"),
("a/:", "a/:"),
];

for (input, expected) in vectors {
Expand Down
2 changes: 1 addition & 1 deletion src/iri/path/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Eq for Segment {}

impl PartialOrd for Segment {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.as_pct_str().partial_cmp(other.as_pct_str())
Some(self.cmp(other))
}
}

Expand Down
1 change: 0 additions & 1 deletion src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod userinfo;

use crate::common::AuthorityImpl;

pub use super::{InvalidScheme, Scheme, SchemeBuf};
pub use host::*;
pub use port::*;
pub use userinfo::*;
Expand Down
2 changes: 1 addition & 1 deletion src/uri/path/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Eq for Segment {}

impl PartialOrd for Segment {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.as_pct_str().partial_cmp(other.as_pct_str())
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit 7fb32b3

Please sign in to comment.