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
use std::collections::HashMap;

use super::errors::Error;

/// Values maps a string key to a list of values.
/// It is typically used for query parameters and form values.
/// Unlike in the http.Header map, the keys in a Values map
/// are case-sensitive.
///
/// # Example
///
/// ```
/// use net::url::Values;
///
/// fn main() {
///     let mut v = Values::default();
///
///     v.set("name".to_string(), "Ava");
///     v.add("friend", "Jess");
///     v.add("friend", "Sarah");
///     v.add("friend", "Zoe");
///
///     // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe"
///     assert_eq!(v.get("name").unwrap(), "Ava");
///     assert_eq!(v.get("friend").unwrap(), "Jess");
///
///     let friends: Vec<String> = vec!["Jess", "Sarah", "Zoe"]
///         .iter()
///         .map(|v| v.to_string())
///         .collect();
///     assert_eq!(v.0["friend"], friends);
/// }
/// ```
#[derive(Debug, Default)]
pub struct Values(pub HashMap<String, Vec<String>>);

impl Values {
    /// add adds the value to key. It appends to any existing
    /// values associated with key.
    pub fn add<K, V>(&mut self, key: K, value: V)
    where
        K: ToString,
        V: ToString,
    {
        let key = key.to_string();
        if !self.0.contains_key(&key) {
            self.0.insert(key.clone(), Vec::new());
        }

        self.0.get_mut(&key).unwrap().push(value.to_string());
    }

    /// del deletes the values associated with key.
    pub fn del(&mut self, key: &str) {
        self.0.remove(key);
    }

    /// encode encodes the values into "URL encoded" form
    /// ("bar=baz&foo=quux") sorted by key.
    pub fn encode(&self) -> String {
        let keys = {
            let mut keys = self.0.iter().map(|(k, _)| k).collect::<Vec<_>>();
            keys.sort();

            keys
        };

        let mut out = String::new();
        for k in keys {
            let values = self.0.get(k).unwrap();
            let key_escaped = super::query_escape(k);

            for v in values {
                if out.len() > 0 {
                    out.push('&');
                }

                out.push_str(key_escaped.as_str());
                out.push('=');
                out.push_str(super::query_escape(v).as_str());
            }
        }

        out
    }

    /// get gets the first value associated with the given key.
    /// If there are no values associated with the key, get returns
    /// None. To access multiple values, use the map directly.
    pub fn get(&self, key: &str) -> Option<&str> {
        self.0.get(key).map(|v| v[0].as_str())
    }

    /// set sets the key to value. It replaces any existing values.
    pub fn set<K, V>(&mut self, key: K, value: V)
    where
        K: ToString,
        V: ToString,
    {
        self.0.insert(key.to_string(), vec![value.to_string()]);
    }
}

/// parse_query parses the URL-encoded query string and returns
/// a map listing the values specified for each key.
/// parse_query always returns a non-nil map containing all the
/// valid query parameters found; err describes the first decoding error
/// encountered, if any.
///
/// Query is expected to be a list of key=value settings separated by
/// ampersands or semicolons. A setting without an equals sign is
/// interpreted as a key set to an empty value.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
///
/// fn main() {
///     let m = net::url::parse_query("x=1&y=2&y=3;z").unwrap();
///
///     let expected = {
///         let mut v = HashMap::new();
///         v.insert("x".to_string(), vec!["1".to_string()]);
///         v.insert("y".to_string(), vec!["2".to_string(), "3".to_string()]);
///         v.insert("z".to_string(), vec!["".to_string()]);
///         v
///     };
///
///     assert_eq!(expected, m.0);
/// }
/// ```
pub fn parse_query(query: &str) -> Result<Values, (Values, Error)> {
    let mut err: Option<Error> = None;
    let mut out = Values(HashMap::new());

    let kv = query
        .split(|v| v == '&' || v == ';')
        .filter(|v| v.len() != 0)
        .map(|v| v.splitn(2, '=').collect::<Vec<_>>());

    for x in kv {
        let (k, v) = if x.len() == 1 {
            (x[0], "")
        } else {
            (x[0], x[1])
        };

        let kk = match super::query_unescape(k) {
            Ok(v) => v,
            Err(e) => {
                if err.is_none() {
                    err = Some(e);
                }
                continue;
            }
        };

        let vv = match super::query_unescape(v) {
            Ok(v) => v,
            Err(e) => {
                if err.is_none() {
                    err = Some(e);
                }
                continue;
            }
        };

        if !out.0.contains_key(&kk) {
            out.0.insert(kk.to_string(), Vec::new());
        }

        out.0.get_mut(&kk).unwrap().push(vv);
    }

    match err {
        Some(v) => Err((out, v)),
        None => Ok(out),
    }
}