Skip to content

Commit 6c76f2e

Browse files
author
bors-servo
authored
Auto merge of #517 - servo:2.0, r=nox
2.0 <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/517) <!-- Reviewable:end -->
2 parents 695351b + 50c3efe commit 6c76f2e

34 files changed

+1627
-2117
lines changed

.travis.yml

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
language: rust
2+
script: cargo test --all-features --all
23

34
jobs:
45
include:
5-
- rust: 1.17.0
6-
install:
7-
# --precise requires Cargo.lock to already exist
8-
- cargo update
9-
# getopts is only used in tests. Its versions 0.2.16+ don’t build on 1.17.0
10-
- cargo update -p getopts --precise 0.2.15
11-
12-
- cargo update -p unicode-normalization --precise 0.1.5
13-
14-
# data-url uses pub(crate) which is unstable in 1.17
15-
script: cargo test --all-features -p url -p idna -p percent-encoding -p url_serde
16-
6+
- rust: 1.30.0
177
- rust: stable
18-
script: cargo test --all-features --all
19-
208
- rust: beta
21-
script: cargo test --all-features --all
22-
239
- rust: nightly
24-
script: cargo test --all-features --all
25-
2610
- rust: nightly
2711
env: TARGET=WASM32 # For job list UI
2812
install: rustup target add wasm32-unknown-unknown

Cargo.toml

+5-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name = "url"
44
# When updating version, also modify html_root_url in the lib.rs
5-
version = "1.7.2"
5+
version = "2.0.0"
66
authors = ["The rust-url developers"]
77

88
description = "URL library for Rust, based on the WHATWG URL Standard"
@@ -18,7 +18,7 @@ travis-ci = { repository = "servo/rust-url" }
1818
appveyor = { repository = "Manishearth/rust-url" }
1919

2020
[workspace]
21-
members = [".", "idna", "percent_encoding", "url_serde", "data-url"]
21+
members = [".", "idna", "percent_encoding", "data-url"]
2222

2323
[[test]]
2424
name = "unit"
@@ -32,27 +32,16 @@ test = false
3232

3333
[dev-dependencies]
3434
rustc-test = "0.3"
35-
rustc-serialize = "0.3"
36-
serde_json = ">=0.6.1, <0.9"
35+
serde_json = "1.0"
3736

3837
bencher = "0.1"
3938

40-
[features]
41-
query_encoding = ["encoding"]
42-
heap_size = ["heapsize"]
43-
4439
[dependencies]
45-
encoding = {version = "0.2", optional = true}
46-
heapsize = {version = ">=0.4.1, <0.5", optional = true}
47-
idna = { version = "0.1.0", path = "./idna" }
40+
idna = { version = "0.2.0", path = "./idna" }
4841
matches = "0.1"
4942
percent-encoding = { version = "1.0.0", path = "./percent_encoding" }
50-
rustc-serialize = {version = "0.3", optional = true}
51-
serde = {version = ">=0.6.1, <0.9", optional = true}
43+
serde = {version = "1.0", optional = true}
5244

5345
[[bench]]
5446
name = "parse_url"
5547
harness = false
56-
57-
[package.metadata.docs.rs]
58-
features = ["query_encoding"]

data-url/src/forgiving_base64.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl From<DecodeError<Impossible>> for InvalidBase64 {
2929
fn from(e: DecodeError<Impossible>) -> Self {
3030
match e {
3131
DecodeError::InvalidBase64(e) => e,
32-
DecodeError::WriteError(e) => match e {}
32+
DecodeError::WriteError(e) => match e {},
3333
}
3434
}
3535
}
@@ -46,14 +46,20 @@ pub fn decode_to_vec(input: &[u8]) -> Result<Vec<u8>, InvalidBase64> {
4646
}
4747

4848
/// <https://infra.spec.whatwg.org/#forgiving-base64-decode>
49-
pub struct Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
49+
pub struct Decoder<F, E>
50+
where
51+
F: FnMut(&[u8]) -> Result<(), E>,
52+
{
5053
write_bytes: F,
5154
bit_buffer: u32,
5255
buffer_bit_length: u8,
5356
padding_symbols: u8,
5457
}
5558

