#http-request #request-response #http-parser #raw #convert #build

http_request_parser

Converts raw http request to a Request and build Response

5 releases

0.2.0 Dec 17, 2022
0.1.3 Oct 26, 2022
0.1.2 Oct 26, 2022
0.1.1 Oct 21, 2022
0.1.0 Oct 21, 2022

#39 in #http-parser

MIT/Apache

5KB
77 lines

http_parser

Converts raw request to Request and build Responses

Parse the raw http request to Request

    for stream in listener.incoming(){
        let mut tcp_stream = stream.unwrap();
        let request = http_parser::Request::from(&tcp_stream);

And now you can use Request properties to build a Response and send it

        let request = http_request_parser::Request::from(&tcp_stream);
        let mut response = http_request_parser::Response::new();

        if request.path == "/" {
            response.body = "Hello, World!".to_owned();
        } else {
            response.headers = vec!["Content-Type: application/json".to_owned()];
            response.body = format!("{{\n\t\"actualPath\":\"{}\"\n}}", request.path);
        }
        response.send(&tcp_stream)
    }

No runtime deps