Struct Request

Source
pub struct Request { /* private fields */ }
Expand description

Represents a parsed HTTP request.

Implementations§

Source§

impl Request

Source

pub async fn http_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, ) -> RequestReaderHandleResult

Creates a new Request object from a TCP stream.

§Parameters
  • reader: A mut reference to a &mut BufReader<&mut TcpStream>.
  • buffer_size: Request buffer size.
§Returns
  • Ok: A Request object populated with the HTTP request data.
  • Err: An RequestError if the request is invalid or cannot be read.
Source

pub async fn http_request_from_stream( stream: &ArcRwLockStream, buffer_size: usize, ) -> RequestReaderHandleResult

Creates a new Request object from a TCP stream.

§Parameters
  • stream: A reference to a &ArcRwLockStream representing the incoming connection.
  • buffer_size: Request buffer size.
§Returns
  • Ok: A Request object populated with the HTTP request data.
  • Err: An RequestError if the request is invalid or cannot be read.
Source

pub async fn ws_request_from_stream( stream: &ArcRwLockStream, buffer_size: usize, request: &mut Self, ) -> RequestReaderHandleResult

Creates a new Request object from a TCP stream.

§Parameters
  • stream: A reference to a &ArcRwLockStream representing the incoming connection.
  • buffer_size: Request buffer size.
  • request: A reference to a Request object. This object is used as a template.
§Returns
  • Ok: A Request object populated with the HTTP request data.
  • Err: An RequestError if the request is invalid or cannot be read.
Source

pub async fn ws_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, request: &mut Self, ) -> RequestReaderHandleResult

Reads a WebSocket request from a TCP stream and constructs a Request object.

This function reads data from the provided BufReader wrapped around a TcpStream. It attempts to read up to 1024 bytes into a buffer and constructs a Request object based on the received data. The request body is set using the received bytes.

§Arguments
  • reader - A mutable reference to a BufReader wrapping a TcpStream. This reader is used to read the incoming WebSocket request data.
  • buffer_size: - Request buffer size.
  • request - A reference to a Request object. This object is used as a template.
§Returns
  • Ok(Request) - A Request object constructed from the received data.
    • If no data is read (Ok(0)), an empty Request object is returned.
    • If data is successfully read, the request body is set with the received bytes.
  • Err(RequestError::InvalidWebSocketRequest) - If an error occurs while reading from the stream.
Source

pub fn get_query<K>(&self, key: K) -> OptionRequestQuerysValue

Retrieves the value of a query parameter by its key.

§Parameters
  • key: The query parameter’s key, which can be of any type that implements Into<RequestQuerysKey>.
§Returns
  • OptionRequestQuerysValue: Returns Some(value) if the key exists in the query parameters, or None if the key does not exist.
Source

pub fn get_header<K>(&self, key: K) -> OptionRequestHeadersValue

Retrieves the value of a request header by its key.

§Parameters
  • key: The header’s key, which can be of any type that implements Into<RequestHeadersKey>.
§Returns
  • OptionRequestHeadersValue: Returns Some(value) if the key exists in the request headers, or None if the key does not exist.
Source

pub fn get_body_string(&self) -> String

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

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

§Returns

A String containing the body content.

Source

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

Deserializes the body content of the object into a specified type T.

This method first retrieves the body content as a UTF-8 encoded string using self.get_body(). It then attempts to deserialize the string into the specified type T using json_from_slice.

§Type Parameters
  • T: The target type to deserialize into. It must implement the DeserializeOwned trait.
§Returns
  • Ok(T): The deserialized object of type T if the deserialization is successful.
  • Err(ResultJsonError): An error if the deserialization fails (e.g., invalid JSON format or type mismatch).
Source

pub fn get_string(&self) -> String

Converts the request to a formatted string representation.

  • Returns: A String containing formatted request details.
Source

pub fn get_upgrade_type(&self) -> UpgradeType

Retrieves the upgrade type from the request headers.

  • Returns: The UpgradeType extracted from the UPGRADE header. If the header is missing or invalid, returns the default UpgradeType.
Source

pub fn is_ws(&self) -> bool

Checks whether the WebSocket upgrade is enabled.

  • Returns: true if the upgrade type is WebSocket; otherwise, false.
