Skip to main content

Codec

Struct Codec 

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

Stateful HTTP/1 request decoder and response encoder.

The codec tracks the version, connection behavior, request method, and streaming state of the most recently decoded request.

§Decoding

Decoder::decode incrementally consumes one request head and returns its Request together with a PayloadType. For PayloadType::Payload, pass subsequent bytes to the returned payload decoder until framing completes before decoding another request head. Bytes for a pipelined request can already remain in the input buffer. PayloadType::Stream ends HTTP message framing; transfer the connection and any buffered bytes to the upgraded protocol instead of decoding another HTTP request.

Ok(None) means that more bytes are required. A DecodeError indicates invalid framing or a configured request-head limit and should be treated as a connection-level protocol failure.

§Encoding

Encoder::encode accepts a Message<(Response<()>, BodySize)>. Encode the response head first, followed by body chunks and a final Message::Chunk(None) when the response has a body. The codec selects fixed-length, chunked, or connection-close framing from the response, request method, version, and supplied BodySize. An EncodeError indicates invalid response encoding or an incomplete fixed-length body.

The codec only transforms buffers; it does not perform I/O, flush output, or apply transport backpressure.

Implementations§

Source§

impl Codec

Source

pub fn new(con_id: usize, cfg: Cfg<HttpServiceConfig>) -> Self

Creates an HTTP/1 codec.

con_id identifies the connection in decoded request heads. Protocol limits and keep-alive behavior are read from cfg.

Source

pub fn keepalive(&self) -> bool

Returns whether the current HTTP connection state is persistent.

Before the first request is decoded, this reflects whether keep-alive is enabled in the service configuration. Decoding a request or encoding a response can update the value.

Trait Implementations§

Source§

impl Clone for Codec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Codec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decoder for Codec

Source§

type Item = (Request, PayloadType)

The type of decoded frames.
Source§

type Error = DecodeError

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode(&self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>

Attempts to decode a frame from the buffered input. Read more
Source§

fn decode_eof( &self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

Attempts to decode a frame once the transport reached a clean EOF. Read more
Source§

impl Encoder for Codec

Source§

type Item = Message<(Response<()>, BodySize)>

The type of frames consumed by the encoder.
Source§

type Error = EncodeError

The type of encoding errors.
Source§

fn encode( &self, item: Self::Item, dst: &mut BytePages, ) -> Result<(), Self::Error>

Encodes a frame and appends it to dst. Read more

Auto Trait Implementations§

§

impl !Freeze for Codec

§

impl !RefUnwindSafe for Codec

§

impl !Send for Codec

§

impl !Sync for Codec

§

impl !UnwindSafe for Codec

§

impl Unpin for Codec

§

impl UnsafeUnpin for Codec

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.