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-encoding an iterator.

  • Loading branch information
SimonSapin committed Mar 3, 2016
commit 244d999c8a5f0e9618937046ed6987ae8fff3d2c
@@ -16,7 +16,7 @@
use std::borrow::Borrow;
use std::ascii::AsciiExt;
use encoding::EncodingOverride;
use percent_encoding::{percent_encode_to, percent_decode, FORM_URLENCODED_ENCODE_SET};
use percent_encoding::{percent_encode, percent_decode, FORM_URLENCODED_ENCODE_SET};


/// Convert a byte string in the `application/x-www-form-urlencoded` format
@@ -125,7 +125,7 @@ where I: IntoIterator, I::Item: Borrow<(K, V)>, K: AsRef<str>, V: AsRef<str> {
if byte == b' ' {
output.push_str("+")
} else {
percent_encode_to(&[byte], FORM_URLENCODED_ENCODE_SET, output)
output.extend(percent_encode(&[byte], FORM_URLENCODED_ENCODE_SET))
}
}
}
@@ -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, percent_decode};
use percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode, percent_decode};
use std::cmp;
use std::fmt;
use std::hash;
@@ -588,7 +588,8 @@ fn path_to_file_url_segments(path: &Path, serialization: &mut String) -> Result<
// skip the root component
for component in path.components().skip(1) {
serialization.push('/');
percent_encode_to(component.as_os_str().as_bytes(), PATH_SEGMENT_ENCODE_SET, serialization)
serialization.extend(percent_encode(
component.as_os_str().as_bytes(), PATH_SEGMENT_ENCODE_SET))
}
Ok(())
}
@@ -626,7 +627,7 @@ fn path_to_file_url_segments_windows(path: &Path, serialization: &mut String) ->
// FIXME: somehow work with non-unicode?
let component = try!(component.as_os_str().to_str().ok_or(()));
serialization.push('/');
percent_encode_to(component.as_bytes(), PATH_SEGMENT_ENCODE_SET, serialization);
serialization.extend(percent_encode(component.as_bytes(), PATH_SEGMENT_ENCODE_SET));
}
Ok(())
}
@@ -13,7 +13,7 @@ use std::fmt::{self, Formatter, Write};
use super::{Url, EncodingOverride};
use host::{self, HostInternal};
use percent_encoding::{
utf8_percent_encode_to, percent_encode_to,
utf8_percent_encode, percent_encode,
SIMPLE_ENCODE_SET, DEFAULT_ENCODE_SET, USERINFO_ENCODE_SET, QUERY_ENCODE_SET
};

@@ -604,7 +604,7 @@ impl<'a> Parser<'a> {
_ => {
self.check_url_code_point(input, i, c);
let utf8_c = &input[i..next_i];
utf8_percent_encode_to(utf8_c, USERINFO_ENCODE_SET, &mut self.serialization);
self.serialization.extend(utf8_percent_encode(utf8_c, USERINFO_ENCODE_SET));
}
}
}
@@ -794,8 +794,8 @@ impl<'a> Parser<'a> {
'\t' | '\n' | '\r' => self.syntax_violation("invalid characters"),
_ => {
self.check_url_code_point(input, i, c);
utf8_percent_encode_to(
&input[i..next_i], DEFAULT_ENCODE_SET, &mut self.serialization);
self.serialization.extend(utf8_percent_encode(
&input[i..next_i], DEFAULT_ENCODE_SET));
}
}
}
@@ -861,8 +861,8 @@ impl<'a> Parser<'a> {
'\t' | '\n' | '\r' => self.syntax_violation("invalid character"),
_ => {
self.check_url_code_point(input, i, c);
utf8_percent_encode_to(
&input[i..next_i], SIMPLE_ENCODE_SET, &mut self.serialization);
self.serialization.extend(utf8_percent_encode(
&input[i..next_i], SIMPLE_ENCODE_SET));
}
}
}
@@ -941,7 +941,7 @@ impl<'a> Parser<'a> {
_ => EncodingOverride::utf8(),
};
let query_bytes = encoding.encode(&query);
percent_encode_to(&query_bytes, QUERY_ENCODE_SET, &mut self.serialization);
self.serialization.extend(percent_encode(&query_bytes, QUERY_ENCODE_SET));
remaining
}

