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 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
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 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 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 {
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 pub fn clear(&mut self) -> &mut Self {
237 string(&mut self.target).truncate(self.start_position);
238 self
239 }
240
241 pub fn encoding_override(&mut self, new: EncodingOverride<'a>) -> &mut Self {
243 self.encoding = new;
244 self
245 }
246
247 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 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 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 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 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#[allow(ambiguous_wide_pointer_comparisons)]
397pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
398 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 let raw_utf8: *const [u8] = utf8.as_bytes();
412 debug_assert!(core::ptr::eq(raw_utf8, &*bytes));
413
414 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]>>;