Struct pingora_proxy::Session
source · pub struct Session {
pub downstream_session: Box<ServerSession>,
pub cache: HttpCache,
pub upstream_compression: ResponseCompressionCtx,
pub downstream_compression: ResponseCompressionCtx,
pub ignore_downstream_range: bool,
/* private fields */
}
Expand description
The established HTTP session
This object is what users interact with in order to access the request itself or change the proxy behavior.
Fields§
§downstream_session: Box<ServerSession>
the HTTP session to downstream (the client)
cache: HttpCache
The interface to control HTTP caching
upstream_compression: ResponseCompressionCtx
(de)compress responses coming into the proxy (from upstream)
downstream_compression: ResponseCompressionCtx
(de)compress responses leaving the proxy (to downstream)
ignore_downstream_range: bool
ignore downstream range (skip downstream range filters)
Implementations§
source§impl Session
impl Session
pub fn as_downstream_mut(&mut self) -> &mut HttpSession
pub fn as_downstream(&self) -> &HttpSession
Methods from Deref<Target = HttpSession>§
sourcepub async fn read_request(&mut self) -> Result<bool, Box<Error>>
pub async fn read_request(&mut self) -> Result<bool, Box<Error>>
Read the request header. This method is required to be called first before doing anything else with the session.
Ok(true)
: successfulOk(false)
: client exit without sending any bytes. This is normal on reused connection. In this case the user should give up this session.
sourcepub fn req_header(&self) -> &RequestHeader
pub fn req_header(&self) -> &RequestHeader
Return the request header it just read.
§Panic
This function will panic if Self::read_request()
is not called.
sourcepub fn req_header_mut(&mut self) -> &mut RequestHeader
pub fn req_header_mut(&mut self) -> &mut RequestHeader
Return a mutable reference to request header it just read.
§Panic
This function will panic if Self::read_request()
is not called.
sourcepub fn get_header<K>(&self, key: K) -> Option<&HeaderValue>where
K: AsHeaderName,
pub fn get_header<K>(&self, key: K) -> Option<&HeaderValue>where
K: AsHeaderName,
Return the header by name. None if the header doesn’t exist.
In case there are multiple headers under the same name, the first one will be returned. To
get all the headers: use self.req_header().headers.get_all()
.
sourcepub fn get_header_bytes<K>(&self, key: K) -> &[u8] ⓘwhere
K: AsHeaderName,
pub fn get_header_bytes<K>(&self, key: K) -> &[u8] ⓘwhere
K: AsHeaderName,
Get the header value in its raw format. If the header doesn’t exist, return an empty slice.
sourcepub async fn read_request_body(&mut self) -> Result<Option<Bytes>, Box<Error>>
pub async fn read_request_body(&mut self) -> Result<Option<Bytes>, Box<Error>>
Read the request body. Ok(None) if no (more) body to read
sourcepub async fn write_response_header(
&mut self,
resp: Box<ResponseHeader>
) -> Result<(), Box<Error>>
pub async fn write_response_header( &mut self, resp: Box<ResponseHeader> ) -> Result<(), Box<Error>>
Write the response header to client Informational headers (status code 100-199, excluding 101) can be written multiple times the final response header (status code 200+ or 101) is written.
sourcepub async fn write_response_header_ref(
&mut self,
resp: &ResponseHeader
) -> Result<(), Box<Error>>
pub async fn write_response_header_ref( &mut self, resp: &ResponseHeader ) -> Result<(), Box<Error>>
Similar to write_response_header()
, this fn will clone the resp
internally
sourcepub async fn write_response_body(
&mut self,
data: Bytes
) -> Result<(), Box<Error>>
pub async fn write_response_body( &mut self, data: Bytes ) -> Result<(), Box<Error>>
Write the response body to client
sourcepub async fn write_response_trailers(
&mut self,
trailers: HeaderMap
) -> Result<(), Box<Error>>
pub async fn write_response_trailers( &mut self, trailers: HeaderMap ) -> Result<(), Box<Error>>
Write the response trailers to client
pub async fn response_duplex_vec( &mut self, tasks: Vec<HttpTask> ) -> Result<bool, Box<Error>>
sourcepub fn set_keepalive(&mut self, duration: Option<u64>)
pub fn set_keepalive(&mut self, duration: Option<u64>)
Set connection reuse. duration
defines how long the connection is kept open for the next
request to reuse. Noop for h2
sourcepub fn request_summary(&self) -> String
pub fn request_summary(&self) -> String
Return a digest of the request including the method, path and Host header
sourcepub fn response_written(&self) -> Option<&ResponseHeader>
pub fn response_written(&self) -> Option<&ResponseHeader>
Return the written response header. None
if it is not written yet.
Only the final (status code >= 200 or 101) response header will be returned
sourcepub async fn shutdown(&mut self)
pub async fn shutdown(&mut self)
Give up the http session abruptly. For H1 this will close the underlying connection For H2 this will send RESET frame to end this stream without impacting the connection
pub fn to_h1_raw(&self) -> Bytes
sourcepub fn is_body_done(&mut self) -> bool
pub fn is_body_done(&mut self) -> bool
Whether the whole request body is sent
sourcepub async fn finish_body(&mut self) -> Result<(), Box<Error>>
pub async fn finish_body(&mut self) -> Result<(), Box<Error>>
Notify the client that the entire body is sent for H1 chunked encoding, this will end the last empty chunk for H1 content-length, this has no effect. for H2, this will send an empty DATA frame with END_STREAM flag
sourcepub async fn respond_error(&mut self, error: u16)
pub async fn respond_error(&mut self, error: u16)
Send error response to client
sourcepub fn is_body_empty(&mut self) -> bool
pub fn is_body_empty(&mut self) -> bool
Whether there is no request body
pub fn retry_buffer_truncated(&self) -> bool
pub fn enable_retry_buffering(&mut self)
pub fn get_retry_buffer(&self) -> Option<Bytes>
sourcepub async fn read_body_or_idle(
&mut self,
no_body_expected: bool
) -> Result<Option<Bytes>, Box<Error>>
pub async fn read_body_or_idle( &mut self, no_body_expected: bool ) -> Result<Option<Bytes>, Box<Error>>
Read body (same as read_request_body()
) or pending forever until downstream
terminates the session.
pub fn as_http1(&self) -> Option<&HttpSession>
pub fn as_http2(&self) -> Option<&HttpSession>
sourcepub async fn write_continue_response(&mut self) -> Result<(), Box<Error>>
pub async fn write_continue_response(&mut self) -> Result<(), Box<Error>>
Write a 100 Continue response to the client.
sourcepub fn is_upgrade_req(&self) -> bool
pub fn is_upgrade_req(&self) -> bool
Whether this request is for upgrade (e.g., websocket)
sourcepub fn body_bytes_sent(&self) -> usize
pub fn body_bytes_sent(&self) -> usize
Return how many response body bytes (application, not wire) already sent downstream
sourcepub fn body_bytes_read(&self) -> usize
pub fn body_bytes_read(&self) -> usize
Return how many request body bytes (application, not wire) already read from downstream
sourcepub fn client_addr(&self) -> Option<&SocketAddr>
pub fn client_addr(&self) -> Option<&SocketAddr>
Return the client (peer) address of the connnection.
sourcepub fn server_addr(&self) -> Option<&SocketAddr>
pub fn server_addr(&self) -> Option<&SocketAddr>
Return the server (local) address of the connection.