HttpContext

Struct HttpContext 

Source
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: Locals

Implementations§

Source§

impl HttpContext

Source

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.

Source

pub fn new_client(host: String, safety: HttpSafety) -> HttpContext

Creates a new client context

Source

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.

Source

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.

Source

pub fn client_ip_only(&self) -> Option<IpAddr>

Returns just the client’s IP address without the port. If unknown, this returns None.

Source

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.

Source

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.

Source

pub fn server_addr_or_default(&self) -> SocketAddr

Returns the server’s bound socket address, or 0.0.0.0:0 if unknown.

Source

pub fn remote_addr(&self) -> Option<SocketAddr>

Returns the remote socket address (alias for client_ip in server context). If unknown, this returns None.

Source

pub fn remote_addr_or_default(&self) -> SocketAddr

Returns the remote socket address, or 0.0.0.0:0 if unknown.

Source

pub fn local_addr(&self) -> Option<SocketAddr>

Returns the local socket address (alias for server_addr). If unknown, this returns None.

Source

pub fn local_addr_or_default(&self) -> SocketAddr

Returns the local socket address, or 0.0.0.0:0 if unknown.

Source

pub async fn read_request( app: Arc<App>, reader: &mut BufReader<ReadHalf<TcpConnectionStream>>, ) -> Result<HttpRequest, ConnectionError>

Source

pub async fn send_response( response: HttpResponse, writer: &mut BufWriter<WriteHalf<TcpConnectionStream>>, )

Sends the response

Source

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.

Source

pub fn request_check( &mut self, endpoint: &Arc<Url<HttpContext>>, ) -> Result<(), StatusCode>

Checks whether the request fulfills the endpoint’s security requirements.

Source

pub fn meta(&mut self) -> &mut HttpMeta

Returns the meta in the request as reference

Source

pub fn app(&self) -> Option<Arc<App>>

Returns the Arc if this is a server context

Source

pub fn endpoint(&self) -> Option<Arc<Url<HttpContext>>>

Returns the endpoint URL if this is a server context

Source

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.

Source

pub async fn form(&mut self) -> Option<&UrlEncodedForm>

Returns the body of the request as a reference to HttpBody.

Source

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.

Source

pub async fn files(&mut self) -> Option<&MultiForm>

Returns the body of the request as a reference to MultiForm.

Source

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.

Source

pub async fn json(&mut self) -> Option<&Value>

Returns the body of the request as a reference to HttpBody::Binary.

Source

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.

Source

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”

Source

pub fn path(&self) -> String

Get the whole path

Source

pub fn param<A>(&mut self, name: A) -> Option<String>
where A: AsRef<str>,

Get a named path parameter from the URL pattern For example, with pattern “/users/”, param(“id”) returns the value in place of

Source

pub fn pattern<A>(&mut self, name: A) -> Option<String>
where A: AsRef<str>,

Alias for param() - kept for backward compatibility

Source

pub fn query<T>(&mut self, key: T) -> Option<String>
where T: Into<String>,

Get a query parameter value by key For example, in “/search?q=rust&limit=10”, query(“q”) returns Some(“rust”)

Source

pub fn get_preferred_language(&mut self) -> Option<String>

Get the preferred by the user

Source

pub fn get_preferred_language_or_default<T>(&mut self, default: T) -> String
where T: AsRef<str>,

Get the preferred by the user with a default value

Source

pub fn method(&mut self) -> HttpMethod

Returns the method of the request.

Source

pub fn headers(&self) -> &HashMap<String, HeaderValue>

Convenience method to get request headers directly. Avoids the long chain: req.request.meta.header

Source

pub fn header(&self, key: &str) -> Option<&HeaderValue>

Convenience method to get a specific header value.

Source

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.

Source

pub fn has_header(&self, key: &str) -> bool

Convenience method to check if a header exists.

Source

pub fn get_cookies(&mut self) -> &CookieMap

Get teh full cookie map

Get a single cookie

Get a cookie. If not found a default cookie will be returned

Source

pub fn response_mut(&mut self) -> &mut HttpResponse

Get a mutable reference to the response for chaining.

Source

pub fn set_status(&mut self, code: u16) -> &mut HttpContext

Set the response status code.

Source

pub fn add_response_header( &mut self, key: String, value: String, ) -> &mut HttpContext

Add a response header.

Source

pub fn set_body(&mut self, body: HttpBody) -> &mut HttpContext

Set the response body.

Source§

impl HttpContext

Source

pub fn bad_request(&mut self)

Source§

impl HttpContext

Source

pub fn new_res(config: HttpSafety, host: impl Into<String>) -> HttpContext

Creates a client context for sending requests (backward compatibility)

Source

pub async fn send_request<T>( host: T, request: HttpRequest, safety_config: HttpSafety, ) -> Result<HttpResponse, ConnectionError>
where T: Into<String>,

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.

Source

pub fn request(&mut self, request: HttpRequest)

Source

pub async fn write_frame( write_stream: &mut BufWriter<WriteHalf<TcpConnectionStream>>, request_frame: HttpRequest, ) -> Result<(), ConnectionError>

Write an HTTP request frame to the stream

Source

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

Trait Implementations§

Source§

impl AsyncMiddleware<HttpContext> for LoggingMiddleware

Source§

fn handle<'a>( &'a self, req: HttpContext, next: Box<dyn Fn(HttpContext) -> Pin<Box<dyn Future<Output = HttpContext> + Send>> + Sync + Send>, ) -> Pin<Box<dyn Future<Output = HttpContext> + Send>>

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn return_self() -> LoggingMiddleware

Used when creating the mddleware
Source§

impl RequestContext for HttpContext

Source§

type Request = HttpRequest

The request type for this context
Source§

type Response = HttpResponse

The response type for this context
Source§

fn handle_error(&mut self)

Handle protocol errors (bad request for server, bad response for client)
Source§

fn role(&self) -> ProtocolRole

Get the role of this context (Server or Client)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V