pub struct Request {
pub method: String,
pub path: String,
pub version: String,
pub headers: HashMap<String, String>,
}Expand description
Represents a parsed HTTP/1.x request line and headers.
You receive this type after successful stream parsing. It is the primary request model used by the synchronous server path and shared response-generation helpers.
§Examples
use http_handle::request::Request;
use std::collections::HashMap;
let request = Request {
method: "GET".to_string(),
path: "/".to_string(),
version: "HTTP/1.1".to_string(),
headers: HashMap::new(),
};
assert_eq!(request.method(), "GET");§Panics
This type does not panic on construction.
Fields§
§method: StringHTTP method of the request.
path: StringRequested path.
version: StringHTTP version of the request.
headers: HashMap<String, String>Parsed request headers (header-name lowercased).
Implementations§
Source§impl Request
impl Request
Sourcepub fn from_stream(stream: &TcpStream) -> Result<Self, ServerError>
pub fn from_stream(stream: &TcpStream) -> Result<Self, ServerError>
Parses a request line and headers from a TcpStream.
This method reads the first line of an HTTP request from the given TCP stream,
parses it, and constructs a Request instance if the input is valid.
§Arguments
stream- A reference to theTcpStreamfrom which the request will be read.
§Returns
Ok(Request)- If the request is valid and successfully parsed.Err(ServerError)- If the request is malformed, cannot be read, or is invalid.
§Errors
This function returns a ServerError::InvalidRequest error if:
- The request line is too long (exceeds
MAX_REQUEST_LINE_LENGTH) - The request line does not contain exactly three parts
- The HTTP method is not recognized
- The request path does not start with a forward slash (except
OPTIONS *) - The HTTP version is not supported (only HTTP/1.0 and HTTP/1.1 are accepted)
§Examples
use std::net::TcpStream;
use http_handle::request::Request;
let stream = TcpStream::connect("127.0.0.1:8080").expect("connect");
let parsed = Request::from_stream(&stream);
assert!(parsed.is_ok() || parsed.is_err());§Panics
This function does not intentionally panic.
Sourcepub fn method(&self) -> &str
pub fn method(&self) -> &str
Returns the HTTP method of the request.
§Returns
A string slice containing the HTTP method (e.g., “GET”, “POST”).
Sourcepub fn version(&self) -> &str
pub fn version(&self) -> &str
Returns the HTTP version of the request.
§Returns
A string slice containing the HTTP version (e.g., “HTTP/1.1”).
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Returns the value of a header by case-insensitive name.
§Examples
use http_handle::request::Request;
use std::collections::HashMap;
let mut headers = HashMap::new();
headers.insert("content-type".to_string(), "text/plain".to_string());
let request = Request {
method: "GET".to_string(),
path: "/".to_string(),
version: "HTTP/1.1".to_string(),
headers,
};
assert_eq!(request.header("Content-Type"), Some("text/plain"));§Panics
This function does not panic.