#http-header #header #hyper #hyper-http #hyperium

headers-accept-encoding

Hypper typed HTTP headers with Accept-Encoding + zstd support

4 releases (stable)

Uses old Rust 2015

1.1.0 Dec 5, 2023
1.0.1 Sep 16, 2023
1.0.0 Apr 19, 2023
0.3.8 Apr 13, 2023

#6 in #hyperium

Download history 956/week @ 2023-12-18 957/week @ 2023-12-25 969/week @ 2024-01-01 813/week @ 2024-01-08 618/week @ 2024-01-15 606/week @ 2024-01-22 549/week @ 2024-01-29 482/week @ 2024-02-05 451/week @ 2024-02-12 143/week @ 2024-02-19 264/week @ 2024-02-26 278/week @ 2024-03-04 541/week @ 2024-03-11 323/week @ 2024-03-18 238/week @ 2024-03-25 394/week @ 2024-04-01

1,540 downloads per month
Used in static-web-server

MIT license

330KB
6K SLoC

Rust HTTP headers with Accept-Encoding + zstd Content Coding support

Build Status

A headers crate fork with Accept-Encoding + zstd Content Coding support and synced with the upstream hyperium/headers.

This fork is used by static-web-server.


lib.rs:

Typed HTTP Headers

hyper has the opinion that headers should be strongly-typed, because that's why we're using Rust in the first place. To set or get any header, an object must implement the Header trait from this module. Several common headers are already provided, such as Host, ContentType, UserAgent, and others.

Why Typed?

Or, why not stringly-typed? Types give the following advantages:

  • More difficult to typo, since typos in types should be caught by the compiler
  • Parsing to a proper type by default

Defining Custom Headers

Implementing the Header trait

Consider a Do Not Track header. It can be true or false, but it represents that via the numerals 1 and 0.

extern crate http;
extern crate headers;

use headers::{Header, HeaderName, HeaderValue};

struct Dnt(bool);

impl Header for Dnt {
    fn name() -> &'static HeaderName {
         &http::header::DNT
    }

    fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
    where
        I: Iterator<Item = &'i HeaderValue>,
    {
        let value = values
            .next()
            .ok_or_else(headers::Error::invalid)?;

        if value == "0" {
            Ok(Dnt(false))
        } else if value == "1" {
            Ok(Dnt(true))
        } else {
            Err(headers::Error::invalid())
        }
    }

    fn encode<E>(&self, values: &mut E)
    where
        E: Extend<HeaderValue>,
    {
        let s = if self.0 {
            "1"
        } else {
            "0"
        };

        let value = HeaderValue::from_static(s);

        values.extend(std::iter::once(value));
    }
}

Dependencies

~1.5MB
~33K SLoC