diff --git a/src/http.rs b/src/http.rs index b034b6d..96f51d6 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,8 +1,16 @@ +mod consts; +mod parser; +mod handler; + use std::net::TcpStream; use openssl::ssl::SslStream; use std::io::{Write, Read}; use std::fmt::Formatter; +pub use handler::*; + +static REQUESTS_PER_CONNECTION: u32 = 200; + pub enum Stream { Tcp(TcpStream), Ssl(SslStream), @@ -61,6 +69,67 @@ 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 { + if *code == status_code { + return class.clone(); + } + } + match status_code { + 100..=199 => StatusClass::Informational, + 200..=299 => StatusClass::Success, + 300..=399 => StatusClass::Redirection, + 400..=499 => StatusClass::ClientError, + 500..=599 => StatusClass::ServerError, + _ => panic!("invalid status code") + } + } +} + +#[derive(Clone)] +pub struct Status { + code: u16, + message: String, + class: StatusClass, +} + +impl Status { + pub fn from_code(status_code: u16) -> Status { + for (code, class, msg, _desc) in &consts::HTTP_STATUSES { + if *code == status_code { + return Status { + code: status_code, + message: msg.to_string(), + class: class.clone(), + } + } + } + panic!("invalid status code"); + } + + pub fn new_custom(status_code: u16, message: &str) -> Status { + if status_code < 100 || status_code > 599 { + panic!("invalid status code"); + } + Status { + code: status_code, + message: message.to_string(), + class: StatusClass::from_code(status_code), + } + } +} + + pub struct HeaderField { name: String, value: String, @@ -73,17 +142,44 @@ impl std::fmt::Display for HeaderField { } pub struct Request { + version: String, method: Method, uri: String, header_fields: Vec } pub struct Response { - status_code: u16, - status_message: String, + version: String, + status: Status, header_fields: Vec } +impl Response { + fn new() -> Response { + Response { + version: "1.1".to_string(), + status: Status::from_code(200), + header_fields: Vec::new(), + } + } + + fn add_header(&mut self, name: &str, value: &str) { + self.header_fields.push(HeaderField { + name: String::from(name), + value: String::from(value), + }); + } + + 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); + for header_field in &self.header_fields { + header.push_str(format!("{}: {}\r\n", header_field.name, header_field.value).as_str()); + } + header.push_str("\r\n"); + stream.write_all(header.as_bytes()) + } +} + impl Stream { pub fn read(&mut self, buf: &mut [u8]) -> Result { match self { @@ -102,401 +198,21 @@ impl Stream { pub fn peek(&mut self, buf: &mut [u8]) -> Result { match self { Stream::Tcp(stream) => stream.peek(buf), - Stream::Ssl(_stream) => todo!("Not implemented"), + Stream::Ssl(_stream) => todo!("Not implemented in rust-openssl"), } } - pub fn write(&mut self, buf: &mut [u8]) -> Result { + pub fn write(&mut self, buf: &[u8]) -> Result { match self { Stream::Tcp(stream) => stream.write(buf), Stream::Ssl(stream) => stream.write(buf), } } -} -pub mod handler { - pub fn connection_handler(client: super::Stream) { - let mut client = super::HttpStream { - stream: client, - request_num: 0, - }; - - while client.request_num < 200 { - request_handler(&mut client); - client.request_num += 1; - } - } - - fn request_handler(client: &mut super::HttpStream) { - let req = super::parser::parse_request(&mut client.stream).unwrap(); - println!("{} {}", req.method, req.uri); - } -} - -pub mod parser { - use crate::http; - use std::os::linux::raw::stat; - - pub fn parse_request(stream: &mut http::Stream) -> Result { - let mut buf = [0; 4096]; - let size = stream.peek(&mut buf).unwrap(); - - 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 { - name: String::from(name), - value: String::from(value), - }); - } - - let request = http::Request { - method: http::Method::from_str(parser.method.unwrap()), - uri: String::from(parser.uri.unwrap()), - header_fields, - }; - - stream.read_exact(&mut buf[..header_size]).unwrap(); - - Ok(request) - } - - pub fn parse_response(stream: &mut http::Stream) -> Result { - let mut buf = [0; 4096]; - let size = stream.peek(&mut buf).unwrap(); - - let mut parser = Parser::new_request_parser(&buf[..size]); - let header_size = parser.parse().unwrap(); - - let status_code = parser.status_code.unwrap(); - let status_code = match status_code.parse() { - Ok(v) => v, - Err(e) => return Err(format!("{}", e)), - }; - - let mut header_fields = Vec::new(); - for (name, value) in parser.headers { - header_fields.push(http::HeaderField { - name: String::from(name), - value: String::from(value), - }); - } - - let response = http::Response { - status_code, - status_message: String::from(parser.status_message.unwrap()), - header_fields, - }; - - stream.read_exact(&mut buf[..header_size]).unwrap(); - - Ok(response) - } - - #[derive(Copy, Clone)] - enum State<'a> { - Method, - Uri, - Http(&'a State<'a>), - HttpVersion(&'a State<'a>), - StatusCode, - StatusMessage, - HeaderName, - HeaderValue, - Finish, - CRLF(&'a State<'a>), - Error, - } - - struct Parser<'a> { - state: State<'a>, - buf: &'a [u8], - str_start: usize, - header_size: usize, - method: Option<&'a str>, - uri: Option<&'a str>, - http_version: Option<&'a str>, - status_code: Option<&'a str>, - status_message: Option<&'a str>, - headers: Vec<(&'a str, &'a str)>, - } - - impl Parser<'_> { - fn new_request_parser(buf: &[u8]) -> Parser { - Parser { - state: State::Method, - buf, - str_start: 0, - header_size: 0, - method: None, - uri: None, - http_version: None, - status_code: None, - status_message: None, - headers: Vec::new(), - } - } - - fn new_response_parser(buf: &[u8]) -> Parser { - Parser { - state: State::Http(&State::StatusCode), - buf, - str_start: 0, - header_size: 0, - method: None, - uri: None, - http_version: None, - status_code: None, - status_message: None, - headers: Vec::new(), - } - } - - fn parse(&mut self) -> Result { - for char in self.buf { - 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)), - _ => {}, - } - } - return Err(String::from("input too short")); - } - - 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() - }; - 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::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::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::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::StatusMessage => { - match char { - 0x20..=0x7E => State::StatusMessage, - 0x0D => { - self.status_message = Some(get_str()); - 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 - } - }, - 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, - } - }, - } - } - } - - #[cfg(test)] - mod tests { - use std::panic::panic_any; - - #[test] - fn simple_request() { - let request: &str = "GET /index.html HTTP/1.1\r\n\ - Host: www.example.com\r\n\ - \r\n"; - - let mut parser = super::Parser::new_request_parser(request.as_bytes()); - let size = parser.parse().unwrap(); - - assert_eq!(51, size); - assert_eq!("GET", parser.method.unwrap()); - assert_eq!("/index.html", parser.uri.unwrap()); - assert_eq!("1.1", parser.http_version.unwrap()); - assert_eq!(None, parser.status_code); - assert_eq!(None, parser.status_message); - - assert_eq!(1, parser.headers.len()); - assert_eq!(("Host", "www.example.com"), parser.headers[0]); - } - - #[test] - fn complex_request() { - let request: &str = "POST /upload/file.txt HTTP/1.3\r\n\ - Host: www.example.com \r\n\ - Content-Length: 13 \r\n\ - User-Agent: Mozilla/5.0 (X11; Linux x86_64) \r\n\ - \r\n\ - username=test"; - - let mut parser = super::Parser::new_request_parser(request.as_bytes()); - let size = parser.parse().unwrap(); - - assert_eq!(129, size); - assert_eq!("POST", parser.method.unwrap()); - assert_eq!("/upload/file.txt", parser.uri.unwrap()); - assert_eq!("1.3", parser.http_version.unwrap()); - assert_eq!(None, parser.status_code); - assert_eq!(None, parser.status_message); - - 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!("username=test", &request[size..]); - } - - #[test] - fn invalid_request_1() { - let request: &str = "GET /files/größe.txt HTTP/1.1\r\n\r\n"; - let mut parser = super::Parser::new_request_parser(request.as_bytes()); - match parser.parse() { - Ok(v) => panic!("should fail"), - Err(e) => assert_eq!("invalid character at position 13", e), - } - } - - #[test] - fn invalid_request_2() { - let request: &str = "GET /index.html HTT"; - let mut parser = super::Parser::new_request_parser(request.as_bytes()); - match parser.parse() { - Ok(v) => panic!("should fail"), - Err(e) => assert_eq!("input too short", e), - } - } - - #[test] - fn simple_response() { - let response: &str = "HTTP/1.1 200 OK\r\n\ - Content-Length: 12\r\n\ - Content-Type: text/plain; charset=us-ascii\r\n\ - \r\n\ - Hello world!"; - - let mut parser = super::Parser::new_response_parser(response.as_bytes()); - let size = parser.parse().unwrap(); - - assert_eq!(83, size); - assert_eq!("200", parser.status_code.unwrap()); - assert_eq!("OK", parser.status_message.unwrap()); - assert_eq!("1.1", parser.http_version.unwrap()); - assert_eq!(None, parser.method); - assert_eq!(None, parser.uri); - - 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!("Hello world!", &response[size..]); + pub fn write_all(&mut self, buf: &[u8]) -> Result<(), std::io::Error> { + match self { + Stream::Tcp(stream) => stream.write_all(buf), + Stream::Ssl(stream) => stream.write_all(buf), } } } diff --git a/src/http/consts.rs b/src/http/consts.rs new file mode 100644 index 0000000..190b0bc --- /dev/null +++ b/src/http/consts.rs @@ -0,0 +1,91 @@ +use super::StatusClass::*; +use super::StatusClass; + +pub static HTTP_STATUSES: [(u16, StatusClass, &str, &str); 41] = [ + (100, Informational, "Continue", + "The client SHOULD continue with its request."), + (101, Informational, "Switching Protocols", + "The server understands and is willing to comply with the clients request, via the Upgrade message header field, for a change in the application protocol being used on this connection."), + + (200, Success, "OK", + "The request has succeeded."), + (201, Success, "Created", + "The request has been fulfilled and resulted in a new resource being created."), + (202, Success, "Accepted", + "The request has been accepted for processing, but the processing has not been completed."), + (203, Success, "Non-Authoritative Information", + "The returned meta information in the entity-header is not the definitive set as available from the origin server, but is gathered from a local or a third-party copy."), + (204, Success, "No Content", + "The server has fulfilled the request but does not need to return an entity-body, and might want to return updated meta information."), + (205, Success, "Reset Content", + "The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent."), + (206, Success, "Partial Content", + "The server has fulfilled the partial GET request for the resource."), + + (300, Redirection, "Multiple Choices", + "The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent-driven negotiation information is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location."), + (301, Redirection, "Moved Permanently", + "The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs."), + (302, Redirection, "Found", + "The requested resource resides temporarily under a different URI."), + (303, Redirection, "See Other", + "The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource."), + (304, Success, "Not Modified", + "The request has been fulfilled and the requested resource has not been modified."), + (305, Redirection, "Use Proxy", + "The requested resource MUST be accessed through the proxy given by the Location field."), + (307, Redirection, "Temporary Redirect", + "The requested resource resides temporarily under a different URI."), + (308, Redirection, "Permanent Redirect", + "The requested resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs."), + + (400, ClientError, "Bad Request", + "The request could not be understood by the server due to malformed syntax."), + (401, ClientError, "Unauthorized", + "The request requires user authentication."), + (402, ClientError, "Payment Required", + ""), + (403, ClientError, "Forbidden", + "The server understood the request, but is refusing to fulfill it."), + (404, ClientError, "Not Found", + "The server has not found anything matching the Request-URI."), + (405, ClientError, "Method Not Allowed", + "The method specified in the Request-Line is not allowed for the resource identified by the Request-URI."), + (406, ClientError, "Not Acceptable", + "The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request."), + (407, ClientError, "Proxy Authentication Required", + "The request requires user authentication on the proxy."), + (408, ClientError, "Request Timeout", + "The client did not produce a request within the time that the server was prepared to wait."), + (409, ClientError, "Conflict", + "The request could not be completed due to a conflict with the current state of the resource."), + (410, ClientError, "Gone", + "The requested resource is no longer available at the server and no forwarding address is known."), + (411, ClientError, "Length Required", + "The server refuses to accept the request without a defined Content-Length."), + (412, ClientError, "Precondition Failed", + "The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server."), + (413, ClientError, "Request Entity Too Large", + "The server is refusing to process a request because the request entity is larger than the server is willing or able to process."), + (414, ClientError, "Request-URI Too Long", + "The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret."), + (415, ClientError, "Unsupported Media Type", + "The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."), + (416, ClientError, "Range Not Satisfiable", + "None of the ranges in the requests Range header field overlap the current extent of the selected resource or that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges."), + (417, ClientError, "Expectation Failed", + "The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server."), + + (500, ServerError, "Internal Server Error", + "The server encountered an unexpected condition which prevented it from fulfilling the request." ), + (501, ServerError, "Not Implemented", + "The server does not support the functionality required to fulfill the request."), + (502, ServerError, "Bad Gateway", + "The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request."), + (503, ServerError, "Service Unavailable", + "The server is currently unable to handle the request due to a temporary overloading or maintenance of the server."), + (504, ServerError, "Gateway Timeout", + "The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI or some other auxiliary server it needed to access in attempting to complete the request."), + (505, ServerError, "HTTP Version Not Supported", + "The server does not support, or refuses to support, the HTTP protocol version that was used in the request message."), +]; diff --git a/src/http/handler.rs b/src/http/handler.rs new file mode 100644 index 0000000..c066182 --- /dev/null +++ b/src/http/handler.rs @@ -0,0 +1,24 @@ + +pub fn connection_handler(client: super::Stream) { + let mut client = super::HttpStream { + stream: client, + request_num: 0, + }; + + while client.request_num < super::REQUESTS_PER_CONNECTION { + request_handler(&mut client); + client.request_num += 1; + } +} + +fn request_handler(client: &mut super::HttpStream) { + let req = super::parser::parse_request(&mut client.stream).unwrap(); + println!("{} {}", req.method, req.uri); + + let mut res = super::Response::new(); + let doc = "Locutus Server

