pub struct Request { /* private fields */ }
Expand description
HTTP request representation.
Contains all components of an HTTP 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
Parses an HTTP request from a buffered TCP stream reader.
Reads request line, headers and body from the stream and constructs a Request object.
§Arguments
&mut BufReader<&mut TcpStream>
- The buffered TCP stream reader.usize
- The buffer size for reading.
§Returns
Result<Request, RequestError>
- The parsed request or an error.
Sourcepub async fn http_from_stream(
stream: &ArcRwLockStream,
buffer_size: usize,
) -> RequestReaderHandleResult
pub async fn http_from_stream( stream: &ArcRwLockStream, buffer_size: usize, ) -> RequestReaderHandleResult
Sourcepub async fn ws_from_stream(
stream: &ArcRwLockStream,
buffer_size: usize,
request: &mut Self,
) -> RequestReaderHandleResult
pub async fn ws_from_stream( stream: &ArcRwLockStream, buffer_size: usize, request: &mut Self, ) -> RequestReaderHandleResult
Parses a WebSocket request from a TCP stream.
Wraps the stream in a buffered reader and delegates to ws_from_reader
.
§Arguments
&ArcRwLock<TcpStream>
- The TCP stream to read from.usize
- The buffer size for reading.&mut Request
- The request template to populate.
§Returns
Result<Request, RequestError>
- The parsed WebSocket request or an error.
Sourcepub async fn ws_from_reader(
reader: &mut BufReader<&mut TcpStream>,
buffer_size: usize,
request: &mut Self,
) -> RequestReaderHandleResult
pub async fn ws_from_reader( reader: &mut BufReader<&mut TcpStream>, buffer_size: usize, request: &mut Self, ) -> RequestReaderHandleResult
Parses a WebSocket request from a buffered TCP stream.
Handles WebSocket frames including text, binary, ping, pong and close frames. Assembles the request body from frame payload data.
§Arguments
&mut BufReader<&mut TcpStream>
- The buffered TCP stream reader.usize
- The buffer size for reading frames.&mut Request
- The request template to populate.
§Returns
Result<Request, RequestError>
- The parsed WebSocket request or an error.
Sourcepub fn try_get_query<K>(&self, key: K) -> OptionRequestQuerysValuewhere
K: Into<RequestQuerysKey>,
pub fn try_get_query<K>(&self, key: K) -> OptionRequestQuerysValuewhere
K: Into<RequestQuerysKey>,
Sourcepub fn try_get_header<K>(&self, key: K) -> OptionRequestHeadersValuewhere
K: Into<RequestHeadersKey>,
pub fn try_get_header<K>(&self, key: K) -> OptionRequestHeadersValuewhere
K: Into<RequestHeadersKey>,
Sourcepub fn try_get_header_front<K>(&self, key: K) -> OptionRequestHeadersValueItemwhere
K: Into<RequestHeadersKey>,
pub fn try_get_header_front<K>(&self, key: K) -> OptionRequestHeadersValueItemwhere
K: Into<RequestHeadersKey>,
Sourcepub fn try_get_header_back<K>(&self, key: K) -> OptionRequestHeadersValueItemwhere
K: Into<RequestHeadersKey>,
pub fn try_get_header_back<K>(&self, key: K) -> OptionRequestHeadersValueItemwhere
K: Into<RequestHeadersKey>,
Sourcepub fn get_header_length<K>(&self, key: K) -> usizewhere
K: Into<RequestHeadersKey>,
pub fn get_header_length<K>(&self, key: K) -> usizewhere
K: Into<RequestHeadersKey>,
Sourcepub fn get_headers_values_length(&self) -> usize
pub fn get_headers_values_length(&self) -> usize
Retrieves the total number of header values across all headers.
§Returns
usize
- The total count of all header values.
Sourcepub fn get_headers_length(&self) -> usize
pub fn get_headers_length(&self) -> usize
Sourcepub fn has_header<K>(&self, key: K) -> boolwhere
K: Into<RequestHeadersKey>,
pub fn has_header<K>(&self, key: K) -> boolwhere
K: Into<RequestHeadersKey>,
Sourcepub fn has_header_value<K, V>(&self, key: K, value: V) -> bool
pub fn has_header_value<K, V>(&self, key: K, value: V) -> bool
Sourcepub fn get_body_string(&self) -> String
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()
into 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.
Sourcepub fn get_body_json<T>(&self) -> ResultJsonError<T>where
T: DeserializeOwned,
pub fn get_body_json<T>(&self) -> ResultJsonError<T>where
T: DeserializeOwned,
Deserializes the body content of the request into 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 into the specified type T
using json_from_slice
.
§Type Parameters
T
- The target type to deserialize into (must implement DeserializeOwned).
§Returns
ResultJsonError<T>
- The deserialization result.
Sourcepub fn get_string(&self) -> String
pub fn get_string(&self) -> String
Converts the request to a formatted string representation.
This method provides a human-readable summary of the request, including its method, host, version, path, query parameters, headers, and body information.
§Returns
String
- The 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.
This method looks for the UPGRADE
header and attempts to parse its value
into an UpgradeType
. If the header is missing or the value is invalid,
it returns the default UpgradeType
.
§Returns
UpgradeType
- The parsed upgrade type.
Sourcepub fn is_ws(&self) -> bool
pub fn is_ws(&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.
Sourcepub fn is_h2c(&self) -> bool
pub fn is_h2c(&self) -> bool
Checks if the current upgrade type is HTTP/2 cleartext (h2c).
§Returns
bool
- Whether the upgrade type is h2c.
Sourcepub fn is_tls(&self) -> bool
pub fn is_tls(&self) -> bool
Checks if the current upgrade type is TLS (any version).
§Returns
bool
- Whether the upgrade type is TLS.
Sourcepub fn is_unknown_upgrade(&self) -> bool
pub fn is_unknown_upgrade(&self) -> bool
Sourcepub fn is_http1_1_or_higher(&self) -> bool
pub fn is_http1_1_or_higher(&self) -> bool
Checks if the HTTP version is HTTP/1.1 or higher.
§Returns
bool
- Whether the version is HTTP/1.1 or higher.
Sourcepub fn is_http0_9(&self) -> bool
pub fn is_http0_9(&self) -> bool
Sourcepub fn is_http1_0(&self) -> bool
pub fn is_http1_0(&self) -> bool
Sourcepub fn is_http1_1(&self) -> bool
pub fn is_http1_1(&self) -> bool
Sourcepub fn is_unknown_version(&self) -> bool
pub fn is_unknown_version(&self) -> bool
Sourcepub fn is_options(&self) -> bool
pub fn is_options(&self) -> bool
Sourcepub fn is_connect(&self) -> bool
pub fn is_connect(&self) -> bool
Sourcepub fn is_unknown_method(&self) -> bool
pub fn is_unknown_method(&self) -> bool
Sourcepub fn is_enable_keep_alive(&self) -> bool
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:
- 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).
- Returns
- If no
Connection
header is present:- Returns
true
if the HTTP version is 1.1 or higher. - Returns
false
otherwise.
- Returns
§Returns
bool
- Whether keep-alive should be enabled.
Sourcepub fn is_disable_keep_alive(&self) -> bool
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
impl Request
pub fn get_method(&self) -> &RequestMethod
pub fn get_host(&self) -> &RequestHost
pub fn get_version(&self) -> &RequestVersion
pub fn get_path(&self) -> &RequestPath
pub fn get_querys(&self) -> &RequestQuerys
pub fn get_headers(&self) -> &RequestHeaders
pub fn get_body(&self) -> &RequestBody
Trait Implementations§
Source§impl Default for Request
Provides a default value for Request
.
impl Default for Request
Provides a default value for Request
.
Returns a new Request
instance with all fields initialized to their default values.