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
impl Request
Sourcepub async fn http_from_reader(
reader: &mut BufReader<&mut TcpStream>,
buffer_size: usize,
) -> RequestReaderHandleResult
pub async fn http_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, ) -> RequestReaderHandleResult
Sourcepub async fn http_request_from_stream(
stream: &ArcRwLockStream,
buffer_size: usize,
) -> RequestReaderHandleResult
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&ArcRwLockStreamrepresenting the incoming connection.buffer_size: Request buffer size
§Returns
Ok: ARequestobject populated with the HTTP request data.Err: AnRequestErrorif the request is invalid or cannot be read.
Sourcepub async fn websocket_request_from_stream(
stream: &ArcRwLockStream,
buffer_size: usize,
) -> RequestReaderHandleResult
pub async fn websocket_request_from_stream( stream: &ArcRwLockStream, buffer_size: usize, ) -> RequestReaderHandleResult
Creates a new Request object from a TCP stream.
§Parameters
stream: A reference to a&ArcRwLockStreamrepresenting the incoming connection.buffer_size: Request buffer size
§Returns
Ok: ARequestobject populated with the HTTP request data.Err: AnRequestErrorif the request is invalid or cannot be read.
Sourcepub async fn websocket_from_reader(
reader: &mut BufReader<&mut TcpStream>,
buffer_size: usize,
) -> RequestReaderHandleResult
pub async fn websocket_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, ) -> 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 aBufReaderwrapping aTcpStream. This reader is used to read the incoming WebSocket request data.buffer_size: - Request buffer size
§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) -> OptionRequestQuerysValuewhere
K: Into<RequestQuerysKey>,
pub fn get_query<K>(&self, key: K) -> OptionRequestQuerysValuewhere
K: Into<RequestQuerysKey>,
Sourcepub fn get_header<K>(&self, key: K) -> OptionRequestHeadersValuewhere
K: Into<RequestHeadersKey>,
pub fn get_header<K>(&self, key: K) -> OptionRequestHeadersValuewhere
K: Into<RequestHeadersKey>,
Sourcepub fn get_body_string(&self) -> String
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.
Sourcepub fn get_body_json<T>(&self) -> ResultSerdeJsonError<T>where
T: DeserializeOwned,
pub fn get_body_json<T>(&self) -> ResultSerdeJsonError<T>where
T: DeserializeOwned,
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 serde_json::from_str.
§Type Parameters
T: The target type to deserialize into. It must implement theDeserializeOwnedtrait.
§Returns
Ok(T): The deserialized object of typeTif the deserialization is successful.Err(serde_json::Error): An error if the deserialization fails (e.g., invalid JSON format or type mismatch).
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.
Sourcepub fn get_string(&self) -> String
pub fn get_string(&self) -> String
Converts the request to a formatted string representation.
- Returns: A
Stringcontaining formatted request details.
Sourcepub fn get_upgrade_type(&self) -> UpgradeType
pub fn get_upgrade_type(&self) -> UpgradeType
Retrieves the upgrade type from the request headers.
- Returns: The
UpgradeTypeextracted from theUPGRADEheader. If the header is missing or invalid, returns the defaultUpgradeType.
Sourcepub fn upgrade_type_is_websocket(&self) -> bool
pub fn upgrade_type_is_websocket(&self) -> bool
Checks whether the WebSocket upgrade is enabled.
- Returns:
trueif the upgrade type is WebSocket; otherwise,false.
Sourcepub fn upgrade_type_is_http(&self) -> bool
pub fn upgrade_type_is_http(&self) -> bool
Checks whether the upgrade type is HTTP.
- Returns:
trueif the upgrade type is HTTP; otherwise,false.
Sourcepub fn upgrade_type_is_unknown(&self) -> bool
pub fn upgrade_type_is_unknown(&self) -> bool
Checks whether the upgrade type is unknown.
- Returns:
trueif the upgrade type is unknown; otherwise,false.
Sourcepub fn version_is_http1_1_or_higher(&self) -> bool
pub fn version_is_http1_1_or_higher(&self) -> bool
Checks if the HTTP version is HTTP/1.1 or higher.
- Returns:
trueif the HTTP version is 1.1 or higher; otherwise,false.
Sourcepub fn version_is_http0_9(&self) -> bool
pub fn version_is_http0_9(&self) -> bool
Checks whether the HTTP version is HTTP/0.9.
- Returns:
trueif the version is HTTP/0.9; otherwise,false.
Sourcepub fn version_is_http1_0(&self) -> bool
pub fn version_is_http1_0(&self) -> bool
Checks whether the HTTP version is HTTP/1.0.
- Returns:
trueif the version is HTTP/1.0; otherwise,false.
Sourcepub fn version_is_http1_1(&self) -> bool
pub fn version_is_http1_1(&self) -> bool
Checks whether the HTTP version is HTTP/1.1.
- Returns:
trueif the version is HTTP/1.1; otherwise,false.
Sourcepub fn version_is_http2(&self) -> bool
pub fn version_is_http2(&self) -> bool
Checks whether the HTTP version is HTTP/2.
- Returns:
trueif the version is HTTP/2; otherwise,false.
Sourcepub fn version_is_http3(&self) -> bool
pub fn version_is_http3(&self) -> bool
Checks whether the HTTP version is HTTP/3.
- Returns:
trueif the version is HTTP/3; otherwise,false.
Sourcepub fn version_is_unknown(&self) -> bool
pub fn version_is_unknown(&self) -> bool
Checks whether the HTTP version is unknown.
- Returns:
trueif the version is unknown; otherwise,false.
Sourcepub fn version_is_http(&self) -> bool
pub fn version_is_http(&self) -> bool
Checks whether the version belongs to the HTTP family.
- Returns:
trueif the version is a valid HTTP version; otherwise,false.
Sourcepub fn method_is_get(&self) -> bool
pub fn method_is_get(&self) -> bool
Checks whether the request method is GET.
- Returns:
trueif the method isGET; otherwise,false.
Sourcepub fn method_is_post(&self) -> bool
pub fn method_is_post(&self) -> bool
Checks whether the request method is POST.
- Returns:
trueif the method isPOST; otherwise,false.
Sourcepub fn method_is_put(&self) -> bool
pub fn method_is_put(&self) -> bool
Checks whether the request method is PUT.
- Returns:
trueif the method isPUT; otherwise,false.
Sourcepub fn method_is_delete(&self) -> bool
pub fn method_is_delete(&self) -> bool
Checks whether the request method is DELETE.
- Returns:
trueif the method isDELETE; otherwise,false.
Sourcepub fn method_is_patch(&self) -> bool
pub fn method_is_patch(&self) -> bool
Checks whether the request method is PATCH.
- Returns:
trueif the method isPATCH; otherwise,false.
Sourcepub fn method_is_head(&self) -> bool
pub fn method_is_head(&self) -> bool
Checks whether the request method is HEAD.
- Returns:
trueif the method isHEAD; otherwise,false.
Sourcepub fn method_is_options(&self) -> bool
pub fn method_is_options(&self) -> bool
Checks whether the request method is OPTIONS.
- Returns:
trueif the method isOPTIONS; otherwise,false.
Sourcepub fn method_is_connect(&self) -> bool
pub fn method_is_connect(&self) -> bool
Checks whether the request method is CONNECT.
- Returns:
trueif the method isCONNECT; otherwise,false.
Sourcepub fn method_is_trace(&self) -> bool
pub fn method_is_trace(&self) -> bool
Checks whether the request method is TRACE.
- Returns:
trueif the method isTRACE; otherwise,false.
Sourcepub fn method_is_unknown(&self) -> bool
pub fn method_is_unknown(&self) -> bool
Checks whether the request method is UNKNOWN.
- Returns:
trueif the method isUNKNOWN; otherwise,false.
Sourcepub fn is_enable_keep_alive(&self) -> bool
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:
- If Connection header exists:
- Returns true if header value is “keep-alive”
- Returns false if header value is “close”
- 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