From db268687f57751e4a3b8751a661d25e7e01bf75d Mon Sep 17 00:00:00 2001 From: Compositr Date: Fri, 1 Nov 2024 10:03:17 +1100 Subject: [PATCH] refactor: mod responses; send stream back on error --- src/http/requests.rs | 20 ++++++++++++++++---- src/http/responses.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/http/requests.rs b/src/http/requests.rs index bff260e..8f3e52a 100644 --- a/src/http/requests.rs +++ b/src/http/requests.rs @@ -4,6 +4,8 @@ use std::{ net::TcpStream, }; +use super::responses; + pub struct URL { raw: String, pub path: String, @@ -49,7 +51,9 @@ pub struct Request { pub enum RequestStatus { Ok(Request), - MalformedHTTP, + // Send stream back to the caller to decide what to do with it + // (usually close it) + MalformedHTTP(TcpStream), } impl Request { @@ -59,12 +63,12 @@ impl Request { let request_line = match lines.next() { Some(Ok(line)) => line, - _ => return RequestStatus::MalformedHTTP, + _ => return RequestStatus::MalformedHTTP(stream), }; let request_line_parts = request_line.split(" ").collect::>(); if request_line_parts.len() != 3 { - return RequestStatus::MalformedHTTP; + return RequestStatus::MalformedHTTP(stream); } let (method, url_str) = ( @@ -73,9 +77,17 @@ impl Request { ); let url = match URL::new(url_str) { Some(path) => path, - None => return RequestStatus::MalformedHTTP, + None => return RequestStatus::MalformedHTTP(stream), }; RequestStatus::Ok(Request { stream, method, url }) } + + pub fn send_404(&self) -> responses::UnitOrBoxedError { + responses::send_response(&self.stream, 404, "Not Found") + } + + pub fn send_ok(&self, body: &str) -> responses::UnitOrBoxedError { + responses::send_response(&self.stream, 200, body) + } } diff --git a/src/http/responses.rs b/src/http/responses.rs index 7e750a9..58d074c 100644 --- a/src/http/responses.rs +++ b/src/http/responses.rs @@ -2,9 +2,27 @@ use std::error::Error; use std::io::prelude::*; use std::net::TcpStream; -type UnitOrBoxedError = Result<(), Box>; +pub struct Response<'a> { + stream: &'a mut TcpStream, + pub status: u16, +} -fn send_response(mut stream: TcpStream, status: u16, body: &str) -> UnitOrBoxedError { +impl<'a> Response<'a> { + pub fn new(stream: &'a mut TcpStream, status: u16) -> Self { + Response { + stream, + status, + } + } + + pub fn send(&mut self) -> UnitOrBoxedError { + send_response(self.stream, self.status, "") + } +} + +pub type UnitOrBoxedError = Result<(), Box>; + +pub fn send_response(mut stream: &TcpStream, status: u16, body: &str) -> UnitOrBoxedError { let status_line = match status { 200 => "HTTP/1.1 200 OK", 400 => "HTTP/1.1 400 BAD REQUEST", @@ -14,7 +32,7 @@ fn send_response(mut stream: TcpStream, status: u16, body: &str) -> UnitOrBoxedE }; let response = format!( - "{}\r\nContent-Length: {}\r\n\r\n{}", + "{}\r\nContent-Length: {}\r\nServer: luciders/0.1.0\r\n\r\n{}", status_line, body.len(), body @@ -37,18 +55,6 @@ fn send_response(mut stream: TcpStream, status: u16, body: &str) -> UnitOrBoxedE Ok(()) } -pub fn ok(stream: TcpStream, body: &str) -> UnitOrBoxedError { +pub fn ok(stream: &TcpStream, body: &str) -> UnitOrBoxedError { send_response(stream, 200, body) } - -pub fn send_400(stream: TcpStream) -> UnitOrBoxedError { - send_response(stream, 400, "Bad Request") -} - -pub fn send_404(stream: TcpStream) -> UnitOrBoxedError { - send_response(stream, 404, "Not Found") -} - -pub fn send_405(stream: TcpStream) -> UnitOrBoxedError { - send_response(stream, 405, "Method Not Allowed") -}