Skip to content

Commit a4af4a7

Browse files
author
lishuo
committed
fix typo. skip data tests for idna if feature not enabled. opt out unicode_serialziation for Origin if idna not enabled
1 parent fc7dfa5 commit a4af4a7

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

url/src/host.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ impl Host<String> {
8383
}
8484
let domain = percent_decode(input.as_bytes()).decode_utf8_lossy();
8585

86-
#[cfg(feature = "idna")]
87-
let domain = idna::domain_to_ascii(&domain)?;
88-
#[cfg(not(feature = "idna"))]
89-
let domain = domain.to_string();
86+
let domain = Self::domain_to_ascii(&domain)?;
9087

9188
if domain.is_empty() {
9289
return Err(ParseError::EmptyHost);
@@ -161,6 +158,24 @@ impl Host<String> {
161158
))
162159
}
163160
}
161+
162+
/// convert domain with idna
163+
#[cfg(feature = "idna")]
164+
fn domain_to_ascii(domain: &str) -> Result<String, ParseError> {
165+
idna::domain_to_ascii(&domain).map_err(Into::into)
166+
}
167+
168+
/// checks domain is ascii
169+
#[cfg(not(feature = "idna"))]
170+
fn domain_to_ascii(domain: &str) -> Result<String, ParseError> {
171+
// without idna feature, we can't verify that xn-- domains correctness
172+
let domain = domain.to_lowercase();
173+
if domain.is_ascii() && !domain.starts_with("xn--") {
174+
Ok(domain)
175+
} else {
176+
Err(ParseError::InvalidDomainCharacter)
177+
}
178+
}
164179
}
165180

166181
impl<S: AsRef<str>> fmt::Display for Host<S> {

url/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ url = { version = "2", features = ["serde"] }
124124
You can opt out [idna](https://en.wikipedia.org/wiki/Internationalized_domain_name) support
125125
to reduce final binary size.
126126
127-
```tomo
127+
```toml
128128
url = { version = "2", default-features = false }
129129
```
130130

url/src/origin.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,14 @@ impl Origin {
8686
}
8787

8888
/// <https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin>
89+
#[cfg(feature = "idna")]
8990
pub fn unicode_serialization(&self) -> String {
9091
match *self {
9192
Origin::Opaque(_) => "null".to_owned(),
9293
Origin::Tuple(ref scheme, ref host, port) => {
9394
let host = match *host {
9495
Host::Domain(ref domain) => {
95-
#[cfg(feature = "idna")]
9696
let (domain, _errors) = idna::domain_to_unicode(domain);
97-
#[cfg(not(feature = "idna"))]
98-
let domain = domain.clone();
99-
10097
Host::Domain(domain)
10198
}
10299
_ => host.clone(),

url/tests/data.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ use url::{quirks, Url};
1616

1717
#[test]
1818
fn urltestdata() {
19+
#[cfg(not(feature = "idna"))]
20+
let idna_skip_inputs = [
21+
"http://www.foo。bar.com",
22+
"http://Go.com",
23+
"http://你好你好",
24+
"https://faß.ExAmPlE/",
25+
"http://0Xc0.0250.01",
26+
"ftp://%e2%98%83",
27+
"https://%e2%98%83",
28+
"file://a\u{ad}b/p",
29+
"file://a%C2%ADb/p",
30+
"http://GOO\u{200b}\u{2060}\u{feff}goo.com",
31+
];
32+
1933
// Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
2034
let mut json = Value::from_str(include_str!("urltestdata.json"))
2135
.expect("JSON parse error in urltestdata.json");
@@ -30,6 +44,11 @@ fn urltestdata() {
3044
let input = entry.take_string("input");
3145
let failure = entry.take_key("failure").is_some();
3246

47+
#[cfg(not(feature = "idna"))]
48+
if idna_skip_inputs.contains(&input.as_str()) {
49+
continue;
50+
}
51+
3352
let base = match Url::parse(&base) {
3453
Ok(base) => base,
3554
Err(_) if failure => continue,
@@ -106,6 +125,12 @@ fn setters_tests() {
106125
let mut tests = json.take_key(attr).unwrap();
107126
for mut test in tests.as_array_mut().unwrap().drain(..) {
108127
let comment = test.take_key("comment").map(|s| s.string());
128+
#[cfg(not(feature = "idna"))]
129+
if let Some(comment) = comment.as_ref() {
130+
if comment.starts_with("IDNA Nontransitional_Processing") {
131+
continue;
132+
}
133+
}
109134
let href = test.take_string("href");
110135
let new_value = test.take_string("new_value");
111136
let name = format!("{:?}.{} = {:?}", href, attr, new_value);

url/tests/unit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ fn host_serialization() {
296296
);
297297
}
298298

299+
#[cfg(feature = "idna")]
299300
#[test]
300301
fn test_idna() {
301302
assert!("http://goșu.ro".parse::<Url>().is_ok());
@@ -531,6 +532,7 @@ fn test_origin_opaque() {
531532
assert!(!&Url::parse("blob:malformed//").unwrap().origin().is_tuple())
532533
}
533534

535+
#[cfg(feature = "idna")]
534536
#[test]
535537
fn test_origin_unicode_serialization() {
536538
let data = [

0 commit comments

Comments
 (0)