Refactoring
This commit is contained in:
@ -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),
|
||||
_ => {
|
||||
|
@ -16,6 +16,13 @@ pub enum Stream {
|
||||
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 {
|
||||
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<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 {
|
||||
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<String>,
|
||||
}
|
||||
|
||||
impl Status {
|
||||
pub fn from_code(status_code: u16) -> Option<Status> {
|
||||
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 {
|
||||
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() }
|
||||
|
@ -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])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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<Self, Error> {
|
||||
pub fn from(socket: &mut http::Stream) -> Result<Self, Error> {
|
||||
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));
|
||||
}
|
||||
|
Reference in New Issue
Block a user