Struct fluke_hpack::encoder::Encoder

source ·
pub struct Encoder<'a> { /* private fields */ }
Expand description

Represents an HPACK encoder. Allows clients to encode arbitrary header sets and tracks the encoding context. That is, encoding subsequent header sets will use the context built by previous encode calls.

This is the main API for performing HPACK encoding of headers.

§Examples

Encoding a header two times in a row produces two different representations, due to the utilization of HPACK compression.

use fluke_hpack::Encoder;

let mut encoder = Encoder::new();

let headers = vec![
    (b"custom-key".to_vec(), b"custom-value".to_vec()),
];
// First encoding...
let result = encoder.encode(headers.iter().map(|h| (&h.0[..], &h.1[..])));
// The result is a literal encoding of the header name and value, with an
// initial byte representing the type of the encoding
// (incremental indexing).
assert_eq!(
    vec![0x40,
         10, b'c', b'u', b's', b't', b'o', b'm', b'-', b'k', b'e', b'y',
         12, b'c', b'u', b's', b't', b'o', b'm', b'-', b'v', b'a', b'l',
         b'u', b'e'],
    result);

// Encode the same headers again!
let result = encoder.encode(headers.iter().map(|h| (&h.0[..], &h.1[..])));
// The result is simply the index of the header in the header table (62),
// with a flag representing that the decoder should use the index.
assert_eq!(vec![0x80 | 62], result);

Implementations§

source§

impl<'a> Encoder<'a>

source

pub fn new() -> Encoder<'a>

Creates a new Encoder with a default static table, as defined by the HPACK spec (Appendix A).

source

pub fn set_max_table_size(&mut self, new_max_size: usize)

Sets a new maximum dynamic table size for the encoder.

source

pub fn encode<'b, I>(&mut self, headers: I) -> Vec<u8>
where I: IntoIterator<Item = (&'b [u8], &'b [u8])>,

Encodes the given headers using the HPACK rules and returns a newly allocated Vec containing the bytes representing the encoded header set.

The encoder so far supports only a single, extremely simple encoding strategy, whereby each header is represented as an indexed header if already found in the header table and a literal otherwise. When a header isn’t found in the table, it is added if the header name wasn’t found either (i.e. there are never two header names with different values in the produced header table). Strings are always encoded as literals (Huffman encoding is not used).

source

pub fn encode_into<'b, I, W>( &mut self, headers: I, writer: &mut W ) -> Result<()>
where I: IntoIterator<Item = (&'b [u8], &'b [u8])>, W: Write,

Encodes the given headers into the given io::Write instance. If the io::Write raises an Error at any point, this error is propagated out. Any changes to the internal state of the encoder will not be rolled back, though, so care should be taken to ensure that the paired decoder also ends up seeing the same state updates or that their pairing is cancelled.

source

pub fn encode_header_into<W: Write>( &mut self, header: (&[u8], &[u8]), writer: &mut W ) -> Result<()>

Encodes a single given header into the given io::Write instance.

Any errors are propagated, similarly to the encode_into method, and it is the callers responsiblity to make sure that the paired encoder sees them too.

Trait Implementations§

source§

impl<'a> Default for Encoder<'a>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Encoder<'a>

§

impl<'a> RefUnwindSafe for Encoder<'a>

§

impl<'a> Send for Encoder<'a>

§

impl<'a> Sync for Encoder<'a>

§

impl<'a> Unpin for Encoder<'a>

§

impl<'a> UnwindSafe for Encoder<'a>

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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