aboutsummaryrefslogtreecommitdiff
path: root/src/request.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/request.rs')
-rw-r--r--src/request.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/request.rs b/src/request.rs
new file mode 100644
index 0000000..8728e79
--- /dev/null
+++ b/src/request.rs
@@ -0,0 +1,43 @@
+use crate::http_types::*;
+
+#[derive(Debug)]
+pub struct Request {
+ pub method: HTTPMethod,
+ pub headers: Option<Headers>,
+ body: Option<String>,
+}
+
+impl Request {
+ fn new(method: HTTPMethod, headers: Headers, body: String) -> Self {
+ let headers = if headers.0.len() == 0 {
+ None
+ } else {
+ Some(headers)
+ };
+ let body = if body.is_empty() { None } else { Some(body) };
+ Request {
+ method,
+ headers,
+ body,
+ }
+ }
+}
+
+impl From<&str> for Request {
+ fn from(val: &str) -> Self {
+ let request: Vec<&str> = val.split("\r\n").collect();
+ match &request[..] {
+ [request_line, headers @ .., body] => {
+ let (method, headers, body) = (
+ HTTPMethod::from(*request_line),
+ Headers::from(headers),
+ body.to_string(),
+ );
+ Request::new(method, headers, body)
+ }
+ _ => {
+ unreachable!();
+ }
+ }
+ }
+}