Expand description
typed http headers
rama 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.
use rama_http::{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));
}
}
Modules§
- Authorization header and types.
Structs§
Accept
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 CORS- The
Access-Control-Allow-Origin
response header, part of CORS Access-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 CORSAge
header, defined in RFC7234Allow
header, defined in RFC7231Authorization
header, defined in RFC7235- CF-Connecting-IP provides the client IP address connecting to Cloudflare to the origin web server.
- Client-Ip is used by some proxy software to set the real client Ip Address (known to them).
Connection
header, defined in RFC7230- A
Content-Disposition
header, (re)defined in RFC6266. Content-Encoding
header, defined in RFC7231Content-Length
header, defined in RFC7230Content-Location
header, defined in RFC7231- Content-Range, described in RFC7233
Content-Type
header, defined in RFC7231Cookie
header, defined in RFC6265Date
header, defined in RFC7231ETag
header, defined in RFC7232- Errors trying to decode a header.
- The
Expect
header. Expires
header, defined in RFC7234- Forwarding information stored as a chain.
- The
Host
header. If-Match
header, defined in RFC7232If-Modified-Since
header, defined in RFC7232If-None-Match
header, defined in RFC7232If-Range
header, defined in RFC7233If-Unmodified-Since
header, defined in RFC7232Last-Modified
header, defined in RFC7232Location
header, defined in RFC7231- The
Origin
header. - The
Pragma
header defined by HTTP/1.0. Proxy-Authorization
header, defined in RFC7235- Represents a quality used in quality values.
- Represents an item with a quality value as defined in RFC7231.
Range
header, defined in RFC7233Referer
header, defined in RFC7231Referrer-Policy
header, part of Referrer Policy- The
Retry-After
header. - The
Sec-Websocket-Accept
header. - The
Sec-Websocket-Key
header. - The
Sec-Websocket-Version
header. Server
header, defined in RFC7231Set-Cookie
header, defined RFC6265StrictTransportSecurity
header, defined in RFC6797TE
header, defined in RFC7230Transfer-Encoding
header, defined in RFC7230- True-Client-IP provides the original client IP address to the origin web server (Cloudflare Enterprise).
Upgrade
header, defined in RFC7230User-Agent
header, defined in RFC7231Vary
header, defined in RFC7231- The Via general header is added by proxies, both forward and reverse.
- X-Client-Ip is used by some proxy software to set the real client Ip Address (known to them).
- The
X-Forwarded-For
(XFF) request header is a de-facto standard header for identifying the originating IP address of a client connecting to a web server through a proxy server. - The X-Forwarded-Host (XFH) header is a de-facto standard header for identifying the original host requested by the client in the Host HTTP request header.
- The X-Forwarded-Proto (XFP) header is a de-facto standard header for identifying the protocol (HTTP or HTTPS) that a client used to connect to your proxy or load balancer.
- X-Real-Ip is used by some proxy software to set the real client Ip Address (known to them).
Traits§
- A trait for types headers that is used by middleware which supports headers that can be converted into and from Forward data.
- A trait for any object that will represent a header field and value.
- An external trait adding helper methods to types which implement
Header
trait. - An extension trait adding “typed” methods to
http::HeaderMap
.