diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-02-18 00:25:11 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-02-18 00:25:11 +0400 |
| commit | 9b69d6460e91244bfd77d8ea77c13d21d9050e4c (patch) | |
| tree | 74a891c6c6ec6b565cb92187db2cece11a6ff1d9 /spider/src | |
| parent | 04a3c64713ae4f0a7f1b5cfdab132f78cc33a862 (diff) | |
Added parsing of query params in GET requests handler
Diffstat (limited to 'spider/src')
| -rw-r--r-- | spider/src/http_method.rs | 1 | ||||
| -rw-r--r-- | spider/src/request.rs | 63 |
2 files changed, 46 insertions, 18 deletions
diff --git a/spider/src/http_method.rs b/spider/src/http_method.rs index a0b2e17..814c93b 100644 --- a/spider/src/http_method.rs +++ b/spider/src/http_method.rs @@ -1,5 +1,6 @@ use std::fmt; +#[derive(PartialEq)] pub enum HttpMethod { GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH } diff --git a/spider/src/request.rs b/spider/src/request.rs index db1f2ad..3bd6a8f 100644 --- a/spider/src/request.rs +++ b/spider/src/request.rs @@ -5,21 +5,21 @@ use crate::http_method::HttpMethod; pub struct Request { pub method: HttpMethod, - resource: String, - http_version: String, - headers: HashMap<String, String>, - body: Vec<u8>, + pub path: String, + pub http_version: String, + pub headers: HashMap<String, String>, + pub body: Vec<u8>, } impl Request { pub fn new( method: HttpMethod - , resource: String + , path: String , http_version: String , headers: HashMap<String, String> , body: Vec<u8> ) -> Request { return Request { method, - resource, + path, http_version, headers, body, @@ -49,15 +49,17 @@ impl Request { } } - let body = data[idx..].to_vec(); + let mut body = data[idx..].to_vec(); let mut headers: HashMap<String, String> = HashMap::new(); - for line in &lines[1..] { - let line = line - .trim_end_matches("\r\n") - .split(": ") - .collect::<Vec<&str>>(); - headers.insert(String::from(line[0]), line[1..].join(" ")); + if lines.len() > 0 { + for line in &lines[1..] { + let line = line + .trim_end_matches("\r\n") + .split(": ") + .collect::<Vec<&str>>(); + headers.insert(String::from(line[0]), line[1..].join(" ")); + } } let request_line = lines[0] @@ -65,23 +67,48 @@ impl Request { .split(" ") .collect::<Vec<&str>>(); + let (path, params) = split_path(String::from(request_line[1])); + let method = String::from(request_line[0]); match HttpMethod::parse(method) { - Some(method) => + Some(method) => { + if method == HttpMethod::GET { + if let Some(p) = params { + body = p.as_bytes().to_vec(); + } + } return Some(Request::new( method, - // TODO(andrew): add parsing of resource line. - String::from(request_line[1]), + path, String::from(request_line[2]), headers, - body )), + body )); + } None => return None }; } } +fn split_path(resource: String) -> (String, Option<String>) { + let mut idx = None; + for (i, c) in resource.chars().enumerate() { + if c == '?' { + idx = Some(i); + break; + } + } + match idx { + Some(n) => { + let path = &resource[..n]; + let params = &resource[n + 1..]; + return (path.to_string(), Some(params.to_string())); + }, + None => return (resource, None) + }; +} + impl fmt::Display for Request { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - return write!(f, "Request({}, {})", self.method, self.resource); + return write!(f, "Request({}, {})", self.method, self.path); } } |