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
use crate::util;
use std::collections::HashMap;
#[derive(Debug)]
pub struct Request {
pub path: String,
pub method: String,
pub params: HashMap<String, String>,
}
impl Request {
pub fn new() -> Request {
Request {
path: "/".to_string(),
method: "GET".to_string(),
params: HashMap::new(),
}
}
pub fn method(&self) -> &str {
&self.method
}
pub fn path(&self) -> &str {
&self.path
}
pub fn has_param(&mut self, name: &str) -> bool {
self.parse_params();
self.params.contains_key(name)
}
pub fn param(&self, name: &str) -> Option<&String> {
self.params.get(name)
}
pub(crate) fn parse_params(&mut self) {
if !self.params.is_empty() {
self.params.clear();
}
let mut map = HashMap::new();
let path = self.path();
if let Some(start) = path.find('?') {
parse_query_into_map(&path[start + 1..], &mut map);
}
if self.method() == "POST" {
todo!();
}
if !map.is_empty() {
if let Some(idx) = self.path.find('?') {
self.path = self.path[..idx].to_string();
}
self.params = map;
}
}
}
fn parse_query_into_map(params: &str, map: &mut HashMap<String, String>) {
for kv in params.split('&') {
let mut parts = kv.splitn(2, '=');
if let Some(key) = parts.next() {
if let Some(val) = parts.next() {
map.insert(key.to_string(), util::decode_form_value(val));
} else {
map.insert(key.to_string(), "".to_string());
}
}
}
}