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
impl HttpSession
HTTP 1.x client session
Sourcepub fn new(stream: Box<dyn IO>) -> HttpSession
pub fn new(stream: Box<dyn IO>) -> HttpSession
Create a new http client session from an established (TCP or TLS) Stream
.
Sourcepub async fn write_request_header(
&mut self,
req: Box<RequestHeader>,
) -> Result<usize, Box<Error>>
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.
Sourcepub async fn write_body(
&mut self,
buf: &[u8],
) -> Result<Option<usize>, Box<Error>>
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
Sourcepub async fn finish_body(&mut self) -> Result<Option<usize>, Box<Error>>
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.
Sourcepub async fn read_response(&mut self) -> Result<usize, Box<Error>>
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.
Sourcepub async fn read_resp_header_parts(
&mut self,
) -> Result<Box<ResponseHeader>, Box<Error>>
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.
Sourcepub fn resp_header(&self) -> Option<&ResponseHeader>
pub fn resp_header(&self) -> Option<&ResponseHeader>
Return a reference of the ResponseHeader
if the response is read
Sourcepub fn get_header(&self, name: impl AsHeaderName) -> Option<&HeaderValue>
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.
Sourcepub fn get_header_bytes(&self, name: impl AsHeaderName) -> &[u8] ⓘ
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
Sourcepub fn get_status(&self) -> Option<StatusCode>
pub fn get_status(&self) -> Option<StatusCode>
Return the status code of the response if read
Sourcepub async fn read_body_ref(&mut self) -> Result<Option<&[u8]>, Box<Error>>
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.
Sourcepub async fn read_body_bytes(&mut self) -> Result<Option<Bytes>, Box<Error>>
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.
Sourcepub fn is_body_done(&mut self) -> bool
pub fn is_body_done(&mut self) -> bool
Whether there is no more body to read.
Sourcepub fn get_headers_raw_bytes(&self) -> Bytes
pub fn get_headers_raw_bytes(&self) -> Bytes
Get the raw response header bytes
Sourcepub fn respect_keepalive(&mut self)
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
.
pub fn will_keepalive(&self) -> bool
Sourcepub async fn shutdown(&mut self)
pub async fn shutdown(&mut self)
Close the connection abruptly. This allows to signal the server that the connection is closed
before dropping HttpSession
Sourcepub async fn reuse(self) -> Option<Box<dyn IO>>
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.
Sourcepub fn is_upgrade_req(&self) -> bool
pub fn is_upgrade_req(&self) -> bool
Whether this request is for upgrade
pub async fn read_response_task(&mut self) -> Result<HttpTask, Box<Error>>
Sourcepub fn digest(&self) -> &Digest
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.
Sourcepub fn digest_mut(&mut self) -> &mut Digest
pub fn digest_mut(&mut self) -> &mut Digest
Return a mutable Digest reference for the connection.
Sourcepub fn server_addr(&self) -> Option<&SocketAddr>
pub fn server_addr(&self) -> Option<&SocketAddr>
Return the server (peer) address recorded in the connection digest.
Sourcepub fn client_addr(&self) -> Option<&SocketAddr>
pub fn client_addr(&self) -> Option<&SocketAddr>
Return the client (local) address recorded in the connection digest.
Sourcepub fn stream(&self) -> &Box<dyn IO>
pub fn stream(&self) -> &Box<dyn IO>
Get the reference of the Stream that this HTTP session is operating upon.
Sourcepub fn into_inner(self) -> Box<dyn IO>
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.