Struct HttpSession

Source
pub struct HttpSession {
    pub read_timeout: Option<Duration>,
    pub write_timeout: Option<Duration>,
    /* private fields */
}
Expand description

The HTTP 1.x client session

Fields§

§read_timeout: Option<Duration>

The read timeout, which will be applied to both reading the header and the body. The timeout is reset on every read. This is not a timeout on the overall duration of the response.

§write_timeout: Option<Duration>

The write timeout which will be applied to both writing request header and body. The timeout is reset on every write. This is not a timeout on the overall duration of the request.

Implementations§

Source§

impl HttpSession

HTTP 1.x client session

Source

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

Create a new http client session from an established (TCP or TLS) Stream.

Source

pub async fn write_request_header( &mut self, req: Box<RequestHeader>, ) -> Result<usize, Box<Error>>

Write the request header to the server After the request header is sent. The caller can either start reading the response or sending request body if any.

Source

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

Write request body. Return Ok(None) if no more body should be written, either due to Content-Length or the last chunk is already sent

Source

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

Flush local buffer and notify the server by sending the last chunk if chunked encoding is used.

Source

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

Read the response header from the server This function can be called multiple times, if the headers received are just informational headers.

Source

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

Similar to Self::read_response(), read the response header and then return a copy of it.

Source

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

Return a reference of the ResponseHeader if the response is read

Source

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

Get the header value for the given header name from the response header If there are multiple headers under the same name, the first one will be returned Use self.resp_header().header.get_all(name) to get all the headers under the same name Always return None if the response is not read yet.

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 or response not read

Source

pub fn get_status(&self) -> Option<StatusCode>

Return the status code of the response if read

Source

pub async fn read_body_ref(&mut self) -> Result<Option<&[u8]>, Box<Error>>

Read the response body into the internal buffer. Return Ok(Some(ref)) after a successful read. Return Ok(None)` if there is no more body to read.

Source

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

Similar to Self::read_body_ref but return Bytes instead of a slice reference.

Source

pub fn is_body_done(&mut self) -> bool

Whether there is no more body to read.

Source

pub fn get_headers_raw_bytes(&self) -> Bytes

Get the raw response header bytes

Source

pub fn respect_keepalive(&mut self)

Apply keepalive settings according to the server’s response 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 fn will_keepalive(&self) -> bool

Source

pub async fn shutdown(&mut self)

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

Source

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

Consume self, if the connection can be reused, the underlying stream will be returned. The returned connection can be kept in a connection pool so that next time the same server is being contacted. A new client session can be created via Self::new(). If the connection cannot be reused, the underlying stream will be closed and None will be returned.

Source

pub fn is_upgrade_req(&self) -> bool

Whether this request is for upgrade

Source

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

Source

pub fn digest(&self) -> &Digest

Return the Digest of the connection

For reused connection, the timing in the digest will reflect its initial handshakes The caller should check if the connection is reused to avoid misuse the timing field.

Source

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

Return a mutable Digest reference for the connection.

Source

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

Return the server (peer) address recorded in the connection digest.

Source

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

Return the client (local) address recorded in the connection digest.

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. It is not flushed prior to being returned.

Trait Implementations§

Source§

impl UniqueID for HttpSession

Source§

fn id(&self) -> i32

The ID returned should be unique among all existing connections of the same type. But ID can be recycled after a connection is shutdown.

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> 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<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