From f2a6a5af4e27fec98bfb5658a7c4f261d7bc26a6 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 22 May 2021 10:58:47 +0200 Subject: [PATCH] Refactoring --- src/http/handler.rs | 9 +---- src/http/mod.rs | 87 ++++++++++++++++++++++------------------ src/websocket/handler.rs | 12 ++++-- src/websocket/mod.rs | 13 ++++-- 4 files changed, 66 insertions(+), 55 deletions(-) diff --git a/src/http/handler.rs b/src/http/handler.rs index fa2cc3e..4e808df 100644 --- a/src/http/handler.rs +++ b/src/http/handler.rs @@ -4,13 +4,6 @@ use crate::usimp; use crate::websocket; use serde_json; -pub struct HttpStream { - pub stream: super::Stream, - pub request_num: u32, - pub client_keep_alive: bool, - pub server_keep_alive: bool, -} - pub fn connection_handler(client: super::Stream) { let mut client = super::HttpStream { stream: client, @@ -60,7 +53,7 @@ fn request_handler(client: &mut super::HttpStream) { ["entity", entity] => { res.status(501); error = Some(Error::new(Kind::NotImplementedError, Class::ServerError)) - }, + } [endpoint] => match req.method { Method::POST => return endpoint_handler(client, &req, res, endpoint), _ => { diff --git a/src/http/mod.rs b/src/http/mod.rs index 91393ce..088f760 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -16,6 +16,13 @@ pub enum Stream { Ssl(SslStream), } +pub struct HttpStream { + pub stream: Stream, + pub request_num: u32, + pub client_keep_alive: bool, + pub server_keep_alive: bool, +} + pub enum Method { GET, POST, @@ -28,6 +35,46 @@ pub enum Method { Custom(String), } +#[derive(Copy, Clone)] +pub enum StatusClass { + Informational, + Success, + Redirection, + ClientError, + ServerError, +} + +#[derive(Clone)] +pub struct Status { + code: u16, + message: String, + desc: &'static str, + class: StatusClass, + info: Option, +} + +pub struct HeaderField { + name: String, + value: String, +} + +pub struct Header { + fields: Vec, +} + +pub struct Request { + version: String, + pub method: Method, + pub uri: String, + header: Header, +} + +pub struct Response { + version: String, + status: Status, + header: Header, +} + impl Method { pub fn from_str(v: &str) -> Method { match v { @@ -64,15 +111,6 @@ impl std::fmt::Display for Method { } } -#[derive(Copy, Clone)] -pub enum StatusClass { - Informational, - Success, - Redirection, - ClientError, - ServerError, -} - impl StatusClass { pub fn from_code(status_code: u16) -> StatusClass { for (code, class, _msg, _desc) in &consts::HTTP_STATUSES { @@ -91,15 +129,6 @@ impl StatusClass { } } -#[derive(Clone)] -pub struct Status { - code: u16, - message: String, - desc: &'static str, - class: StatusClass, - info: Option, -} - impl Status { pub fn from_code(status_code: u16) -> Option { for (code, class, msg, desc) in &consts::HTTP_STATUSES { @@ -140,34 +169,12 @@ impl Status { } } -pub struct HeaderField { - name: String, - value: String, -} - -pub struct Header { - fields: Vec, -} - impl std::fmt::Display for HeaderField { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[{}: {}]", self.name, self.value) } } -pub struct Request { - version: String, - pub method: Method, - pub uri: String, - header: Header, -} - -pub struct Response { - version: String, - status: Status, - header: Header, -} - impl Header { pub fn new() -> Self { Header { fields: Vec::new() } diff --git a/src/websocket/handler.rs b/src/websocket/handler.rs index b6d3c93..b69adeb 100644 --- a/src/websocket/handler.rs +++ b/src/websocket/handler.rs @@ -1,10 +1,10 @@ use crate::error::*; use crate::http; +use crate::websocket::FrameHeader; use base64; use crypto; use crypto::digest::Digest; -use crate::websocket::FrameHeader; pub fn connection_handler( client: &mut http::HttpStream, @@ -106,7 +106,10 @@ pub fn connection_handler( } let mut buf = [0u8; 8192]; - client.stream.read_exact(&mut buf[..header.payload_len() as usize]).unwrap(); + client + .stream + .read_exact(&mut buf[..header.payload_len() as usize]) + .unwrap(); if header.mask { let key: [u8; 4] = [ @@ -120,6 +123,9 @@ pub fn connection_handler( } } - println!("Msg: {}", String::from_utf8_lossy(&buf[..header.payload_len() as usize])); + println!( + "Msg: {}", + String::from_utf8_lossy(&buf[..header.payload_len() as usize]) + ); } } diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs index 864d9a0..f024b16 100644 --- a/src/websocket/mod.rs +++ b/src/websocket/mod.rs @@ -1,8 +1,13 @@ mod handler; -pub use handler::*; -use crate::http::Stream; use crate::error::Error; +use crate::http; +pub use handler::*; + +pub struct WebSocketStream { + stream: http::HttpStream, + compression: bool, +} pub struct FrameHeader { fin: bool, @@ -17,7 +22,7 @@ pub struct FrameHeader { } impl FrameHeader { - pub fn from(socket: &mut Stream) -> Result { + pub fn from(socket: &mut http::Stream) -> Result { let mut data = [0u8; 2]; socket.read_exact(&mut data)?; @@ -44,7 +49,7 @@ impl FrameHeader { } if header.mask { - let mut data =[0u8; 4]; + let mut data = [0u8; 4]; socket.read_exact(&mut data)?; header.masking_key = Some(u32::from_be_bytes(data)); }