Skip to content

Commit

Permalink
Avoid percent encoding some things in a query
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Feb 18, 2025
1 parent 7e41dff commit cc1104f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,33 @@ use std::iter::Enumerate;
use std::ops::Deref;
use std::str::Chars;

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

pub const ENCODED_IN_QUERY: &AsciiSet = &CONTROLS
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'$')
.add(b'%')
.add(b'&')
.add(b'+')
.add(b',')
.add(b'/')
.add(b':')
.add(b';')
.add(b'<')
.add(b'=')
.add(b'>')
.add(b'?')
.add(b'@')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
.add(b'`')
.add(b'{')
.add(b'|')
.add(b'}');

#[derive(Clone)]
pub(crate) struct QueryParam<'a> {
Expand All @@ -18,7 +44,7 @@ enum Source<'a> {
}

pub fn url_enc(i: &str) -> Cow<str> {
utf8_percent_encode(i, percent_encoding::NON_ALPHANUMERIC).into()
utf8_percent_encode(i, ENCODED_IN_QUERY).into()
}

impl<'a> QueryParam<'a> {
Expand Down Expand Up @@ -144,4 +170,11 @@ mod test {
assert_eq!(p("foo=bar&"), vec!["foo=bar"]);
assert_eq!(p("foo=bar&foo2=bar2"), vec!["foo=bar", "foo2=bar2"]);
}

#[test]
fn do_not_url_encode_some_things() {
const NOT_ENCODE: &str = &"!'()*-._~";
let q = QueryParam::new_key_value("key", NOT_ENCODE);
assert_eq!(q.as_str(), format!("key={}", NOT_ENCODE));
}
}

0 comments on commit cc1104f

Please sign in to comment.