pub enum Connection<'b, T, const N: usize = DEFAULT_MAX_HEADERS_COUNT> {
Transition(TransitionState),
Unbound(T),
Request(RequestState<'b, T, N>),
Response(ResponseState<T>),
}Expand description
A connection state machine for handling HTTP server requests-response cycles.
Variants§
Implementations§
Source§impl<'b, T, const N: usize> Connection<'b, T, N>
impl<'b, T, const N: usize> Connection<'b, T, N>
Sourcepub async fn new(
buf: &'b mut [u8],
io: T,
) -> Result<Connection<'b, T, N>, Error<T::Error>>
pub async fn new( buf: &'b mut [u8], io: T, ) -> Result<Connection<'b, T, N>, Error<T::Error>>
Create a new connection state machine for an incoming request
Note that the connection does not have any built-in read/write timeouts:
- To add a timeout on each IO operation, wrap the
iotype with theedge_nal::WithTimeoutwrapper. - To add a global request-response timeout, wrap your complete request-response processing
logic with the
edge_nal::with_timeoutfunction.
Parameters:
buf: A buffer to store the request headersio: A socket stream
Sourcepub fn is_request_initiated(&self) -> bool
pub fn is_request_initiated(&self) -> bool
Return true of the connection is in request state (i.e. the initial state upon calling new)
Sourcepub fn split(&mut self) -> (&RequestHeaders<'b, N>, &mut Body<'b, T>)
pub fn split(&mut self) -> (&RequestHeaders<'b, N>, &mut Body<'b, T>)
Split the connection into request headers and body
Sourcepub fn headers(&self) -> Result<&RequestHeaders<'b, N>, Error<T::Error>>
pub fn headers(&self) -> Result<&RequestHeaders<'b, N>, Error<T::Error>>
Return a reference to the request headers
Sourcepub fn is_ws_upgrade_request(&self) -> Result<bool, Error<T::Error>>
pub fn is_ws_upgrade_request(&self) -> Result<bool, Error<T::Error>>
Return true if the request is a WebSocket upgrade request
Sourcepub async fn initiate_response(
&mut self,
status: u16,
message: Option<&str>,
headers: &[(&str, &str)],
) -> Result<(), Error<T::Error>>
pub async fn initiate_response( &mut self, status: u16, message: Option<&str>, headers: &[(&str, &str)], ) -> Result<(), Error<T::Error>>
Switch the connection into a response state
Parameters:
status: The HTTP status codemessage: An optional HTTP status messageheaders: An array of HTTP response headers. Note that if noContent-LengthorTransfer-Encodingheaders are provided, the body will be send with chunked encoding (for HTTP1.1 only and if the connection is not Close)
Sourcepub async fn initiate_ws_upgrade_response(
&mut self,
buf: &mut [u8; 33],
) -> Result<(), Error<T::Error>>
pub async fn initiate_ws_upgrade_response( &mut self, buf: &mut [u8; 33], ) -> Result<(), Error<T::Error>>
A convenience method to initiate a WebSocket upgrade response
Sourcepub fn is_response_initiated(&self) -> bool
pub fn is_response_initiated(&self) -> bool
Return true if the connection is in response state
Sourcepub async fn complete(&mut self) -> Result<(), Error<T::Error>>
pub async fn complete(&mut self) -> Result<(), Error<T::Error>>
Completes the response and switches the connection back to the unbound state If the connection is still in a request state, and empty 200 OK response is sent
Sourcepub async fn complete_err(&mut self, err: &str) -> Result<(), Error<T::Error>>
pub async fn complete_err(&mut self, err: &str) -> Result<(), Error<T::Error>>
Completes the response with an error message and switches the connection back to the unbound state
If the connection is still in a request state, an empty 500 Internal Error response is sent
Sourcepub fn needs_close(&self) -> bool
pub fn needs_close(&self) -> bool
Return true if the connection needs to be closed
This is determined by the connection type (i.e. Connection: Close header)
Trait Implementations§
Source§impl<T, const N: usize> Read for Connection<'_, T, N>
impl<T, const N: usize> Read for Connection<'_, T, N>
Source§async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Source§async fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf. Read more