| Crates.io | http-rust |
| lib.rs | http-rust |
| version | 0.1.2 |
| created_at | 2023-12-25 09:38:55.172192+00 |
| updated_at | 2023-12-25 10:27:08.323927+00 |
| description | http library implementation for small scale web apps |
| homepage | https://techmornach.vercel.app |
| repository | https://github.com/techmornach/http-rust |
| max_upload_size | |
| id | 1080147 |
| size | 13,907 |
This Rust library provides basic functionality for handling HTTP requests and generating HTTP responses. It consists of two modules: httprequest for parsing incoming HTTP requests and httpresponse for creating HTTP responses.
To use http-rust in your Rust project, add the following line to your Cargo.toml file:
http-rust = "0.1.0"
Alternatively, you can run the following Cargo command in your project directory:
cargo add http-rust
httprequest ModuleResourceHttpRequestmethod: HTTP method (Get, Post, or Uninitialized).version: HTTP version (V1_1, V2_0, or Uninitialized).resource: Resource path (Path).headers: HashMap containing request headers.msg_body: String containing the message body.impl From<String> for HttpRequestHttpRequest struct.fn process_req_line(s: &str) -> (Method, Resource, Version)fn process_req_header(s: &str) -> (String, String)MethodGet, Post, or Uninitialized).VersionV1_1, V2_0, or Uninitialized).tests module to ensure proper functionality.httpresponse ModuleHttpResponse<'a>version: HTTP version.status_code: HTTP status code.status_text: HTTP status text.headers: Optional HashMap containing response headers.body: Optional response body.fn new(status_code: &'a str, headers: Option<HashMap<&'a str, &'a str>>, body: Option<String>) -> HttpResponse<'a>HttpResponse instance with the specified status code, headers, and body.fn send_response(&self, write_stream: &mut impl Write) -> Result<()>impl<'a> From<HttpResponse<'a>> for StringHttpResponse into a formatted HTTP response string.tests module to ensure proper functionality.use http_rust::httprequest::HttpRequest;
use http_rust::httpresponse::HttpResponse;
use std::collections::HashMap;
fn main() {
// Parse incoming HTTP request
let request_str = "GET /greeting HTTP/1.1\r\nHost: localhost:3000\r\nUser-Agent: curl/7.64.1\r\nAccept: */*\r\n\r\n";
let request: HttpRequest = request_str.into();
// Create an HTTP response
let headers = Some({
let mut h = HashMap::new();
h.insert("Content-Type", "text/html");
h
});
let body = Some("Hello, world!".to_string());
let response = HttpResponse::new("200", headers, body);
// Send the response
let mut write_stream = Vec::new();
response.send_response(&mut write_stream).unwrap();
let response_str = String::from_utf8(write_stream).unwrap();
println!("Received Request: {:?}", request);
println!("Generated Response: {}", response_str);
}
This project is licensed under the terms of the MIT License. See the LICENSE file for more details.
Feel free to customize and extend the library based on your specific use case and requirements. If you have any questions or encounter issues, please refer to the provided unit tests or reach out for support.