http_server_rs/
httperror.rs

1use std::{io::Write, net::TcpStream};
2use crate::httpconstants::*;
3
4#[allow(dead_code)]
5#[derive(Debug)]
6pub struct HttpErrorWrapper {
7    possible_http_error : Option<HttpError>,
8}
9
10#[allow(dead_code)]
11impl HttpErrorWrapper {    
12    pub fn get_http_error(self) -> Option<HttpError> {
13        return self.possible_http_error;
14    }
15}
16
17#[allow(dead_code)]
18#[derive(Debug)]
19pub struct HttpError {
20    error_type : HttpErrorType,
21    code : i32,
22    message : &'static str,
23}
24
25impl HttpError {
26    const fn new(error_type : HttpErrorType, code : i32, message : &'static str) -> HttpError {
27        return HttpError {error_type, code, message};
28    }
29    
30    pub fn to_wrapped(self) -> HttpErrorWrapper {
31        return HttpErrorWrapper {possible_http_error : Option::Some(self)};
32    }
33    
34    pub fn to_wrapped_respond(self, mut tcpstream : &TcpStream) -> HttpErrorWrapper {
35        let line = self.code.to_string() + HttpConstants::get_code_text(self.code);
36        let error_msg = "<h1>".to_string() + &line + "</h1>" + self.message;
37        
38        let mut response_bytes : Vec<u8> = Vec::new();
39        
40        response_bytes.append(
41            &mut ("HTTP/1.1 ".to_string() + 
42            line.as_str() + 
43            "\r\n")
44            .into_bytes());
45        
46        response_bytes.append(
47            &mut (
48            "Content-Length: ".to_string() +
49            error_msg.len().to_string().as_str() +
50            "\r\n" +
51            "Content-Type: text/html\r\n" + 
52            "Connection: close\r\n" +
53            "\r\n" +
54            &error_msg
55            ).into_bytes());
56        
57        tcpstream.write(response_bytes.as_slice()).unwrap_or_default();
58                
59        return self.to_wrapped();
60    }
61}
62
63#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
64#[allow(deprecated)]
65#[non_exhaustive]
66pub enum HttpErrorType {
67    BadRequestLine,
68    IncompleteHttpRequest,
69    IncompleteHttpBody,
70    IncorrectContentLength,
71    IncorrectHeaderFormat,
72    EndpointNotFound,
73    ClientTimeout,
74}
75
76pub const BAD_REQUEST_LINE : HttpError = HttpError::new(HttpErrorType::BadRequestLine, HTTP_BAD_REQUEST, "Bad Request Line");
77pub const INCOMPLETE_REQUEST : HttpError = HttpError::new(HttpErrorType::IncompleteHttpRequest, HTTP_BAD_REQUEST, "Incomplete Request");
78pub const INCOMPLETE_BODY : HttpError = HttpError::new(HttpErrorType::IncompleteHttpBody, HTTP_BAD_REQUEST, "Incomplete Body");
79pub const INCORRECT_CONTENT_LENGTH : HttpError = HttpError::new(HttpErrorType::IncorrectContentLength, HTTP_BAD_REQUEST, "Incorrect Content Length");
80pub const INCORRECT_HEADER_FORMAT : HttpError = HttpError::new(HttpErrorType::IncorrectHeaderFormat, HTTP_BAD_REQUEST, "Incorrect Header Format");
81pub const ENDPOINT_NOT_FOUND : HttpError = HttpError::new(HttpErrorType::EndpointNotFound, HTTP_NOT_FOUND, "Endpoint Not Found");
82pub const CLIENT_TIMEOUT : HttpError = HttpError::new(HttpErrorType::ClientTimeout, HTTP_CLIENT_TIMEOUT, "Client Timed Out");