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

New IntoUrl trait #177

Closed
wants to merge 40 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
918352b
Make it possible to define new encode sets in other crates.
SimonSapin Dec 4, 2015
db9de70
Define encode sets based on another set.
SimonSapin Dec 4, 2015
691aec2
Remove the HTTP_VALUE encode set. It can be defined in another crate.
SimonSapin Dec 4, 2015
d140dc8
Rewrite ALL THE THINGS!
SimonSapin Dec 9, 2015
9edff44
Remove the dependency on uuid.
SimonSapin Feb 8, 2016
576bd2a
Add URL slicing/indexing by component.
SimonSapin Feb 8, 2016
7b11445
Add stubs with partial implementation for the WebIDL API.
SimonSapin Feb 8, 2016
c617ed1
Shorter Cargo.toml syntax.
SimonSapin Feb 8, 2016
22cf104
serde_serialization -> serde
SimonSapin Feb 8, 2016
0cb3f2b
Make rustc-serialize an optional dependency.
SimonSapin Feb 8, 2016
61a8185
Rename *{Start,End} posititons to {Before,After}*
SimonSapin Feb 9, 2016
813d270
Replace from_hex() with char::to_digit(16)
SimonSapin Feb 9, 2016
0b5ffb4
Make percent-decoding an iterator.
SimonSapin Feb 9, 2016
244d999
Make percent-encoding an iterator.
SimonSapin Feb 9, 2016
7b33b33
Add percent-encoding convienience wrappers.
SimonSapin Feb 9, 2016
ca9f87d
Update tests from https://github.com/w3c/web-platform-tests/blob/mast…
SimonSapin Feb 10, 2016
7a0e467
Remove Url::has_host
SimonSapin Feb 11, 2016
9a8d394
Remove unused ParseError variants
SimonSapin Feb 12, 2016
903f1d2
Make context a field of Parser.
SimonSapin Feb 11, 2016
a9b4e71
Remove the redundant is_relative field.
SimonSapin Feb 15, 2016
ded48a2
Add Url::domain and Url::ip_address
SimonSapin Feb 15, 2016
d3dba86
Implement ToSocketAddrs
SimonSapin Feb 15, 2016
088c3ed
Remove Url::ip_address for now
SimonSapin Feb 15, 2016
641f940
Add Unicode and ASCII serializations of origins
SimonSapin Feb 16, 2016
946d950
Test WebIdl::origin
SimonSapin Feb 16, 2016
4dff876
Add a fragment setter
SimonSapin Feb 11, 2016
0ae07ed
Add a query setter.
SimonSapin Feb 12, 2016
542feb0
Make Url::parse_with usable. (EncodingOverride is private.)
SimonSapin Feb 19, 2016
dd0436a
Add Origin::is_tuple
SimonSapin Feb 19, 2016
f7e0d7c
More consistent checks for URL with authority or path-only.
SimonSapin Feb 19, 2016
fd16b74
Re-export OpaqueOrigin. It is exposed publicly through Origin::Opaque
SimonSapin Feb 19, 2016
f1bdaa6
Add a scheme setter
SimonSapin Feb 19, 2016
158145f
Add host setters.
SimonSapin Feb 19, 2016
b1b0916
More setters
SimonSapin Feb 23, 2016
47e31ef
Add a path setter
SimonSapin Feb 26, 2016
e7a4dc0
Username and passowrd setters
SimonSapin Feb 26, 2016
5b26c89
More WebIDL implementations.
SimonSapin Feb 26, 2016
bf0f670
Port setters
SimonSapin Mar 1, 2016
b89d7d7
All setters.
SimonSapin Mar 1, 2016
3f9dcd4
New IntoUrl trait
cmbrandenburg Mar 4, 2016
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Make percent-decoding an iterator.

  • Loading branch information
SimonSapin committed Mar 3, 2016
commit 0b5ffb444074d9f3fb8d6d524b04fee76d03b100
@@ -84,8 +84,8 @@ fn parse_internal(input: &[u8], mut encoding_override: EncodingOverride, mut use
}

Some(pairs.into_iter().map(|(name, value)| (
encoding_override.decode(&percent_decode(&name)),
encoding_override.decode(&percent_decode(&value))
encoding_override.decode(&percent_decode(&name).collect::<Vec<u8>>()),
encoding_override.decode(&percent_decode(&value).collect::<Vec<u8>>()),
)).collect())
}

@@ -10,7 +10,7 @@ use std::cmp;
use std::fmt::{self, Formatter, Write};
use std::net::{Ipv4Addr, Ipv6Addr};
use parser::{ParseResult, ParseError};
use percent_encoding::percent_decode;
use percent_encoding::lossy_utf8_percent_decode;
use idna;

