use crate::encoding_support::EncodingOverride;
use percent_encoding::{percent_decode, percent_encode_byte};
use std::{
borrow::{Borrow, Cow},
fmt, str,
};
#[inline]
pub fn parse(input: &[u8]) -> Parse<'_> {
Parse { input, encoding: EncodingOverride::utf8() }
}
#[cfg(feature = "query_encoding")]
pub fn parse_with_encoding<'a>(
input: &'a [u8],
encoding_override: Option<::encoding::EncodingRef>,
use_charset: bool,
) -> Result<Parse<'a>, ()> {
let mut encoding = EncodingOverride::from_opt_encoding(encoding_override);
if !(encoding.is_utf8() || input.is_ascii()) {
return Err(());
}
if use_charset {
for sequence in input.split(|&b| b == b'&') {
if sequence.starts_with(b"_charset_=") {
let value = &sequence[b"_charset_=".len()..];
if let Some(e) = EncodingOverride::lookup(value) {
encoding = e;
break;
}
}
}
}
Ok(Parse { input, encoding })
}
#[derive(Copy, Clone, Debug)]
pub struct Parse<'a> {
input: &'a [u8],
encoding: EncodingOverride,
}
impl<'a> Iterator for Parse<'a> {
type Item = (Cow<'a, str>, Cow<'a, str>);
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.input.is_empty() {
return None;
}
let mut split2 = self.input.splitn(2, |&b| b == b'&');
let sequence = split2.next().unwrap();
self.input = split2.next().unwrap_or(&[][..]);
if sequence.is_empty() {
continue;
}
let mut split2 = sequence.splitn(2, |&b| b == b'=');
let name = split2.next().unwrap();
let value = split2.next().unwrap_or(&[][..]);
return Some((decode(name, self.encoding), decode(value, self.encoding)));
}
}
}
fn decode(input: &[u8], encoding: EncodingOverride) -> Cow<'_, str> {
let replaced = replace_plus(input);
encoding.decode(match percent_decode(&replaced).if_any() {
Some(vec) => Cow::Owned(vec),
None => replaced,
})
}
fn replace_plus(input: &[u8]) -> Cow<'_, [u8]> {
match input.iter().position(|&b| b == b'+') {
None => Cow::Borrowed(input),
Some(first_position) => {
let mut replaced = input.to_owned();
replaced[first_position] = b' ';
for byte in &mut replaced[first_position + 1..] {
if *byte == b'+' {
*byte = b' ';
}
}
Cow::Owned(replaced)
}
}
}
impl<'a> Parse<'a> {
pub fn into_owned(self) -> ParseIntoOwned<'a> {
ParseIntoOwned { inner: self }
}
}
#[derive(Debug)]
pub struct ParseIntoOwned<'a> {
inner: Parse<'a>,
}
impl<'a> Iterator for ParseIntoOwned<'a> {
type Item = (String, String);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, v)| (k.into_owned(), v.into_owned()))
}
}
pub fn byte_serialize(input: &[u8]) -> ByteSerialize<'_> {
ByteSerialize { bytes: input }
}
#[derive(Debug)]
pub struct ByteSerialize<'a> {
bytes: &'a [u8],
}
fn byte_serialized_unchanged(byte: u8) -> bool {
matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
}
impl<'a> Iterator for ByteSerialize<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if let Some((&first, tail)) = self.bytes.split_first() {
if !byte_serialized_unchanged(first) {
self.bytes = tail;
return Some(if first == b' ' { "+" } else { percent_encode_byte(first) });
}
let position = tail.iter().position(|&b| !byte_serialized_unchanged(b));
let (unchanged_slice, remaining) = match position {
Some(i) => self.bytes.split_at(1 + i),
None => (self.bytes, &[][..]),
};
self.bytes = remaining;
Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.bytes.is_empty() {
(0, Some(0))
} else {
(1, Some(self.bytes.len()))
}
}
}
#[derive(Debug)]
pub struct Serializer<T: Target> {
target: Option<T>,
start_position: usize,
encoding: EncodingOverride,
custom_encoding: Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<'_, [u8]>>>>,
}
struct SilentDebug<T>(T);
impl<T> fmt::Debug for SilentDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("…")
}
}
pub trait Target {
fn as_mut_string(&mut self) -> &mut String;
fn finish(self) -> Self::Finished;
type Finished;
}
impl Target for String {
fn as_mut_string(&mut self) -> &mut String {
self
}
fn finish(self) -> Self {
self
}
type Finished = Self;
}
impl<'a> Target for &'a mut String {
fn as_mut_string(&mut self) -> &mut String {
&mut **self
}
fn finish(self) -> Self {
self
}
type Finished = Self;
}
impl<'a> Target for crate::UrlQuery<'a> {
fn as_mut_string(&mut self) -> &mut String {
&mut self.url.as_mut().unwrap().serialization
}
fn finish(mut self) -> &'a mut crate::Url {
let url = self.url.take().unwrap();
url.restore_already_parsed_fragment(self.fragment.take());
url
}
type Finished = &'a mut crate::Url;
}
impl<T: Target> Serializer<T> {
pub fn new(target: T) -> Self {
Self::for_suffix(target, 0)
}
pub fn for_suffix(mut target: T, start_position: usize) -> Self {
&target.as_mut_string()[start_position..];
Serializer {
target: Some(target),
start_position,
encoding: EncodingOverride::utf8(),
custom_encoding: None,
}
}
pub fn clear(&mut self) -> &mut Self {
string(&mut self.target).truncate(self.start_position);
self
}
#[cfg(feature = "query_encoding")]
pub fn encoding_override(&mut self, new: Option<::encoding::EncodingRef>) -> &mut Self {
self.encoding = EncodingOverride::from_opt_encoding(new).to_output_encoding();
self
}
pub fn custom_encoding_override<F>(&mut self, encode: F) -> &mut Self
where
F: FnMut(&str) -> Cow<'_, [u8]> + 'static,
{
self.custom_encoding = Some(SilentDebug(Box::new(encode)));
self
}
pub fn append_pair(&mut self, name: &str, value: &str) -> &mut Self {
append_pair(
string(&mut self.target),
self.start_position,
self.encoding,
&mut self.custom_encoding,
name,
value,
);
self
}
pub fn extend_pairs<I, K, V>(&mut self, iter: I) -> &mut Self
where
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: AsRef<str>,
{
{
let string = string(&mut self.target);
for pair in iter {
let &(ref k, ref v) = pair.borrow();
append_pair(
string,
self.start_position,
self.encoding,
&mut self.custom_encoding,
k.as_ref(),
v.as_ref(),
);
}
}
self
}
#[cfg(feature = "query_encoding")]
pub fn append_charset(&mut self) -> &mut Self {
assert!(
self.custom_encoding.is_none(),
"Cannot use both custom_encoding_override() and append_charset()"
);
{
let string = string(&mut self.target);
append_separator_if_needed(string, self.start_position);
string.push_str("_charset_=");
string.push_str(self.encoding.name());
}
self
}
pub fn finish(&mut self) -> T::Finished {
self.target.take().expect("url::form_urlencoded::Serializer double finish").finish()
}
}
fn append_separator_if_needed(string: &mut String, start_position: usize) {
if string.len() > start_position {
string.push('&')
}
}
fn string<T: Target>(target: &mut Option<T>) -> &mut String {
target.as_mut().expect("url::form_urlencoded::Serializer finished").as_mut_string()
}
fn append_pair(
string: &mut String,
start_position: usize,
encoding: EncodingOverride,
custom_encoding: &mut Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<'_, [u8]>>>>,
name: &str,
value: &str,
) {
append_separator_if_needed(string, start_position);
append_encoded(name, string, encoding, custom_encoding);
string.push('=');
append_encoded(value, string, encoding, custom_encoding);
}
fn append_encoded(
s: &str,
string: &mut String,
encoding: EncodingOverride,
custom_encoding: &mut Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<'_, [u8]>>>>,
) {
let bytes = if let Some(SilentDebug(ref mut custom)) = *custom_encoding {
custom(s)
} else {
encoding.encode(s.into())
};
string.extend(byte_serialize(&bytes));
}