1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use serde::de;

use super::*;

/// To override the default serialization parameters, first construct a new
/// Config.
///
/// A `max_depth` of 0 implies no nesting: the result will be a flat map.
/// This is mostly useful when the maximum nested depth is known beforehand,
/// to prevent denial of service attacks by providing incredibly deeply nested
/// inputs.
///
/// The default value for `max_depth` is 5.
///
/// ```
/// use serde_qs::Config;
/// use std::collections::HashMap;
///
/// let config = Config::with_max_depth(0);
/// let map: HashMap<String, String> = config.deserialize_str("a[b][c]=1")
///                                          .unwrap();
/// assert_eq!(map.get("a[b][c]").unwrap(), "1");
///
/// let config = Config::with_max_depth(10);
/// let map: HashMap<String, HashMap<String, HashMap<String, String>>> =
///             config.deserialize_str("a[b][c]=1").unwrap();
/// assert_eq!(map.get("a").unwrap().get("b").unwrap().get("c").unwrap(), "1");
/// ```
///
pub struct Config {
    /// Specifies the maximum depth key that `serde_qs` will attempt to
    /// deserialize. Default is 5.
    max_depth: usize,
}

impl Default for Config {
    fn default() -> Self {
        Config { max_depth: 5 }
    }
}

impl Config {
    /// Construct a new `Config` with the specified maximum depth of nesting.
    pub fn with_max_depth(depth: usize) -> Config {
        Config {
            max_depth: depth
        }
    }

    /// Get maximum depth parameter.
    pub fn max_depth(&self) -> usize {
        self.max_depth
    }
}

impl Config {
    /// Deserializes a querystring from a `&[u8]` using this `Config`.
    pub fn deserialize_bytes<'de, T: de::Deserialize<'de>>(&self,
                                                 input: &[u8])
                                                 -> Result<T> {
        T::deserialize(QsDeserializer::with_config(self, input))
    }

    /// Deserializes a querystring from a `&str` using this `Config`.
    pub fn deserialize_str<'de, T: de::Deserialize<'de>>(&self,
                                               input: &str)
                                               -> Result<T> {
        self.deserialize_bytes(input.as_bytes())
    }

    /// Deserializes a querystring from a reader using this `Config`.
    pub fn deserialize_reader<'de, T, R>(&self, mut reader: R) -> Result<T>
        where T: de::Deserialize<'de>,
              R: Read,
    {
        let mut buf = vec![];
        let _ = reader.read_to_end(&mut buf).map_err(Error::from)?;
        self.deserialize_bytes(&buf)
    }
}


macro_rules! tu {
    ($x:expr) => (
        match $x {
            Some(x) => x,
            None => return Err(
                de::Error::custom("query string ended before expected"))
        }
    )
}

use std::iter::Iterator;
use std::str;

pub struct Parser<I: Iterator<Item = u8>> {
    inner: I,
    acc: Vec<u8>,
    peeked: Option<u8>,
    depth: usize,
}

impl<I: Iterator<Item = u8>> Iterator for Parser<I> {
    type Item = u8;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}


fn insert_into_map(node: &mut Level, key: String, value: String) {
    if let Level::Nested(ref mut map) = *node {
        match map.entry(key) {
            Entry::Occupied(mut o) => {
                // Throw away old result; map is now invalid anyway.
                let _ = o.insert(Level::Invalid("Multiple values for one key"));
            },
            Entry::Vacant(vm) => {
                // Map is empty, result is None
                let _ = vm.insert(Level::Flat(value));
            },
        }
    } else  {
        // To reach here, node is either an Nested or nothing.
        let mut map = BTreeMap::default();
        let _ = map.insert(key, Level::Flat(value));
        *node = Level::Nested(map);
    }
}

fn insert_into_ord_seq(node: &mut Level, key: usize, value: String) {
    if let Level::OrderedSeq(ref mut map) = *node {
        match map.entry(key) {
            Entry::Occupied(mut o) => {
                // Throw away old result; map is now invalid anyway.
                let _ = o.insert(Level::Invalid("Multiple values for one key"));
            },
            Entry::Vacant(vm) => {
                // Map is empty, result is None
                let _ = vm.insert(Level::Flat(value));
            },
        }
    } else {
        // To reach here, node is either an OrderedSeq or nothing.
        let mut map = BTreeMap::default();
        let _ = map.insert(key, Level::Flat(value));
        *node = Level::OrderedSeq(map);
    }
}

