pub struct HttpRequest {
pub method: Method,
pub path: ByteStr,
pub headers: HeaderMap,
pub body: Bytes,
pub path_params: RouteParams,
pub extensions: Extensions,
/* private fields */
}Expand description
HTTP request wrapper
The path and body are Bytes-backed, so cloning a request is a handful of
refcount bumps rather than a deep copy of the target and payload.
Fields§
§method: MethodThe request method.
Was a String. An unrecognized token is carried as Method::Other
rather than rejected here; routing answers it with 404
(crate::Error::RouteNotFound) since no route can match the token.
path: ByteStrThe raw request target, query string included.
Was a String. A ByteStr so it can be a slice of the connection read
buffer once the serve path moves onto armature-h1; Deref<Target = str>
keeps &req.path working wherever a &str is wanted.
headers: HeaderMapRequest headers stored in a SmallVec-backed HeaderMap.
For typical requests (<12 headers) this is stored inline on the stack,
avoiding the per-request HashMap heap allocation on the read path.
The API is HashMap-compatible (get/insert/iter/contains_key/…),
with case-insensitive header name lookup.
body: BytesThe request body.
Was a Vec<u8> shadowed by an optional Bytes that could disagree with
it. One field, always authoritative.
path_params: RouteParams§extensions: ExtensionsType-safe extensions for storing application state.
Use this to pass typed data to handlers without DI container lookups.
Access via the State<T> extractor for zero-cost state retrieval.
Implementations§
Source§impl HttpRequest
Extension methods for HttpRequest to support content negotiation.
impl HttpRequest
Extension methods for HttpRequest to support content negotiation.
Sourcepub fn accept_language(&self) -> AcceptLanguage
pub fn accept_language(&self) -> AcceptLanguage
Get the Accept-Language header parsed into language tags.
Sourcepub fn accept_encoding(&self) -> AcceptEncoding
pub fn accept_encoding(&self) -> AcceptEncoding
Get the Accept-Encoding header parsed into encodings.
Sourcepub fn accept_charset(&self) -> AcceptCharset
pub fn accept_charset(&self) -> AcceptCharset
Get the Accept-Charset header parsed into charsets.
Sourcepub fn accepts(&self, media_type: &MediaType) -> bool
pub fn accepts(&self, media_type: &MediaType) -> bool
Check if the client accepts a specific media type.
Sourcepub fn prefers_json(&self) -> bool
pub fn prefers_json(&self) -> bool
Check if the client prefers JSON over HTML.
Sourcepub fn prefers_html(&self) -> bool
pub fn prefers_html(&self) -> bool
Check if the client prefers HTML over JSON.
Sourcepub fn negotiate_media_type<'a>(
&self,
available: &'a [MediaType],
) -> Option<&'a MediaType>
pub fn negotiate_media_type<'a>( &self, available: &'a [MediaType], ) -> Option<&'a MediaType>
Negotiate the best media type from available options.
Sourcepub fn negotiate_language<'a>(
&self,
available: &'a [LanguageTag],
) -> Option<&'a LanguageTag>
pub fn negotiate_language<'a>( &self, available: &'a [LanguageTag], ) -> Option<&'a LanguageTag>
Negotiate the best language from available options.
Sourcepub fn negotiate_encoding(&self, available: &[Encoding]) -> Option<Encoding>
pub fn negotiate_encoding(&self, available: &[Encoding]) -> Option<Encoding>
Negotiate the best encoding from available options.
Source§impl HttpRequest
impl HttpRequest
Sourcepub fn new(method: impl Into<Method>, path: impl Into<ByteStr>) -> Self
pub fn new(method: impl Into<Method>, path: impl Into<ByteStr>) -> Self
Create a request.
Generic in the method so every existing HttpRequest::new("GET", …)
call site compiles unchanged.
Sourcepub fn with_extensions_capacity(
method: impl Into<Method>,
path: impl Into<ByteStr>,
capacity: usize,
) -> Self
pub fn with_extensions_capacity( method: impl Into<Method>, path: impl Into<ByteStr>, capacity: usize, ) -> Self
Create a new request with pre-allocated extensions capacity.
Sourcepub fn with_bytes_body(
method: impl Into<Method>,
path: impl Into<ByteStr>,
body: Bytes,
) -> Self
pub fn with_bytes_body( method: impl Into<Method>, path: impl Into<ByteStr>, body: Bytes, ) -> Self
Create a new request with a Bytes body (zero-copy).
This is the most efficient way to create a request from Hyper’s body, as it avoids copying the body data.
Sourcepub fn set_body_bytes(&mut self, bytes: Bytes)
pub fn set_body_bytes(&mut self, bytes: Bytes)
Set the body (zero-copy).
Sourcepub fn body_bytes(&self) -> Bytes
pub fn body_bytes(&self) -> Bytes
The body as Bytes. A refcount bump, not a copy.
Sourcepub fn body_slice(&self) -> &[u8] ⓘ
pub fn body_slice(&self) -> &[u8] ⓘ
The body as a byte slice.
Sourcepub fn path_only(&self) -> &str
pub fn path_only(&self) -> &str
The request target with any query string removed.
This is what routing matches on, and what most callers mean when they
say “the path” — path/path_str are the raw target, which is what the
query is parsed out of.
Sourcepub fn request_body(&self) -> RequestBody
pub fn request_body(&self) -> RequestBody
Get the body as a RequestBody (zero-copy wrapper).
Sourcepub fn has_bytes_body(&self) -> bool
pub fn has_bytes_body(&self) -> bool
Whether the body holds anything.
Kept for call-site compatibility from when the body could live in either
of two fields; it is always Bytes now.
Sourcepub fn method_str(&self) -> &str
pub fn method_str(&self) -> &str
The method as a string, for logging and for code that compares tokens.
Sourcepub fn set_body(&mut self, body: Vec<u8>)
pub fn set_body(&mut self, body: Vec<u8>)
Set the body from a Vec<u8>, taking over its allocation.
Sourcepub fn from_parts(
method: impl Into<Method>,
path: impl Into<ByteStr>,
headers: HashMap<String, String>,
body: Vec<u8>,
path_params: HashMap<String, String>,
query_params: HashMap<String, String>,
) -> Self
pub fn from_parts( method: impl Into<Method>, path: impl Into<ByteStr>, headers: HashMap<String, String>, body: Vec<u8>, path_params: HashMap<String, String>, query_params: HashMap<String, String>, ) -> Self
Create a request from all parts (for compatibility in tests).
Sourcepub fn insert_extension<T: Send + Sync + 'static>(&mut self, value: T)
pub fn insert_extension<T: Send + Sync + 'static>(&mut self, value: T)
Sourcepub fn insert_extension_arc<T: Send + Sync + 'static>(&mut self, value: Arc<T>)
pub fn insert_extension_arc<T: Send + Sync + 'static>(&mut self, value: Arc<T>)
Insert an Arc-wrapped value into request extensions.
This is more efficient when you already have an Arc.
Sourcepub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a reference to a typed extension.
Returns None if no value of this type exists.
Sourcepub fn extension_arc<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn extension_arc<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Get an Arc reference to a typed extension.
Sourcepub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Sourcepub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Parse URL-encoded form data
Sourcepub fn form_map(&self) -> Result<HashMap<String, String>, Error>
pub fn form_map(&self) -> Result<HashMap<String, String>, Error>
Parse URL-encoded form data into a HashMap
Sourcepub fn param_bytes(&self, name: &str) -> Option<&Bytes>
pub fn param_bytes(&self, name: &str) -> Option<&Bytes>
A captured route parameter, raw.
Sourcepub fn push_param(&mut self, name: &str, value: impl Into<Bytes>)
pub fn push_param(&mut self, name: &str, value: impl Into<Bytes>)
Add one captured route parameter, interning its name.
The router uses HttpRequest::set_params with names already interned
at registration; this is for callers assembling a request by hand. The
interner is hard-capped (crate::param_intern::MAX_INTERNED), so
feeding this a request-derived name cannot grow the process without
bound — past the cap the name resolves to
crate::param_intern::OVERFLOW_NAME and the parameter is no longer
retrievable by its own name.
Sourcepub fn set_params(&mut self, params: RouteParams)
pub fn set_params(&mut self, params: RouteParams)
Replace the captured parameters. Called by the router.
Sourcepub fn query_string(&self) -> Option<&str>
pub fn query_string(&self) -> Option<&str>
The raw query string, without the ?.
Sourcepub fn query(&self) -> QueryView<'_>
pub fn query(&self) -> QueryView<'_>
A parsed view of the query string.
Parses on the first call and memoizes; a handler that never calls this
pays nothing. Note the shape change: this used to take a name and return
one value — that accessor is now HttpRequest::query_param.
Sourcepub fn push_query_param(
&mut self,
name: impl AsRef<str>,
value: impl AsRef<str>,
)
pub fn push_query_param( &mut self, name: impl AsRef<str>, value: impl AsRef<str>, )
Append a query parameter to the target, percent-encoding both sides.
The query lives in path now, so this is how a caller adds one without
hand-assembling the target. Any memoized parse is discarded, since the
target it was parsed from no longer describes this request.
Sourcepub fn query_param(&self, name: &str) -> Option<&str>
pub fn query_param(&self, name: &str) -> Option<&str>
The first query value for name.
Source§impl HttpRequest
Extension methods for HttpRequest related to caching.
impl HttpRequest
Extension methods for HttpRequest related to caching.
Sourcepub fn cache_control(&self) -> Option<CacheControl>
pub fn cache_control(&self) -> Option<CacheControl>
Get the Cache-Control header from the request.
Sourcepub fn allows_cached(&self) -> bool
pub fn allows_cached(&self) -> bool
Check if the request allows cached responses.
Sourcepub fn cache_key_with_vary(&self, vary_headers: &[&str]) -> CacheKey
pub fn cache_key_with_vary(&self, vary_headers: &[&str]) -> CacheKey
Generate a cache key with Vary headers.
Trait Implementations§
Source§impl Clone for HttpRequest
impl Clone for HttpRequest
Source§fn clone(&self) -> HttpRequest
fn clone(&self) -> HttpRequest
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more