Skip to content

Commit

Permalink
Merge branch 'master' into addl-percent-encode-sets
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Jul 18, 2023
2 parents 81d0620 + edabc79 commit e2ebbf0
Show file tree
Hide file tree
Showing 20 changed files with 2,302 additions and 820 deletions.
24 changes: 21 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches: ["master"]
pull_request:
merge_group:
types: [checks_requested]

env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
Expand Down Expand Up @@ -40,12 +42,12 @@ jobs:
# Run tests enabling the serde feature
- name: Run tests with the serde feature
run: cargo test --features "url/serde,url/expose_internals"
# The #[debugger_visualizer] attribute is currently gated behind an unstable feature flag.
# In order to test the visualizers for the url crate, they have to be tested on a nightly build.
# The #[debugger_visualizer] attribute is currently gated behind a feature flag until #[debugger_visualizer]
# is available in all rustc versions past our MSRV. As such, we only run the tests on newer rustc versions.
- name: Run debugger_visualizer tests
if: |
matrix.os == 'windows-latest' &&
matrix.rust == 'nightly'
matrix.rust != '1.56.0'
run: cargo test --test debugger_visualizer --features "url/debugger_visualizer,url_debug_tests/debugger_visualizer" -- --test-threads=1
- name: Test `no_std` support
run: cargo test --no-default-features --features=alloc
Expand Down Expand Up @@ -75,3 +77,19 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@v1

Result:
name: Result
runs-on: ubuntu-latest
needs:
- "Test"
- "WASM"
- "Lint"
- "Audit"
steps:
- name: Mark the job as successful
run: exit 0
if: success()
- name: Mark the job as unsuccessful
run: exit 1
if: "!success()"
3 changes: 3 additions & 0 deletions data-url/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ test = false
[[test]]
name = "wpt"
harness = false

[package.metadata.docs.rs]
rustdoc-args = ["--generate-link-to-definition"]
31 changes: 31 additions & 0 deletions data-url/src/forgiving_base64.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
use alloc::vec::Vec;
use core::fmt;

#[derive(Debug)]
pub struct InvalidBase64(InvalidBase64Details);

impl fmt::Display for InvalidBase64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
InvalidBase64Details::UnexpectedSymbol(code_point) => {
write!(f, "symbol with codepoint {} not expected", code_point)
}
InvalidBase64Details::AlphabetSymbolAfterPadding => {
write!(f, "alphabet symbol present after padding")
}
InvalidBase64Details::LoneAlphabetSymbol => write!(f, "lone alphabet symbol present"),
InvalidBase64Details::Padding => write!(f, "incorrect padding"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidBase64 {}

#[derive(Debug)]
enum InvalidBase64Details {
UnexpectedSymbol(u8),
Expand All @@ -19,6 +38,18 @@ pub enum DecodeError<E> {
WriteError(E),
}

impl<E: fmt::Display> fmt::Display for DecodeError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidBase64(inner) => write!(f, "base64 not valid: {}", inner),
Self::WriteError(err) => write!(f, "write error: {}", err),
}
}
}

#[cfg(feature = "std")]
impl<E: std::error::Error> std::error::Error for DecodeError<E> {}

