Expand description
Headers container, and common header fields.
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
Hyper provides many of the most commonly used headers in HTTP. If
you need to define a custom header, it’s easy to do while still taking
advantage of the type system. Hyper includes a header!
macro for defining
many wrapper-style headers.
#[macro_use] extern crate hyper;
use hyper::header::Headers;
header! { (XRequestGuid, "X-Request-Guid") => [String] }
fn main () {
let mut headers = Headers::new();
headers.set(XRequestGuid("a proper guid".to_owned()))
}
This works well for simple “string” headers. If you need more control, you can implement the trait directly.
§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
.
use std::fmt;
use hyper::header::{self, Header, Raw};
#[derive(Debug, Clone, Copy)]
struct Dnt(bool);
impl Header for Dnt {
fn header_name() -> &'static str {
"DNT"
}
fn parse_header(raw: &Raw) -> hyper::Result<Dnt> {
if raw.len() == 1 {
let line = &raw[0];
if line.len() == 1 {
let byte = line[0];
match byte {
b'0' => return Ok(Dnt(true)),
b'1' => return Ok(Dnt(false)),
_ => ()
}
}
}
Err(hyper::Error::Header)
}
fn fmt_header(&self, f: &mut header::Formatter) -> fmt::Result {
let value = if self.0 {
"1"
} else {
"0"
};
f.fmt_line(&value)
}
}
Modules§
- Utility functions for Header implementations.
Macros§
Structs§
Accept
header, defined in RFC7231Accept-Charset
header, defined in RFC7231Accept-Encoding
header, defined in RFC7231Accept-Language
header, defined in RFC7231Accept-Ranges
header, defined in RFC7233Access-Control-Allow-Credentials
header, part of CORSAccess-Control-Allow-Headers
header, part of CORSAccess-Control-Allow-Methods
header, part of CORSAccess-Control-Expose-Headers
header, part of CORSAccess-Control-Max-Age
header, part of CORSAccess-Control-Request-Headers
header, part of CORSAccess-Control-Request-Method
header, part of CORSAllow
header, defined in RFC7231Authorization
header, defined in RFC7235- Credential holder for Basic Authentication
- Token holder for Bearer Authentication, most often seen with oauth
Cache-Control
header, defined in RFC7234Connection
header, defined in RFC7230- A
Content-Disposition
header, (re)defined in RFC6266. Content-Encoding
header, defined in RFC7231Content-Language
header, defined in RFC7231Content-Length
header, defined in RFC7230Content-Location
header, defined in RFC7231Content-Range
header, defined in RFC7233Content-Type
header, defined in RFC7231Cookie
header, defined in RFC6265- Iterator for cookie.
Date
header, defined in RFC7231ETag
header, defined in RFC7232- An entity tag, defined in RFC7232
Expires
header, defined in RFC7234- A formatter used to serialize headers to an output stream.
From
header, defined in RFC7231- Returned with the
HeadersItems
iterator. - A map of header fields on requests and responses.
- An
Iterator
over the fields in aHeaders
map. - The
Host
header. - A timestamp with HTTP formatting and parsing
If-Modified-Since
header, defined in RFC7232If-Unmodified-Since
header, defined in RFC7232- A language tag as described in BCP47.
Last-Event-ID
header, defined in RFC3864Last-Modified
header, defined in RFC7232- The
Link
header, defined in RFC5988 Location
header, defined in RFC7231- The
Origin
header. Prefer
header, defined in RFC7240Preference-Applied
header, defined in RFC7240- Protocols that appear in the
Upgrade
header field Proxy-Authorization
header, defined in RFC7235- Represents a quality used in quality values.
- Represents an item with a quality value as defined in RFC7231.
- A raw header value.
Referer
header, defined in RFC7231Server
header, defined in RFC7231Set-Cookie
header, defined RFC6265StrictTransportSecurity
header, defined in RFC6797TE
header, defined in RFC7230Transfer-Encoding
header, defined in RFC7230Upgrade
header, defined in RFC7230User-Agent
header, defined in RFC7231Warning
header, defined in RFC7234
Enums§
- The
Access-Control-Allow-Origin
response header, part of CORS - Each
Range::Bytes
header can contain one or moreByteRangeSpecs
. EachByteRangeSpec
defines a range of bytes to fetch CacheControl
contains a list of these directives.- A Mime charset.
- Values that can be in the
Connection
header. - Content-Range, described in RFC7233
- A parameter to the disposition type.
- The implied disposition of the content of the HTTP body.
- A value to represent an encoding used in
Transfer-Encoding
orAccept-Encoding
header. - The
Expect
header. If-Match
header, defined in RFC7232If-None-Match
header, defined in RFC7232If-Range
header, defined in RFC7233- A Media Descriptors Enum based on: https://www.w3.org/TR/html401/types.html#h-6.13
- The
Pragma
header defined by HTTP/1.0. - Prefer contains a list of these preferences.
- A protocol name used to identify a specific protocol. Names are case-sensitive except for the
WebSocket
value. Range
header, defined in RFC7233- Range Units, described in RFC7233
Referrer-Policy
header, part of Referrer Policy- A Link Relation Type Enum based on: RFC5988
- The
Retry-After
header. Vary
header, defined in RFC7231
Traits§
- A trait for any object that will represent a header field and value.
- An Authorization scheme to be used in the header.
Functions§
- Convenience function to create a
Quality
from a float or integer. - Convenience function to wrap a value in a
QualityItem
Setsq
to the default 1.0