56-
impl<F, E> Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
59+
impl<F, E> Decoder<F, E>
60+
where
61+
F: FnMut(&[u8]) -> Result<(), E>,
62+
{
5763
pub fn new(write_bytes: F) -> Self {
5864
Self {
5965
write_bytes,
@@ -72,12 +78,12 @@ impl<F, E> Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
7278

7379
// Remove ASCII whitespace
7480
if matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0C') {
75-
continue
81+
continue;
7682
}
7783

7884
if byte == b'=' {
7985
self.padding_symbols = self.padding_symbols.saturating_add(1);
80-
continue
86+
continue;
8187
}
8288

8389
Err(InvalidBase64Details::UnexpectedSymbol(byte))?
@@ -115,39 +121,30 @@ impl<F, E> Decoder<F, E> where F: FnMut(&[u8]) -> Result<(), E> {
115121
(12, 2) | (12, 0) => {
116122
// A multiple of four of alphabet symbols, followed by two more symbols,
117123
// optionally followed by two padding characters (which make a total multiple of four).
118-
let byte_buffer = [
119-
(self.bit_buffer >> 4) as u8,
120-
];
124+
let byte_buffer = [(self.bit_buffer >> 4) as u8];
121125
(self.write_bytes)(&byte_buffer).map_err(DecodeError::WriteError)?;
122126
}
123127
(18, 1) | (18, 0) => {
124128
// A multiple of four of alphabet symbols, followed by three more symbols,
125129
// optionally followed by one padding character (which make a total multiple of four).
126-
let byte_buffer = [
127-
(self.bit_buffer >> 10) as u8,
128-
(self.bit_buffer >> 2) as u8,
129-
];
130+
let byte_buffer = [(self.bit_buffer >> 10) as u8, (self.bit_buffer >> 2) as u8];
130131
(self.write_bytes)(&byte_buffer).map_err(DecodeError::WriteError)?;
131132
}
132-
(6, _) => {
133-
Err(InvalidBase64Details::LoneAlphabetSymbol)?
134-
}
135-
_ => {
136-
Err(InvalidBase64Details::Padding)?
137-
}
133+
(6, _) => Err(InvalidBase64Details::LoneAlphabetSymbol)?,
134+
_ => Err(InvalidBase64Details::Padding)?,
138135
}
139136
Ok(())
140137
}
141138
}
142139

143-
144140
/// Generated by `make_base64_decode_table.py` based on "Table 1: The Base 64 Alphabet"
145141
/// at <https://tools.ietf.org/html/rfc4648#section-4>
146142
///
147143
/// Array indices are the byte value of symbols.
148144
/// Array values are their positions in the base64 alphabet,
149145
/// or -1 for symbols not in the alphabet.
150146
/// The position contributes 6 bits to the decoded bytes.
147+
#[rustfmt::skip]
151148
const BASE64_DECODE_TABLE: [i8; 256] = [
152149
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153150
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

data-url/src/lib.rs

+46-30
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
//! assert!(fragment.is_none());
1616
//! ```
1717
18-
#[macro_use] extern crate matches;
18+
#[macro_use]
19+
extern crate matches;
1920

2021
macro_rules! require {
2122
($condition: expr) => {
2223
if !$condition {
23-
return None
24+
return None;
2425
}
25-
}
26+
};
2627
}
2728

2829
pub mod forgiving_base64;
@@ -53,7 +54,11 @@ impl<'a> DataUrl<'a> {
5354

5455
let (mime_type, base64) = parse_header(from_colon_to_comma);
5556

56-
Ok(DataUrl { mime_type, base64, encoded_body_plus_fragment })
57+
Ok(DataUrl {
58+
mime_type,
59+
base64,
60+
encoded_body_plus_fragment,
61+
})
5762
}
5863