#[derive(Copy, Clone, Debug)]
@@ -64,8 +64,7 @@ impl Host<String> {
}
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6)
}
let decoded = percent_decode(input.as_bytes());
let domain = String::from_utf8_lossy(&decoded);
let domain = lossy_utf8_percent_decode(input.as_bytes());
let domain = try!(idna::domain_to_ascii(&domain));
if domain.find(|c| matches!(c,
'\0' | '\t' | '\n' | '\r' | ' ' | '#' | '%' | '/' | ':' | '?' | '@' | '[' | '\\' | ']'
@@ -127,7 +127,7 @@ extern crate unicode_normalization;
extern crate unicode_bidi;

use host::HostInternal;
use percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode_to};
use percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode_to, percent_decode};
use std::cmp;
use std::fmt;
use std::hash;
@@ -637,12 +637,10 @@ fn file_url_segments_to_pathbuf(segments: str::Split<char>) -> Result<PathBuf, (
use std::os::unix::prelude::OsStrExt;
use std::path::PathBuf;

use percent_encoding::percent_decode_to;

let mut bytes = Vec::new();
for segment in segments {
bytes.push(b'/');
percent_decode_to(segment.as_bytes(), &mut bytes);
bytes.extend(percent_decode(segment.as_bytes()));
}
let os_str = OsStr::from_bytes(&bytes);
let path = PathBuf::from(os_str);
@@ -659,8 +657,6 @@ fn file_url_segments_to_pathbuf(segments: str::Split<char>) -> Result<PathBuf, (
// Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
#[cfg_attr(not(windows), allow(dead_code))]
fn file_url_segments_to_pathbuf_windows(mut segments: str::Split<char>) -> Result<PathBuf, ()> {
use percent_encoding::percent_decode;

let first = try!(segments.next().ok_or(()));
if first.len() != 2 || !first.starts_with(parser::ascii_alpha)
|| first.as_bytes()[1] != b':' {
@@ -671,7 +667,7 @@ fn file_url_segments_to_pathbuf_windows(mut segments: str::Split<char>) -> Resul
string.push('\\');

// Currently non-unicode windows paths cannot be represented
match String::from_utf8(percent_decode(segment.as_bytes())) {
match String::from_utf8(percent_decode(segment.as_bytes()).collect()) {
Ok(s) => string.push_str(&s),
Err(..) => return Err(()),
}
@@ -7,7 +7,9 @@
// except according to those terms.

use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::fmt::Write;
use std::slice;

/// Represents a set of characters / bytes that should be percent-encoded.
///
@@ -163,41 +165,53 @@ pub fn utf8_percent_encode<E: EncodeSet>(input: &str, encode_set: E) -> String {
}


/// Percent-decode the given bytes, and push the result to `output`.
pub fn percent_decode_to(input: &[u8], output: &mut Vec<u8>) {
let mut i = 0;
while i < input.len() {
let c = input[i];
if c == b'%' && i + 2 < input.len() {
let h = (input[i + 1] as char).to_digit(16);
let l = (input[i + 2] as char).to_digit(16);
if let (Some(h), Some(l)) = (h, l) {
output.push(h as u8 * 0x10 + l as u8);
i += 3;
continue
}
}

output.push(c);
i += 1;
/// Percent-decode the given bytes and return an iterator of bytes.
#[inline]
pub fn percent_decode(input: &[u8]) -> PercentDecode {
PercentDecode {
iter: input.iter()
}
}


/// Percent-decode the given bytes.
#[inline]
pub fn percent_decode(input: &[u8]) -> Vec<u8> {
let mut output = Vec::new();
percent_decode_to(input, &mut output);
output
pub struct PercentDecode<'a> {
iter: slice::Iter<'a, u8>,
}

impl<'a> Iterator for PercentDecode<'a> {
type Item = u8;

fn next(&mut self) -> Option<u8> {
self.iter.next().map(|&byte| {
if byte == b'%' {
let after_percent_sign = self.iter.clone();
let h = self.iter.next().and_then(|&b| (b as char).to_digit(16));
let l = self.iter.next().and_then(|&b| (b as char).to_digit(16));
if let (Some(h), Some(l)) = (h, l) {
return h as u8 * 0x10 + l as u8
}
self.iter = after_percent_sign;
}
byte
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.iter.size_hint();
(low, high.and_then(|high| high.checked_mul(3)))
}
}

/// Percent-decode the given bytes, and decode the result as UTF-8.
///
/// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
/// will be replaced � U+FFFD, the replacement character.
#[inline]
pub fn lossy_utf8_percent_decode(input: &[u8]) -> String {
String::from_utf8_lossy(&percent_decode(input)).to_string()
let bytes = percent_decode(input).collect::<Vec<u8>>();
match String::from_utf8_lossy(&bytes) {
Cow::Owned(s) => return s,
Cow::Borrowed(_) => {}
}
unsafe {
String::from_utf8_unchecked(bytes)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.