summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-07-02 22:15:24 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-07-02 22:15:24 -0400
commit41fe16978ea506127bf43c21e977fd9581d87282 (patch)
treeb5bf1ef7a37404350d45f0b6e8a00c5b32216e08 /src/http
parentmore info (diff)
downloadbashttp-41fe16978ea506127bf43c21e977fd9581d87282.tar.gz
bashttp-41fe16978ea506127bf43c21e977fd9581d87282.tar.bz2
bashttp-41fe16978ea506127bf43c21e977fd9581d87282.zip
add methods to routes, allow custom headers and response codes
Diffstat (limited to 'src/http')
-rw-r--r--src/http/code.rs8
-rw-r--r--src/http/header.rs7
-rw-r--r--src/http/method.rs16
-rw-r--r--src/http/mod.rs1
-rw-r--r--src/http/request.rs4
-rw-r--r--src/http/response.rs8
6 files changed, 24 insertions, 20 deletions
diff --git a/src/http/code.rs b/src/http/code.rs
deleted file mode 100644
index ba47282..0000000
--- a/src/http/code.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-#[derive(Debug, Clone)]
-#[allow(dead_code)]
-pub enum Code {
- Success = 200,
- MethodNotAllowed = 405,
- TooManyRequests = 429,
- InternalServerError = 500,
-}
diff --git a/src/http/header.rs b/src/http/header.rs
index e6fc552..03e4303 100644
--- a/src/http/header.rs
+++ b/src/http/header.rs
@@ -57,9 +57,7 @@ impl HeaderMap {
string
}
- pub fn serialize(lines: &mut Split<char>) -> Self {
-
- let mut headers = Self::new();
+ pub fn serialize(&mut self, lines: &mut Split<char>) {
loop {
let Some(header) = lines.next() else { break };
@@ -69,10 +67,9 @@ impl HeaderMap {
let Some(key) = parts.next() else { continue };
let Some(value) = parts.next() else { continue };
- headers.put(Header::new(key.trim(), value.trim()));
+ self.put(Header::new(key.trim(), value.trim()));
}
- headers
}
pub fn new() -> Self {
diff --git a/src/http/method.rs b/src/http/method.rs
index 55cea65..cecafbf 100644
--- a/src/http/method.rs
+++ b/src/http/method.rs
@@ -1,4 +1,4 @@
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Method {
Get,
Head,
@@ -26,5 +26,19 @@ impl Method {
_ => None
}
}
+
+ pub fn deserialize(&self) -> &str {
+ match self {
+ Method::Get => "GET",
+ Method::Head => "HEAD",
+ Method::Post => "POST",
+ Method::Put => "PUT",
+ Method::Delete => "DELETE",
+ Method::Connect => "CONNECT",
+ Method::Options => "OPTIONS",
+ Method::Trace => "TRACE",
+ Method::Patch => "PATCH",
+ }
+ }
}
diff --git a/src/http/mod.rs b/src/http/mod.rs
index 62151bb..02ba89d 100644
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -1,4 +1,3 @@
-pub mod code;
pub mod method;
pub mod uri;
pub mod request;
diff --git a/src/http/request.rs b/src/http/request.rs
index 5ba72c9..286efa3 100644
--- a/src/http/request.rs
+++ b/src/http/request.rs
@@ -39,7 +39,9 @@ impl Request {
return None
};
- let headers = HeaderMap::serialize(&mut lines);
+ let mut headers = HeaderMap::new();
+ headers.serialize(&mut lines);
+
let body: String = lines.collect();
Some(Self {
diff --git a/src/http/response.rs b/src/http/response.rs
index 850f41e..85430a4 100644
--- a/src/http/response.rs
+++ b/src/http/response.rs
@@ -1,8 +1,8 @@
-use super::{code::Code, header::{HeaderMap, Header}};
+use super::{header::{HeaderMap, Header}};
#[derive(Debug, Clone)]
pub struct Response {
- pub status: Code,
+ pub status: u16,
pub headers: HeaderMap,
pub body: Option<String>
}
@@ -20,7 +20,7 @@ impl Response {
headers.put(Header::new("Server", "bashttp"));
return Self {
- status: Code::Success,
+ status: 200,
headers,
body: None
}
@@ -29,7 +29,7 @@ impl Response {
pub fn deserialize(&self) -> String {
let mut string = String::new();
- string += &format!("HTTP/1.1 {}\n", self.status.clone() as u16);
+ string += &format!("HTTP/1.1 {}\n", self.status);
string += &self.headers.deserialize();
if let Some(body) = &self.body {