Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private-cookies = ["cookie/secure"]

[dependencies]
smallvec = "1.0"
percent-encoding = "1"
percent-encoding = "2"
hyper = { version = "0.13.0", default-features = false }
http = "0.2"
mime = "0.3.13"
Expand Down
2 changes: 2 additions & 0 deletions core/http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(const_if_match)]
#![feature(const_loop)]
#![feature(proc_macro_hygiene)]
#![recursion_limit="512"]

Expand Down
2 changes: 1 addition & 1 deletion core/http/src/parse/uri/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const PATH_CHARS: [u8; 256] = [
];

#[inline(always)]
pub fn is_pchar(c: u8) -> bool {
pub const fn is_pchar(c: u8) -> bool {
PATH_CHARS[c as usize] == c
}

Expand Down
63 changes: 37 additions & 26 deletions core/http/src/uri/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
use std::marker::PhantomData;
use std::borrow::Cow;

use percent_encoding::{EncodeSet, utf8_percent_encode};
use percent_encoding::{AsciiSet, utf8_percent_encode};

use crate::uri::{UriPart, Path, Query};
use crate::parse::uri::is_pchar;

#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
pub struct UNSAFE_ENCODE_SET<P: UriPart>(PhantomData<P>);
pub trait EncodeSet {
const SET: AsciiSet;
}

const fn build_set_from_table() -> AsciiSet {
const ASCII_RANGE_LEN: u8 = 0x80;

let mut set = percent_encoding::CONTROLS.remove(0);
let mut b: u8 = 0;
while b < ASCII_RANGE_LEN {
if !is_pchar(b) {
set = set.add(b);
}
b += 1;
}
set
}

const PATH_SET: AsciiSet = build_set_from_table();

impl<P: UriPart> Default for UNSAFE_ENCODE_SET<P> {
#[inline(always)]
fn default() -> Self { UNSAFE_ENCODE_SET(PhantomData) }
}

impl EncodeSet for UNSAFE_ENCODE_SET<Path> {
#[inline(always)]
fn contains(&self, byte: u8) -> bool {
!is_pchar(byte) || byte == b'%'
}
const SET: AsciiSet = PATH_SET
.add(b'%');
}

impl EncodeSet for UNSAFE_ENCODE_SET<Query> {
#[inline(always)]
fn contains(&self, byte: u8) -> bool {
(!is_pchar(byte) && (byte != b'?')) || byte == b'%' || byte == b'+'
}
const SET: AsciiSet = PATH_SET
.remove(b'?')
.add(b'%')
.add(b'+');
}

#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
pub struct ENCODE_SET<P: UriPart>(PhantomData<P>);

impl EncodeSet for ENCODE_SET<Path> {
#[inline(always)]
fn contains(&self, byte: u8) -> bool {
<UNSAFE_ENCODE_SET<Path>>::default().contains(byte) || byte == b'/'
}
const SET: AsciiSet = <UNSAFE_ENCODE_SET<Path>>::SET
.add(b'/');
}

impl EncodeSet for ENCODE_SET<Query> {
#[inline(always)]
fn contains(&self, byte: u8) -> bool {
<UNSAFE_ENCODE_SET<Query>>::default().contains(byte) || match byte {
b'&' | b'=' => true,
_ => false
}
}
const SET: AsciiSet = <UNSAFE_ENCODE_SET<Query>>::SET
.add(b'&')
.add(b'=');
}

#[derive(Default, Clone, Copy)]
#[allow(non_camel_case_types)]
pub struct DEFAULT_ENCODE_SET;

impl EncodeSet for DEFAULT_ENCODE_SET {
#[inline(always)]
fn contains(&self, byte: u8) -> bool {
ENCODE_SET::<Path>(PhantomData).contains(byte) ||
ENCODE_SET::<Query>(PhantomData).contains(byte)
}
const SET: AsciiSet = <ENCODE_SET<Path>>::SET
.add(b'%')
.add(b'+')
.add(b'&')
.add(b'=');
}

pub fn unsafe_percent_encode<P: UriPart>(string: &str) -> Cow<'_, str> {
Expand All @@ -71,5 +82,5 @@ pub fn unsafe_percent_encode<P: UriPart>(string: &str) -> Cow<'_, str> {
}

pub fn percent_encode<S: EncodeSet + Default>(string: &str) -> Cow<'_, str> {
utf8_percent_encode(string, S::default()).into()
utf8_percent_encode(string, &S::SET).into()
}
2 changes: 1 addition & 1 deletion core/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ num_cpus = "1.0"
state = "0.4.1"
time = "0.1"
memchr = "2" # TODO: Use pear instead.
base64 = "0.11"
base64 = "0.12"
base16 = "0.2"
pear = "0.1"
atty = "0.2"
Expand Down