Skip to content

Commit

Permalink
Auto merge of servo#469 - sfackler:backslash-path, r=SimonSapin
Browse files Browse the repository at this point in the history
Escape backslash in special URL path components

Closes servo#468

<!-- 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/469)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Nov 6, 2018
2 parents a07eac0 + 85af99e commit 1cc36c0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

name = "url"
# When updating version, also modify html_root_url in the lib.rs
version = "1.7.1"
version = "1.7.2"
authors = ["The rust-url developers"]

description = "URL library for Rust, based on the WHATWG URL Standard"
Expand Down
2 changes: 1 addition & 1 deletion percent_encoding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "percent-encoding"
version = "1.0.1"
version = "1.0.2"
authors = ["The rust-url developers"]
description = "Percent encoding and decoding"
repository = "https://github.com/servo/rust-url/"
Expand Down
5 changes: 5 additions & 0 deletions percent_encoding/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ define_encode_set! {
/// space, double quote ("), hash (#), inequality qualifiers (<), (>), backtick (`),
/// question mark (?), and curly brackets ({), (}), percent sign (%), forward slash (/) are
/// encoded.
///
/// # Note
///
/// For [special URLs](https://url.spec.whatwg.org/#is-special), the backslash (\) character should
/// additionally be escaped, but that is *not* included in this encode set.
pub PATH_SEGMENT_ENCODE_SET = [DEFAULT_ENCODE_SET] | {'%', '/'}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");
#[cfg(feature="heapsize")] #[macro_use] extern crate heapsize;

pub extern crate idna;
#[macro_use]
pub extern crate percent_encoding;

use encoding::EncodingOverride;
Expand Down
15 changes: 13 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ use percent_encoding::{
PATH_SEGMENT_ENCODE_SET
};

define_encode_set! {
// The backslash (\) character is treated as a path separator in special URLs
// so it needs to be additionally escaped in that case.
pub SPECIAL_PATH_SEGMENT_ENCODE_SET = [PATH_SEGMENT_ENCODE_SET] | {'\\'}
}

pub type ParseResult<T> = Result<T, ParseError>;

macro_rules! simple_enum_error {
Expand Down Expand Up @@ -1011,8 +1017,13 @@ impl<'a> Parser<'a> {
_ => {
self.check_url_code_point(c, &input);
if self.context == Context::PathSegmentSetter {
self.serialization.extend(utf8_percent_encode(
utf8_c, PATH_SEGMENT_ENCODE_SET));
if scheme_type.is_special() {
self.serialization.extend(utf8_percent_encode(
utf8_c, SPECIAL_PATH_SEGMENT_ENCODE_SET));
} else {
self.serialization.extend(utf8_percent_encode(
utf8_c, PATH_SEGMENT_ENCODE_SET));
}
} else {
self.serialization.extend(utf8_percent_encode(
utf8_c, DEFAULT_ENCODE_SET));
Expand Down
11 changes: 11 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ fn new_directory_paths() {
}
}

#[test]
fn path_backslash_fun() {
let mut special_url = "http://foobar.com".parse::<Url>().unwrap();
special_url.path_segments_mut().unwrap().push("foo\\bar");
assert_eq!(special_url.as_str(), "http://foobar.com/foo%5Cbar");

let mut nonspecial_url = "thing://foobar.com".parse::<Url>().unwrap();
nonspecial_url.path_segments_mut().unwrap().push("foo\\bar");
assert_eq!(nonspecial_url.as_str(), "thing://foobar.com/foo\\bar");
}

#[test]
fn from_str() {
assert!("http://testing.com/this".parse::<Url>().is_ok());
Expand Down

0 comments on commit 1cc36c0

Please sign in to comment.