pub enum ServerSession {
H1(HttpSession),
H2(HttpSession),
}Expand description
HTTP server session object for both HTTP/1.x and HTTP/2
Variants§
H1(HttpSession)
H2(HttpSession)
Implementations§
Source§impl Session
impl Session
Sourcepub fn new_http1(stream: Box<dyn IO>) -> Session
pub fn new_http1(stream: Box<dyn IO>) -> Session
Create a new Session from an established connection for HTTP/1.x
Sourcepub fn new_http2(session: HttpSession) -> Session
pub fn new_http2(session: HttpSession) -> Session
Create a new Session from an established HTTP/2 stream
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 drain_request_body(&mut self) -> Result<(), Box<Error>>
pub async fn drain_request_body(&mut self) -> Result<(), Box<Error>>
Discard the request body by reading it until completion.
This is useful for making streams reusable (in particular for HTTP/1.1) after returning an error before the whole body has been 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,
end: bool,
) -> Result<(), Box<Error>>
pub async fn write_response_body( &mut self, data: Bytes, end: bool, ) -> 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
Sourcepub async fn finish(self) -> Result<Option<Box<dyn IO>>, Box<Error>>
pub async fn finish(self) -> Result<Option<Box<dyn IO>>, Box<Error>>
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.
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 get_keepalive(&self) -> Option<u64>
pub fn get_keepalive(&self) -> Option<u64>
Get the keepalive timeout. None if keepalive is disabled. Not applicable for h2
Sourcepub fn set_read_timeout(&mut self, timeout: Option<Duration>)
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.
This is a noop for h2.
Sourcepub fn set_write_timeout(&mut self, timeout: Option<Duration>)
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.
Sourcepub fn set_total_drain_timeout(&mut self, timeout: Option<Duration>)
pub fn set_total_drain_timeout(&mut self, timeout: Option<Duration>)
Sets the total drain timeout, which will be applied while discarding the
request body using drain_request_body.
For HTTP/1.1, reusing a session requires ensuring that the request body is consumed. If the timeout is exceeded, the caller should give up on trying to reuse the session.
Sourcepub fn set_min_send_rate(&mut self, rate: Option<usize>)
pub fn set_min_send_rate(&mut self, 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.
This is a noop for h2.
Sourcepub fn set_ignore_info_resp(&mut self, ignore: bool)
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.
Sourcepub fn set_close_on_response_before_downstream_finish(&mut self, close: bool)
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 is a 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
pub fn generate_error(error: u16) -> ResponseHeader
Sourcepub async fn respond_error(&mut self, error: u16) -> Result<(), Box<Error>>
pub async fn respond_error(&mut self, error: u16) -> Result<(), Box<Error>>
Send error response to client using a pre-generated error message.
Sourcepub async fn respond_error_with_body(
&mut self,
error: u16,
body: Bytes,
) -> Result<(), Box<Error>>
pub async fn respond_error_with_body( &mut self, error: u16, body: Bytes, ) -> Result<(), Box<Error>>
Send error response to client using a pre-generated error message and custom body.
Sourcepub async fn write_error_response(
&mut self,
resp: ResponseHeader,
body: Bytes,
) -> Result<(), Box<Error>>
pub async fn write_error_response( &mut self, resp: ResponseHeader, body: Bytes, ) -> Result<(), Box<Error>>
Send an error response to a client with a response header and body.
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 digest_mut(&mut self) -> Option<&mut Digest>
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.
Sourcepub fn client_addr(&self) -> Option<&SocketAddr>
pub fn client_addr(&self) -> Option<&SocketAddr>
Return the client (peer) address of the connection.
Sourcepub fn server_addr(&self) -> Option<&SocketAddr>
pub fn server_addr(&self) -> Option<&SocketAddr>
Return the server (local) address of the connection.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.