diff options
Diffstat (limited to 'src/response.rs')
-rw-r--r-- | src/response.rs | 55 |
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()))) + } +} |