rama_http/headers/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//! 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.
//!
//! ```rust
//! 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));
//! }
//! }
//! ```
#[doc(inline)]
pub use rama_http_types::headers::{Header, HeaderMapExt};
#[doc(inline)]
pub use rama_http_types::headers::{
AcceptRanges, AccessControlAllowCredentials, AccessControlAllowHeaders,
AccessControlAllowMethods, AccessControlAllowOrigin, AccessControlExposeHeaders,
AccessControlMaxAge, AccessControlRequestHeaders, AccessControlRequestMethod, Age, Allow,
Authorization, CacheControl, Connection, ContentDisposition, ContentEncoding, ContentLength,
ContentLocation, ContentRange, ContentType, Cookie, Date, ETag, Error, Expect, Expires, Host,
IfMatch, IfModifiedSince, IfNoneMatch, IfRange, IfUnmodifiedSince, LastModified, Location,
Origin, Pragma, ProxyAuthorization, Range, Referer, ReferrerPolicy, RetryAfter,
SecWebsocketAccept, SecWebsocketKey, SecWebsocketVersion, Server, SetCookie,
StrictTransportSecurity, Te, TransferEncoding, Upgrade, UserAgent, Vary,
};
mod common;
#[doc(inline)]
pub use common::Accept;
mod forwarded;
#[doc(inline)]
pub use forwarded::{
CFConnectingIp, ClientIp, ForwardHeader, Forwarded, TrueClientIp, Via, XClientIp,
XForwardedFor, XForwardedHost, XForwardedProto, XRealIp,
};
pub mod authorization {
//! Authorization header and types.
#[doc(inline)]
pub use ::headers::authorization::Credentials;
#[doc(inline)]
pub use ::headers::authorization::{Authorization, Basic, Bearer};
}
#[doc(inline)]
pub use ::rama_http_types::headers::HeaderExt;
pub(crate) mod util;
pub use util::quality_value::{Quality, QualityValue};