1use percent_encoding::{percent_decode, percent_encode_byte};
17use std::borrow::{Borrow, Cow};
18use std::str;
19
20#[inline]
28pub fn parse(input: &[u8]) -> Parse<'_> {
29 Parse { input }
30}
31#[derive(Copy, Clone)]
33pub struct Parse<'a> {
34 input: &'a [u8],
35}
36
37impl<'a> Iterator for Parse<'a> {
38 type Item = (Cow<'a, str>, Cow<'a, str>);
39
40 fn next(&mut self) -> Option<Self::Item> {
41 loop {
42 if self.input.is_empty() {
43 return None;
44 }
45 let mut split2 = self.input.splitn(2, |&b| b == b'&');
46 let sequence = split2.next().unwrap();
47 self.input = split2.next().unwrap_or(&[][..]);
48 if sequence.is_empty() {
49 continue;
50 }
51 let mut split2 = sequence.splitn(2, |&b| b == b'=');
52 let name = split2.next().unwrap();
53 let value = split2.next().unwrap_or(&[][..]);
54 return Some((decode(name), decode(value)));
55 }
56 }
57}
58
59fn decode(input: &[u8]) -> Cow<'_, str> {
60 let replaced = replace_plus(input);
61 decode_utf8_lossy(match percent_decode(&replaced).into() {
62 Cow::Owned(vec) => Cow::Owned(vec),
63 Cow::Borrowed(_) => replaced,
64 })
65}
66
67fn replace_plus(input: &[u8]) -> Cow<'_, [u8]> {
69 match input.iter().position(|&b| b == b'+') {
70 None => Cow::Borrowed(input),
71 Some(first_position) => {
72 let mut replaced = input.to_owned();
73 replaced[first_position] = b' ';
74 for byte in &mut replaced[first_position + 1..] {
75 if *byte == b'+' {
76 *byte = b' ';
77 }
78 }
79 Cow::Owned(replaced)
80 }
81 }
82}
83
84impl<'a> Parse<'a> {
85 pub fn into_owned(self) -> ParseIntoOwned<'a> {
87 ParseIntoOwned { inner: self }
88 }
89}
90
91pub struct ParseIntoOwned<'a> {
93 inner: Parse<'a>,
94}
95
96impl<'a> Iterator for ParseIntoOwned<'a> {
97 type Item = (String, String);
98
99 fn next(&mut self) -> Option<Self::Item> {
100 self.inner
101 .next()
102 .map(|(k, v)| (k.into_owned(), v.into_owned()))
103 }
104}
105
106pub fn byte_serialize(input: &[u8]) -> ByteSerialize<'_> {
111 ByteSerialize { bytes: input }
112}
113
114#[derive(Debug)]
116pub struct ByteSerialize<'a> {
117 bytes: &'a [u8],
118}
119
120fn byte_serialized_unchanged(byte: u8) -> bool {
121 matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
122}
123
124impl<'a> Iterator for ByteSerialize<'a> {
125 type Item = &'a str;
126
127 fn next(&mut self) -> Option<&'a str> {
128 if let Some((&first, tail)) = self.bytes.split_first() {
129 if !byte_serialized_unchanged(first) {
130 self.bytes = tail;
131 return Some(if first == b' ' {
132 "+"
133 } else {
134 percent_encode_byte(first)
135 });
136 }
137 let position = tail.iter().position(|&b| !byte_serialized_unchanged(b));
138 let (unchanged_slice, remaining) = match position {
139 Some(i) => self.bytes.split_at(1 + i),
141 None => (self.bytes, &[][..]),
142 };
143 self.bytes = remaining;
144 Some(unsafe { str::from_utf8_unchecked(unchanged_slice) })
149 } else {
150 None
151 }
152 }
153
154 fn size_hint(&self) -> (usize, Option<usize>) {
155 if self.bytes.is_empty() {
156 (0, Some(0))
157 } else {
158 (1, Some(self.bytes.len()))
159 }
160 }
161}
162
163pub struct Serializer<'a, T: Target> {
166 target: Option<T>,
167 start_position: usize,
168 encoding: EncodingOverride<'a>,
169}
170
171pub trait Target {
172 fn as_mut_string(&mut self) -> &mut String;
173 fn finish(self) -> Self::Finished;
174 type Finished;
175}
176
177impl Target for String {
178 fn as_mut_string(&mut self) -> &mut String {
179 self
180 }
181 fn finish(self) -> Self {
182 self
183 }
184 type Finished = Self;
185}
186
187impl<'a> Target for &'a mut String {
188 fn as_mut_string(&mut self) -> &mut String {
189 &mut **self
190 }
191 fn finish(self) -> Self {
192 self
193 }
194 type Finished = Self;
195}
196
197impl<'a, T: Target> Serializer<'a, T> {
198 pub fn new(target: T) -> Self {
203 Self::for_suffix(target, 0)
204 }
205
206 pub fn for_suffix(mut target: T, start_position: usize) -> Self {
212 if target.as_mut_string().len() < start_position {
213 panic!(
214 "invalid length {} for target of length {}",
215 start_position,
216 target.as_mut_string().len()
217 );
218 }
219
220 Serializer {
221 target: Some(target),
222 start_position,
223 encoding: None,
224 }
225 }
226
227 pub fn clear(&mut self) -> &mut Self {
231 string(&mut self.target).truncate(self.start_position);
232 self
233 }
234
235 pub fn encoding_override(&mut self, new: EncodingOverride<'a>) -> &mut Self {
237 self.encoding = new;
238 self
239 }
240
241 pub fn append_pair(&mut self, name: &str, value: &str) -> &mut Self {
245 append_pair(
246 string(&mut self.target),
247 self.start_position,
248 self.encoding,
249 name,
250 value,
251 );
252 self
253 }
254
255 pub fn append_key_only(&mut self, name: &str) -> &mut Self {
259 append_key_only(
260 string(&mut self.target),
261 self.start_position,
262 self.encoding,
263 name,
264 );
265 self
266 }
267
268 pub fn extend_pairs<I, K, V>(&mut self, iter: I) -> &mut Self
276 where
277 I: IntoIterator,
278 I::Item: Borrow<(K, V)>,
279 K: AsRef<str>,
280 V: AsRef<str>,
281 {
282 {
283 let string = string(&mut self.target);
284 for pair in iter {
285 let &(ref k, ref v) = pair.borrow();
286 append_pair(
287 string,
288 self.start_position,
289 self.encoding,
290 k.as_ref(),
291 v.as_ref(),
292 );
293 }
294 }
295 self
296 }
297
298 pub fn extend_keys_only<I, K>(&mut self, iter: I) -> &mut Self
306 where
307 I: IntoIterator,
308 I::Item: Borrow<K>,
309 K: AsRef<str>,
310 {
311 {
312 let string = string(&mut self.target);
313 for key in iter {
314 let k = key.borrow().as_ref();
315 append_key_only(string, self.start_position, self.encoding, k);
316 }
317 }
318 self
319 }
320
321 pub fn finish(&mut self) -> T::Finished {
334 self.target
335 .take()
336 .expect("url::form_urlencoded::Serializer double finish")
337 .finish()
338 }
339}
340
341fn append_separator_if_needed(string: &mut String, start_position: usize) {
342 if string.len() > start_position {
343 string.push('&')
344 }
345}
346
347fn string<T: Target>(target: &mut Option<T>) -> &mut String {
348 target
349 .as_mut()
350 .expect("url::form_urlencoded::Serializer finished")
351 .as_mut_string()
352}
353
354fn append_pair(
355 string: &mut String,
356 start_position: usize,
357 encoding: EncodingOverride<'_>,
358 name: &str,
359 value: &str,
360) {
361 append_separator_if_needed(string, start_position);
362 append_encoded(name, string, encoding);
363 string.push('=');
364 append_encoded(value, string, encoding);
365}
366
367fn append_key_only(
368 string: &mut String,
369 start_position: usize,
370 encoding: EncodingOverride,
371 name: &str,
372) {
373 append_separator_if_needed(string, start_position);
374 append_encoded(name, string, encoding);
375}
376
377fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride<'_>) {
378 string.extend(byte_serialize(&encode(encoding, s)))
379}
380
381pub(crate) fn encode<'a>(encoding_override: EncodingOverride<'_>, input: &'a str) -> Cow<'a, [u8]> {
382 if let Some(o) = encoding_override {
383 return o(input);
384 }
385 input.as_bytes().into()
386}
387
388pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
389 match input {
391 Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
392 Cow::Owned(bytes) => {
393 match String::from_utf8_lossy(&bytes) {
394 Cow::Borrowed(utf8) => {
395 let raw_utf8: *const [u8] = utf8.as_bytes();
403 debug_assert!(raw_utf8 == &*bytes as *const [u8]);
404
405 Cow::Owned(unsafe { String::from_utf8_unchecked(bytes) })
409 }
410 Cow::Owned(s) => Cow::Owned(s),
411 }
412 }
413 }
414}
415
416pub type EncodingOverride<'a> = Option<&'a dyn Fn(&str) -> Cow<'_, [u8]>>;