pub struct HttpContext {
pub request: HttpRequest,
pub response: HttpResponse,
pub executable: Executable,
pub host: Option<String>,
pub safety: HttpSafety,
pub params: Params,
pub locals: Locals,
/* private fields */
}Expand description
Unified HTTP context for both server and client operations.
This context flows through handlers and middleware, supporting both server-side request handling and client-side response processing.
Fields§
§request: HttpRequest§response: HttpResponse§executable: Executable§host: Option<String>§safety: HttpSafety§params: Params§locals: LocalsImplementations§
Source§impl HttpContext
impl HttpContext
Sourcepub fn new_server(
app: Arc<App>,
endpoint: Arc<Url<HttpContext>>,
request: HttpRequest,
remote_addr: Option<SocketAddr>,
local_addr: Option<SocketAddr>,
) -> HttpContext
pub fn new_server( app: Arc<App>, endpoint: Arc<Url<HttpContext>>, request: HttpRequest, remote_addr: Option<SocketAddr>, local_addr: Option<SocketAddr>, ) -> HttpContext
Creates a new server context with socket addresses.
Sourcepub fn new_client(host: String, safety: HttpSafety) -> HttpContext
pub fn new_client(host: String, safety: HttpSafety) -> HttpContext
Creates a new client context
Sourcepub fn client_ip(&self) -> Option<SocketAddr>
pub fn client_ip(&self) -> Option<SocketAddr>
Returns the client’s socket address (IP and port).
For server context, this is the remote peer that connected.
For client context, this is the server we connected to.
If unknown, this returns None.
Sourcepub fn client_ip_or_default(&self) -> SocketAddr
pub fn client_ip_or_default(&self) -> SocketAddr
Returns the client’s socket address (IP and port), or 0.0.0.0:0 if unknown.
Sourcepub fn client_ip_only(&self) -> Option<IpAddr>
pub fn client_ip_only(&self) -> Option<IpAddr>
Returns just the client’s IP address without the port.
If unknown, this returns None.
Sourcepub fn client_ip_only_or_default(&self) -> IpAddr
pub fn client_ip_only_or_default(&self) -> IpAddr
Returns just the client’s IP address without the port, or 0.0.0.0 if unknown.
Sourcepub fn server_addr(&self) -> Option<SocketAddr>
pub fn server_addr(&self) -> Option<SocketAddr>
Returns the server’s bound socket address.
For server context, this is the local address we’re listening on.
For client context, this is our local ephemeral port.
If unknown, this returns None.
Sourcepub fn server_addr_or_default(&self) -> SocketAddr
pub fn server_addr_or_default(&self) -> SocketAddr
Returns the server’s bound socket address, or 0.0.0.0:0 if unknown.
Sourcepub fn remote_addr(&self) -> Option<SocketAddr>
pub fn remote_addr(&self) -> Option<SocketAddr>
Returns the remote socket address (alias for client_ip in server context).
If unknown, this returns None.
Sourcepub fn remote_addr_or_default(&self) -> SocketAddr
pub fn remote_addr_or_default(&self) -> SocketAddr
Returns the remote socket address, or 0.0.0.0:0 if unknown.
Sourcepub fn local_addr(&self) -> Option<SocketAddr>
pub fn local_addr(&self) -> Option<SocketAddr>
Returns the local socket address (alias for server_addr).
If unknown, this returns None.
Sourcepub fn local_addr_or_default(&self) -> SocketAddr
pub fn local_addr_or_default(&self) -> SocketAddr
Returns the local socket address, or 0.0.0.0:0 if unknown.
pub async fn read_request( app: Arc<App>, reader: &mut BufReader<ReadHalf<TcpConnectionStream>>, ) -> Result<HttpRequest, ConnectionError>
Sourcepub async fn send_response(
response: HttpResponse,
writer: &mut BufWriter<WriteHalf<TcpConnectionStream>>,
)
pub async fn send_response( response: HttpResponse, writer: &mut BufWriter<WriteHalf<TcpConnectionStream>>, )
Sends the response
Sourcepub async fn run(
self,
) -> Result<(HttpResponse, ConnectionStatus), ConnectionError>
pub async fn run( self, ) -> Result<(HttpResponse, ConnectionStatus), ConnectionError>
Runs the endpoint and sending the response.
§Return
Returns the response and a boolean indicating whether the connection should be closed. Response is the response of the endpoint, and the boolean indicates whether the connection should be closed.
Sourcepub fn request_check(
&mut self,
endpoint: &Arc<Url<HttpContext>>,
) -> Result<(), StatusCode>
pub fn request_check( &mut self, endpoint: &Arc<Url<HttpContext>>, ) -> Result<(), StatusCode>
Checks whether the request fulfills the endpoint’s security requirements.
Sourcepub fn endpoint(&self) -> Option<Arc<Url<HttpContext>>>
pub fn endpoint(&self) -> Option<Arc<Url<HttpContext>>>
Returns the endpoint URL if this is a server context
Sourcepub async fn parse_body(&mut self)
pub async fn parse_body(&mut self)
Parses the body of the request, reading it into the HttpBody field of the request.
Note that request body will not be automatically parsed unless this function is called
The automatic parsing is not recommended, as it can lead to performance issues and security vulnerabilities.
If you didn’t parse body, the body will be HttpBody::Unparsed.
Sourcepub async fn form(&mut self) -> Option<&UrlEncodedForm>
pub async fn form(&mut self) -> Option<&UrlEncodedForm>
Returns the body of the request as a reference to HttpBody.
Sourcepub async fn form_or_default(&mut self) -> &UrlEncodedForm
pub async fn form_or_default(&mut self) -> &UrlEncodedForm
Returns the body of the request as a reference to UrlEncodedForm, or an empty form if not present.
Sourcepub async fn files(&mut self) -> Option<&MultiForm>
pub async fn files(&mut self) -> Option<&MultiForm>
Returns the body of the request as a reference to MultiForm.
Sourcepub async fn files_or_default(&mut self) -> &MultiForm
pub async fn files_or_default(&mut self) -> &MultiForm
Returns the body of the request as a reference to MultiForm, or an empty form if not present.
Sourcepub async fn json(&mut self) -> Option<&Value>
pub async fn json(&mut self) -> Option<&Value>
Returns the body of the request as a reference to HttpBody::Binary.
Sourcepub async fn json_or_default(&mut self) -> &Value
pub async fn json_or_default(&mut self) -> &Value
Returns the body of the request as a reference to HttpBody::Binary, or an empty JSON if not present.
Sourcepub fn segment(&mut self, index: usize) -> String
pub fn segment(&mut self, index: usize) -> String
Get a path segment by index position For example, in “/api/users/123”, segment(0) = “api”, segment(1) = “users”, segment(2) = “123”
Sourcepub fn param<A>(&mut self, name: A) -> Option<String>
pub fn param<A>(&mut self, name: A) -> Option<String>
Get a named path parameter from the URL pattern
For example, with pattern “/users/
Sourcepub fn pattern<A>(&mut self, name: A) -> Option<String>
pub fn pattern<A>(&mut self, name: A) -> Option<String>
Alias for param() - kept for backward compatibility
Sourcepub fn query<T>(&mut self, key: T) -> Option<String>
pub fn query<T>(&mut self, key: T) -> Option<String>
Get a query parameter value by key For example, in “/search?q=rust&limit=10”, query(“q”) returns Some(“rust”)
Sourcepub fn get_preferred_language(&mut self) -> Option<String>
pub fn get_preferred_language(&mut self) -> Option<String>
Get the preferred by the user
Sourcepub fn get_preferred_language_or_default<T>(&mut self, default: T) -> String
pub fn get_preferred_language_or_default<T>(&mut self, default: T) -> String
Get the preferred by the user with a default value
Sourcepub fn method(&mut self) -> HttpMethod
pub fn method(&mut self) -> HttpMethod
Returns the method of the request.
Sourcepub fn headers(&self) -> &HashMap<String, HeaderValue>
pub fn headers(&self) -> &HashMap<String, HeaderValue>
Convenience method to get request headers directly. Avoids the long chain: req.request.meta.header
Sourcepub fn header(&self, key: &str) -> Option<&HeaderValue>
pub fn header(&self, key: &str) -> Option<&HeaderValue>
Convenience method to get a specific header value.
Sourcepub fn header_str(&self, key: &str) -> Option<&str>
pub fn header_str(&self, key: &str) -> Option<&str>
Convenience method to get a header value as a string. Returns the first value if multiple values exist.
Sourcepub fn has_header(&self, key: &str) -> bool
pub fn has_header(&self, key: &str) -> bool
Convenience method to check if a header exists.
Get teh full cookie map
Get a single cookie
Get a cookie. If not found a default cookie will be returned
Sourcepub fn response_mut(&mut self) -> &mut HttpResponse
pub fn response_mut(&mut self) -> &mut HttpResponse
Get a mutable reference to the response for chaining.
Sourcepub fn set_status(&mut self, code: u16) -> &mut HttpContext
pub fn set_status(&mut self, code: u16) -> &mut HttpContext
Set the response status code.
Sourcepub fn add_response_header(
&mut self,
key: String,
value: String,
) -> &mut HttpContext
pub fn add_response_header( &mut self, key: String, value: String, ) -> &mut HttpContext
Add a response header.
Sourcepub fn set_body(&mut self, body: HttpBody) -> &mut HttpContext
pub fn set_body(&mut self, body: HttpBody) -> &mut HttpContext
Set the response body.
Source§impl HttpContext
impl HttpContext
pub fn bad_request(&mut self)
Source§impl HttpContext
impl HttpContext
Sourcepub fn new_res(config: HttpSafety, host: impl Into<String>) -> HttpContext
pub fn new_res(config: HttpSafety, host: impl Into<String>) -> HttpContext
Creates a client context for sending requests (backward compatibility)
Sourcepub async fn send_request<T>(
host: T,
request: HttpRequest,
safety_config: HttpSafety,
) -> Result<HttpResponse, ConnectionError>
pub async fn send_request<T>( host: T, request: HttpRequest, safety_config: HttpSafety, ) -> Result<HttpResponse, ConnectionError>
Sends a request to the given host and returns a HttpResCtx context.
This function will automatically determine whether to use HTTP or HTTPS based on the host string.
pub fn request(&mut self, request: HttpRequest)
Sourcepub async fn write_frame(
write_stream: &mut BufWriter<WriteHalf<TcpConnectionStream>>,
request_frame: HttpRequest,
) -> Result<(), ConnectionError>
pub async fn write_frame( write_stream: &mut BufWriter<WriteHalf<TcpConnectionStream>>, request_frame: HttpRequest, ) -> Result<(), ConnectionError>
Write an HTTP request frame to the stream
Sourcepub async fn read_next_frame(
config: &HttpSafety,
read_stream: &mut BufReader<ReadHalf<TcpConnectionStream>>,
) -> Result<HttpResponse, ConnectionError>
pub async fn read_next_frame( config: &HttpSafety, read_stream: &mut BufReader<ReadHalf<TcpConnectionStream>>, ) -> Result<HttpResponse, ConnectionError>
Read an HTTP response frame from the stream