Skip to main content

Request

Struct Request 

Source
pub struct Request {
    pub method: Method,
    pub host: String,
    pub version: HttpVersion,
    pub path: String,
    pub querys: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>,
    pub headers: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>,
    pub body: Vec<u8>,
}
Expand description

HTTP request representation.

Contains all components of an HTTP request.

The fields stay pub: type/tests/request builds fixtures through them, so this is published API and §17.14 does not recall it (semver).

Fields§

§method: Method

HTTP request method.

§host: String

Request host.

§version: HttpVersion

HTTP protocol version.

§path: String

Request path.

§querys: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

URL query parameters.

§headers: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

HTTP headers collection.

§body: Vec<u8>

Request body content.

Implementations§

Source§

impl Request

Source

pub fn reset(&mut self) -> &mut Request

Resets the request to its default state while retaining allocated capacity.

This keeps the header map, query map, body, and string allocations so persistent (keep-alive) connections avoid repeated allocation per request.

§Returns
  • &mut Self - A mutable reference to self for chaining.
Source

pub fn try_get_query<K>(&self, key: K) -> Option<VecDeque<String>>
where K: AsRef<str>,

Tries to get a query parameter by key.

The key type must implement AsRef conversion. A key that repeats in the query carries every one of its values.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • Option<RequestQuerysValue> - Every value of the parameter if it exists.
Source

pub fn try_get_query_front<K>(&self, key: K) -> Option<String>
where K: AsRef<str>,

Tries to get the first query parameter value by key.

Use this when the parameter is expected to appear once; use try_get_query when every value matters.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • Option<RequestQuerysValueItem> - The first parameter value if it exists.
Source

pub fn try_get_query_back<K>(&self, key: K) -> Option<String>
where K: AsRef<str>,

Tries to get the last query parameter value by key.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • Option<RequestQuerysValueItem> - The last parameter value if it exists.
Source

pub fn try_get_query_size<K>(&self, key: K) -> Option<usize>
where K: AsRef<str>,

Tries to get the number of values of a query parameter.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • Option<usize> - The number of values if the parameter exists.
Source

pub fn has_query<K>(&self, key: K) -> bool
where K: AsRef<str>,

Checks whether a query parameter exists.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • bool - Whether the parameter exists.
Source

pub fn get_query<K>(&self, key: K) -> VecDeque<String>
where K: AsRef<str>,

Gets a query parameter by key.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • RequestQuerysValue - Every value of the parameter.
§Panics

This function will panic if the query parameter key is not found.

Source

pub fn get_query_front<K>(&self, key: K) -> String
where K: AsRef<str>,

Gets the first query parameter value by key.

§Arguments
  • K - The query parameter key, which implements AsRef<str>.
§Returns
  • RequestQuerysValueItem - The first parameter value.
§Panics

This function will panic if the query parameter key is not found.

Source

pub fn get_querys_size(&self) -> usize

Retrieves the number of query parameters.

§Returns
  • usize - The count of distinct query parameter keys.
Source

pub fn try_get_header<K>(&self, key: K) -> Option<VecDeque<String>>
where K: AsRef<str>,

Tries to retrieve the value of a request header by its key.

The key is matched case-insensitively, because the parser stores every header name lowercased and an HTTP header name is case-insensitive by specification. Looking a header up with Content-Type and with content-type therefore find the same value.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • Option<RequestHeadersValue> - The optional header values.
Source

pub fn get_header<K>(&self, key: K) -> VecDeque<String>
where K: AsRef<str>,

Retrieves the value of a request header by its key.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • RequestHeadersValue - The optional header values.
§Panics

This function will panic if the header key is not found.

Source

pub fn try_get_header_front<K>(&self, key: K) -> Option<String>
where K: AsRef<str>,

Tries to retrieve the first value of a request header by its key.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • Option<RequestHeadersValueItem> - The first header value if exists.
Source

pub fn get_header_front<K>(&self, key: K) -> String
where K: AsRef<str>,

Retrieves the first value of a request header by its key.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • RequestHeadersValueItem - The first header value if exists.
§Panics

This function will panic if the header key is not found.

Source

pub fn try_get_header_back<K>(&self, key: K) -> Option<String>
where K: AsRef<str>,

Tries to retrieve the last value of a request header by its key.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • Option<RequestHeadersValueItem> - The last header value if exists.
Source

pub fn get_header_back<K>(&self, key: K) -> String
where K: AsRef<str>,

Retrieves the last value of a request header by its key.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • RequestHeadersValueItem - The last header value if exists.
§Panics

This function will panic if the header key is not found.

Source

pub fn try_get_header_size<K>(&self, key: K) -> Option<usize>
where K: AsRef<str>,

Tries to retrieve the number of values for a specific header.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • Option<usize> - The count of values for the header if exists.
Source

pub fn get_header_size<K>(&self, key: K) -> usize
where K: AsRef<str>,

Retrieves the number of values for a specific header.

