forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.rs
More file actions
362 lines (300 loc) · 10.6 KB
/
query.rs
File metadata and controls
362 lines (300 loc) · 10.6 KB
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
//! The `Query` struct used to parse and store the URL query parameters.
//!
/// ```text
/// URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
/// ```
use std::panic::Location;
use std::str::FromStr;
use multimap::MultiMap;
use thiserror::Error;
type ParamName = String;
type ParamValue = String;
/// It represents a URL query component.
///
/// ```text
/// URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
/// ```
#[derive(Debug)]
pub struct Query {
/* code-review:
- Consider using a third-party crate.
- Conversion from/to string is not deterministic. Params can be in a different order in the query string.
*/
params: MultiMap<ParamName, NameValuePair>,
}
impl Query {
/// It return `Some(value)` for a URL query param if the param with the
/// input `name` exists. For example:
///
/// ```rust
/// use bittorrent_http_tracker_protocol::v1::query::Query;
///
/// let raw_query = "param1=value1¶m2=value2";
///
/// let query = raw_query.parse::<Query>().unwrap();
///
/// assert_eq!(query.get_param("param1").unwrap(), "value1");
/// assert_eq!(query.get_param("param2").unwrap(), "value2");
/// ```
///
/// It returns only the first param value even if it has multiple values:
///
/// ```rust
/// use bittorrent_http_tracker_protocol::v1::query::Query;
///
/// let raw_query = "param1=value1¶m1=value2";
///
/// let query = raw_query.parse::<Query>().unwrap();
///
/// assert_eq!(query.get_param("param1").unwrap(), "value1");
/// ```
#[must_use]
pub fn get_param(&self, name: &str) -> Option<String> {
self.params.get(name).map(|pair| pair.value.clone())
}
/// Returns all the param values as a vector.
///
/// ```rust
/// use bittorrent_http_tracker_protocol::v1::query::Query;
///
/// let query = "param1=value1¶m1=value2".parse::<Query>().unwrap();
///
/// assert_eq!(
/// query.get_param_vec("param1"),
/// Some(vec!["value1".to_string(), "value2".to_string()])
/// );
/// ```
///
/// Returns all the param values as a vector even if it has only one value.
///
/// ```rust
/// use bittorrent_http_tracker_protocol::v1::query::Query;
///
/// let query = "param1=value1".parse::<Query>().unwrap();
///
/// assert_eq!(
/// query.get_param_vec("param1"), Some(vec!["value1".to_string()])
/// );
/// ```
#[must_use]
pub fn get_param_vec(&self, name: &str) -> Option<Vec<String>> {
self.params.get_vec(name).map(|pairs| {
let mut param_values = vec![];
for pair in pairs {
param_values.push(pair.value.clone());
}
param_values
})
}
}
/// This error can be returned when parsing a [`Query`]
/// from a string.
#[derive(Error, Debug)]
pub enum ParseQueryError {
/// Invalid URL query param. For example: `"name=value=value"`. It contains
/// an unescaped `=` character.
#[error("invalid param {raw_param} in {location}")]
InvalidParam {
location: &'static Location<'static>,
raw_param: String,
},
}
impl FromStr for Query {
type Err = ParseQueryError;
fn from_str(raw_query: &str) -> Result<Self, Self::Err> {
let mut params: MultiMap<ParamName, NameValuePair> = MultiMap::new();
let raw_params = raw_query.trim().trim_start_matches('?').split('&').collect::<Vec<&str>>();
for raw_param in raw_params {
let pair: NameValuePair = raw_param.parse()?;
let param_name = pair.name.clone();
params.insert(param_name, pair);
}
Ok(Self { params })
}
}
impl From<Vec<(&str, &str)>> for Query {
fn from(raw_params: Vec<(&str, &str)>) -> Self {
let mut params: MultiMap<ParamName, NameValuePair> = MultiMap::new();
for raw_param in raw_params {
params.insert(raw_param.0.to_owned(), NameValuePair::new(raw_param.0, raw_param.1));
}
Self { params }
}
}
impl std::fmt::Display for Query {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let query = self
.params
.iter_all()
.map(|param| format!("{}", FieldValuePairSet::from_vec(param.1)))
.collect::<Vec<String>>()
.join("&");
write!(f, "{query}")
}
}
#[derive(Debug, PartialEq, Clone)]
struct NameValuePair {
name: ParamName,
value: ParamValue,
}
impl NameValuePair {
pub fn new(name: &str, value: &str) -> Self {
Self {
name: name.to_owned(),
value: value.to_owned(),
}
}
}
impl FromStr for NameValuePair {
type Err = ParseQueryError;
fn from_str(raw_param: &str) -> Result<Self, Self::Err> {
let pair = raw_param.split('=').collect::<Vec<&str>>();
if pair.len() != 2 {
return Err(ParseQueryError::InvalidParam {
location: Location::caller(),
raw_param: raw_param.to_owned(),
});
}
Ok(Self {
name: pair[0].to_owned(),
value: pair[1].to_owned(),
})
}
}
impl std::fmt::Display for NameValuePair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}={}", self.name, self.value)
}
}
#[derive(Debug, PartialEq)]
struct FieldValuePairSet {
pairs: Vec<NameValuePair>,
}
impl FieldValuePairSet {
fn from_vec(pair_vec: &Vec<NameValuePair>) -> Self {
let mut pairs: Vec<NameValuePair> = vec![];
for pair in pair_vec {
pairs.push(pair.clone());
}
Self { pairs }
}
}
impl std::fmt::Display for FieldValuePairSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let query = self
.pairs
.iter()
.map(|pair| format!("{pair}"))
.collect::<Vec<String>>()
.join("&");
write!(f, "{query}")
}
}
#[cfg(test)]
mod tests {
mod url_query {
use crate::v1::query::Query;
#[test]
fn should_parse_the_query_params_from_an_url_query_string() {
let raw_query =
"info_hash=%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0&peer_id=-qB00000000000000001&port=17548";
let query = raw_query.parse::<Query>().unwrap();
assert_eq!(
query.get_param("info_hash").unwrap(),
"%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0"
);
assert_eq!(query.get_param("peer_id").unwrap(), "-qB00000000000000001");
assert_eq!(query.get_param("port").unwrap(), "17548");
}
#[test]
fn should_be_instantiated_from_a_string_pair_vector() {
let query = Query::from(vec![("param1", "value1"), ("param2", "value2")]);
assert_eq!(query.get_param("param1"), Some("value1".to_string()));
assert_eq!(query.get_param("param2"), Some("value2".to_string()));
}
#[test]
fn should_ignore_duplicate_param_values_when_asked_to_return_only_one_value() {
let query = Query::from(vec![("param1", "value1"), ("param1", "value2")]);
assert_eq!(query.get_param("param1"), Some("value1".to_string()));
}
#[test]
fn should_fail_parsing_an_invalid_query_string() {
let invalid_raw_query = "name=value=value";
let query = invalid_raw_query.parse::<Query>();
assert!(query.is_err());
}
#[test]
fn should_ignore_the_preceding_question_mark_if_it_exists() {
let raw_query = "?name=value";
let query = raw_query.parse::<Query>().unwrap();
assert_eq!(query.get_param("name"), Some("value".to_string()));
}
#[test]
fn should_trim_whitespaces() {
let raw_query = " name=value ";
let query = raw_query.parse::<Query>().unwrap();
assert_eq!(query.get_param("name"), Some("value".to_string()));
}
mod should_allow_more_than_one_value_for_the_same_param {
use crate::v1::query::Query;
#[test]
fn instantiated_from_a_vector() {
let query1 = Query::from(vec![("param1", "value1"), ("param1", "value2")]);
assert_eq!(
query1.get_param_vec("param1"),
Some(vec!["value1".to_string(), "value2".to_string()])
);
}
#[test]
fn parsed_from_an_string() {
let query2 = "param1=value1¶m1=value2".parse::<Query>().unwrap();
assert_eq!(
query2.get_param_vec("param1"),
Some(vec!["value1".to_string(), "value2".to_string()])
);
}
}
mod should_be_displayed {
use crate::v1::query::Query;
#[test]
fn with_one_param() {
assert_eq!("param1=value1".parse::<Query>().unwrap().to_string(), "param1=value1");
}
#[test]
fn with_multiple_params() {
let query = "param1=value1¶m2=value2".parse::<Query>().unwrap().to_string();
assert!(query == "param1=value1¶m2=value2" || query == "param2=value2¶m1=value1");
}
#[test]
fn with_multiple_values_for_the_same_param() {
let query = "param1=value1¶m1=value2".parse::<Query>().unwrap().to_string();
assert!(query == "param1=value1¶m1=value2" || query == "param1=value2¶m1=value1");
}
}
mod param_name_value_pair {
use crate::v1::query::NameValuePair;
#[test]
fn should_parse_a_single_query_param() {
let raw_param = "name=value";
let param = raw_param.parse::<NameValuePair>().unwrap();
assert_eq!(
param,
NameValuePair {
name: "name".to_string(),
value: "value".to_string(),
}
);
}
#[test]
fn should_fail_parsing_an_invalid_query_param() {
let invalid_raw_param = "name=value=value";
let query = invalid_raw_param.parse::<NameValuePair>();
assert!(query.is_err());
}
#[test]
fn should_be_displayed() {
assert_eq!("name=value".parse::<NameValuePair>().unwrap().to_string(), "name=value");
}
}
}
}