aboutsummaryrefslogtreecommitdiff
path: root/src/response.rs
diff options
context:
space:
mode:
authorlamp2023-03-05 21:37:45 +0000
committerlamp2023-03-05 21:37:45 +0000
commitd4c3b70d6290c52f903e674e9957b979abd4ce07 (patch)
treeba415f0f85809ffe07dd17d83ed46e5c09f2c14d /src/response.rs
initmain
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/response.rs b/src/response.rs
new file mode 100644
index 0000000..8817148
--- /dev/null
+++ b/src/response.rs
@@ -0,0 +1,55 @@
+use futures::prelude::*;
+
+pub trait Response: Send + Sync {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send>;
+}
+
+pub struct Ok {
+ pub file_stream: Box<dyn Stream<Item = Vec<u8>> + Unpin + Send + Sync>,
+}
+
+impl Response for Ok {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send> {
+ Box::new(stream::iter(vec![Vec::from(b"HTTP/1.1 200 OK\r\n\r\n" as &[u8])].into_iter().map(|entry| entry.to_owned())).chain(self.file_stream))
+ }
+}
+
+pub struct BadRequest {
+
+}
+
+impl Response for BadRequest {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send> {
+ Box::new(stream::iter(vec![Vec::from(b"HTTP/1.1 400 Bad Request\r\n\r\n" as &[u8]), include_bytes!("../res/400.html").to_vec()].into_iter().map(|entry| entry.to_owned())))
+ }
+}
+
+pub struct NotFound {
+
+}
+
+impl Response for NotFound {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send> {
+ Box::new(stream::iter(vec![Vec::from(b"HTTP/1.1 404 Not Found\r\n\r\n" as &[u8]), include_bytes!("../res/404.html").to_vec()].into_iter().map(|entry| entry.to_owned())))
+ }
+}
+
+pub struct NotImplemented {
+
+}
+
+impl Response for NotImplemented {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send> {
+ Box::new(stream::iter(vec![Vec::from(b"HTTP/1.1 501 Not Implemented\r\n\r\n" as &[u8]), include_bytes!("../res/501.html").to_vec()].into_iter().map(|entry| entry.to_owned())))
+ }
+}
+
+pub struct InternalServerError {
+
+}
+
+impl Response for InternalServerError {
+ fn response_bytes(self: Box<Self>) -> Box<dyn Stream<Item = Vec<u8>> + Unpin + Send> {
+ Box::new(stream::iter(vec![Vec::from(b"HTTP/1.1 500 InternalServerError\r\n\r\n" as &[u8]), include_bytes!("../res/500.html").to_vec()].into_iter().map(|entry| entry.to_owned())))
+ }
+}