§Arguments
  • K - The header’s key, which must implement AsRef<str>.
§Returns
  • usize - The count of values for the header.
§Panics

This function will panic if the header key is not found.

Source

pub fn get_headers_values_size(&self) -> usize

Retrieves the total number of header values across all headers.

§Returns
  • usize - The total count of all header values.
Source

pub fn get_headers_size(&self) -> usize

Retrieves the number of unique headers.

§Returns
  • usize - The count of unique header keys.
Source

pub fn has_header<K>(&self, key: K) -> bool
where K: AsRef<str>,

Checks if a specific header exists.

§Arguments
  • K - The header key to check, which must implement AsRef<str>.
§Returns
  • bool - Whether the header exists.
Source

pub fn has_header_value<K, V>(&self, key: K, value: V) -> bool
where K: AsRef<str>, V: AsRef<str>,

Checks if a header contains a specific value.

§Arguments
  • K - The header key to check, which must implement AsRef<str>.
  • V - The value to search for, which must implement AsRef<str>.
§Returns
  • bool - Whether the header contains the value.
Source

pub fn try_get_cookies( &self, ) -> Option<HashMap<String, String, BuildHasherDefault<Hasher>>>

Tries to parse cookies from the Cookie header.

This method retrieves the Cookie header value and parses it into a collection of key-value pairs representing the cookies.

§Returns
  • Option<Cookies> - The parsed cookies if the header exists, otherwise None.
Source

pub fn get_cookies(&self) -> HashMap<String, String, BuildHasherDefault<Hasher>>

Parses cookies from the Cookie header.

This method retrieves the Cookie header value and parses it into a collection of key-value pairs representing the cookies.

§Returns
  • Cookies - The parsed cookies.
§Panics

This function will panic if the Cookie header is not found.

Tries to get a cookie value by its key.

This method first parses the cookies from the Cookie header, then attempts to retrieve the value for the specified key.

§Arguments
  • K - The cookie key, which implements AsRef<str>.
§Returns
  • Option<CookieValue> - The cookie value if exists.

Gets a cookie value by its key.

This method first parses the cookies from the Cookie header, then retrieves the value for the specified key.

§Arguments
  • K - The cookie key, which implements AsRef<str>.
§Returns
  • CookieValue - The cookie value.
§Panics

This function will panic if the Cookie header is not found or the cookie key does not exist.

Source

pub fn get_upgrade_type(&self) -> UpgradeType

Retrieves the upgrade type from the request headers.

This method looks for the UPGRADE header and attempts to parse its value as_ref an UpgradeType. If the header is missing or the value is invalid, it returns the default UpgradeType.

§Returns
  • UpgradeType - The parsed upgrade type.
Source

pub fn get_body_string(&self) -> String

Retrieves the body content of the request as a UTF-8 encoded string.

This method uses String::from_utf8_lossy to convert the byte slice returned by self.get_body() as a string. If the byte slice contains invalid UTF-8 sequences, they will be replaced with the Unicode replacement character ().

§Returns
  • String - The body content as a string.
Source

pub fn try_get_body_json<T>(&self) -> Result<T, Error>

Deserializes the body content of the request as_ref a specified type T.

This method first retrieves the body content as a byte slice using self.get_body(). It then attempts to deserialize the byte slice as_ref the specified type T using json_from_slice.

The target type T is a type parameter of this method and must implement DeserializeOwned.

§Returns
  • Result<T, serde_json::Error> - The deserialization result.
Source

pub fn get_body_json<T>(&self) -> T

Deserializes the body content of the request as_ref a specified type T.

This method first retrieves the body content as a byte slice using self.get_body(). It then attempts to deserialize the byte slice as_ref the specified type T using json_from_slice.

The target type T is a type parameter of this method and must implement DeserializeOwned.

§Returns
  • T - The deserialized body content.
§Panics

This function will panic if the deserialization fails.

Source

pub fn is_ws_upgrade_type(&self) -> bool

Checks whether the WebSocket upgrade is enabled for this request.

This method determines if the UPGRADE header indicates a WebSocket connection.

§Returns
  • bool - Whether WebSocket upgrade is enabled.
Source

pub fn is_multipart_form_data(&self) -> bool

Checks whether the request declares a multipart/form-data body.

§Returns
  • bool - Whether the request content type is multipart/form-data.
Source

pub fn try_get_content_type(&self) -> Option<String>

Tries to get the request content type with its parameters stripped.

A Content-Type header such as text/html; charset=utf-8 yields the media type alone, so a caller comparing against a media type does not have to know that parameters may follow it.

§Returns
  • Option<RequestContentType> - The media type, or None when the header is absent.
Source

pub fn try_get_content_type_parameter<K>(&self, name: K) -> Option<String>
where K: AsRef<str>,

Tries to get one parameter of the request Content-Type header.

The name is matched case-insensitively and a quoted value is unquoted, so both boundary=abc and boundary="abc" yield abc.