Hello World! :D

"; + res.add_header("Content-Length", doc.len().to_string().as_str()); + res.add_header("Content-Type", "text/html; charset=utf-8"); + res.send(&mut client.stream).unwrap(); + client.stream.write_all(doc.as_bytes()).unwrap(); +} diff --git a/src/http/parser.rs b/src/http/parser.rs new file mode 100644 index 0000000..2f4e927 --- /dev/null +++ b/src/http/parser.rs @@ -0,0 +1,364 @@ +use crate::http; +use crate::http::Status; + +pub fn parse_request(stream: &mut http::Stream) -> Result { + let mut buf = [0; 4096]; + let size = stream.peek(&mut buf).unwrap(); + + 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 { + name: String::from(name), + value: String::from(value), + }); + } + + let request = http::Request { + version: String::from(parser.http_version.unwrap()), + method: http::Method::from_str(parser.method.unwrap()), + uri: String::from(parser.uri.unwrap()), + header_fields, + }; + + stream.read_exact(&mut buf[..header_size]).unwrap(); + + Ok(request) +} + +pub fn parse_response(stream: &mut http::Stream) -> Result { + let mut buf = [0; 4096]; + let size = stream.peek(&mut buf).unwrap(); + + let mut parser = Parser::new_request_parser(&buf[..size]); + let header_size = parser.parse().unwrap(); + + let status_code = parser.status_code.unwrap(); + let status_code = match status_code.parse() { + Ok(v) => v, + Err(e) => return Err(format!("{}", e)), + }; + + let mut header_fields = Vec::new(); + for (name, value) in parser.headers { + header_fields.push(http::HeaderField { + name: String::from(name), + value: String::from(value), + }); + } + + let response = http::Response { + version: String::from(parser.http_version.unwrap()), + status: Status::new_custom(status_code, parser.status_message.unwrap()), + header_fields, + }; + + stream.read_exact(&mut buf[..header_size]).unwrap(); + + Ok(response) +} + +#[derive(Copy, Clone)] +enum State<'a> { + Method, + Uri, + Http(&'a State<'a>), + HttpVersion(&'a State<'a>), + StatusCode, + StatusMessage, + HeaderName, + HeaderValue, + Finish, + CRLF(&'a State<'a>), + Error, +} + +struct Parser<'a> { + state: State<'a>, + buf: &'a [u8], + str_start: usize, + header_size: usize, + method: Option<&'a str>, + uri: Option<&'a str>, + http_version: Option<&'a str>, + status_code: Option<&'a str>, + status_message: Option<&'a str>, + headers: Vec<(&'a str, &'a str)>, +} + +impl Parser<'_> { + fn new_request_parser(buf: &[u8]) -> Parser { + Parser { + state: State::Method, + buf, + str_start: 0, + header_size: 0, + method: None, + uri: None, + http_version: None, + status_code: None, + status_message: None, + headers: Vec::new(), + } + } + + fn new_response_parser(buf: &[u8]) -> Parser { + Parser { + state: State::Http(&State::StatusCode), + buf, + str_start: 0, + header_size: 0, + method: None, + uri: None, + http_version: None, + status_code: None, + status_message: None, + headers: Vec::new(), + } + } + + fn parse(&mut self) -> Result { + for char in self.buf { + 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)), + _ => {}, + } + } + return Err(String::from("input too short")); + } + + 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() + }; + 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::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::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::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::StatusMessage => { + match char { + 0x20..=0x7E => State::StatusMessage, + 0x0D => { + self.status_message = Some(get_str()); + 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 + } + }, + 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, + } + }, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn simple_request() { + let request: &str = "GET /index.html HTTP/1.1\r\n\ + Host: www.example.com\r\n\ + \r\n"; + + let mut parser = super::Parser::new_request_parser(request.as_bytes()); + let size = parser.parse().unwrap(); + + assert_eq!(51, size); + assert_eq!("GET", parser.method.unwrap()); + assert_eq!("/index.html", parser.uri.unwrap()); + assert_eq!("1.1", parser.http_version.unwrap()); + assert_eq!(None, parser.status_code); + assert_eq!(None, parser.status_message); + + assert_eq!(1, parser.headers.len()); + assert_eq!(("Host", "www.example.com"), parser.headers[0]); + } + + #[test] + fn complex_request() { + let request: &str = "POST /upload/file.txt HTTP/1.3\r\n\ + Host: www.example.com \r\n\ + Content-Length: 13 \r\n\ + User-Agent: Mozilla/5.0 (X11; Linux x86_64) \r\n\ + \r\n\ + username=test"; + + let mut parser = super::Parser::new_request_parser(request.as_bytes()); + let size = parser.parse().unwrap(); + + assert_eq!(129, size); + assert_eq!("POST", parser.method.unwrap()); + assert_eq!("/upload/file.txt", parser.uri.unwrap()); + assert_eq!("1.3", parser.http_version.unwrap()); + assert_eq!(None, parser.status_code); + assert_eq!(None, parser.status_message); + + 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!("username=test", &request[size..]); + } + + #[test] + fn invalid_request_1() { + let request: &str = "GET /files/größe.txt HTTP/1.1\r\n\r\n"; + let mut parser = super::Parser::new_request_parser(request.as_bytes()); + match parser.parse() { + Ok(_v) => panic!("should fail"), + Err(e) => assert_eq!("invalid character at position 13", e), + } + } + + #[test] + fn invalid_request_2() { + let request: &str = "GET /index.html HTT"; + let mut parser = super::Parser::new_request_parser(request.as_bytes()); + match parser.parse() { + Ok(_v) => panic!("should fail"), + Err(e) => assert_eq!("input too short", e), + } + } + + #[test] + fn simple_response() { + let response: &str = "HTTP/1.1 200 OK\r\n\ + Content-Length: 12\r\n\ + Content-Type: text/plain; charset=us-ascii\r\n\ + \r\n\ + Hello world!"; + + let mut parser = super::Parser::new_response_parser(response.as_bytes()); + let size = parser.parse().unwrap(); + + assert_eq!(83, size); + assert_eq!("200", parser.status_code.unwrap()); + assert_eq!("OK", parser.status_message.unwrap()); + assert_eq!("1.1", parser.http_version.unwrap()); + assert_eq!(None, parser.method); + assert_eq!(None, parser.uri); + + 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!("Hello world!", &response[size..]); + } +} diff --git a/src/main.rs b/src/main.rs index 78e1680..9a6e073 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn main() { for stream in tcp_socket.incoming() { pool_mutex_ref.lock().unwrap().execute(|| { let stream = stream.unwrap(); - http::handler::connection_handler(http::Stream::Tcp(stream)); + http::connection_handler(http::Stream::Tcp(stream)); }); } })); @@ -41,7 +41,7 @@ fn main() { pool_mutex_ref.lock().unwrap().execute(move || { let stream = stream.unwrap(); let stream = acceptor.accept(stream).unwrap(); - http::handler::connection_handler(http::Stream::Ssl(stream)); + http::connection_handler(http::Stream::Ssl(stream)); }); } }));