impl<I: Iterator<Item = u8>> Parser<I> {
    pub fn new(inner: I, acc: Vec<u8>, peeked: Option<u8>, depth: usize) -> Self {
        Parser {
            inner, acc, peeked, depth
        }
    }

    pub fn as_deserializer(&mut self) -> QsDeserializer {
        let map = BTreeMap::default();
        let mut root = Level::Nested(map);
        while let Ok(x) = self.parse(&mut root) {
            if !x {
                break;
            }
        }
        let iter = match root {
            Level::Nested(map) => map.into_iter(),
            _ => BTreeMap::default().into_iter()
        };
        QsDeserializer {
            iter: iter,
            value: None,
        }
    }

    #[inline]
    fn peek(&mut self) -> Option<<Self as Iterator>::Item> {
        if !self.acc.is_empty() {
            self.peeked
        } else if let Some(x) = self.inner.next() {
            self.acc.push(x);
            self.peeked = Some(x);
            Some(x)
        } else {
            None
        }
    }

    fn parse_key(&mut self,
                 end_on: u8,
                 consume: bool)
                 -> Result<String> {
        loop {
            if let Some(x) = self.next() {
                match x {
                    x if x == end_on => {
                        let res = String::from_utf8(self.acc.split_off(0));
                        self.acc.clear();

                        // Add this character back to the buffer for peek.
                        if !consume {
                            self.acc.push(x);
                            self.peeked = Some(x);
                        }
                        return res.map_err(Error::from);
                    },
                    b'=' => {
                        // Allow the '=' byte when parsing keys within []
                        if end_on == b']' {
                            self.acc.push(b'=');
                        } else {
                            let res = String::from_utf8(self.acc.split_off(0));
                            self.acc.clear();

                            // Add this character back to the buffer for peek.
                            self.acc.push(b'=');
                            self.peeked = Some(b'=');

                            return res.map_err(Error::from);
                        }
                    },
                    b' ' => {
                        self.acc.push(b' ');
                    },
                    b'&' => {
                        let res = String::from_utf8(self.acc.split_off(0));
                        self.acc.clear();
                        self.acc.push(b'&');
                        self.peeked = Some(b'&');
                        return res.map_err(Error::from);
                    },
                    x @ 0x20...0x7e => {
                        self.acc.push(x);
                    },
                    _ => {
                        return Err(de::Error::custom("unexpected character \
                                                      in query string."));
                    },
                }
            } else {
                let res = String::from_utf8(self.acc.split_off(0));
                self.acc.clear();
                return res.map_err(Error::from);
            }
        }
    }

    fn parse_map_value(&mut self,
                       key: String,
                       node: &mut Level)
                       -> Result<()> {
        if let Some(x) = self.peek() {
            match x {
                b'=' => {
                    self.acc.clear();
                    for b in self.inner.by_ref().take_while(|b| b != &b'&') {
                        if b == b'+' {
                            self.acc.push(b' ');
                        } else {
                            self.acc.push(b);
                        }
                    }
                    let value = String::from_utf8(self.acc.split_off(0));
                    let value = value.map_err(Error::from)?;
                    // Reached the end of the key string
                    insert_into_map(node, key, value);
                    Ok(())
                },
                b'&' => {
                    insert_into_map(node, key, "".to_string());
                    Ok(())
                },
                b'[' => {
                    if let Level::Invalid(_) = *node {
                        *node = Level::Nested(BTreeMap::default());
                    }
                    if let Level::Nested(ref mut map) = *node {
                        self.depth -= 1;
                        self.parse(map.entry(key)
                                .or_insert(Level::Invalid("uninitialised")))?;
                        Ok(())
                    } else {
                        Err(de::Error::custom(format!("tried to insert a \
                                                       new key into {:?}",
                                                      node)))
                    }
                },
                _ => {
                    Err(de::Error::custom("Unexpected character found when parsing"))
                },
            }
        } else {
            insert_into_map(node, key, "".to_string());
            Ok(())
        }
    }

