Refactoring

This commit is contained in:
2021-05-22 10:58:47 +02:00
parent 912f2f0bf3
commit f2a6a5af4e
4 changed files with 66 additions and 55 deletions

View File

@ -4,13 +4,6 @@ use crate::usimp;
use crate::websocket; use crate::websocket;
use serde_json; 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) { pub fn connection_handler(client: super::Stream) {
let mut client = super::HttpStream { let mut client = super::HttpStream {
stream: client, stream: client,
@ -60,7 +53,7 @@ fn request_handler(client: &mut super::HttpStream) {
["entity", entity] => { ["entity", entity] => {
res.status(501); res.status(501);
error = Some(Error::new(Kind::NotImplementedError, Class::ServerError)) error = Some(Error::new(Kind::NotImplementedError, Class::ServerError))
}, }
[endpoint] => match req.method { [endpoint] => match req.method {
Method::POST => return endpoint_handler(client, &req, res, endpoint), Method::POST => return endpoint_handler(client, &req, res, endpoint),
_ => { _ => {

View File

@ -16,6 +16,13 @@ pub enum Stream {
Ssl(SslStream<TcpStream>), Ssl(SslStream<TcpStream>),
} }
pub struct HttpStream {
pub stream: Stream,
pub request_num: u32,
pub client_keep_alive: bool,
pub server_keep_alive: bool,
}
pub enum Method { pub enum Method {
GET, GET,
POST, POST,
@ -28,6 +35,46 @@ pub enum Method {
Custom(String), 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<String>,
}
pub struct HeaderField {
name: String,
value: String,
}
pub struct Header {
fields: Vec<HeaderField>,
}
pub struct Request {
version: String,
pub method: Method,
pub uri: String,
header: Header,
}
pub struct Response {
version: String,
status: Status,
header: Header,
}
impl Method { impl Method {
pub fn from_str(v: &str) -> Method { pub fn from_str(v: &str) -> Method {
match v { 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 { impl StatusClass {
pub fn from_code(status_code: u16) -> StatusClass { pub fn from_code(status_code: u16) -> StatusClass {
for (code, class, _msg, _desc) in &consts::HTTP_STATUSES { 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<String>,
}
impl Status { impl Status {
pub fn from_code(status_code: u16) -> Option<Status> { pub fn from_code(status_code: u16) -> Option<Status> {
for (code, class, msg, desc) in &consts::HTTP_STATUSES { 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<HeaderField>,
}
impl std::fmt::Display for HeaderField { impl std::fmt::Display for HeaderField {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}: {}]", self.name, self.value) 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 { impl Header {
pub fn new() -> Self { pub fn new() -> Self {
Header { fields: Vec::new() } Header { fields: Vec::new() }

View File

@ -1,10 +1,10 @@
use crate::error::*; use crate::error::*;
use crate::http; use crate::http;
use crate::websocket::FrameHeader;
use base64; use base64;
use crypto; use crypto;
use crypto::digest::Digest; use crypto::digest::Digest;
use crate::websocket::FrameHeader;
pub fn connection_handler( pub fn connection_handler(
client: &mut http::HttpStream, client: &mut http::HttpStream,
@ -106,7 +106,10 @@ pub fn connection_handler(
} }
let mut buf = [0u8; 8192]; 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 { if header.mask {
let key: [u8; 4] = [ 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])
);
} }
} }

View File

@ -1,8 +1,13 @@
mod handler; mod handler;
pub use handler::*;
use crate::http::Stream;
use crate::error::Error; use crate::error::Error;
use crate::http;
pub use handler::*;
pub struct WebSocketStream {
stream: http::HttpStream,
compression: bool,
}
pub struct FrameHeader { pub struct FrameHeader {
fin: bool, fin: bool,
@ -17,7 +22,7 @@ pub struct FrameHeader {
} }
impl FrameHeader { impl FrameHeader {
pub fn from(socket: &mut Stream) -> Result<Self, Error> { pub fn from(socket: &mut http::Stream) -> Result<Self, Error> {
let mut data = [0u8; 2]; let mut data = [0u8; 2];
socket.read_exact(&mut data)?; socket.read_exact(&mut data)?;