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
- Represents the resource path in an HTTP request.
- Represents an incoming HTTP request.
- Fields:
method
: HTTP method (Get
,Post
, orUninitialized
).version
: HTTP version (V1_1
,V2_0
, orUninitialized
).resource
: Resource path (Path
).headers
: HashMap containing request headers.msg_body
: String containing the message body.
- Converts a raw HTTP request string into an
HttpRequest
struct.
- Processes the request line to extract method, resource, and version.
- Processes a request header line to extract key and value.
- Represents HTTP methods (
Get
,Post
, orUninitialized
).
- Represents HTTP versions (
V1_1
,V2_0
, orUninitialized
).
- Unit tests are provided in the
tests
module to ensure proper functionality.
- Represents an HTTP response.
- Fields:
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>
- Creates a new
HttpResponse
instance with the specified status code, headers, and body.
- Sends the HTTP response to the provided write stream.
- Converts an
HttpResponse
into a formatted HTTP response string.
- Unit tests are provided in the
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.