@@ -969,8 +969,8 @@ impl<'a> Parser<'a> {
'\0' | '\t' | '\n' | '\r' => self.syntax_violation("invalid character"),
_ => {
self.check_url_code_point(input, i, c);
utf8_percent_encode_to(
&input[i..next_i], SIMPLE_ENCODE_SET, &mut self.serialization);
self.serialization.extend(utf8_percent_encode(
&input[i..next_i], SIMPLE_ENCODE_SET));
}
}
}
@@ -8,7 +8,6 @@

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.
@@ -49,7 +48,7 @@ pub trait EncodeSet {
/// pub QUERY_ENCODE_SET = [SIMPLE_ENCODE_SET] | {' ', '"', '#', '<', '>'}
/// }
/// # fn main() {
/// assert_eq!(utf8_percent_encode("foo bar", QUERY_ENCODE_SET), "foo%20bar");
/// assert_eq!(utf8_percent_encode("foo bar", QUERY_ENCODE_SET).collect::<String>(), "foo%20bar");
/// # }
/// ```
#[macro_export]
@@ -116,54 +115,70 @@ define_encode_set! {
}
}

/// Percent-encode the given bytes, and push the result to `output`.
///
/// The pushed strings are within the ASCII range.
/// Percent-encode the given bytes and return an iterator of `char` in the ASCII range.
#[inline]
pub fn percent_encode_to<E: EncodeSet>(input: &[u8], encode_set: E, output: &mut String) {
for &byte in input {
if encode_set.contains(byte) {
write!(output, "%{:02X}", byte).unwrap();
} else {
assert!(byte.is_ascii());
unsafe {
output.as_mut_vec().push(byte)
}
}
pub fn percent_encode<E: EncodeSet>(input: &[u8], encode_set: E) -> PercentEncode<E> {
PercentEncode {
iter: input.iter(),
encode_set: encode_set,
state: PercentEncodeState::NextByte,
}
}


/// Percent-encode the given bytes.
///
/// The returned string is within the ASCII range.
/// Percent-encode the UTF-8 encoding of the given string
/// and return an iterator of `char` in the ASCII range.
#[inline]
pub fn percent_encode<E: EncodeSet>(input: &[u8], encode_set: E) -> String {
let mut output = String::new();
percent_encode_to(input, encode_set, &mut output);
output
pub fn utf8_percent_encode<E: EncodeSet>(input: &str, encode_set: E) -> PercentEncode<E> {
percent_encode(input.as_bytes(), encode_set)
}

pub struct PercentEncode<'a, E: EncodeSet> {
iter: slice::Iter<'a, u8>,
encode_set: E,
state: PercentEncodeState,
}

/// Percent-encode the UTF-8 encoding of the given string, and push the result to `output`.
///
/// The pushed strings are within the ASCII range.
#[inline]
pub fn utf8_percent_encode_to<E: EncodeSet>(input: &str, encode_set: E, output: &mut String) {
percent_encode_to(input.as_bytes(), encode_set, output)
enum PercentEncodeState {
NextByte,
HexHigh(u8),
HexLow(u8),
}

impl<'a, E: EncodeSet> Iterator for PercentEncode<'a, E> {
type Item = char;

/// Percent-encode the UTF-8 encoding of the given string.
///
/// The returned string is within the ASCII range.
#[inline]
pub fn utf8_percent_encode<E: EncodeSet>(input: &str, encode_set: E) -> String {
let mut output = String::new();
utf8_percent_encode_to(input, encode_set, &mut output);
output
}
fn next(&mut self) -> Option<char> {
// str::char::from_digit always returns lowercase.
const UPPER_HEX: [char; 16] = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
match self.state {
PercentEncodeState::HexHigh(byte) => {
self.state = PercentEncodeState::HexLow(byte);
Some(UPPER_HEX[(byte >> 4) as usize])
}
PercentEncodeState::HexLow(byte) => {
self.state = PercentEncodeState::NextByte;
Some(UPPER_HEX[(byte & 0x0F) as usize])
}
PercentEncodeState::NextByte => {
self.iter.next().map(|&byte| {
if self.encode_set.contains(byte) {
self.state = PercentEncodeState::HexHigh(byte);
'%'
} else {
assert!(byte.is_ascii());
byte as char
}
})
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.iter.size_hint();
(low.saturating_add(2) / 3, high)
}
}

/// Percent-decode the given bytes and return an iterator of bytes.
#[inline]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.