§Arguments
  • K - The parameter name, which requires the AsRef<str> bound.
§Returns
  • Option<RequestContentTypeParameter> - The parameter value, or None when absent.
Source

pub fn try_get_body_form_data(&self) -> Option<Vec<BodyPart>>

Tries to decode a multipart/form-data body into its parts.

The boundary is read from the request Content-Type header rather than guessed, so a body whose content type declares no boundary, a truncated body, and a part without a name all yield None.

§Returns
  • Option<MultipartFormDataParts> - The decoded parts, or None when unavailable.
Source

pub fn get_body_form( &self, ) -> HashMap<String, String, BuildHasherDefault<Hasher>>

Decodes an application/x-www-form-urlencoded body into its fields.

§Returns
  • BodyForm - The decoded form fields.
Source

pub fn try_get_body_form_data_text<K>(&self, name: K) -> Option<String>
where K: AsRef<str>,

Tries to get a plain text field of a multipart/form-data body.

A part that uploads a file is skipped, so a file field never silently decodes as text.

§Arguments
  • K - The form field name, which requires the AsRef<str> bound.
§Returns
  • Option<BodyFormValue> - The field value, or None when absent or a file.
Source

pub fn is_h2c_upgrade_type(&self) -> bool

Checks if the current upgrade type is HTTP/2 cleartext (h2c).

§Returns
  • bool - Whether the upgrade type is h2c.
Source

pub fn is_tls_upgrade_type(&self) -> bool

Checks if the current upgrade type is TLS (any version).

§Returns
  • bool - Whether the upgrade type is TLS.
Source

pub fn is_unknown_upgrade_type(&self) -> bool

Checks whether the upgrade type is unknown.

§Returns
  • bool - Whether the upgrade type is unknown.
Source

pub fn is_enable_keep_alive(&self) -> bool

Determines if a keep-alive connection should be enabled for this request.

This function checks the Connection header and the HTTP version to determine if keep-alive should be enabled. The logic is as follows:

  1. If the Connection header exists:
    • Returns true if the header value is “keep-alive” (case-insensitive).
    • Returns false if the header value is “close” (case-insensitive).
  2. If no Connection header is present:
    • Returns true if the HTTP version is 1.1 or higher.
    • Returns false otherwise.
§Returns
  • bool - Whether keep-alive should be enabled.
Source

pub fn is_disable_keep_alive(&self) -> bool

Determines if keep-alive should be disabled for this request.

§Returns
  • bool - Whether keep-alive should be disabled.
Source§

impl Request

Source

pub fn get_method(&self) -> &Method

Source

pub fn get_mut_method(&mut self) -> &mut Method

Source

pub fn set_method(&mut self, val: Method) -> &mut Request

Source

pub fn get_host(&self) -> &String

Source

pub fn get_mut_host(&mut self) -> &mut String

Source

pub fn set_host(&mut self, val: String) -> &mut Request

Source

pub fn get_version(&self) -> &HttpVersion

Source

pub fn get_mut_version(&mut self) -> &mut HttpVersion

Source

pub fn set_version(&mut self, val: HttpVersion) -> &mut Request

Source

pub fn get_path(&self) -> &String

Source

pub fn get_mut_path(&mut self) -> &mut String

Source

pub fn set_path(&mut self, val: String) -> &mut Request

Source

pub fn get_querys( &self, ) -> &HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

Source

pub fn get_mut_querys( &mut self, ) -> &mut HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

Source

pub fn set_querys( &mut self, val: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>, ) -> &mut Request

Source

pub fn get_headers( &self, ) -> &HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

Source

pub fn get_mut_headers( &mut self, ) -> &mut HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>

Source

pub fn set_headers( &mut self, val: HashMap<String, VecDeque<String>, BuildHasherDefault<Hasher>>, ) -> &mut Request

Source

pub fn get_body(&self) -> &Vec<u8> ⓘ

Source

pub fn get_mut_body(&mut self) -> &mut Vec<u8> ⓘ

Source

pub fn set_body(&mut self, val: Vec<u8>) -> &mut Request

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Request

Provides a default value for Request.

Returns a new Request instance with all fields initialized to their default values.

Source§

fn default() -> Request

Builds the default Request with all fields set to their defaults.

Source§

impl<'de> Deserialize<'de> for Request

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Request, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Request

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the standard debug format.

§Arguments
  • &mut std::fmt::Formatter<'_> - The formatter to write the representation into.
§Returns
  • std::fmt::Result - Success or failure of the formatting operation.
Source§

impl Eq for Request

Source§

impl PartialEq for Request

Source§

fn eq(&self, other: &Request) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Request

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Request

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnySend for T
where T: Any + Send,

Source§

impl<T> AnySendClone for T
where T: Any + Send + Clone,

Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> AnySync for T
where T: Any + Sync,

Source§

impl<T> AnySyncClone for T
where T: Any + Sync + Clone,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more