pub struct Context(/* private fields */);Expand description
The main application context, providing thread-safe access to request and response data.
This is a wrapper around ContextInner that uses an Arc<RwLock<>> to allow
for shared, mutable access across asynchronous tasks.
Implementations§
Source§impl Context
Implementation of methods for Context structure.
impl Context
Implementation of methods for Context structure.
Sourcepub async fn get_aborted(&self) -> bool
pub async fn get_aborted(&self) -> bool
Checks if the context has been marked as aborted.
§Returns
bool- True if the context is aborted, otherwise false.
Sourcepub async fn set_aborted(&self, aborted: bool) -> &Self
pub async fn set_aborted(&self, aborted: bool) -> &Self
Sourcepub async fn cancel_aborted(&self) -> &Self
pub async fn cancel_aborted(&self) -> &Self
Sourcepub async fn get_closed(&self) -> bool
pub async fn get_closed(&self) -> bool
Checks if the connection is marked as closed.
§Returns
bool- True if the connection is closed, otherwise false.
Sourcepub async fn set_closed(&self, closed: bool) -> &Self
pub async fn set_closed(&self, closed: bool) -> &Self
Sourcepub async fn cancel_closed(&self) -> &Self
pub async fn cancel_closed(&self) -> &Self
Sourcepub async fn is_terminated(&self) -> bool
pub async fn is_terminated(&self) -> bool
Checks if the connection has been terminated (aborted and closed).
§Returns
bool- True if the connection is both aborted and closed, otherwise false.
Sourcepub async fn try_get_stream(&self) -> OptionArcRwLockStream
pub async fn try_get_stream(&self) -> OptionArcRwLockStream
Retrieves the underlying network stream, if available.
§Returns
OptionArcRwLockStream- The thread-safe, shareable network stream if it exists.
Sourcepub async fn try_get_socket_addr(&self) -> OptionSocketAddr
pub async fn try_get_socket_addr(&self) -> OptionSocketAddr
Retrieves the remote socket address of the connection.
§Returns
OptionSocketAddr- The socket address of the remote peer if available.
Sourcepub async fn get_socket_addr(&self) -> SocketAddr
pub async fn get_socket_addr(&self) -> SocketAddr
Retrieves the remote socket address or a default value if unavailable.
§Returns
SocketAddr- The socket address of the remote peer, or default if unavailable.
Sourcepub async fn try_get_socket_addr_string(&self) -> OptionString
pub async fn try_get_socket_addr_string(&self) -> OptionString
Retrieves the remote socket address as a string.
§Returns
OptionString- The string representation of the socket address if available.
Sourcepub async fn get_socket_addr_string(&self) -> String
pub async fn get_socket_addr_string(&self) -> String
Retrieves the remote socket address as a string, or a default value if unavailable.
§Returns
String- The string representation of the socket address, or default if unavailable.
Sourcepub async fn try_get_socket_host(&self) -> OptionSocketHost
pub async fn try_get_socket_host(&self) -> OptionSocketHost
Retrieves the IP address part of the remote socket address.
§Returns
OptionSocketHost- The IP address of the remote peer if available.
Sourcepub async fn try_get_socket_port(&self) -> OptionSocketPort
pub async fn try_get_socket_port(&self) -> OptionSocketPort
Retrieves the port number part of the remote socket address.
§Returns
OptionSocketPort- The port number of the remote peer if available.
Sourcepub async fn get_request(&self) -> Request
pub async fn get_request(&self) -> Request
Sourcepub async fn with_request<F, Fut, R>(&self, func: F) -> R
pub async fn with_request<F, Fut, R>(&self, func: F) -> R
Sourcepub async fn get_request_string(&self) -> String
pub async fn get_request_string(&self) -> String
Retrieves the string representation of the current request.
§Returns
String- The full request as a string.
Sourcepub async fn get_request_version(&self) -> RequestVersion
pub async fn get_request_version(&self) -> RequestVersion
Retrieves the HTTP version of the request.
§Returns
RequestVersion- The HTTP version of the request.
Sourcepub async fn get_request_method(&self) -> RequestMethod
pub async fn get_request_method(&self) -> RequestMethod
Sourcepub async fn get_request_host(&self) -> RequestHost
pub async fn get_request_host(&self) -> RequestHost
Retrieves the host from the request headers.
§Returns
RequestHost- The host part of the request’s URI.
Sourcepub async fn get_request_path(&self) -> RequestPath
pub async fn get_request_path(&self) -> RequestPath
Sourcepub async fn get_request_querys(&self) -> RequestQuerys
pub async fn get_request_querys(&self) -> RequestQuerys
Retrieves the query parameters of the request.
§Returns
RequestQuerys- A map containing the query parameters.
Sourcepub async fn try_get_request_query<K>(&self, key: K) -> OptionRequestQuerysValue
pub async fn try_get_request_query<K>(&self, key: K) -> OptionRequestQuerysValue
Sourcepub async fn get_request_body(&self) -> RequestBody
pub async fn get_request_body(&self) -> RequestBody
Sourcepub async fn get_request_body_string(&self) -> String
pub async fn get_request_body_string(&self) -> String
Sourcepub async fn get_request_body_json<J>(&self) -> ResultJsonError<J>where
J: DeserializeOwned,
pub async fn get_request_body_json<J>(&self) -> ResultJsonError<J>where
J: DeserializeOwned,
Deserializes the request body from JSON into a specified type.
§Returns
ResultJsonError<J>- The deserialized typeJor a JSON error.
Sourcepub async fn try_get_request_header<K>(
&self,
key: K,
) -> OptionRequestHeadersValue
pub async fn try_get_request_header<K>( &self, key: K, ) -> OptionRequestHeadersValue
Sourcepub async fn get_request_headers(&self) -> RequestHeaders
pub async fn get_request_headers(&self) -> RequestHeaders
Sourcepub async fn try_get_request_header_front<K>(
&self,
key: K,
) -> OptionRequestHeadersValueItem
pub async fn try_get_request_header_front<K>( &self, key: K, ) -> OptionRequestHeadersValueItem
Sourcepub async fn try_get_request_header_back<K>(
&self,
key: K,
) -> OptionRequestHeadersValueItem
pub async fn try_get_request_header_back<K>( &self, key: K, ) -> OptionRequestHeadersValueItem
Sourcepub async fn get_request_header_len<K>(&self, key: K) -> usize
pub async fn get_request_header_len<K>(&self, key: K) -> usize
Sourcepub async fn get_request_headers_values_length(&self) -> usize
pub async fn get_request_headers_values_length(&self) -> usize
Retrieves the total number of values across all request headers.
§Returns
usize- The total count of all values in all headers.
Sourcepub async fn get_request_headers_length(&self) -> usize
pub async fn get_request_headers_length(&self) -> usize
Retrieves the total number of request headers.
§Returns
usize- The total number of headers in the request.
Sourcepub async fn get_request_has_header<K>(&self, key: K) -> bool
pub async fn get_request_has_header<K>(&self, key: K) -> bool
Sourcepub async fn get_request_has_header_value<K, V>(&self, key: K, value: V) -> bool
pub async fn get_request_has_header_value<K, V>(&self, key: K, value: V) -> bool
Parses and retrieves all cookies from the request headers.
§Returns
Cookies- A map of cookies parsed from the request’s Cookie header.
Sourcepub async fn get_request_upgrade_type(&self) -> UpgradeType
pub async fn get_request_upgrade_type(&self) -> UpgradeType
Retrieves the upgrade type of the request.
§Returns
UpgradeType- Indicates if the request is for a WebSocket connection.
Sourcepub async fn get_request_is_ws(&self) -> bool
pub async fn get_request_is_ws(&self) -> bool
Checks if the request is a WebSocket upgrade request.
§Returns
bool- True if this is a WebSocket upgrade request.
Sourcepub async fn get_request_is_h2c(&self) -> bool
pub async fn get_request_is_h2c(&self) -> bool
Checks if the request is an HTTP/2 cleartext (h2c) upgrade.
§Returns
bool- True if this is an h2c upgrade request.
Sourcepub async fn get_request_is_tls(&self) -> bool
pub async fn get_request_is_tls(&self) -> bool
Sourcepub async fn get_request_is_unknown_upgrade(&self) -> bool
pub async fn get_request_is_unknown_upgrade(&self) -> bool
Checks if the request has an unknown upgrade type.
§Returns
bool- True if the upgrade type is unknown.
Sourcepub async fn get_request_is_http1_1_or_higher(&self) -> bool
pub async fn get_request_is_http1_1_or_higher(&self) -> bool
Checks if the request HTTP version is HTTP/1.1 or higher.
§Returns
bool- True if the version is HTTP/1.1 or higher.
Sourcepub async fn get_request_is_http0_9(&self) -> bool
pub async fn get_request_is_http0_9(&self) -> bool
Sourcepub async fn get_request_is_http1_0(&self) -> bool
pub async fn get_request_is_http1_0(&self) -> bool
Sourcepub async fn get_request_is_http1_1(&self) -> bool
pub async fn get_request_is_http1_1(&self) -> bool
Sourcepub async fn get_request_is_http2(&self) -> bool
pub async fn get_request_is_http2(&self) -> bool
Sourcepub async fn get_request_is_http3(&self) -> bool
pub async fn get_request_is_http3(&self) -> bool
Sourcepub async fn get_request_is_unknown_version(&self) -> bool
pub async fn get_request_is_unknown_version(&self) -> bool
Sourcepub async fn get_request_is_http(&self) -> bool
pub async fn get_request_is_http(&self) -> bool
Checks if the request uses HTTP protocol.
§Returns
bool- True if the version belongs to HTTP family.
Sourcepub async fn get_request_is_get(&self) -> bool
pub async fn get_request_is_get(&self) -> bool
Sourcepub async fn get_request_is_post(&self) -> bool
pub async fn get_request_is_post(&self) -> bool
Sourcepub async fn get_request_is_put(&self) -> bool
pub async fn get_request_is_put(&self) -> bool
Sourcepub async fn get_request_is_delete(&self) -> bool
pub async fn get_request_is_delete(&self) -> bool
Sourcepub async fn get_request_is_patch(&self) -> bool
pub async fn get_request_is_patch(&self) -> bool
Sourcepub async fn get_request_is_head(&self) -> bool
pub async fn get_request_is_head(&self) -> bool
Sourcepub async fn get_request_is_options(&self) -> bool
pub async fn get_request_is_options(&self) -> bool
Sourcepub async fn get_request_is_connect(&self) -> bool
pub async fn get_request_is_connect(&self) -> bool
Sourcepub async fn get_request_is_trace(&self) -> bool
pub async fn get_request_is_trace(&self) -> bool
Sourcepub async fn get_request_is_unknown_method(&self) -> bool
pub async fn get_request_is_unknown_method(&self) -> bool
Sourcepub async fn get_request_is_enable_keep_alive(&self) -> bool
pub async fn get_request_is_enable_keep_alive(&self) -> bool
Checks if the connection should be kept alive based on request headers.
§Returns
bool- True if the Connection header suggests keeping the connection alive, otherwise false.
Sourcepub async fn get_request_is_disable_keep_alive(&self) -> bool
pub async fn get_request_is_disable_keep_alive(&self) -> bool
Checks if keep-alive should be disabled for the request.
§Returns
bool- True if keep-alive should be disabled.
Sourcepub async fn get_response(&self) -> Response
pub async fn get_response(&self) -> Response
Sourcepub async fn set_response<T>(&self, response: T) -> &Self
pub async fn set_response<T>(&self, response: T) -> &Self
Sourcepub async fn with_response<F, Fut, R>(&self, func: F) -> R
pub async fn with_response<F, Fut, R>(&self, func: F) -> R
Sourcepub async fn get_response_string(&self) -> String
pub async fn get_response_string(&self) -> String
Retrieves the string representation of the current response.
§Returns
String- The full response as a string.
Sourcepub async fn get_response_version(&self) -> ResponseVersion
pub async fn get_response_version(&self) -> ResponseVersion
Retrieves the HTTP version of the response.
§Returns
ResponseVersion- The HTTP version of the response.
Sourcepub async fn set_response_version(&self, version: ResponseVersion) -> &Self
pub async fn set_response_version(&self, version: ResponseVersion) -> &Self
Sourcepub async fn get_response_headers(&self) -> ResponseHeaders
pub async fn get_response_headers(&self) -> ResponseHeaders
Sourcepub async fn try_get_response_header<K>(
&self,
key: K,
) -> OptionResponseHeadersValue
pub async fn try_get_response_header<K>( &self, key: K, ) -> OptionResponseHeadersValue
Sourcepub async fn set_response_header<K, V>(&self, key: K, value: V) -> &Self
pub async fn set_response_header<K, V>(&self, key: K, value: V) -> &Self
Sourcepub async fn try_get_response_header_front<K>(
&self,
key: K,
) -> OptionResponseHeadersValueItem
pub async fn try_get_response_header_front<K>( &self, key: K, ) -> OptionResponseHeadersValueItem
Sourcepub async fn try_get_response_header_back<K>(
&self,
key: K,
) -> OptionResponseHeadersValueItem
pub async fn try_get_response_header_back<K>( &self, key: K, ) -> OptionResponseHeadersValueItem
Sourcepub async fn get_response_has_header<K>(&self, key: K) -> bool
pub async fn get_response_has_header<K>(&self, key: K) -> bool
Sourcepub async fn get_response_header_value<K, V>(&self, key: K, value: V) -> bool
pub async fn get_response_header_value<K, V>(&self, key: K, value: V) -> bool
Sourcepub async fn get_response_headers_length(&self) -> usize
pub async fn get_response_headers_length(&self) -> usize
Retrieves the total number of response headers.
§Returns
usize- The total number of headers in the response.
Sourcepub async fn get_response_header_length<K>(&self, key: K) -> usize
pub async fn get_response_header_length<K>(&self, key: K) -> usize
Sourcepub async fn get_response_headers_values_length(&self) -> usize
pub async fn get_response_headers_values_length(&self) -> usize
Retrieves the total number of values across all response headers.
§Returns
usize- The total count of all values in all headers.
Sourcepub async fn add_response_header<K, V>(&self, key: K, value: V) -> &Self
pub async fn add_response_header<K, V>(&self, key: K, value: V) -> &Self
Sourcepub async fn remove_response_header<K>(&self, key: K) -> &Self
pub async fn remove_response_header<K>(&self, key: K) -> &Self
Sourcepub async fn remove_response_header_value<K, V>(
&self,
key: K,
value: V,
) -> &Self
pub async fn remove_response_header_value<K, V>( &self, key: K, value: V, ) -> &Self
Sourcepub async fn clear_response_headers(&self) -> &Self
pub async fn clear_response_headers(&self) -> &Self
Parses and retrieves all cookies from the response headers.
§Returns
Cookies- A map of cookies parsed from the response’s Cookie header.
Sourcepub async fn get_response_body(&self) -> ResponseBody
pub async fn get_response_body(&self) -> ResponseBody
Sourcepub async fn set_response_body<B>(&self, body: B) -> &Self
pub async fn set_response_body<B>(&self, body: B) -> &Self
Sourcepub async fn get_response_body_string(&self) -> String
pub async fn get_response_body_string(&self) -> String
Sourcepub async fn get_response_body_json<J>(&self) -> ResultJsonError<J>where
J: DeserializeOwned,
pub async fn get_response_body_json<J>(&self) -> ResultJsonError<J>where
J: DeserializeOwned,
Deserializes the response body from JSON into a specified type.
§Returns
ResultJsonError<J>- The deserialized typeJor a JSON error.
Sourcepub async fn get_response_reason_phrase(&self) -> ResponseReasonPhrase
pub async fn get_response_reason_phrase(&self) -> ResponseReasonPhrase
Retrieves the reason phrase of the response’s status code.
§Returns
ResponseReasonPhrase- The reason phrase associated with the response’s status code.
Sourcepub async fn set_response_reason_phrase<P>(&self, reason_phrase: P) -> &Self
pub async fn set_response_reason_phrase<P>(&self, reason_phrase: P) -> &Self
Sourcepub async fn get_response_status_code(&self) -> ResponseStatusCode
pub async fn get_response_status_code(&self) -> ResponseStatusCode
Sourcepub async fn set_response_status_code(
&self,
status_code: ResponseStatusCode,
) -> &Self
pub async fn set_response_status_code( &self, status_code: ResponseStatusCode, ) -> &Self
Sourcepub async fn get_route_params(&self) -> RouteParams
pub async fn get_route_params(&self) -> RouteParams
Retrieves the parameters extracted from the route path.
§Returns
RouteParams- A map containing the route parameters.
Sourcepub async fn try_get_route_param<T>(&self, name: T) -> OptionString
pub async fn try_get_route_param<T>(&self, name: T) -> OptionString
Sourcepub async fn get_attributes(&self) -> HashMapArcAnySendSync
pub async fn get_attributes(&self) -> HashMapArcAnySendSync
Retrieves all attributes stored in the context.
§Returns
HashMapArcAnySendSync- A map containing all attributes.
Sourcepub async fn try_get_attribute<K, V>(&self, key: K) -> Option<V>
pub async fn try_get_attribute<K, V>(&self, key: K) -> Option<V>
Sourcepub async fn set_attribute<K, V>(&self, key: K, value: V) -> &Self
pub async fn set_attribute<K, V>(&self, key: K, value: V) -> &Self
Sourcepub async fn remove_attribute<K>(&self, key: K) -> &Self
pub async fn remove_attribute<K>(&self, key: K) -> &Self
Sourcepub async fn clear_attribute(&self) -> &Self
pub async fn clear_attribute(&self) -> &Self
Sourcepub async fn try_get_panic(&self) -> OptionPanic
pub async fn try_get_panic(&self) -> OptionPanic
Retrieves panic information if a panic has occurred during handling.
§Returns
OptionPanic- The panic information if a panic was caught.
Sourcepub async fn try_get_hook<K>(
&self,
key: K,
) -> OptionArcFnContextPinBoxSendSync<()>where
K: ToString,
pub async fn try_get_hook<K>(
&self,
key: K,
) -> OptionArcFnContextPinBoxSendSync<()>where
K: ToString,
Sourcepub async fn send(&self) -> ResponseResult
pub async fn send(&self) -> ResponseResult
Sends the response headers and body to the client.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn send_once(&self) -> ResponseResult
pub async fn send_once(&self) -> ResponseResult
Sends the response and then closes the connection.
After sending, the connection will be marked as closed.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn send_body(&self) -> ResponseResult
pub async fn send_body(&self) -> ResponseResult
Sends only the response body to the client.
This is useful for streaming data or for responses where headers have already been sent.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn send_body_once(&self) -> ResponseResult
pub async fn send_body_once(&self) -> ResponseResult
Sends only the response body and then closes the connection.
After sending the body, the connection will be marked as closed.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn send_with_data<D>(&self, data: D) -> ResponseResult
pub async fn send_with_data<D>(&self, data: D) -> ResponseResult
Sourcepub async fn send_once_with_data<D>(&self, data: D) -> ResponseResult
pub async fn send_once_with_data<D>(&self, data: D) -> ResponseResult
Sourcepub async fn send_body_with_data<D>(&self, data: D) -> ResponseResult
pub async fn send_body_with_data<D>(&self, data: D) -> ResponseResult
Sourcepub async fn send_body_once_with_data<D>(&self, data: D) -> ResponseResult
pub async fn send_body_once_with_data<D>(&self, data: D) -> ResponseResult
Sourcepub async fn send_body_list_with_data<I, D>(
&self,
data_iter: I,
) -> ResponseResult
pub async fn send_body_list_with_data<I, D>( &self, data_iter: I, ) -> ResponseResult
Sends a list of response bodies to the client with additional data.
This is useful for streaming multiple data chunks or for responses where headers have already been sent.
§Arguments
I: IntoIterator<Item = D>, D: AsRef<[u8]>- The additional data to send as a list of bodies.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn send_body_list_once_with_data<I, D>(
&self,
data_iter: I,
) -> ResponseResult
pub async fn send_body_list_once_with_data<I, D>( &self, data_iter: I, ) -> ResponseResult
Sends a list of response bodies and then closes the connection with additional data.
After sending the body list, the connection will be marked as closed.
§Arguments
I: IntoIterator<Item = D>, D: AsRef<[u8]>- The additional data to send as a list of bodies.
§Returns
ResponseResult- The outcome of the send operation.
Sourcepub async fn flush(&self) -> ResponseResult
pub async fn flush(&self) -> ResponseResult
Flushes the underlying network stream, ensuring all buffered data is sent.
§Returns
ResponseResult- The outcome of the flush operation.