pub struct HttpRequest {
pub version: Version,
pub headers: HeaderMap<HeaderValue>,
pub uri: Uri,
pub method: Method,
}Expand description
Parsed HTTP request with method, URI, headers, and version.
Contains the request-line and header section of an HTTP message (RFC 9110 §6). The message body is handled separately via streaming.
Fields§
§version: VersionHTTP version (e.g., HTTP/1.1, HTTP/2).
headers: HeaderMap<HeaderValue>Header fields from the request.
uri: UriRequest target URI.
method: MethodHTTP method (GET, POST, CONNECT, etc.).
Implementations§
Source§impl HttpRequest
impl HttpRequest
Sourcepub fn from_parts(parts: Parts) -> Self
pub fn from_parts(parts: Parts) -> Self
Creates a request from hyper request parts.
Sourcepub fn parse_with_len(buf: &[u8]) -> Result<Option<(usize, Self)>>
pub fn parse_with_len(buf: &[u8]) -> Result<Option<(usize, Self)>>
Parses a request from a buffer, returning None if incomplete.
On success, returns the byte length consumed and the parsed request. Use this for incremental parsing when data arrives in chunks.
Sourcepub async fn peek(
reader: &mut Prebuffered<impl AsyncRead + Unpin>,
) -> Result<(usize, Self)>
pub async fn peek( reader: &mut Prebuffered<impl AsyncRead + Unpin>, ) -> Result<(usize, Self)>
Reads and parses the request line and header section.
Does not remove the header section from reader.
Returns the length of the header section and the request.
Returns io::ErrorKind::OutOfMemory if the header section exceeds the buffer limit.
Sourcepub async fn read(
reader: &mut Prebuffered<impl AsyncRead + Unpin>,
) -> Result<Self>
pub async fn read( reader: &mut Prebuffered<impl AsyncRead + Unpin>, ) -> Result<Self>
Reads and parses the request line and header section.
Removes the header section from reader.
Returns io::ErrorKind::OutOfMemory if the header section exceeds the buffer limit.
Sourcepub fn parse(buf: &[u8]) -> Result<Option<Self>>
pub fn parse(buf: &[u8]) -> Result<Option<Self>>
Parses a request from a buffer, returning None if incomplete.
Sourcepub fn try_into_proxy_request(self) -> Result<HttpProxyRequest>
pub fn try_into_proxy_request(self) -> Result<HttpProxyRequest>
Converts to a proxy request for authority-form or absolute-form targets.
§Errors
Returns an error for origin-form requests (GET /path), which lack
routing information for forward proxies.
Sourcepub fn host(&self) -> Option<&str>
pub fn host(&self) -> Option<&str>
Returns the target host from the request.
For HTTP/2+, extracts from the :authority pseudo-header (via URI).
For HTTP/1.x, extracts from the Host header field.
Sourcepub fn header_str(&self, name: impl AsHeaderName) -> Option<&str>
pub fn header_str(&self, name: impl AsHeaderName) -> Option<&str>
Returns a header value as a string, if present and valid UTF-8.
Sourcepub fn classify(&self) -> Result<HttpRequestKind>
pub fn classify(&self) -> Result<HttpRequestKind>
Classifies the request by its target form (RFC 9110 §7.1).
§Errors
Returns an error if a CONNECT request lacks a valid authority-form target, or if an HTTP/1 absolute-form request target includes a scheme but no authority.
Sourcepub fn set_forwarded_for(&mut self, src_addr: SocketAddr) -> &mut Self
pub fn set_forwarded_for(&mut self, src_addr: SocketAddr) -> &mut Self
Appends an X-Forwarded-For header with the client address.
Per the de facto standard, this identifies the originating client IP for requests forwarded through proxies.
Sourcepub fn set_forwarded_for_if_tcp(&mut self, src_addr: SrcAddr) -> &mut Self
pub fn set_forwarded_for_if_tcp(&mut self, src_addr: SrcAddr) -> &mut Self
Appends an X-Forwarded-For header with the client address if the source is a TCP address.
Does nothing if src_addr is SrcAddr::Unix
Sourcepub fn remove_headers(
&mut self,
names: impl IntoIterator<Item = impl AsHeaderName>,
) -> &mut Self
pub fn remove_headers( &mut self, names: impl IntoIterator<Item = impl AsHeaderName>, ) -> &mut Self
Removes the specified headers from the request.
Sourcepub fn set_via(
&mut self,
pseudonym: impl Display,
) -> Result<&mut Self, InvalidHeaderValue>
pub fn set_via( &mut self, pseudonym: impl Display, ) -> Result<&mut Self, InvalidHeaderValue>
Appends a Via header indicating this proxy (RFC 9110 §7.6.3).
The header value includes the protocol version and the given pseudonym.
Sourcepub fn set_target(
&mut self,
target: Uri,
) -> Result<&mut Self, InvalidHeaderValue>
pub fn set_target( &mut self, target: Uri, ) -> Result<&mut Self, InvalidHeaderValue>
Sets the request target URI and updates the Host header.
The original Host value is preserved in X-Forwarded-Host.
Converts the request to absolute-form with the given authority.
Sets the scheme to HTTP and updates the Host header to match.
Used by reverse proxies to transform origin-form requests.