From d4c3b70d6290c52f903e674e9957b979abd4ce07 Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:37:45 +0000 Subject: init --- src/response.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/response.rs (limited to 'src/response.rs') 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) -> Box> + Unpin + Send>; +} + +pub struct Ok { + pub file_stream: Box> + Unpin + Send + Sync>, +} + +impl Response for Ok { + fn response_bytes(self: Box) -> Box> + 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) -> Box> + 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) -> Box> + 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) -> Box> + 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) -> Box> + 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()))) + } +} -- cgit v1.2.3