Skip to main content

form_urlencoded/
lib.rs

1// Copyright 2013-2016 The rust-url developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Parser and serializer for the [`application/x-www-form-urlencoded` syntax](
10//! http://url.spec.whatwg.org/#application/x-www-form-urlencoded),
11//! as used by HTML forms.
12//!
13//! Converts between a string (such as an URL’s query string)
14//! and a sequence of (name, value) pairs.
15#![no_std]
16
17// For forwards compatibility
18#[cfg(feature = "std")]
19extern crate std as _;
20
21extern crate alloc;
22
23#[cfg(not(feature = "alloc"))]
24compile_error!("the `alloc` feature must currently be enabled");
25
26use alloc::borrow::{Borrow, Cow, ToOwned};
27use alloc::string::String;
28use core::str;
29use percent_encoding::{percent_decode, percent_encode_byte};
30
31/// Convert a byte string in the `application/x-www-form-urlencoded` syntax
32/// into a iterator of (name, value) pairs.
33///
34/// Use `parse(input.as_bytes())` to parse a `&str` string.
35///
36/// The names and values are percent-decoded. For instance, `%23first=%25try%25` will be
37/// converted to `[("#first", "%try%")]`.
38#[inline]
39pub fn parse(input: &[u8]) -> Parse<'_> {
40    Parse { input }
41}
42/// The return type of `parse()`.
43#[derive(Copy, Clone)]
44pub struct Parse<'a> {
45    input: &'a [u8],
46}
47
48impl<'a> Iterator for Parse<'a> {
49    type Item = (Cow<'a, str>, Cow<'a, str>);
50
51    fn next(&mut self) -> Option<Self::Item> {
52        loop {
53            if self.input.is_empty() {
54                return None;
55            }
56            let mut split2 = self.input.splitn(2, |&b| b == b'&');
57            let sequence = split2.next().unwrap();
58            self.input = split2.next().unwrap_or(&[][..]);
59            if sequence.is_empty() {
60                continue;
61            }
62            let mut split2 = sequence.splitn(2, |&b| b == b'=');
63            let name = split2.next().unwrap();
64            let value = split2.next().unwrap_or(&[][..]);
65            return Some((decode(name), decode(value)));
66        }
67    }
68}
69
70fn decode(input: &[u8]) -> Cow<'_, str> {
71    let replaced = replace_plus(input);
72    decode_utf8_lossy(match percent_decode(&replaced).into() {
73        Cow::Owned(vec) => Cow::Owned(vec),
74        Cow::Borrowed(_) => replaced,
75    })
76}
77
78/// Replace b'+' with b' '
79fn replace_plus(input: &[u8]) -> Cow<'_, [u8]> {
80    match input.iter().position(|&b| b == b'+') {
81        None => Cow::Borrowed(input),
82        Some(first_position) => {
83            let mut replaced = input.to_owned();
84            replaced[first_position] = b' ';
85            for byte in &mut replaced[first_position + 1..] {
86                if *byte == b'+' {
87                    *byte = b' ';
88                }
89            }
90            Cow::Owned(replaced)
91        }
92    }
93}
94
95impl<'a> Parse<'a> {
96    /// Return a new iterator that yields pairs of `String` instead of pairs of `Cow<str>`.
97    pub fn into_owned(self) -> ParseIntoOwned<'a> {
98        ParseIntoOwned { inner: self }
99    }
100}
101
102/// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
103pub struct ParseIntoOwned<'a> {
104    inner: Parse<'a>,
105}
106
107impl Iterator for ParseIntoOwned<'_> {
108    type Item = (String, String);
109
110    fn next(&mut self) -> Option<Self::Item> {
111        self.inner
112            .next()
113            .map(|(k, v)| (k.into_owned(), v.into_owned()))
114    }
115}
116
117/// The [`application/x-www-form-urlencoded` byte serializer](
118/// https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer).
119///
120/// Return an iterator of `&str` slices.
121pub fn byte_serialize(input: &[u8]) -> ByteSerialize<'_> {
122    ByteSerialize { bytes: input }
123}
124
125/// Return value of `byte_serialize()`.
126#[derive(Debug)]
127pub struct ByteSerialize<'a> {
128    bytes: &'a [u8],
129}
130
131fn byte_serialized_unchanged(byte: u8) -> bool {
132    matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
133}
134
135impl<'a> Iterator for ByteSerialize<'a> {
136    type Item = &'a str;
137
138    fn next(&mut self) -> Option<&'a str> {
139        if let Some((&first, tail)) = self.bytes.split_first() {
140            if !byte_serialized_unchanged(first) {
141                self.bytes = tail;
142                return Some(if first == b' ' {
143                    "+"
144                } else {
145                    percent_encode_byte(first)
146                });
147            }
148            let position = tail.iter().position(|&b| !byte_serialized_unchanged(b));
149            let (unchanged_slice, remaining) = match position {
150                // 1 for first_byte + i unchanged in tail
151                Some(i) => self.bytes.split_at(1 + i),
152                None => (self.bytes, &[][..]),
153            };
154            self.bytes = remaining;
155            // This unsafe is appropriate because we have already checked these
156            // bytes in byte_serialized_unchanged, which checks for a subset
157            // of UTF-8. So we know these bytes are valid UTF-8, and doing
158            // another UTF-8 check would be wasteful.
159            Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
160        } else {
161            None
162        }
163    }
164
165    fn size_hint(&self) -> (usize, Option<usize>) {
166        if self.bytes.is_empty() {
167            (0, Some(0))
168        } else {
169            (1, Some(self.bytes.len()))
170        }
171    }
172}
173
174/// The [`application/x-www-form-urlencoded` serializer](
175/// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
176pub struct Serializer<'a, T: Target> {
177    target: Option<T>,
178    start_position: usize,
179    encoding: EncodingOverride<'a>,
180}
181
182pub trait Target {
183    fn as_mut_string(&mut self) -> &mut String;
184    fn finish(self) -> Self::Finished;
185    type Finished;
186}
187
188impl Target for String {
189    fn as_mut_string(&mut self) -> &mut String {
190        self
191    }
192    fn finish(self) -> Self {
193        self
194    }
195    type Finished = Self;
196}
197
198impl Target for &mut String {
199    fn as_mut_string(&mut self) -> &mut String {
200        self
201    }
202    fn finish(self) -> Self {
203        self
204    }
205    type Finished = Self;
206}
207
208impl<'a, T: Target> Serializer<'a, T> {
209    /// Create a new `application/x-www-form-urlencoded` serializer for the given target.
210    ///
211    /// If the target is non-empty,
212    /// its content is assumed to already be in `application/x-www-form-urlencoded` syntax.
213    pub fn new(target: T) -> Self {
214        Self::for_suffix(target, 0)
215    }
216
217    /// Create a new `application/x-www-form-urlencoded` serializer
218    /// for a suffix of the given target.
219    ///
220    /// If that suffix is non-empty,
221    /// its content is assumed to already be in `application/x-www-form-urlencoded` syntax.
222    /// If `start_position` exceeds the target's length, it is set to the target's length.
223    pub fn for_suffix(mut target: T, start_position: usize) -> Self {
224        let start_position = start_position.min(target.as_mut_string().len());
225
226        Serializer {
227            target: Some(target),
228            start_position,
229            encoding: None,
230        }
231    }
232
233    /// Remove any existing name/value pair.
234    ///
235    /// Panics if called after `.finish()`.
236    pub fn clear(&mut self) -> &mut Self {
237        string(&mut self.target).truncate(self.start_position);
238        self
239    }
240
241    /// Set the character encoding to be used for names and values before percent-encoding.
242    pub fn encoding_override(&mut self, new: EncodingOverride<'a>) -> &mut Self {
243        self.encoding = new;
244        self
245    }
246
247    /// Serialize and append a name/value pair.
248    ///
249    /// Panics if called after `.finish()`.
250    pub fn append_pair(&mut self, name: &str, value: &str) -> &mut Self {
251        append_pair(
252            string(&mut self.target),
253            self.start_position,
254            self.encoding,
255            name,
256            value,
257        );
258        self
259    }
260
261    /// Serialize and append a name of parameter without any value.
262    ///
263    /// Panics if called after `.finish()`.
264    pub fn append_key_only(&mut self, name: &str) -> &mut Self {
265        append_key_only(
266            string(&mut self.target),
267            self.start_position,
268            self.encoding,
269            name,
270        );
271        self
272    }
273
274    /// Serialize and append a number of name/value pairs.
275    ///
276    /// This simply calls `append_pair` repeatedly.
277    /// This can be more convenient, so the user doesn’t need to introduce a block
278    /// to limit the scope of `Serializer`’s borrow of its string.
279    ///
280    /// Panics if called after `.finish()`.
281    pub fn extend_pairs<I, K, V>(&mut self, iter: I) -> &mut Self
282    where
283        I: IntoIterator,
284        I::Item: Borrow<(K, V)>,
285        K: AsRef<str>,
286        V: AsRef<str>,
287    {
288        {
289            let string = string(&mut self.target);
290            for pair in iter {
291                let (k, v) = pair.borrow();
292                append_pair(
293                    string,
294                    self.start_position,
295                    self.encoding,
296                    k.as_ref(),
297                    v.as_ref(),
298                );
299            }
300        }
301        self
302    }
303
304    /// Serialize and append a number of names without values.
305    ///
306    /// This simply calls `append_key_only` repeatedly.
307    /// This can be more convenient, so the user doesn’t need to introduce a block
308    /// to limit the scope of `Serializer`’s borrow of its string.
309    ///
310    /// Panics if called after `.finish()`.
311    pub fn extend_keys_only<I, K>(&mut self, iter: I) -> &mut Self
312    where
313        I: IntoIterator,
314        I::Item: Borrow<K>,
315        K: AsRef<str>,
316    {
317        {
318            let string = string(&mut self.target);
319            for key in iter {
320                let k = key.borrow().as_ref();
321                append_key_only(string, self.start_position, self.encoding, k);
322            }
323        }
324        self
325    }
326
327    /// If this serializer was constructed with a string, take and return that string.
328    ///
329    /// ```rust
330    /// use form_urlencoded;
331    /// let encoded: String = form_urlencoded::Serializer::new(String::new())
332    ///     .append_pair("foo", "bar & baz")
333    ///     .append_pair("saison", "Été+hiver")
334    ///     .finish();
335    /// assert_eq!(encoded, "foo=bar+%26+baz&saison=%C3%89t%C3%A9%2Bhiver");
336    /// ```
337    ///
338    /// Panics if called more than once.
339    pub fn finish(&mut self) -> T::Finished {
340        self.target
341            .take()
342            .expect("url::form_urlencoded::Serializer double finish")
343            .finish()
344    }
345}
346
347fn append_separator_if_needed(string: &mut String, start_position: usize) {
348    if string.len() > start_position {
349        string.push('&')
350    }
351}
352
353fn string<T: Target>(target: &mut Option<T>) -> &mut String {
354    target
355        .as_mut()
356        .expect("url::form_urlencoded::Serializer finished")
357        .as_mut_string()
358}
359
360fn append_pair(
361    string: &mut String,
362    start_position: usize,
363    encoding: EncodingOverride<'_>,
364    name: &str,
365    value: &str,
366) {
367    append_separator_if_needed(string, start_position);
368    append_encoded(name, string, encoding);
369    string.push('=');
370    append_encoded(value, string, encoding);
371}
372
373fn append_key_only(
374    string: &mut String,
375    start_position: usize,
376    encoding: EncodingOverride,
377    name: &str,
378) {
379    append_separator_if_needed(string, start_position);
380    append_encoded(name, string, encoding);
381}
382
383fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride<'_>) {
384    string.extend(byte_serialize(&encode(encoding, s)))
385}
386
387pub(crate) fn encode<'a>(encoding_override: EncodingOverride<'_>, input: &'a str) -> Cow<'a, [u8]> {
388    if let Some(o) = encoding_override {
389        return o(input);
390    }
391    input.as_bytes().into()
392}
393
394// std::ptr::addr_eq was stabilized in rust 1.76. Once we upgrade
395// the MSRV we can remove this lint override.
396#[allow(ambiguous_wide_pointer_comparisons)]
397pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
398    // Note: This function is duplicated in `percent_encoding/lib.rs`.
399    match input {
400        Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
401        Cow::Owned(bytes) => {
402            match String::from_utf8_lossy(&bytes) {
403                Cow::Borrowed(utf8) => {
404                    // If from_utf8_lossy returns a Cow::Borrowed, then we can
405                    // be sure our original bytes were valid UTF-8. This is because
406                    // if the bytes were invalid UTF-8 from_utf8_lossy would have
407                    // to allocate a new owned string to back the Cow so it could
408                    // replace invalid bytes with a placeholder.
409
410                    // First we do a debug_assert to confirm our description above.
411                    let raw_utf8: *const [u8] = utf8.as_bytes();
412                    debug_assert!(core::ptr::eq(raw_utf8, &*bytes));
413
414                    // Given we know the original input bytes are valid UTF-8,
415                    // and we have ownership of those bytes, we re-use them and
416                    // return a Cow::Owned here.
417                    Cow::Owned(unsafe { String::from_utf8_unchecked(bytes) })
418                }
419                Cow::Owned(s) => Cow::Owned(s),
420            }
421        }
422    }
423}
424
425pub type EncodingOverride<'a> = Option<&'a dyn Fn(&str) -> Cow<'_, [u8]>>;