Source

pub fn is_h2c(&self) -> bool

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

  • &self - The current instance (usually a request or context struct).

  • Returns true if the upgrade type is h2c, otherwise false.

Source

pub fn is_tls(&self) -> bool

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

  • &self - The current instance (usually a request or context struct).

  • Returns true if the upgrade type is any Tls variant, otherwise false.

Source

pub fn is_unknown_upgrade(&self) -> bool

Checks whether the upgrade type is unknown.

  • Returns: true if the upgrade type is unknown; otherwise, false.
Source

pub fn is_http1_1_or_higher(&self) -> bool

Checks if the HTTP version is HTTP/1.1 or higher.

  • Returns: true if the HTTP version is 1.1 or higher; otherwise, false.
Source

pub fn is_http0_9(&self) -> bool

Checks whether the HTTP version is HTTP/0.9.

  • Returns: true if the version is HTTP/0.9; otherwise, false.
Source

pub fn is_http1_0(&self) -> bool

Checks whether the HTTP version is HTTP/1.0.

  • Returns: true if the version is HTTP/1.0; otherwise, false.
Source

pub fn is_http1_1(&self) -> bool

Checks whether the HTTP version is HTTP/1.1.

  • Returns: true if the version is HTTP/1.1; otherwise, false.
Source

pub fn is_http2(&self) -> bool

Checks whether the HTTP version is HTTP/2.

  • Returns: true if the version is HTTP/2; otherwise, false.
Source

pub fn is_http3(&self) -> bool

Checks whether the HTTP version is HTTP/3.

  • Returns: true if the version is HTTP/3; otherwise, false.
Source

pub fn is_unknown_version(&self) -> bool

Checks whether the HTTP version is unknown.

  • Returns: true if the version is unknown; otherwise, false.
Source

pub fn is_http(&self) -> bool

Checks whether the version belongs to the HTTP family.

  • Returns: true if the version is a valid HTTP version; otherwise, false.
Source

pub fn is_get(&self) -> bool

Checks whether the request method is GET.

  • Returns: true if the method is GET; otherwise, false.
Source

pub fn is_post(&self) -> bool

Checks whether the request method is POST.

  • Returns: true if the method is POST; otherwise, false.
Source

pub fn is_put(&self) -> bool

Checks whether the request method is PUT.

  • Returns: true if the method is PUT; otherwise, false.
Source

pub fn is_delete(&self) -> bool

Checks whether the request method is DELETE.

  • Returns: true if the method is DELETE; otherwise, false.
Source

pub fn is_patch(&self) -> bool

Checks whether the request method is PATCH.

  • Returns: true if the method is PATCH; otherwise, false.
Source

pub fn is_head(&self) -> bool

Checks whether the request method is HEAD.

  • Returns: true if the method is HEAD; otherwise, false.
Source

pub fn is_options(&self) -> bool

Checks whether the request method is OPTIONS.

  • Returns: true if the method is OPTIONS; otherwise, false.
Source

pub fn is_connect(&self) -> bool

Checks whether the request method is CONNECT.

  • Returns: true if the method is CONNECT; otherwise, false.
Source

pub fn is_trace(&self) -> bool

Checks whether the request method is TRACE.

  • Returns: true if the method is TRACE; otherwise, false.
Source

pub fn is_unknown_method(&self) -> bool

Checks whether the request method is UNKNOWN.

  • Returns: true if the method is UNKNOWN; otherwise, false.
Source

pub fn is_enable_keep_alive(&self) -> bool

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

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

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

impl Request

Source

pub fn get_method(&self) -> &RequestMethod

Source

pub fn get_host(&self) -> &RequestHost

Source

pub fn get_version(&self) -> &RequestVersion

Source

pub fn get_path(&self) -> &RequestPath

Source

pub fn get_querys(&self) -> &RequestQuerys

Source

pub fn get_headers(&self) -> &RequestHeaders

Source

pub fn get_body(&self) -> &RequestBody

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a duplicate of the value. Read more
1.0.0 · Source§

const 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

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

impl Default for Request

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Request

Source§

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

Formats the value using the given formatter. Read more

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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> ErasedDestructor for T
where T: 'static,