rama_http/headers/mod.rs
1//! typed http headers
2//!
3//! rama has the opinion that headers should be strongly-typed,
4//! because that’s why we’re using Rust in the first place. To set or get any header,
5//! an object must implement the Header trait from this module.
6//! Several common headers are already provided, such as Host, ContentType, UserAgent, and others.
7//!
8//! ## Why typed?
9//!
10//! Or, why not stringly-typed? Types give the following advantages:
11//! - More difficult to typo, since typos in types should be caught by the compiler
12//! - Parsing to a proper type by default
13//!
14//! ## Defining Custom Headers
15//!
16//! ### Implementing the [`Header`] trait
17//!
18//! Consider a Do Not Track header. It can be true or false,
19//! but it represents that via the numerals 1 and 0.
20//!
21//! ```rust
22//! use rama_http::{headers::Header, HeaderName, HeaderValue};
23//!
24//! struct Dnt(bool);
25//!
26//! impl Header for Dnt {
27//! fn name() -> &'static HeaderName {
28//! &http::header::DNT
29//! }
30//!
31//! fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
32//! where
33//! I: Iterator<Item = &'i HeaderValue>,
34//! {
35//! let value = values
36//! .next()
37//! .ok_or_else(headers::Error::invalid)?;
38//!
39//! if value == "0" {
40//! Ok(Dnt(false))
41//! } else if value == "1" {
42//! Ok(Dnt(true))
43//! } else {
44//! Err(headers::Error::invalid())
45//! }
46//! }
47//!
48//! fn encode<E>(&self, values: &mut E)
49//! where
50//! E: Extend<HeaderValue>,
51//! {
52//! let s = if self.0 {
53//! "1"
54//! } else {
55//! "0"
56//! };
57//!
58//! let value = HeaderValue::from_static(s);
59//!
60//! values.extend(std::iter::once(value));
61//! }
62//! }
63//! ```
64
65#[doc(inline)]
66pub use rama_http_types::headers::{Header, HeaderMapExt};
67
68#[doc(inline)]
69pub use rama_http_types::headers::{
70 AcceptRanges, AccessControlAllowCredentials, AccessControlAllowHeaders,
71 AccessControlAllowMethods, AccessControlAllowOrigin, AccessControlExposeHeaders,
72 AccessControlMaxAge, AccessControlRequestHeaders, AccessControlRequestMethod, Age, Allow,
73 Authorization, CacheControl, Connection, ContentDisposition, ContentEncoding, ContentLength,
74 ContentLocation, ContentRange, ContentType, Cookie, Date, ETag, Error, Expect, Expires, Host,
75 IfMatch, IfModifiedSince, IfNoneMatch, IfRange, IfUnmodifiedSince, LastModified, Location,
76 Origin, Pragma, ProxyAuthorization, Range, Referer, ReferrerPolicy, RetryAfter,
77 SecWebsocketAccept, SecWebsocketKey, SecWebsocketVersion, Server, SetCookie,
78 StrictTransportSecurity, Te, TransferEncoding, Upgrade, UserAgent, Vary,
79};
80
81mod common;
82#[doc(inline)]
83pub use common::Accept;
84
85mod forwarded;
86#[doc(inline)]
87pub use forwarded::{
88 CFConnectingIp, ClientIp, ForwardHeader, Forwarded, TrueClientIp, Via, XClientIp,
89 XForwardedFor, XForwardedHost, XForwardedProto, XRealIp,
90};
91
92pub mod authorization {
93 //! Authorization header and types.
94
95 #[doc(inline)]
96 pub use ::headers::authorization::Credentials;
97 #[doc(inline)]
98 pub use ::headers::authorization::{Authorization, Basic, Bearer};
99}
100
101#[doc(inline)]
102pub use ::rama_http_types::headers::HeaderExt;
103
104pub(crate) mod util;
105pub use util::quality_value::{Quality, QualityValue};