Skip to content

Instantly share code, notes, and snippets.

@glynos
Last active April 13, 2021 15:31
Show Gist options
  • Save glynos/8a22fe8cf00837596542acaa69aa4a37 to your computer and use it in GitHub Desktop.
Save glynos/8a22fe8cf00837596542acaa69aa4a37 to your computer and use it in GitHub Desktop.
URL with '%' in path
[package]
name = "rust-url"
version = "0.1.0"
authors = ["Glyn Matthews <glyn.matthews@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
url = "2.2"
percent-encoding = "2.1"
use url::{Url};
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS, NON_ALPHANUMERIC, percent_decode_str};
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
const PATH: &AsciiSet = &QUERY.add(b'?').add(b'`').add(b'{').add(b'}');
fn main() {
println!("{}", Url::parse("https://github.com/%25ru st%lang/rust/issues?lab%els=E-easy&state=open").expect("Not a URL"));
println!("{}", utf8_percent_encode("foo <bar>", FRAGMENT).to_string());
println!("{}", utf8_percent_encode("foo%23<bar>", FRAGMENT).to_string());
println!("{}", utf8_percent_encode("foo%<bar>", FRAGMENT).to_string());
println!("{}", utf8_percent_encode("foo%<bar>", NON_ALPHANUMERIC).to_string());
println!("{}", percent_decode_str("foo%<bar>").decode_utf8().unwrap());
println!("{}", percent_decode_str("foo%25%3Cbar%3E").decode_utf8().unwrap());
println!("{}", utf8_percent_encode("%3F/?", PATH).to_string());
}
https://github.com/%25ru%20st%lang/rust/issues?lab%els=E-easy&state=open
foo%20%3Cbar%3E
foo%23%3Cbar%3E
foo%%3Cbar%3E
foo%25%3Cbar%3E
foo%<bar>
foo%<bar>
%3F/%3F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment