pub struct Context {
pub req: Request,
/* private fields */
}Expand description
Context holds request data and provides response building methods
Fields§
§req: RequestImplementations§
Source§impl Context
impl Context
Sourcepub async fn new(req: HyperRequest<Incoming>, params: Params) -> Result<Self>
pub async fn new(req: HyperRequest<Incoming>, params: Params) -> Result<Self>
Create a new context from a request and params
Sourcepub async fn set(&self, key: impl Into<String>, value: impl Into<String>)
pub async fn set(&self, key: impl Into<String>, value: impl Into<String>)
Set a value in the context state (shared between middleware)
Read a request cookie by name.
All request cookies.
Queue a Set-Cookie for the response. Errors if the cookie is invalid.
Queue a deletion of the named cookie (Max-Age=0).
Sourcepub fn peer_addr(&self) -> Option<SocketAddr>
pub fn peer_addr(&self) -> Option<SocketAddr>
The peer address of the underlying connection, if known. This is the
direct socket peer — for the originating client behind a proxy, use
client_ip.
Sourcepub fn client_ip(&self) -> Option<IpAddr>
pub fn client_ip(&self) -> Option<IpAddr>
Best-effort originating client IP.
When proxy trust is enabled (app.trust_proxy(true)) this honors the
left-most X-Forwarded-For entry, then Forwarded: for=…; otherwise (or
if no such header) it falls back to the connection peer. Only enable
proxy trust when the app is actually behind a trusted proxy — these
headers are client-spoofable.
Sourcepub async fn header(&self, name: impl Into<String>, value: impl Into<String>)
pub async fn header(&self, name: impl Into<String>, value: impl Into<String>)
Add a response header
Sourcepub async fn stream<S>(&self, body: S) -> Result<Response>
pub async fn stream<S>(&self, body: S) -> Result<Response>
Return a streaming response body.
Each Ok(bytes) item is sent as a chunk; the response is chunked (no
Content-Length). Any queued header/status set on the context is
applied, except Content-Length/Transfer-Encoding (which would
conflict with the chunked framing and are dropped). If no Content-Type
was set, it defaults to application/octet-stream. An Err(_) item
aborts the connection.
let chunks: Vec<std::result::Result<Bytes, BoxError>> = vec![Ok(Bytes::from("hi"))];
ctx.stream(futures_util::stream::iter(chunks)).awaitSourcepub async fn sse<S>(&self, events: S) -> Result<Response>
pub async fn sse<S>(&self, events: S) -> Result<Response>
Return a Server-Sent Events response streaming typed events.
Sets Content-Type: text/event-stream, Cache-Control: no-cache, and
X-Accel-Buffering: no, then streams each SseEvent
encoded to the SSE wire format (chunked, no Content-Length).
let evs = vec![SseEvent::new(&json!({ "hello": "world" }))?];
ctx.sse(futures_util::stream::iter(evs)).awaitSourcepub async fn sse_keep_alive<S>(
&self,
events: S,
interval: Duration,
) -> Result<Response>
pub async fn sse_keep_alive<S>( &self, events: S, interval: Duration, ) -> Result<Response>
Like sse, but injects a : ping comment whenever the
event stream is idle for interval, so proxies and idle connections
aren’t dropped. Terminates when the event stream ends.
Sourcepub fn last_event_id(&self) -> Option<String>
pub fn last_event_id(&self) -> Option<String>
The Last-Event-ID request header, if present, for app-driven resume.
Trait Implementations§
Source§impl FromRequest for Context
The full request context — the identity extractor, so existing
|ctx: Context| async move { … } handlers keep working unchanged.
impl FromRequest for Context
The full request context — the identity extractor, so existing
|ctx: Context| async move { … } handlers keep working unchanged.