HttpSession

Struct HttpSession 

Source
pub struct HttpSession { /* private fields */ }
Expand description

The HTTP 1.x server session

Implementations§

Source§

impl HttpSession

Source

pub fn new(underlying_stream: Box<dyn IO>) -> HttpSession

Create a new http server session from an established (TCP or TLS) Stream. The created session needs to call Self::read_request() first before performing any other operations.

Source

pub async fn read_request(&mut self) -> Result<Option<usize>, Box<Error>>

Read the request header. Return Ok(Some(n)) where the read and parsing are successful. Return Ok(None) when the client closed the connection without sending any data, which is common on a reused connection.

Source

pub fn req_header(&self) -> &RequestHeader

Return a reference of the RequestHeader this session read

§Panics

this function and most other functions will panic if called before Self::read_request()

Source

pub fn req_header_mut(&mut self) -> &mut RequestHeader

Return a mutable reference of the RequestHeader this session read

§Panics

this function and most other functions will panic if called before Self::read_request()

Source

pub fn get_header(&self, name: impl AsHeaderName) -> Option<&HeaderValue>

Get the header value for the given header name If there are multiple headers under the same name, the first one will be returned Use self.req_header().header.get_all(name) to get all the headers under the same name

Source

pub fn request_summary(&self) -> String

Return a string $METHOD $PATH, Host: $HOST. Mostly for logging and debug purpose

Source

pub fn is_upgrade_req(&self) -> bool

Is the request a upgrade request

Source

pub fn get_header_bytes(&self, name: impl AsHeaderName) -> &[u8]

Get the request header as raw bytes, b"" when the header doesn’t exist

Source

pub async fn read_body_bytes(&mut self) -> Result<Option<Bytes>, Box<Error>>

Read the request body. Ok(None) when there is no (more) body to read.

Source

pub async fn drain_request_body(&mut self) -> Result<(), Box<Error>>

Drain the request body. Ok(()) when there is no (more) body to read.

Source

pub fn is_body_done(&mut self) -> bool

Whether there is no (more) body need to be read.

Source

pub fn is_body_empty(&mut self) -> bool

Whether the request has an empty body Because HTTP 1.1 clients have to send either Content-Length or Transfer-Encoding in order to signal the server that it will send the body, this function returns accurate results even only when the request header is just read.

Source

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

Write the response header to the client. This function can be called more than once to send 1xx informational headers excluding 101.

Source

pub fn response_written(&self) -> Option<&ResponseHeader>

Return the response header if it is already sent.

Source

pub fn is_upgrade(&self, header: &ResponseHeader) -> Option<bool>

Some(true) if the this is a successful upgrade Some(false) if the request is an upgrade but the response refuses it None if the request is not an upgrade.

Source

pub fn get_keepalive_timeout(&self) -> Option<u64>

Source

pub fn will_keepalive(&self) -> bool

Return whether the session will be keepalived for connection reuse.

Source

pub fn respect_keepalive(&mut self)

Apply keepalive settings according to the client For HTTP 1.1, assume keepalive as long as there is no Connection: Close request header. For HTTP 1.0, only keepalive if there is an explicit header Connection: keep-alive.

Source

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

Same as Self::write_response_header() but takes a reference.

Source

pub async fn write_body( &mut self, buf: &[u8], ) -> Result<Option<usize>, Box<Error>>

Write response body to the client. Return Ok(None) when there shouldn’t be more body to be written, e.g., writing more bytes than what the Content-Length header suggests

Source

pub async fn finish_body(&mut self) -> Result<Option<usize>, Box<Error>>

Signal that there is no more body to write. This call will try to flush the buffer if there is any un-flushed data. For chunked encoding response, this call will also send the last chunk. For upgraded sessions, this call will also close the reading of the client body.

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 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 idle(&mut self) -> Result<usize, Box<Error>>

This function will (async) block forever until the client closes the connection.

Source

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

This function will return body bytes (same as Self::read_body_bytes()), but after the client body finishes (Ok(None) is returned), calling this function again will block forever, same as Self::idle().

Source

pub fn get_headers_raw_bytes(&self) -> Bytes

Return the raw bytes of the request header.

Source

pub async fn shutdown(&mut self)

Close the connection abruptly. This allows to signal the client that the connection is closed before dropping HttpSession

Source

pub fn set_server_keepalive(&mut self, keepalive: Option<u64>)

Set the server keepalive timeout. None: disable keepalive, this session cannot be reused. Some(0): reusing this session is allowed and there is no timeout. Some(>0): reusing this session is allowed within the given timeout in seconds. If the client disallows connection reuse, then keepalive will be ignored.

Source

pub fn set_read_timeout(&mut self, timeout: Option<Duration>)

Sets the downstream read timeout. This will trigger if we’re unable to read from the stream after timeout.

Source

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

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

Source

pub fn set_total_drain_timeout(&mut self, timeout: Option<Duration>)

Sets the total drain timeout. For HTTP/1.1, reusing a session requires ensuring that the request body is consumed. This timeout will be used to determine how long to wait for the entirety of the downstream request body to finish after the upstream response is completed to return the session to the reuse pool. If the timeout is exceeded, we will give up on trying to reuse the session.

Note that the downstream read timeout still applies between body byte reads.

Source

pub fn set_min_send_rate(&mut self, min_send_rate: Option<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 equivalent to disabling.

Source

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

Sets whether we ignore writing informational responses downstream.

This is a noop if the response is Upgrade or Continue and Expect: 100-continue was set on the request.

Source

pub fn set_close_on_response_before_downstream_finish(&mut self, close: bool)

Sets whether keepalive should be disabled if response is written prior to downstream body finishing.

This may be set to avoid draining downstream if the body is no longer necessary.

Source

pub fn digest(&self) -> &Digest

Return the Digest of the connection.

Source

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

Return a mutable Digest reference for the connection.

Source

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

Return the client (peer) address of the underlying connection.

Source

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

Return the server (local) address of the underlying connection.

Source

pub async fn reuse(self) -> Result<Option<Box<dyn IO>>, Box<Error>>

Consume self, if the connection can be reused, the underlying stream will be returned to be fed to the next Self::new(). This drains any remaining request body if it hasn’t yet been read and the stream is reusable.

The next session can just call Self::read_request().

If the connection cannot be reused, the underlying stream will be closed and None will be returned. If there was an error while draining any remaining request body that error will be returned.

Source

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

Write a 100 Continue response to the client.

Source

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

Source

pub fn stream(&self) -> &Box<dyn IO>

Get the reference of the Stream that this HTTP session is operating upon.

Source

pub fn into_inner(self) -> Box<dyn IO>

Consume self, the underlying stream will be returned and can be used directly, for example, in the case of HTTP upgrade. The stream is not flushed prior to being returned.

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<T> Upcastable for T
where T: Any + Send + Sync + 'static,

Source§

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

upcast ref
Source§

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

upcast mut ref
Source§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
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