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 http_from_stream(
&self,
request_config: RequestConfig,
) -> Result<Request, RequestError>
pub async fn http_from_stream( &self, request_config: RequestConfig, ) -> Result<Request, RequestError>
Sourcepub async fn ws_from_stream(
&self,
request_config: RequestConfig,
) -> Result<Request, RequestError>
pub async fn ws_from_stream( &self, request_config: RequestConfig, ) -> Result<Request, RequestError>
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 or closed).
§Returns
bool- True if the connection is either aborted or closed, otherwise false.
Sourcepub async fn is_keep_alive(&self, keep_alive: bool) -> bool
pub async fn is_keep_alive(&self, keep_alive: bool) -> bool
Checks if the connection should be kept alive.
This method evaluates whether the connection should remain open based on the closed state and the keep_alive parameter.
§Arguments
bool- Whether keep-alive is enabled for the request.
§Returns
bool- True if the connection should be kept alive, otherwise false.
Sourcepub async fn try_get_stream(&self) -> Option<ArcRwLockStream>
pub async fn try_get_stream(&self) -> Option<ArcRwLockStream>
Retrieves the underlying network stream, if available.
§Returns
Option<ArcRwLockStream>- The thread-safe, shareable network stream if it exists.
Sourcepub async fn get_stream(&self) -> ArcRwLockStream
pub async fn get_stream(&self) -> ArcRwLockStream
Sourcepub async fn try_get_socket_addr(&self) -> Option<SocketAddr>
pub async fn try_get_socket_addr(&self) -> Option<SocketAddr>
Retrieves the remote socket address of the connection.
§Returns
Option<SocketAddr>- 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
Sourcepub async fn try_get_socket_addr_string(&self) -> Option<String>
pub async fn try_get_socket_addr_string(&self) -> Option<String>
Retrieves the remote socket address as a string.
§Returns
Option<String>- 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
Sourcepub async fn try_get_socket_host(&self) -> Option<SocketHost>
pub async fn try_get_socket_host(&self) -> Option<SocketHost>
Retrieves the IP address part of the remote socket address.
§Returns
Option<SocketHost>- The IP address of the remote peer.
Sourcepub async fn get_socket_host(&self) -> SocketHost
pub async fn get_socket_host(&self) -> SocketHost
Sourcepub async fn try_get_socket_port(&self) -> Option<SocketPort>
pub async fn try_get_socket_port(&self) -> Option<SocketPort>
Retrieves the port number part of the remote socket address.
§Returns
Option<SocketPort>- The port number of the remote peer if available.
Sourcepub async fn get_socket_port(&self) -> SocketPort
pub async fn get_socket_port(&self) -> SocketPort
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,
) -> Option<RequestQuerysValue>
pub async fn try_get_request_query<K>( &self, key: K, ) -> Option<RequestQuerysValue>
Sourcepub async fn get_request_query<K>(&self, key: K) -> RequestQuerysValue
pub async fn get_request_query<K>(&self, key: K) -> RequestQuerysValue
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 try_get_request_body_json<J>(&self) -> Result<J, Error>where
J: DeserializeOwned,
pub async fn try_get_request_body_json<J>(&self) -> Result<J, Error>where
J: DeserializeOwned,
Deserializes the request body from JSON into a specified type.
§Returns
Result<J, serde_json::Error>- The deserialized typeJor a JSON error.
Sourcepub async fn get_request_body_json<J>(&self) -> Jwhere
J: DeserializeOwned,
pub async fn get_request_body_json<J>(&self) -> Jwhere
J: DeserializeOwned,
Sourcepub async fn get_request_headers(&self) -> RequestHeaders
pub async fn get_request_headers(&self) -> RequestHeaders
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 try_get_request_header<K>(
&self,
key: K,
) -> Option<RequestHeadersValue>
pub async fn try_get_request_header<K>( &self, key: K, ) -> Option<RequestHeadersValue>
Sourcepub async fn get_request_header<K>(&self, key: K) -> RequestHeadersValue
pub async fn get_request_header<K>(&self, key: K) -> RequestHeadersValue
Sourcepub async fn try_get_request_header_front<K>(
&self,
key: K,
) -> Option<RequestHeadersValueItem>
pub async fn try_get_request_header_front<K>( &self, key: K, ) -> Option<RequestHeadersValueItem>
Sourcepub async fn get_request_header_front<K>(
&self,
key: K,
) -> RequestHeadersValueItem
pub async fn get_request_header_front<K>( &self, key: K, ) -> RequestHeadersValueItem
Sourcepub async fn try_get_request_header_back<K>(
&self,
key: K,
) -> Option<RequestHeadersValueItem>
pub async fn try_get_request_header_back<K>( &self, key: K, ) -> Option<RequestHeadersValueItem>
Sourcepub async fn get_request_header_back<K>(
&self,
key: K,
) -> RequestHeadersValueItem
pub async fn get_request_header_back<K>( &self, key: K, ) -> RequestHeadersValueItem
Sourcepub async fn try_get_request_header_len<K>(&self, key: K) -> Option<usize>
pub async fn try_get_request_header_len<K>(&self, key: K) -> Option<usize>
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_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
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,
) -> Option<ResponseHeadersValue>
pub async fn try_get_response_header<K>( &self, key: K, ) -> Option<ResponseHeadersValue>
Sourcepub async fn get_response_header<K>(&self, key: K) -> ResponseHeadersValue
pub async fn get_response_header<K>(&self, key: K) -> ResponseHeadersValue
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,
) -> Option<ResponseHeadersValueItem>
pub async fn try_get_response_header_front<K>( &self, key: K, ) -> Option<ResponseHeadersValueItem>
Sourcepub async fn get_response_header_front<K>(
&self,
key: K,
) -> ResponseHeadersValueItem
pub async fn get_response_header_front<K>( &self, key: K, ) -> ResponseHeadersValueItem
Sourcepub async fn try_get_response_header_back<K>(
&self,
key: K,
) -> Option<ResponseHeadersValueItem>
pub async fn try_get_response_header_back<K>( &self, key: K, ) -> Option<ResponseHeadersValueItem>
Sourcepub async fn get_response_header_back<K>(
&self,
key: K,
) -> ResponseHeadersValueItem
pub async fn get_response_header_back<K>( &self, key: K, ) -> ResponseHeadersValueItem
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 try_get_response_header_length<K>(&self, key: K) -> Option<usize>
pub async fn try_get_response_header_length<K>(&self, key: K) -> Option<usize>
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 try_get_response_body_json<J>(&self) -> Result<J, Error>where
J: DeserializeOwned,
pub async fn try_get_response_body_json<J>(&self) -> Result<J, Error>where
J: DeserializeOwned,
Deserializes the response body from JSON into a specified type.
§Returns
Result<J, serde_json::Error>- The deserialized typeJor a JSON error.
Sourcepub async fn get_response_body_json<J>(&self) -> Jwhere
J: DeserializeOwned,
pub async fn get_response_body_json<J>(&self) -> Jwhere
J: DeserializeOwned,
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 status code.
§Returns
ResponseReasonPhrase- The reason phrase associated with the response 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
Retrieves the status code of the response.
§Returns
ResponseStatusCode- The status code of the response.
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) -> Option<String>
pub async fn try_get_route_param<T>(&self, name: T) -> Option<String>
Sourcepub async fn get_route_param<T>(&self, name: T) -> String
pub async fn get_route_param<T>(&self, name: T) -> String
Sourcepub async fn get_attributes(&self) -> ThreadSafeAttributeStore
pub async fn get_attributes(&self) -> ThreadSafeAttributeStore
Retrieves all attributes stored in the context.
§Returns
ThreadSafeAttributeStore- 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 get_attribute<K, V>(&self, key: K) -> V
pub async fn get_attribute<K, V>(&self, key: K) -> 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) -> Option<Panic>
pub async fn try_get_panic(&self) -> Option<Panic>
Retrieves panic information if a panic has occurred during handling.
§Returns
Option<Panic>- The panic information if a panic was caught.
Sourcepub async fn try_get_hook<K>(&self, key: K) -> Option<HookHandler<()>>where
K: ToString,
pub async fn try_get_hook<K>(&self, key: K) -> Option<HookHandler<()>>where
K: ToString,
Sourcepub async fn get_hook<K>(&self, key: K) -> HookHandler<()>where
K: ToString,
pub async fn get_hook<K>(&self, key: K) -> HookHandler<()>where
K: ToString,
Sourcepub async fn try_send(&self) -> Result<(), ResponseError>
pub async fn try_send(&self) -> Result<(), ResponseError>
Sends HTTP response data over the stream.
§Returns
Result<(), ResponseError>- Result indicating success or failure.
Sourcepub async fn try_send_body(&self) -> Result<(), ResponseError>
pub async fn try_send_body(&self) -> Result<(), ResponseError>
Sends HTTP response body.
§Returns
Result<(), ResponseError>- Result indicating success or failure.
Sourcepub async fn try_send_body_with_data<D>(
&self,
data: D,
) -> Result<(), ResponseError>
pub async fn try_send_body_with_data<D>( &self, data: D, ) -> Result<(), ResponseError>
Sourcepub async fn send_body_with_data<D>(&self, data: D)
pub async fn send_body_with_data<D>(&self, data: D)
Sourcepub async fn try_send_body_list<I, D>(
&self,
data_iter: I,
) -> Result<(), ResponseError>
pub async fn try_send_body_list<I, D>( &self, data_iter: I, ) -> Result<(), ResponseError>
Sourcepub async fn send_body_list<I, D>(&self, data_iter: I)
pub async fn send_body_list<I, D>(&self, data_iter: I)
Sourcepub async fn try_send_body_list_with_data<I, D>(
&self,
data_iter: I,
) -> Result<(), ResponseError>
pub async fn try_send_body_list_with_data<I, D>( &self, data_iter: I, ) -> Result<(), ResponseError>
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
Result<(), ResponseError>- The result of the send operation.