3

I'd prefer to take advantage of the type safety of Hyper's hyper::header::Headers#get method instead of using get_raw with a &str.

What is the best way to accomplish this?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Colin Dean
  • 1,429
  • 1
  • 15
  • 26

2 Answers2

3

Digging through the hyper::header::Headers source code, I found that there is a neat macro for generating the code: header!. You will need some incantation to make it useful, though:

#[macro_use]
extern crate hyper;

use hyper::{Body, Method, Request, Response};
use std::fmt::{self, Display};
use std::str::FromStr;
use std::num::ParseIntError;

// For a header that looks like this:
//    x-arbitrary-header-with-an-integer: 8

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ArbitraryNumber(i8);

impl Display for ArbitraryNumber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Arbitrary Protocol v{}", self.0)
    }
}

impl FromStr for ArbitraryNumber {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<i8>().map(|int| ArbitraryNumber(int))
    }
}

//impl Header for ArbitraryNumberHeader
header! { (ArbitraryNumberHeader, "x-arbitrary-header-with-an-integer") => [ArbitraryNumber] }

Once you've got a Response named res in scope, you can access this header like so:

let arbitrary_header: AribitraryNumber = res.headers().get::<ArbitraryNumberHeader>().unwrap();
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Colin Dean
  • 1,429
  • 1
  • 15
  • 26
0

I think in the meantime things have changed. I couldn't find the header! macro in hyper. This is my solution to implementing a custom header (I am using axum which re-exports from hyper, it should work the same without axum just with different imports):

use axum::headers::Error;
use http::{HeaderName, HeaderValue};

#[derive(Eq, PartialEq, Clone, Hash)]
pub struct XXsrfToken(pub String);

pub static X_XSRF_TOKEN: HeaderName = HeaderName::from_static("x-xsrf-token");

impl axum::headers::Header for XXsrfToken {
    fn name() -> &'static HeaderName {
        &X_XSRF_TOKEN
    }

    fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
    where
        Self: Sized,
        I: Iterator<Item = &'i HeaderValue>,
    {
        values
            .next()
            .and_then(|v| v.to_str().ok())
            .map(|it| XXsrfToken(it.to_string()))
            .ok_or_else(Error::invalid)
    }

    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
        let bytes = self.0.as_str().as_bytes();
        let val = HeaderValue::from_bytes(bytes).expect("XXsrfToken is a valid HeaderValue");

        values.extend(::std::iter::once(val));
    }
}
Patrick
  • 999
  • 1
  • 9
  • 21