pingora_core::protocols::http::server

Enum Session

Source
pub enum Session {
    H1(HttpSession),
    H2(HttpSession),
}
Expand description

HTTP server session object for both HTTP/1.x and HTTP/2

Variants§

Implementations§

Source§

impl Session

Source

pub fn new_http1(stream: Stream) -> Self

Create a new Session from an established connection for HTTP/1.x

Source

pub fn new_http2(session: SessionV2) -> Self

Create a new Session from an established HTTP/2 stream

Source

pub fn is_http2(&self) -> bool

Whether the session is HTTP/2. If not it is HTTP/1.x

Source

pub async fn read_request(&mut self) -> Result<bool>

Read the request header. This method is required to be called first before doing anything else with the session.

  • Ok(true): successful
  • Ok(false): client exit without sending any bytes. This is normal on reused connection. In this case the user should give up this session.
Source

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.

Source

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.

Source

pub fn get_header<K: AsHeaderName>(&self, key: K) -> Option<&HeaderValue>

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().

Source

pub fn get_header_bytes<K: AsHeaderName>(&self, key: K) -> &[u8]

Get the header value in its raw format. If the header doesn’t exist, return an empty slice.

Source

pub async fn read_request_body(&mut self) -> Result<Option<Bytes>>

Read the request body. Ok(None) if no (more) body to read

Source

pub async fn write_response_header( &mut self, resp: Box<ResponseHeader>, ) -> Result<()>

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.

Source

pub async fn write_response_header_ref( &mut self, resp: &ResponseHeader, ) -> Result<()>

Similar to write_response_header(), this fn will clone the resp internally

Source

pub async fn write_response_body( &mut self, data: Bytes, end: bool, ) -> Result<()>

Write the response body to client

Source

pub async fn write_response_trailers( &mut self, trailers: HeaderMap, ) -> Result<()>

Write the response trailers to client

Source

pub async fn finish(self) -> Result<Option<Stream>>

Finish the life of this request. For H1, if connection reuse is supported, a Some(Stream) will be returned, otherwise None. For H2, always return None because H2 stream is not reusable.

Source

pub async fn response_duplex_vec( &mut self, tasks: Vec<HttpTask>, ) -> Result<bool>

Source

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

Source

pub fn set_write_timeout(&mut self, timeout: Duration)

Sets the downstream write timeout. This will trigger if we’re unable to write to the stream after duration. If a min_send_rate is configured then the min_send_rate calculated timeout has higher priority.

This is a noop for h2.

Source

pub fn set_min_send_rate(&mut self, rate: usize)

Sets the minimum downstream send rate in bytes per second. This is used to calculate a write timeout in seconds based on the size of the buffer being written. If a min_send_rate is configured it has higher priority over a set write_timeout. The minimum send rate must be greater than zero.

Calculated write timeout is guaranteed to be at least 1s if min_send_rate is greater than zero, a send rate of zero is a noop.

This is a noop for h2.

Source

pub fn set_ignore_info_resp(&mut self, ignore: bool)

Sets whether we ignore writing informational responses downstream.

For HTTP/1.1 this is a noop if the response is Upgrade or Continue and Expect: 100-continue was set on the request.

This is a noop for h2 because informational responses are always ignored.

Source

pub fn request_summary(&self) -> String

Return a digest of the request including the method, path and Host header

Source

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

Source

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

Source

pub fn to_h1_raw(&self) -> Bytes

Source

pub fn is_body_done(&mut self) -> bool

Whether the whole request body is sent

Source

pub async fn finish_body(&mut self) -> Result<()>

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

Source

pub fn generate_error(error: u16) -> ResponseHeader

Source

pub async fn respond_error(&mut self, error: u16)

Send error response to client

Source

pub fn is_body_empty(&mut self) -> bool

Whether there is no request body

Source

pub fn retry_buffer_truncated(&self) -> bool

Source

pub fn enable_retry_buffering(&mut self)

Source

pub fn get_retry_buffer(&self) -> Option<Bytes>

Source

pub async fn read_body_or_idle( &mut self, no_body_expected: bool, ) -> Result<Option<Bytes>>

Read body (same as read_request_body()) or pending forever until downstream terminates the session.

Source

pub fn as_http1(&self) -> Option<&SessionV1>

Source

pub fn as_http2(&self) -> Option<&SessionV2>

Source

pub async fn write_continue_response(&mut self) -> Result<()>

Write a 100 Continue response to the client.

Source

pub fn is_upgrade_req(&self) -> bool

Whether this request is for upgrade (e.g., websocket)

Source

pub fn body_bytes_sent(&self) -> usize

Return how many response body bytes (application, not wire) already sent downstream

Source

pub fn body_bytes_read(&self) -> usize

Return how many request body bytes (application, not wire) already read from downstream

Source

pub fn digest(&self) -> Option<&Digest>

Return the Digest for the connection.

Source

pub fn digest_mut(&mut self) -> Option<&mut Digest>

Return a mutable Digest reference for the connection.

Will return None if multiple H2 streams are open.

Source

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

Return the client (peer) address of the connection.

Source

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

Return the server (local) address of the connection.

Source

pub fn stream(&self) -> Option<&Stream>

Get the reference of the Stream that this HTTP/1 session is operating upon. None if the HTTP session is over H2

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more