Skip to content
Open
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
39 changes: 28 additions & 11 deletions harper-core/src/lexing/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ fn lex_hostport(source: &[char]) -> Option<usize> {
let hostname_end = lex_hostname(source)?;

if source.get(hostname_end) == Some(&':') {
Some(
source
.iter()
.enumerate()
.find(|(_, c)| !{
let c = **c;
c.is_ascii_digit()
})
.map(|(i, _)| i)
.unwrap_or(source.len()),
)
// Scan the port number, which begins right after the ':'.
let port_start = hostname_end + 1;
let port_len = source[port_start..]
.iter()
.take_while(|c| c.is_ascii_digit())
.count();

if port_len == 0 {
// A ':' that isn't followed by a port; don't consume it.
Some(hostname_end)
} else {
Some(port_start + port_len)
}
} else {
Some(hostname_end)
}
Expand Down Expand Up @@ -239,6 +241,21 @@ mod tests {
assert_consumes_full("https://github.com/nodesource/distributions#debinstall")
}

#[test]
fn consumes_port() {
assert_consumes_full("http://localhost:8080")
}

#[test]
fn consumes_port_with_path() {
assert_consumes_full("http://localhost:8080/api/v1")
}

#[test]
fn consumes_ip_with_port() {
assert_consumes_full("http://127.0.0.1:3000/health")
}

/// Tests that the URL parser will not throw a panic under some random
/// situations.
#[test]
Expand Down
Loading