Skip to content

Commit 2845cb8

Browse files
authored
Merge pull request #8 from ail-project/fix-bug-domain-with-ip
Fix bug domain with ip
2 parents 94b48f6 + 2df1cab commit 2845cb8

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

faup/src/grammar.pest

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ username = ${ (!(":" | "@" | WHITE_SPACE) ~ ANY)+ }
55
password = ${ (!("@" | WHITE_SPACE) ~ ANY)+ }
66
userinfo = ${ username ~ (":" ~ password)? ~ "@" }
77

8-
host = ${ ipv4 | ipv6 | hostname }
8+
// this rule is used to check if the hostname is actually
9+
// a valid ipv4 address as hostname rule matches any ipv4
10+
ipv4 = ${ SOI ~ (ASCII_DIGIT{1, 3} ~ "."){3} ~ ASCII_DIGIT{1, 3} ~ EOI }
11+
host = ${ ipv6 | hostname }
912
domain_part = ${ (!(":" | "?" | "/" | "#" | "." | WHITE_SPACE) ~ ANY)+ }
1013
tld = ${ (!(":" | "?" | "/" | "#" | WHITE_SPACE) ~ ANY)+ }
1114
hostname = ${ (((domain_part ~ ".")+ ~ tld) | domain_part) }
1215

13-
ipv4 = ${ (ASCII_DIGIT{1, 3} ~ "."){3} ~ ASCII_DIGIT{1, 3} }
1416
ipv6 = ${ "[" ~ (ASCII_HEX_DIGIT{,4} ~ ":"){2, 7} ~ ASCII_HEX_DIGIT{,4} ~ "]" }
1517

1618
encoded_char = ${ "%" ~ ASCII_DIGIT{2} }

faup/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,18 @@ impl<'url> Url<'url> {
597597
let host_pair = p.into_inner().next().unwrap();
598598
match host_pair.as_rule() {
599599
Rule::hostname => {
600-
host = Some(Host::Hostname(Hostname::from_str(host_pair.as_str())))
601-
}
602-
Rule::ipv4 => {
603-
host = Some(
604-
Ipv4Addr::from_str(host_pair.as_str())
605-
.map(IpAddr::from)
606-
.map(Host::Ip)
607-
.map_err(|_| Error::InvalidIPv4)?,
608-
);
600+
if let Ok(ipv4) =
601+
UrlParser::parse(Rule::ipv4, host_pair.as_str()).map(|p| p.as_str())
602+
{
603+
host = Some(
604+
Ipv4Addr::from_str(ipv4)
605+
.map(IpAddr::from)
606+
.map(Host::Ip)
607+
.map_err(|_| Error::InvalidIPv4)?,
608+
);
609+
} else {
610+
host = Some(Host::Hostname(Hostname::from_str(host_pair.as_str())))
611+
}
609612
}
610613

611614
Rule::ipv6 => {
@@ -984,6 +987,7 @@ mod tests {
984987
"http://full.custom-tld.test.b32.i2p",
985988
"https://alex:adore-la-quiche@avec-des-œufs.be#et-des-lardons",
986989
"https://%40lex:adore:la:quiche@%61vec-des-œufs.be/../../..some/directory/traversal/../#et-des-lardons",
990+
"https://44.129.205.92.host.secureserver.net",
987991
];
988992

989993
for url in test_urls {

0 commit comments

Comments
 (0)