1#![no_std]
16
17#[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#[inline]
39pub fn parse(input: &[u8]) -> Parse<'_> {
40 Parse { input }
41}
42#[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
78fn 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 pub fn into_owned(self) -> ParseIntoOwned<'a> {
98 ParseIntoOwned { inner: self }
99 }
100}
101
102pub struct ParseIntoOwned<'a> {
104 inner: Parse<'a>,
105}
106
107impl<'a> Iterator for ParseIntoOwned<'a> {
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
117pub fn byte_serialize(input: &[u8]) -> ByteSerialize<'_> {
122 ByteSerialize { bytes: input }
123}
124
125#[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 Some(i) => self.bytes.split_at(1 + i),
152 None => (self.bytes, &[][..]),
153 };
154 self.bytes = remaining;
155 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
174pub 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<'a> Target for &'a 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 pub fn new(target: T) -> Self {
214 Self::for_suffix(target, 0)
215 }
216
217 pub fn for_suffix(mut target: T, start_position: usize) -> Self {
223 if target.as_mut_string().len() < start_position {
224 panic!(
225 "invalid length {} for target of length {}",
226 start_position,
227 target.as_mut_string().len()
228 );
229 }
230
231 Serializer {
232 target: Some(target),
233 start_position,
234 encoding: None,
235 }
236 }
237
238 pub fn clear(&mut self) -> &mut Self {
242 string(&mut self.target).truncate(self.start_position);
243 self
244 }
245
246 pub fn encoding_override(&mut self, new: EncodingOverride<'a>) -> &mut Self {
248 self.encoding = new;
249 self
250 }
251
252 pub fn append_pair(&mut self, name: &str, value: &str) -> &mut Self {
256 append_pair(
257 string(&mut self.target),
258 self.start_position,
259 self.encoding,
260 name,
261 value,
262 );
263 self
264 }
265
266 pub fn append_key_only(&mut self, name: &str) -> &mut Self {
270 append_key_only(
271 string(&mut self.target),
272 self.start_position,
273 self.encoding,
274 name,
275 );
276 self
277 }
278
279 pub fn extend_pairs<I, K, V>(&mut self, iter: I) -> &mut Self
287 where
288 I: IntoIterator,
289 I::Item: Borrow<(K, V)>,
290 K: AsRef<str>,
291 V: AsRef<str>,
292 {
293 {
294 let string = string(&mut self.target);
295 for pair in iter {
296 let (k, v) = pair.borrow();
297 append_pair(
298 string,
299 self.start_position,
300 self.encoding,
301 k.as_ref(),
302 v.as_ref(),
303 );
304 }
305 }
306 self
307 }
308
309 pub fn extend_keys_only<I, K>(&mut self, iter: I) -> &mut Self
317 where
318 I: IntoIterator,
319 I::Item: Borrow<K>,
320 K: AsRef<str>,
321 {
322 {
323 let string = string(&mut self.target);
324 for key in iter {
325 let k = key.borrow().as_ref();
326 append_key_only(string, self.start_position, self.encoding, k);
327 }
328 }
329 self
330 }
331
332 pub fn finish(&mut self) -> T::Finished {
345 self.target
346 .take()
347 .expect("url::form_urlencoded::Serializer double finish")
348 .finish()
349 }
350}
351
352fn append_separator_if_needed(string: &mut String, start_position: usize) {
353 if string.len() > start_position {
354 string.push('&')
355 }
356}
357
358fn string<T: Target>(target: &mut Option<T>) -> &mut String {
359 target
360 .as_mut()
361 .expect("url::form_urlencoded::Serializer finished")
362 .as_mut_string()
363}
364
365fn append_pair(
366 string: &mut String,
367 start_position: usize,
368 encoding: EncodingOverride<'_>,
369 name: &str,
370 value: &str,
371) {
372 append_separator_if_needed(string, start_position);
373 append_encoded(name, string, encoding);
374 string.push('=');
375 append_encoded(value, string, encoding);
376}
377
378fn append_key_only(
379 string: &mut String,
380 start_position: usize,
381 encoding: EncodingOverride,
382 name: &str,
383) {
384 append_separator_if_needed(string, start_position);
385 append_encoded(name, string, encoding);
386}
387
388fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride<'_>) {
389 string.extend(byte_serialize(&encode(encoding, s)))
390}
391
392pub(crate) fn encode<'a>(encoding_override: EncodingOverride<'_>, input: &'a str) -> Cow<'a, [u8]> {
393 if let Some(o) = encoding_override {
394 return o(input);
395 }
396 input.as_bytes().into()
397}
398
399pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
400 match input {
402 Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
403 Cow::Owned(bytes) => {
404 match String::from_utf8_lossy(&bytes) {
405 Cow::Borrowed(utf8) => {
406 let raw_utf8: *const [u8] = utf8.as_bytes();
414 debug_assert!(raw_utf8 == &*bytes as *const [u8]);
415
416 Cow::Owned(unsafe { String::from_utf8_unchecked(bytes) })
420 }
421 Cow::Owned(s) => Cow::Owned(s),
422 }
423 }
424 }
425}
426
427pub type EncodingOverride<'a> = Option<&'a dyn Fn(&str) -> Cow<'_, [u8]>>;