pub struct Request { /* private fields */ }Expand description
Represents an HTTP request.
§Fields
method: The HTTP method of the request (e.g., GET, POST).host: The host of the request (e.g., example.com).version: The version of the request (e.g., HTTP/1.1).path: The path of the request (e.g., /api/v1/resource).query: The query string of the request (e.g., ?key=value).headers: A collection of HTTP headers as key-value pairs.body: The binary body of the request.upgrade_type: Upgrade type of the request.
Implementations§
Source§impl Request
impl Request
Sourcepub async fn from_reader(
reader: &mut BufReader<&mut TcpStream>,
) -> RequestNewResult
pub async fn from_reader( reader: &mut BufReader<&mut TcpStream>, ) -> RequestNewResult
Sourcepub async fn from_stream(
stream: &ArcRwLockStream,
is_websocket: bool,
) -> RequestNewResult
pub async fn from_stream( stream: &ArcRwLockStream, is_websocket: bool, ) -> RequestNewResult
Sourcepub async fn from_ws_reader(
reader: &mut BufReader<&mut TcpStream>,
) -> RequestNewResult
pub async fn from_ws_reader( reader: &mut BufReader<&mut TcpStream>, ) -> RequestNewResult
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 aBufReaderwrapping aTcpStream. This reader is used to read the incoming WebSocket request data.
§Returns
Ok(Request)- ARequestobject constructed from the received data.- If no data is read (
Ok(0)), an emptyRequestobject is returned. - If data is successfully read, the request body is set with the received bytes.
- If no data is read (
Err(RequestError::InvalidWebSocketRequest)- If an error occurs while reading from the stream.
Sourcepub fn get_query<K>(&self, key: K) -> Option<RequestQuerysValue>where
K: Into<RequestQuerysKey>,
pub fn get_query<K>(&self, key: K) -> Option<RequestQuerysValue>where
K: Into<RequestQuerysKey>,
Sourcepub fn get_header<K>(&self, key: K) -> Option<RequestHeadersValue>where
K: Into<RequestHeadersKey>,
pub fn get_header<K>(&self, key: K) -> Option<RequestHeadersValue>where
K: Into<RequestHeadersKey>,
Sourcepub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
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 aString.value: The value of the header, which will be converted into aString.
§Returns
- Returns a mutable reference to the current instance (
&mut Self), allowing for method chaining.
Sourcepub fn set_body<T: Into<RequestBody>>(&mut self, body: T) -> &mut Self
pub fn set_body<T: Into<RequestBody>>(&mut self, body: T) -> &mut Self
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 aRequestBodyusing theIntotrait.
§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 aRequestBodyusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn set_method<T: Into<RequestMethod>>(&mut self, method: T) -> &mut Self
pub fn set_method<T: Into<RequestMethod>>(&mut self, method: T) -> &mut Self
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 aRequestMethodusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn set_host<T: Into<RequestHost>>(&mut self, host: T) -> &mut Self
pub fn set_host<T: Into<RequestHost>>(&mut self, host: T) -> &mut Self
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 aRequestHostusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn set_path<T: Into<RequestPath>>(&mut self, path: T) -> &mut Self
pub fn set_path<T: Into<RequestPath>>(&mut self, path: T) -> &mut Self
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 aRequestPathusing theIntotrait.
§Return Value
- Returns a mutable reference to the current instance of the struct, enabling method chaining.
Sourcepub fn set_query<K: Into<RequestQuerysKey>, V: Into<RequestQuerysValue>>(
&mut self,
key: K,
value: V,
) -> &mut Self
pub fn set_query<K: Into<RequestQuerysKey>, V: Into<RequestQuerysValue>>( &mut self, key: K, value: V, ) -> &mut Self
Sets a query parameter for the request.
§Parameters
key: The query parameter’s key, which can be of any type that implementsInto<RequestQuerysKey>.value: The query parameter’s value, which can be of any type that implementsInto<RequestQuerysValue>.
§Returns
- Returns a mutable reference to the current instance (
Self), allowing for method chaining.