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

Re-align to specification #718

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions url/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl Host<String> {
| '\\'
| ']'
| '^'
| '|'
)
};

Expand Down
10 changes: 8 additions & 2 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ impl<'a> Parser<'a> {
scheme_end: u32,
scheme_type: SchemeType,
) -> ParseResult<(u32, HostInternal, Option<u16>, Input<'i>)> {
let (host, remaining) = Parser::parse_host(input, scheme_type)?;
let (host, remaining) = Parser::parse_host(input, scheme_type, false)?;
write!(&mut self.serialization, "{}", host).unwrap();
let host_end = to_u32(self.serialization.len())?;
if let Host::Domain(h) = &host {
Expand Down Expand Up @@ -983,6 +983,7 @@ impl<'a> Parser<'a> {
pub fn parse_host(
mut input: Input<'_>,
scheme_type: SchemeType,
error_on_port: bool,
) -> ParseResult<(Host<String>, Input<'_>)> {
if scheme_type.is_file() {
return Parser::get_file_host(input);
Expand All @@ -996,7 +997,12 @@ impl<'a> Parser<'a> {
let mut bytes = 0;
for c in input_str.chars() {
match c {
':' if !inside_square_brackets => break,
':' if !inside_square_brackets => {
if error_on_port {
return Err(ParseError::InvalidPort);
}
break;
}
'\\' if scheme_type.is_special() => break,
'/' | '?' | '#' => break,
'\t' | '\n' | '\r' => {
Expand Down
4 changes: 2 additions & 2 deletions url/src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
return Ok(());
}

if let Ok((h, remaining)) = Parser::parse_host(input, scheme_type) {
if let Ok((h, remaining)) = Parser::parse_host(input, scheme_type, false) {
host = h;
opt_port = if let Some(remaining) = remaining.split_prefix(':') {
if remaining.is_empty() {
Expand Down Expand Up @@ -171,7 +171,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
return Ok(());
}

if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type, true) {
if let Host::Domain(h) = &host {
if h.is_empty() {
// Empty host on special not file url
Expand Down
51 changes: 32 additions & 19 deletions url/tests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use url::{quirks, Url};

#[test]
fn urltestdata() {
// Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
// Copied form https://github.com/web-platform-tests/wpt/blob/master/url/
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
let mut json = Value::from_str(include_str!("urltestdata.json"))
.expect("JSON parse error in urltestdata.json");

Expand All @@ -26,25 +26,33 @@ fn urltestdata() {
continue; // ignore comments
}

let base = entry.take_string("base");
let maybe_base = entry
.take_key("base")
.expect("missing base key")
.maybe_string();
let input = entry.take_string("input");
let failure = entry.take_key("failure").is_some();

let base = match Url::parse(&base) {
Ok(base) => base,
Err(_) if failure => continue,
Err(message) => {
eprint_failure(
format!(" failed: error parsing base {:?}: {}", base, message),
&format!("parse base for {:?}", input),
None,
);
passed = false;
continue;
}
let res = if let Some(base) = maybe_base {
let base = match Url::parse(&base) {
Ok(base) => base,
Err(_) if failure => continue,
Err(message) => {
eprint_failure(
format!(" failed: error parsing base {:?}: {}", base, message),
&format!("parse base for {:?}", input),
None,
);
passed = false;
continue;
}
};
base.join(&input)
} else {
Url::parse(&input)
};

let url = match (base.join(&input), failure) {
let url = match (res, failure) {
(Ok(url), false) => url,
(Err(_), true) => continue,
(Err(message), false) => {
Expand Down Expand Up @@ -153,6 +161,7 @@ fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool {
trait JsonExt {
fn take_key(&mut self, key: &str) -> Option<Value>;
fn string(self) -> String;
fn maybe_string(self) -> Option<String>;
fn take_string(&mut self, key: &str) -> String;
}

Expand All @@ -162,10 +171,14 @@ impl JsonExt for Value {
}

fn string(self) -> String {
if let Value::String(s) = self {
s
} else {
panic!("Not a Value::String")
self.maybe_string().expect("")
}

fn maybe_string(self) -> Option<String> {
match self {
Value::String(s) => Some(s),
Value::Null => None,
_ => panic!("Not a Value::String or Value::Null"),
}
}

Expand Down
Loading