5964
pub fn mime_type(&self) -> &mime::Mime {
@@ -62,9 +67,12 @@ impl<'a> DataUrl<'a> {
6267

6368
/// Streaming-decode the data URL’s body to `write_body_bytes`,
6469
/// and return the URL’s fragment identifier if it has one.
65-
pub fn decode<F, E>(&self, write_body_bytes: F)
66-
-> Result<Option<FragmentIdentifier<'a>>, forgiving_base64::DecodeError<E>>
67-
where F: FnMut(&[u8]) -> Result<(), E>
70+
pub fn decode<F, E>(
71+
&self,
72+
write_body_bytes: F,
73+
) -> Result<Option<FragmentIdentifier<'a>>, forgiving_base64::DecodeError<E>>
74+
where
75+
F: FnMut(&[u8]) -> Result<(), E>,
6876
{
6977
if self.base64 {
7078
decode_with_base64(self.encoded_body_plus_fragment, write_body_bytes)
@@ -75,9 +83,9 @@ impl<'a> DataUrl<'a> {
7583
}
7684

7785
/// Return the decoded body, and the URL’s fragment identifier if it has one.
78-
pub fn decode_to_vec(&self)
79-
-> Result<(Vec<u8>, Option<FragmentIdentifier<'a>>), forgiving_base64::InvalidBase64>
80-
{
86+
pub fn decode_to_vec(
87+
&self,
88+
) -> Result<(Vec<u8>, Option<FragmentIdentifier<'a>>), forgiving_base64::InvalidBase64> {
8189
let mut body = Vec::new();
8290
let fragment = self.decode(|bytes| Ok(body.extend_from_slice(bytes)))?;
8391
Ok((body, fragment))
@@ -100,7 +108,7 @@ impl<'a> FragmentIdentifier<'a> {
100108
percent_encode(byte, &mut string)
101109
}
102110
// Printable ASCII
103-
_ => string.push(byte as char)
111+
_ => string.push(byte as char),
104112
}
105113
}
106114
string
@@ -125,7 +133,9 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
125133
let mut bytes = left_trimmed.bytes();
126134
{
127135
// Ignore ASCII tabs or newlines like the URL parser would
128-
let mut iter = bytes.by_ref().filter(|&byte| !matches!(byte, b'\t' | b'\n' | b'\r'));
136+
let mut iter = bytes
137+
.by_ref()
138+
.filter(|&byte| !matches!(byte, b'\t' | b'\n' | b'\r'));
129139
require!(iter.next()?.to_ascii_lowercase() == b'd');
130140
require!(iter.next()?.to_ascii_lowercase() == b'a');
131141
require!(iter.next()?.to_ascii_lowercase() == b't');
@@ -142,10 +152,10 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
142152
fn find_comma_before_fragment(after_colon: &str) -> Option<(&str, &str)> {
143153
for (i, byte) in after_colon.bytes().enumerate() {
144154
if byte == b',' {
145-
return Some((&after_colon[..i], &after_colon[i + 1..]))
155+
return Some((&after_colon[..i], &after_colon[i + 1..]));
146156
}
147157
if byte == b'#' {
148-
break
158+
break;
149159
}
150160
}
151161
None
@@ -187,18 +197,16 @@ fn parse_header(from_colon_to_comma: &str) -> (mime::Mime, bool) {
187197
}
188198

189199
// Printable ASCII
190-
_ => string.push(byte as char)
200+
_ => string.push(byte as char),
191201
}
192202
}
193203

194204
// FIXME: does Mime::from_str match the MIME Sniffing Standard’s parsing algorithm?
195205
// <https://mimesniff.spec.whatwg.org/#parse-a-mime-type>
196-
let mime_type = string.parse().unwrap_or_else(|_| {
197-
mime::Mime {
198-
type_: String::from("text"),
199-
subtype: String::from("plain"),
200-
parameters: vec![(String::from("charset"), String::from("US-ASCII"))],
201-
}
206+
let mime_type = string.parse().unwrap_or_else(|_| mime::Mime {
207+
type_: String::from("text"),
208+
subtype: String::from("plain"),
209+
parameters: vec![(String::from("charset"), String::from("US-ASCII"))],
202210
});
203211

204212
(mime_type, base64)
@@ -209,7 +217,9 @@ fn remove_base64_suffix(s: &str) -> Option<&str> {
209217
let mut bytes = s.bytes();
210218
{
211219
// Ignore ASCII tabs or newlines like the URL parser would
212-
let iter = bytes.by_ref().filter(|&byte| !matches!(byte, b'\t' | b'\n' | b'\r'));
220+
let iter = bytes
221+
.by_ref()
222+
.filter(|&byte| !matches!(byte, b'\t' | b'\n' | b'\r'));
213223

214224
// Search from the end
215225
let mut iter = iter.rev();
@@ -240,9 +250,12 @@ fn percent_encode(byte: u8, string: &mut String) {
240250
/// Anything that would have been UTF-8 percent-encoded by the URL parser
241251
/// would be percent-decoded here.
242252
/// We skip that round-trip and pass it through unchanged.
243-
fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes: F)
244-
-> Result<Option<FragmentIdentifier>, E>
245-
where F: FnMut(&[u8]) -> Result<(), E>
253+
fn decode_without_base64<F, E>(
254+
encoded_body_plus_fragment: &str,
255+
mut write_bytes: F,
256+
) -> Result<Option<FragmentIdentifier>, E>
257+
where
258+
F: FnMut(&[u8]) -> Result<(), E>,
246259
{
247260
let bytes = encoded_body_plus_fragment.as_bytes();
248261
let mut slice_start = 0;
@@ -275,11 +288,11 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
275288
b'#' => {
276289
let fragment_start = i + 1;
277290
let fragment = &encoded_body_plus_fragment[fragment_start..];
278-
return Ok(Some(FragmentIdentifier(fragment)))
291+
return Ok(Some(FragmentIdentifier(fragment)));
279292
}
280293

281294
// Ignore over '\t' | '\n' | '\r'
282-
_ => slice_start = i + 1
295+
_ => slice_start = i + 1,
283296
}
284297
}
285298
}
@@ -290,9 +303,12 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
290303
/// `decode_without_base64()` composed with
291304
/// <https://infra.spec.whatwg.org/#isomorphic-decode> composed with
292305
/// <https://infra.spec.whatwg.org/#forgiving-base64-decode>.
293-
fn decode_with_base64<F, E>(encoded_body_plus_fragment: &str, write_bytes: F)
294-
-> Result<Option<FragmentIdentifier>, forgiving_base64::DecodeError<E>>
295-
where F: FnMut(&[u8]) -> Result<(), E>
306+
fn decode_with_base64<F, E>(
307+
encoded_body_plus_fragment: &str,
308+
write_bytes: F,
309+
) -> Result<Option<FragmentIdentifier>, forgiving_base64::DecodeError<E>>
310+
where
311+
F: FnMut(&[u8]) -> Result<(), E>,
296312
{
297313
let mut decoder = forgiving_base64::Decoder::new(write_bytes);
298314
let fragment = decode_without_base64(encoded_body_plus_fragment, |bytes| decoder.feed(bytes))?;

0 commit comments

Comments
 (0)