    fn parse_seq_value(&mut self, node: &mut Level) -> Result<()> {
        match tu!(self.peek()) {
            b'=' => {
                self.acc.clear();
                for b in self.inner.by_ref().take_while(|b| b != &b'&') {
                    self.acc.push(b);
                }
                let value = String::from_utf8(self.acc.split_off(0));
                let value = value.map_err(Error::from)?;
                // Reached the end of the key string
                if let Level::Sequence(ref mut seq) = *node {
                    seq.push(Level::Flat(value));
                } else {
                    let mut seq = Vec::new();
                    seq.push(Level::Flat(value));
                    *node = Level::Sequence(seq);
                }
                Ok(())
            },
            _ => {
                Err(de::Error::custom("non-indexed sequence of structs not \
                                       supported"))
            },
        }
    }

    fn parse_ord_seq_value(&mut self, key: usize, node: &mut Level) -> Result<()> {
        if let Some(x) = self.peek() {
            match x {
                b'=' => {
                    self.acc.clear();
                    for b in self.inner.by_ref().take_while(|b| b != &b'&') {
                        if b == b'+' {
                            self.acc.push(b' ');
                        } else {
                            self.acc.push(b);
                        }
                    }
                    let value = String::from_utf8(self.acc.split_off(0));
                    let value = value.map_err(Error::from)?;
                    // Reached the end of the key string
                    insert_into_ord_seq(node, key, value);
                    Ok(())
                },
                b'&' => {
                    insert_into_ord_seq(node, key, "".to_string());
                    Ok(())
                },
                b'[' => {
                    if let Level::Invalid(_) = *node {
                        *node = Level::OrderedSeq(BTreeMap::default());
                    }
                    if let Level::OrderedSeq(ref mut map) = *node {
                        self.depth -= 1;
                        self.parse(map.entry(key)
                                .or_insert(Level::Invalid("uninitialised")))?;
                        Ok(())
                    } else {
                        Err(de::Error::custom(format!("tried to insert a \
                                                       new key into {:?}",
                                                      node)))
                    }
                },
                _ => {
                    Err(de::Error::custom("Unexpected character found when parsing"))
                },
            }
        } else {
            insert_into_ord_seq(node, key, "".to_string());
            Ok(())
        }
    }


    fn parse(&mut self, node: &mut Level) -> Result<bool> {
        // First character determines parsing type
        if self.depth == 0 {
            let key = self.parse_key(b'\x00', true)?;
            self.parse_map_value(key, node)?;
            self.depth += 1;
            return Ok(true);
        }
        match self.peek() {
            Some(x) => {
                match x {
                    b'[' => {
                        self.acc.clear();
                        match tu!(self.peek()) {
                            // key is of the form "[...", not really allowed.
                            b'[' => {
                                Err(de::Error::custom("found another opening bracket before the closed bracket"))

                            },
                            // key is simply "[]", so treat as a seq.
                            b']' => {
                                self.acc.clear();
                                self.parse_seq_value(node)?;
                                self.depth += 1;
                                Ok(true)

                            },
                            // First character is an integer, attempt to parse it as an integer key
                            b'0'...b'9' => {
                                let key = self.parse_key(b']', true)?;
                                let key = usize::from_str_radix(&key, 10).map_err(Error::from)?;
                                self.parse_ord_seq_value(key, node)?;
                                self.depth += 1;
                                Ok(true)
                            }
                            // Key is "[a..." so parse up to the closing "]"
                            0x20...0x2f | 0x3a...0x5a | 0x5c | 0x5e...0x7e => {
                                let key = self.parse_key(b']', true)?;
                                self.parse_map_value(key, node)?;
                                self.depth += 1;
                                Ok(true)
                            },
                            c => {
                                Err(de::Error::custom(format!("unexpected character: {}", c)))
                            },
                        }
                    },
                    // This means the key should be a root key
                    // of the form "abc" or "abc[...]"
                    // We do actually allow integer keys here since they cannot
                    // be confused with sequences
                    0x20...0x5a | 0x5c...0x7e => {
                        let key = self.parse_key(b'[', false)?;
                        self.parse_map_value(key, node)?;
                        self.depth += 1;
                        Ok(true)
                    },
                    c => {
                        Err(de::Error::custom(format!("unexpected character: {}", c)))
                    }
                }
            },
            // Ran out of characters to parse
            None => Ok(false),
        }
    }
}