Struct Request

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

Represents an HTTP request.

§Fields

  • method: The HTTP method of the request.
  • host: The host of the request.
  • version: The version of the request.
  • path: The path of the request.
  • querys: The query string of the request.
  • headers: A collection of HTTP headers as key-value pairs.
  • body: The binary body of the request.

Implementations§

Source§

impl Request

Source

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

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, ) -> Result<Request, Error>

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 websocket_request_from_stream( stream: &ArcRwLockStream, buffer_size: usize, ) -> Result<Request, Error>

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 websocket_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, ) -> Result<Request, Error>

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
§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) -> Option<String>
where K: Into<String>,

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) -> Option<String>
where K: Into<String>,

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 set_header<K, V>(&mut self, key: K, value: V) -> &mut Request
where K: Into<String>, V: Into<String>,

Adds a header to the request.

This function inserts a key-value pair into the request headers. The key and value are converted into String, allowing for efficient handling of both owned and borrowed string data.

§Parameters
  • key: The header key, which will be converted into a String.
  • value: The value of the header, which will be converted into a String.
§Returns
  • Returns a mutable reference to the current instance (&mut Self), allowing for method chaining.
Source

pub fn set_body<T>(&mut self, body: T) -> &mut Request
where T: Into<Vec<u8>>,

Set the body of the response.

This method allows you to set the body of the response by converting the provided value into a RequestBody type. The body is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • body: The body of the response to be set. It can be any type that can be converted into a RequestBody using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining. Set the body of the response.

This method allows you to set the body of the response by converting the provided value into a RequestBody type. The body is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • body: The body of the response to be set. It can be any type that can be converted into a RequestBody using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub fn set_method<T>(&mut self, method: T) -> &mut Request
where T: Into<Methods>,

Set the HTTP method of the request.

This method allows you to set the HTTP method (e.g., GET, POST) of the request by converting the provided value into a RequestMethod type. The method is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • method: The HTTP method to be set for the request. It can be any type that can be converted into a RequestMethod using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub fn set_host<T>(&mut self, host: T) -> &mut Request
where T: Into<String>,

Set the host of the request.

This method allows you to set the host (e.g., www.example.com) for the request by converting the provided value into a RequestHost type. The host is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • host: The host to be set for the request. It can be any type that can be converted into a RequestHost using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub fn set_path<T>(&mut self, path: T) -> &mut Request
where T: Into<String>,

Set the path of the request.

This method allows you to set the path (e.g., /api/v1/resource) for the request by converting the provided value into a RequestPath type. The path is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • path: The path to be set for the request. It can be any type that can be converted into a RequestPath using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub fn set_query<K, V>(&mut self, key: K, value: V) -> &mut Request
where K: Into<String>, V: Into<String>,

Sets a query parameter for the request.

§Parameters
  • key: The query parameter’s key, which can be of any type that implements Into<RequestQuerysKey>.
  • value: The query parameter’s value, which can be of any type that implements Into<RequestQuerysValue>.
§Returns
  • Returns a mutable reference to the current instance (Self), allowing for method chaining.
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_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

pub fn is_disable_keep_alive(&self) -> bool

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

This is the inverse of is_enable_keep_alive(). It returns true when keep-alive should be disabled, and false when it should be enabled.

§Returns
  • bool: true if keep-alive should be disabled, false otherwise
Source§

impl Request

Source

pub fn get_method(&self) -> &Methods

Source

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

Source

pub fn get_host(&self) -> &String

Source

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

Source

pub fn get_version(&self) -> &HttpVersion

Source

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

Source

pub fn get_path(&self) -> &String

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a copy of the value. Read more
1.0.0 · 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

Source§

fn default() -> Request

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

impl Display for Request

Source§

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

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,

Source§

impl<T> MaybeSendSync for T