Skip to content

Commit

Permalink
Merge pull request #43 from mihaigalos/fix/ip/regex-lazy-matching
Browse files Browse the repository at this point in the history
fix: IP - Regex lazy matchin
  • Loading branch information
mihaigalos authored May 12, 2024
2 parents 4c4f6bc + a1d98bd commit 99e731f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "url-parse"
version = "1.0.7"
version = "1.0.8"
description = "🔗 A Rust library for parsing URLs."

categories = ["parser-implementations", "web-programming", "encoding"]
Expand Down
15 changes: 14 additions & 1 deletion src/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Parser {

/// Mixes out the ip v4 into a Domain structure.
fn domain_ipv4<'a>(&self, input: &'a str) -> Option<Domain<'a>> {
let re = Regex::new(r"([0-9]+?)\.([0-9]+?)\.([0-9]+?)\.([0-9]+?)").unwrap();
let re = Regex::new(r"([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap();
let caps = re.captures(input);
caps.as_ref()?;
return Some(Domain {
Expand Down Expand Up @@ -95,6 +95,19 @@ mod tests {

#[test]
fn test_domain_ipv4_when_typical() {
let input = "https://192.168.178.242/dir";
let expected = Domain {
subdomain: None,
domain: Some("192.168.178.242"),
top_level_domain: None,
};
let result = Parser::new(None).domain_ipv4(input).unwrap();

assert_eq!(result, expected);
}

#[test]
fn test_domain_ipv4_when_port() {
let input = "https://1.2.3.4:443/blog/article/search?docid=720&hl=en#dayone";
let expected = Domain {
subdomain: None,
Expand Down
22 changes: 22 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,26 @@ mod tests {
}
);
}

#[test]
fn test_parse_works_when_ip() {
let input = "ftp://192.168.178.242/dir";
let result = Parser::new(None).parse(input).unwrap();
assert_eq!(
result,
Url {
scheme: Some("ftp".to_string()),
user_pass: (None, None),
subdomain: None,
domain: Some("192.168.178.242".to_string()),
top_level_domain: None,
port: Some(21),
path: Some(vec![
"dir".to_string(),
]),
query: None,
anchor: None,
}
);
}
}
2 changes: 1 addition & 1 deletion src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Url {
}

if self.path.is_some() {
for (_, segment) in self.path_segments().unwrap().iter().enumerate() {
for segment in self.path_segments().unwrap().iter() {
result += "/";
result += segment;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Utils {
let mut result = parser
.scheme(input)
.map(|s| s.0.to_string() + &<SchemeSeparator as Into<String>>::into(s.1))
.unwrap_or_else(|| "".to_string());
.unwrap_or_default();

let subpath = Self::trim_leading_slash(subpath);
let (similarity, input_splits) = Utils::compute_similarity(parser, input, subpath);
Expand Down

0 comments on commit 99e731f

Please sign in to comment.