impl<E> From<InvalidBase64Details> for DecodeError<E> {
fn from(e: InvalidBase64Details) -> Self {
DecodeError::InvalidBase64(InvalidBase64(e))
Expand Down
18 changes: 17 additions & 1 deletion data-url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

// For forwards compatibility
#[cfg(feature = "std")]
extern crate std as _;
extern crate std;

#[macro_use]
extern crate alloc;
Expand All @@ -27,6 +27,7 @@ extern crate alloc;
compile_error!("the `alloc` feature must be enabled");

use alloc::{string::String, vec::Vec};
use core::fmt;

macro_rules! require {
($condition: expr) => {
Expand All @@ -51,6 +52,21 @@ pub enum DataUrlError {
NoComma,
}

impl fmt::Display for DataUrlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotADataUrl => write!(f, "not a valid data url"),
Self::NoComma => write!(
f,
"data url is missing comma delimiting attributes and body"
),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DataUrlError {}

impl<'a> DataUrl<'a> {
/// <https://fetch.spec.whatwg.org/#data-url-processor>
/// but starting from a string rather than a parsed `Url`, to avoid extra string copies.
Expand Down
9 changes: 9 additions & 0 deletions data-url/src/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ impl Mime {
#[derive(Debug)]
pub struct MimeParsingError(());

impl fmt::Display for MimeParsingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid mime type")
}
}

#[cfg(feature = "std")]
impl std::error::Error for MimeParsingError {}

/// <https://mimesniff.spec.whatwg.org/#parsing-a-mime-type>
impl FromStr for Mime {
type Err = MimeParsingError;
Expand Down
4 changes: 2 additions & 2 deletions debug_metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ types, descibe how to display those types. (For writing a pretty printer, see: h

### Embedding Visualizers

Through the use of the currently unstable `#[debugger_visualizer]` attribute, the `url`
crate can embed debugger visualizers into the crate metadata.
Through the use of the `#[debugger_visualizer]` attribute, the `url` crate can embed
debugger visualizers into the crate metadata.

Currently the two types of visualizers supported are Natvis and Pretty printers.

Expand Down
3 changes: 3 additions & 0 deletions form_urlencoded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ alloc = ["percent-encoding/alloc"]

[dependencies]
percent-encoding = { version = "2.3.0", default-features = false, path = "../percent_encoding" }

[package.metadata.docs.rs]
rustdoc-args = ["--generate-link-to-definition"]
3 changes: 3 additions & 0 deletions idna/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ unicode-normalization = { version = "0.1.22", default-features = false }
[[bench]]
name = "all"
harness = false

[package.metadata.docs.rs]
rustdoc-args = ["--generate-link-to-definition"]
2 changes: 1 addition & 1 deletion idna/src/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl Idna {
return Errors::default();
}
let mut errors = processing(domain, self.config, &mut self.normalized, out);
self.output = std::mem::replace(out, String::with_capacity(out.len()));
self.output = core::mem::replace(out, String::with_capacity(out.len()));
let mut first = true;
for label in self.output.split('.') {
if !first {
Expand Down
3 changes: 3 additions & 0 deletions percent_encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ rust-version = "1.51"
default = ["std"]
std = ["alloc"]
alloc = []

[package.metadata.docs.rs]
rustdoc-args = ["--generate-link-to-definition"]
10 changes: 8 additions & 2 deletions url/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ edition = "2018"
rust-version = "1.56"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bencher = "0.1"

Expand All @@ -28,19 +29,24 @@ serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
default = []
# UNSTABLE FEATURES (requires Rust nightly)
# Enable to use the #[debugger_visualizer] attribute.
# Enable to use the #[debugger_visualizer] attribute. This feature requires Rust >= 1.71.
debugger_visualizer = []
# Expose internal offsets of the URL.
expose_internals = []

[[test]]
name = "url_wpt"
path = "tests/wpt.rs"
harness = false

[[bench]]
name = "parse_url"
path = "benches/parse_url.rs"
harness = false

[package.metadata.docs.rs]
features = ["serde"]
rustdoc-args = ["--generate-link-to-definition"]

[package.metadata.playground]
features = ["serde"]
28 changes: 22 additions & 6 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,24 @@ See [serde documentation](https://serde.rs) for more information.
url = { version = "2", features = ["serde"] }
```
# Feature: `debugger_visualizer`
If you enable the `debugger_visualizer` feature, the `url` crate will include
a [natvis file](https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects)
for [Visual Studio](https://www.visualstudio.com/) that allows you to view
[`Url`](struct.Url.html) objects in the debugger.
This feature requires Rust 1.71 or later.
```toml
url = { version = "2", features = ["debugger_visualizer"] }
```
*/

#![doc(html_root_url = "https://docs.rs/url/2.4.0")]
#![cfg_attr(
feature = "debugger_visualizer",
feature(debugger_visualizer),
debugger_visualizer(natvis_file = "../../debug_metadata/url.natvis")
)]

Expand Down Expand Up @@ -1486,7 +1498,7 @@ impl Url {
if let Some(input) = fragment {
self.fragment_start = Some(to_u32(self.serialization.len()).unwrap());
self.serialization.push('#');
self.mutate(|parser| parser.parse_fragment(parser::Input::no_trim(input)))
self.mutate(|parser| parser.parse_fragment(parser::Input::new_no_trim(input)))
} else {
self.fragment_start = None;
self.strip_trailing_spaces_from_opaque_path();
Expand Down Expand Up @@ -1549,7 +1561,7 @@ impl Url {
parser.parse_query(
scheme_type,
scheme_end,
parser::Input::trim_tab_and_newlines(input, vfn),
parser::Input::new_trim_tab_and_newlines(input, vfn),
)
});
} else {
Expand Down Expand Up @@ -1670,10 +1682,14 @@ impl Url {
parser.serialization.push_str("%2F");
path = &path[1..];
}
parser.parse_cannot_be_a_base_path(parser::Input::new(path));
parser.parse_cannot_be_a_base_path(parser::Input::new_no_trim(path));
} else {
let mut has_host = true; // FIXME
parser.parse_path_start(scheme_type, &mut has_host, parser::Input::new(path));
parser.parse_path_start(
scheme_type,
&mut has_host,
parser::Input::new_no_trim(path),
);
}
});
self.restore_after_path(old_after_path_pos, &after_path);
Expand Down Expand Up @@ -2343,7 +2359,7 @@ impl Url {
#[allow(clippy::result_unit_err, clippy::suspicious_operation_groupings)]
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
let mut parser = Parser::for_setter(String::new());
let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
let remaining = parser.parse_scheme(parser::Input::new_no_trim(scheme))?;
let new_scheme_type = SchemeType::from(&parser.serialization);
let old_scheme_type = SchemeType::from(self.scheme());
// If url’s scheme is a special scheme and buffer is not a special scheme, then return.
Expand Down
15 changes: 7 additions & 8 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,13 @@ pub struct Input<'i> {
}

impl<'i> Input<'i> {
pub fn new(input: &'i str) -> Self {
Input::with_log(input, None)
}

pub fn no_trim(input: &'i str) -> Self {
pub fn new_no_trim(input: &'i str) -> Self {
Input {
chars: input.chars(),
}
}

pub fn trim_tab_and_newlines(
pub fn new_trim_tab_and_newlines(
original_input: &'i str,
vfn: Option<&dyn Fn(SyntaxViolation)>,
) -> Self {
Expand All @@ -195,7 +191,10 @@ impl<'i> Input<'i> {
}
}

pub fn with_log(original_input: &'i str, vfn: Option<&dyn Fn(SyntaxViolation)>) -> Self {
pub fn new_trim_c0_control_and_space(
original_input: &'i str,
vfn: Option<&dyn Fn(SyntaxViolation)>,
) -> Self {
let input = original_input.trim_matches(c0_control_or_space);
if let Some(vfn) = vfn {
if input.len() < original_input.len() {
Expand Down Expand Up @@ -345,7 +344,7 @@ impl<'a> Parser<'a> {

/// https://url.spec.whatwg.org/#concept-basic-url-parser
pub fn parse_url(mut self, input: &str) -> ParseResult<Url> {
let input = Input::with_log(input, self.violation_fn);
let input = Input::new_trim_c0_control_and_space(input, self.violation_fn);
if let Ok(remaining) = self.parse_scheme(input.clone()) {
return self.parse_with_scheme(remaining);
}
Expand Down
2 changes: 1 addition & 1 deletion url/src/path_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'a> PathSegmentsMut<'a> {
scheme_type,
&mut has_host,
path_start,
parser::Input::new(segment),
parser::Input::new_no_trim(segment),
);
}
});
Expand Down
6 changes: 3 additions & 3 deletions url/src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
}
// Host parsing rules are strict,
// We don't want to trim the input
let input = Input::no_trim(new_host);
let input = Input::new_no_trim(new_host);
let host;
let opt_port;
{
Expand Down Expand Up @@ -203,7 +203,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
return Err(());
}
// Host parsing rules are strict we don't want to trim the input
let input = Input::no_trim(new_hostname);
let input = Input::new_no_trim(new_hostname);
let scheme_type = SchemeType::from(url.scheme());
if scheme_type == SchemeType::File && new_hostname.is_empty() {
url.set_host_internal(Host::Domain(String::new()), None);
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
return Err(());
}
result = Parser::parse_port(
Input::new(new_port),
Input::new_no_trim(new_port),
|| default_port(scheme),
Context::Setter,
)
Expand Down
Loading

0 comments on commit e2ebbf0

Please sign in to comment.