// Copyright 2013-2016 The rust-url developers. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Parser and serializer for the [`application/x-www-form-urlencoded` syntax]( //! http://url.spec.whatwg.org/#application/x-www-form-urlencoded), //! as used by HTML forms. //! //! Converts between a string (such as an URL’s query string) //! and a sequence of (name, value) pairs. #![no_std]
// For forwards compatibility #[cfg(feature = "std")] externcrate std as _;
externcrate alloc;
#[cfg(not(feature = "alloc"))]
compile_error!("the `alloc` feature must currently be enabled");
use alloc::borrow::{Borrow, Cow, ToOwned}; use alloc::string::String; use core::str; use percent_encoding::{percent_decode, percent_encode_byte};
/// Convert a byte string in the `application/x-www-form-urlencoded` syntax /// into a iterator of (name, value) pairs. /// /// Use `parse(input.as_bytes())` to parse a `&str` string. /// /// The names and values are percent-decoded. For instance, `%23first=%25try%25` will be /// converted to `[("#first", "%try%")]`. #[inline] pubfn parse(input: &[u8]) -> Parse<'_> {
Parse { input }
} /// The return type of `parse()`. #[derive(Copy, Clone)] pubstruct Parse<'a> {
input: &'a [u8],
}
impl<'a> Iterator for Parse<'a> { type Item = (Cow<'a, str>, Cow<'a, str>);
fn next(&mutself) -> Option<Self::Item> { loop { ifself.input.is_empty() { return None;
} letmut split2 = self.input.splitn(2, |&b| b == b'&'); let sequence = split2.next().unwrap(); self.input = split2.next().unwrap_or(&[][..]); if sequence.is_empty() { continue;
} letmut split2 = sequence.splitn(2, |&b| b == b'='); let name = split2.next().unwrap(); let value = split2.next().unwrap_or(&[][..]); return Some((decode(name), decode(value)));
}
}
}
/// Replace b'+' with b' ' fn replace_plus(input: &[u8]) -> Cow<'_, [u8]> { match input.iter().position(|&b| b == b'+') {
None => Cow::Borrowed(input),
Some(first_position) => { letmut 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> { /// Return a new iterator that yields pairs of `String` instead of pairs of `Cow<str>`. pubfn into_owned(self) -> ParseIntoOwned<'a> {
ParseIntoOwned { inner: self }
}
}
/// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`. pubstruct ParseIntoOwned<'a> {
inner: Parse<'a>,
}
impl<'a> Iterator for ParseIntoOwned<'a> { type Item = (String, String);
impl<'a> Iterator for ByteSerialize<'a> { type Item = &'a str;
fn next(&mutself) -> Option<&'a str> { iflet 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 { // 1 for first_byte + i unchanged in tail
Some(i) => self.bytes.split_at(1 + i),
None => (self.bytes, &[][..]),
}; self.bytes = remaining; // This unsafe is appropriate because we have already checked these // bytes in byte_serialized_unchanged, which checks for a subset // of UTF-8. So we know these bytes are valid UTF-8, and doing // another UTF-8 check would be wasteful.
Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
} else {
None
}
}
impl<'a, T: Target> Serializer<'a, T> { /// Create a new `application/x-www-form-urlencoded` serializer for the given target. /// /// If the target is non-empty, /// its content is assumed to already be in `application/x-www-form-urlencoded` syntax. pubfn new(target: T) -> Self { Self::for_suffix(target, 0)
}
/// Create a new `application/x-www-form-urlencoded` serializer /// for a suffix of the given target. /// /// If that suffix is non-empty, /// its content is assumed to already be in `application/x-www-form-urlencoded` syntax. pubfn for_suffix(mut target: T, start_position: usize) -> Self { if target.as_mut_string().len() < start_position {
panic!( "invalid length {} for target of length {}",
start_position,
target.as_mut_string().len()
);
}
/// Remove any existing name/value pair. /// /// Panics if called after `.finish()`. pubfn clear(&mutself) -> &mutSelf {
string(&mutself.target).truncate(self.start_position); self
}
/// Set the character encoding to be used for names and values before percent-encoding. pubfn encoding_override(&mutself, new: EncodingOverride<'a>) -> &mut Self { self.encoding = new; self
}
/// Serialize and append a name/value pair. /// /// Panics if called after `.finish()`. pubfn append_pair(&mutself, name: &str, value: &str) -> &='color:red'>mutSelf {
append_pair(
string(&mutself.target), self.start_position, self.encoding,
name,
value,
); self
}
/// Serialize and append a name of parameter without any value. /// /// Panics if called after `.finish()`. pubfn append_key_only(&mutself, name: &str) -> &mutSelf {
append_key_only(
string(&mutself.target), self.start_position, self.encoding,
name,
); self
}
/// Serialize and append a number of name/value pairs. /// /// This simply calls `append_pair` repeatedly. /// This can be more convenient, so the user doesn’t need to introduce a block /// to limit the scope of `Serializer`’s borrow of its string. /// /// Panics if called after `.finish()`. pubfn extend_pairs<I, K, V>(&mutself, iter: I) -> &mutSelf where
I: IntoIterator,
I::Item: Borrow<(K, V)>,
K: AsRef<str>,
V: AsRef<str>,
{
{ let string = string(&mutself.target); for pair in iter { let (k, v) = pair.borrow();
append_pair(
string, self.start_position, self.encoding,
k.as_ref(),
v.as_ref(),
);
}
} self
}
/// Serialize and append a number of names without values. /// /// This simply calls `append_key_only` repeatedly. /// This can be more convenient, so the user doesn’t need to introduce a block /// to limit the scope of `Serializer`’s borrow of its string. /// /// Panics if called after `.finish()`. pubfn extend_keys_only<I, K>(&mutself, iter: I) -> &mutSelf where
I: IntoIterator,
I::Item: Borrow<K>,
K: AsRef<str>,
{
{ let string = string(&mutself.target); for key in iter { let k = key.borrow().as_ref();
append_key_only(string, self.start_position, self.encoding, k);
}
} self
}
/// If this serializer was constructed with a string, take and return that string. /// /// ```rust /// use form_urlencoded; /// let encoded: String = form_urlencoded::Serializer::new(String::new()) /// .append_pair("foo", "bar & baz") /// .append_pair("saison", "Été+hiver") /// .finish(); /// assert_eq!(encoded, "foo=bar+%26+baz&saison=%C3%89t%C3%A9%2Bhiver"); /// ``` /// /// Panics if called more than once. pubfn finish(&mutself) -> T::Finished { self.target
.take()
.expect("url::form_urlencoded::Serializer double finish")
.finish()
}
}
pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> { // Note: This function is duplicated in `percent_encoding/lib.rs`. match input {
Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
Cow::Owned(bytes) => { match String::from_utf8_lossy(&bytes) {
Cow::Borrowed(utf8) => { // If from_utf8_lossy returns a Cow::Borrowed, then we can // be sure our original bytes were valid UTF-8. This is because // if the bytes were invalid UTF-8 from_utf8_lossy would have // to allocate a new owned string to back the Cow so it could // replace invalid bytes with a placeholder.
// First we do a debug_assert to confirm our description above. let raw_utf8: *const [u8] = utf8.as_bytes();
debug_assert!(raw_utf8 == &*bytes as *const [u8]);
// Given we know the original input bytes are valid UTF-8, // and we have ownership of those bytes, we re-use them and // return a Cow::Owned here.
Cow::Owned(unsafe { String::from_utf8_unchecked(bytes) })
}
Cow::Owned(s) => Cow::Owned(s),
}
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.