From a8c6962b20a5436b5a0c3ab8d9e16d801b4a8a4c Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 15 May 2021 23:24:12 +0200 Subject: [PATCH] Run rustfmt --- src/http/consts.rs | 2 +- src/http/handler.rs | 28 +++--- src/http/mod.rs | 20 ++-- src/http/parser.rs | 223 ++++++++++++++++++++++---------------------- src/main.rs | 17 +++- src/udp.rs | 9 +- src/usimp/mod.rs | 1 - 7 files changed, 156 insertions(+), 144 deletions(-) diff --git a/src/http/consts.rs b/src/http/consts.rs index 190b0bc..d81469d 100644 --- a/src/http/consts.rs +++ b/src/http/consts.rs @@ -1,5 +1,5 @@ -use super::StatusClass::*; use super::StatusClass; +use super::StatusClass::*; pub static HTTP_STATUSES: [(u16, StatusClass, &str, &str); 41] = [ (100, Informational, "Continue", diff --git a/src/http/handler.rs b/src/http/handler.rs index 5fb11ba..a203a26 100644 --- a/src/http/handler.rs +++ b/src/http/handler.rs @@ -1,7 +1,7 @@ -use json; -use chrono; -use crate::usimp; use super::Method; +use crate::usimp; +use chrono; +use json; pub struct HttpStream { stream: super::Stream, @@ -30,7 +30,13 @@ pub fn connection_handler(client: super::Stream) { fn request_handler(client: &mut super::HttpStream) { let mut res = super::Response::new(); res.add_header("Server", "Locutus"); - res.add_header("Date", chrono::Utc::now().format("%a, %d %b %Y %H:%M:%S GMT").to_string().as_str()); + res.add_header( + "Date", + chrono::Utc::now() + .format("%a, %d %b %Y %H:%M:%S GMT") + .to_string() + .as_str(), + ); let req = super::parser::parse_request(&mut client.stream).unwrap(); println!("{} {}", req.method, req.uri); @@ -53,14 +59,12 @@ fn request_handler(client: &mut super::HttpStream) { let parts: Vec<&str> = req.uri.split('/').collect(); match parts[2..] { ["entity", entity] => res.status(501), - [func] => { - match usimp::is_valid(func) { - true => match req.method { - Method::POST => res.status(200), - _ => res.status(405), - }, - false => res.status(400), - } + [func] => match usimp::is_valid(func) { + true => match req.method { + Method::POST => res.status(200), + _ => res.status(405), + }, + false => res.status(400), }, _ => res.status(400), } diff --git a/src/http/mod.rs b/src/http/mod.rs index b582c79..04ced60 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1,11 +1,11 @@ mod consts; -mod parser; mod handler; +mod parser; -use std::net::TcpStream; use openssl::ssl::SslStream; -use std::io::{Write, Read}; use std::fmt::Formatter; +use std::io::{Read, Write}; +use std::net::TcpStream; pub use handler::*; @@ -86,7 +86,7 @@ impl StatusClass { 300..=399 => StatusClass::Redirection, 400..=499 => StatusClass::ClientError, 500..=599 => StatusClass::ServerError, - _ => panic!("invalid status code") + _ => panic!("invalid status code"), } } } @@ -106,7 +106,7 @@ impl Status { code: status_code, message: msg.to_string(), class: class.clone(), - } + }; } } panic!("invalid status code"); @@ -124,7 +124,6 @@ impl Status { } } - pub struct HeaderField { name: String, value: String, @@ -140,13 +139,13 @@ pub struct Request { version: String, method: Method, uri: String, - header_fields: Vec + header_fields: Vec, } pub struct Response { version: String, status: Status, - header_fields: Vec + header_fields: Vec, } impl Response { @@ -170,7 +169,10 @@ impl Response { } fn send(&self, stream: &mut Stream) -> Result<(), std::io::Error> { - let mut header = format!("HTTP/{} {:03} {}\r\n", self.version, self.status.code, self.status.message); + let mut header = format!( + "HTTP/{} {:03} {}\r\n", + self.version, self.status.code, self.status.message + ); for header_field in &self.header_fields { header.push_str(format!("{}: {}\r\n", header_field.name, header_field.value).as_str()); } diff --git a/src/http/parser.rs b/src/http/parser.rs index 2f4e927..598e729 100644 --- a/src/http/parser.rs +++ b/src/http/parser.rs @@ -8,8 +8,6 @@ pub fn parse_request(stream: &mut http::Stream) -> Result let mut parser = Parser::new_request_parser(&buf[..size]); let header_size = parser.parse().unwrap(); - - let mut header_fields = Vec::new(); for (name, value) in parser.headers { header_fields.push(http::HeaderField { @@ -126,8 +124,13 @@ impl Parser<'_> { self.next(*char); match self.state { State::Finish => return Ok(self.header_size), - State::Error => return Err(format!("invalid character at position {}", self.header_size - 1)), - _ => {}, + State::Error => { + return Err(format!( + "invalid character at position {}", + self.header_size - 1 + )) + } + _ => {} } } return Err(String::from("input too short")); @@ -135,134 +138,120 @@ impl Parser<'_> { fn next(&mut self, char: u8) { self.header_size += 1; - let get_str = || { - std::str::from_utf8(&self.buf[self.str_start..self.header_size - 1]).unwrap() - }; + let get_str = + || std::str::from_utf8(&self.buf[self.str_start..self.header_size - 1]).unwrap(); self.state = match &self.state { State::Error => State::Error, State::Finish => State::Error, - State::Method => { - match char { - 0x41..=0x5A => State::Method, - 0x20 => { - self.method = Some(get_str()); - self.str_start = self.header_size; - State::Uri - }, - _ => State::Error, + State::Method => match char { + 0x41..=0x5A => State::Method, + 0x20 => { + self.method = Some(get_str()); + self.str_start = self.header_size; + State::Uri } + _ => State::Error, }, - State::Uri => { - match char { - 0x21..=0x7E => State::Uri, - 0x20 => { - self.uri = Some(get_str()); - self.str_start = self.header_size; - State::Http(&State::HeaderName) - }, - _ => State::Error, + State::Uri => match char { + 0x21..=0x7E => State::Uri, + 0x20 => { + self.uri = Some(get_str()); + self.str_start = self.header_size; + State::Http(&State::HeaderName) } + _ => State::Error, }, - State::Http(next) => { - match char { - 0x48 | 0x54 | 0x50 => State::Http(next), - 0x2F => { - let http = get_str(); - self.str_start = self.header_size; - if http != "HTTP" { - State::Error - } else { - State::HttpVersion(next) - } - }, - _ => State::Error, + State::Http(next) => match char { + 0x48 | 0x54 | 0x50 => State::Http(next), + 0x2F => { + let http = get_str(); + self.str_start = self.header_size; + if http != "HTTP" { + State::Error + } else { + State::HttpVersion(next) + } } + _ => State::Error, }, - State::HttpVersion(next) => { - match char { - 0x30..=0x39 | 0x2E => State::HttpVersion(next), - 0x0D => { - match next { - State::HeaderName => { - self.http_version = Some(get_str()); - State::CRLF(next) - }, - _ => State::Error, - } - }, - 0x20 => { - match next { - State::StatusCode => { - self.http_version = Some(get_str()); - self.str_start = self.header_size; - State::StatusCode - }, - _ => State::Error, - } + State::HttpVersion(next) => match char { + 0x30..=0x39 | 0x2E => State::HttpVersion(next), + 0x0D => match next { + State::HeaderName => { + self.http_version = Some(get_str()); + State::CRLF(next) } _ => State::Error, - } - }, - State::StatusCode => { - match char { - 0x30..=0x39 => State::StatusCode, - 0x20 => { - self.status_code = Some(get_str()); + }, + 0x20 => match next { + State::StatusCode => { + self.http_version = Some(get_str()); self.str_start = self.header_size; - State::StatusMessage - }, + State::StatusCode + } _ => State::Error, - } + }, + _ => State::Error, }, - State::StatusMessage => { - match char { - 0x20..=0x7E => State::StatusMessage, - 0x0D => { - self.status_message = Some(get_str()); - State::CRLF(&State::HeaderName) - }, - _ => State::Error, + State::StatusCode => match char { + 0x30..=0x39 => State::StatusCode, + 0x20 => { + self.status_code = Some(get_str()); + self.str_start = self.header_size; + State::StatusMessage } + _ => State::Error, }, - State::HeaderName => { - match char { - 0x0D => { - if self.header_size == self.str_start + 1 { - State::CRLF(&State::Finish) - } else { - State::Error - } - }, - 0x3A => { - let header_name = get_str(); - self.headers.push((header_name, "")); - self.str_start = self.header_size; - State::HeaderValue - }, - 0x00..=0x1F | 0x7F | 0x80..=0xFF | - 0x20 | 0x28 | 0x29 | 0x2C | 0x2F | - 0x3A..=0x40 | 0x5B..=0x5D | 0x7B | 0x7D => State::Error, - _ => State::HeaderName, + State::StatusMessage => match char { + 0x20..=0x7E => State::StatusMessage, + 0x0D => { + self.status_message = Some(get_str()); + State::CRLF(&State::HeaderName) } + _ => State::Error, }, - State::HeaderValue => { - match char { - 0x20..=0x7E | 0x09 => State::HeaderValue, - 0x0D => { - self.headers.last_mut().unwrap().1 = get_str().trim(); - State::CRLF(&State::HeaderName) - }, - _ => State::Error, + State::HeaderName => match char { + 0x0D => { + if self.header_size == self.str_start + 1 { + State::CRLF(&State::Finish) + } else { + State::Error + } } - } - State::CRLF(next) => { - match char { - 0x0A => { - self.str_start = self.header_size; - *next.clone() - }, - _ => State::Error, + 0x3A => { + let header_name = get_str(); + self.headers.push((header_name, "")); + self.str_start = self.header_size; + State::HeaderValue } + 0x00..=0x1F + | 0x7F + | 0x80..=0xFF + | 0x20 + | 0x28 + | 0x29 + | 0x2C + | 0x2F + | 0x3A..=0x40 + | 0x5B..=0x5D + | 0x7B + | 0x7D => State::Error, + _ => State::HeaderName, + }, + State::HeaderValue => match char { + 0x20..=0x7E | 0x09 => State::HeaderValue, + 0x0D => { + self.headers.last_mut().unwrap().1 = get_str().trim(); + State::CRLF(&State::HeaderName) + } + _ => State::Error, + }, + State::CRLF(next) => match char { + 0x0A => { + self.str_start = self.header_size; + *next.clone() + } + _ => State::Error, }, } } @@ -312,7 +301,10 @@ mod tests { assert_eq!(3, parser.headers.len()); assert_eq!(("Host", "www.example.com"), parser.headers[0]); assert_eq!(("Content-Length", "13"), parser.headers[1]); - assert_eq!(("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)"), parser.headers[2]); + assert_eq!( + ("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)"), + parser.headers[2] + ); assert_eq!("username=test", &request[size..]); } @@ -357,7 +349,10 @@ mod tests { assert_eq!(2, parser.headers.len()); assert_eq!(("Content-Length", "12"), parser.headers[0]); - assert_eq!(("Content-Type", "text/plain; charset=us-ascii"), parser.headers[1]); + assert_eq!( + ("Content-Type", "text/plain; charset=us-ascii"), + parser.headers[1] + ); assert_eq!("Hello world!", &response[size..]); } diff --git a/src/main.rs b/src/main.rs index f1fac33..a50ce5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,12 @@ mod http; mod udp; mod usimp; -use std::thread; -use threadpool::ThreadPool; +use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslStream}; use std::net::{TcpListener, UdpSocket}; -use openssl::ssl::{SslMethod, SslAcceptor, SslStream, SslFiletype}; use std::sync::Arc; use std::sync::Mutex; +use std::thread; +use threadpool::ThreadPool; fn main() { let mut threads = Vec::new(); @@ -32,8 +32,15 @@ fn main() { let mut ssl_socket = TcpListener::bind("[::]:8443").unwrap(); let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); - acceptor.set_certificate_chain_file("/home/lorenz/Certificates/chakotay.pem").unwrap(); - acceptor.set_private_key_file("/home/lorenz/Certificates/priv/chakotay.key", SslFiletype::PEM).unwrap(); + acceptor + .set_certificate_chain_file("/home/lorenz/Certificates/chakotay.pem") + .unwrap(); + acceptor + .set_private_key_file( + "/home/lorenz/Certificates/priv/chakotay.key", + SslFiletype::PEM, + ) + .unwrap(); acceptor.check_private_key().unwrap(); let acceptor = Arc::new(acceptor.build()); diff --git a/src/udp.rs b/src/udp.rs index 9d29a6e..5117744 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,4 +1,4 @@ -use std::net::{UdpSocket, SocketAddr}; +use std::net::{SocketAddr, UdpSocket}; pub struct Request { socket: UdpSocket, @@ -8,7 +8,12 @@ pub struct Request { } impl Request { - pub fn new(socket: &UdpSocket, address: SocketAddr, size: usize, buf: &[u8; 65_536]) -> Request { + pub fn new( + socket: &UdpSocket, + address: SocketAddr, + size: usize, + buf: &[u8; 65_536], + ) -> Request { Request { socket: socket.try_clone().unwrap(), address, diff --git a/src/usimp/mod.rs b/src/usimp/mod.rs index 32fe35e..4e16b6f 100644 --- a/src/usimp/mod.rs +++ b/src/usimp/mod.rs @@ -1,4 +1,3 @@ - pub fn is_valid(evt: &str) -> bool { false }