Expand description
Headers container, and common header fields.
mco_http 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 mco_http;
use mco_http::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. But the header system actually involves 2 parts: parsing, and formatting. If you need to customize either part, you can do so.
§Header and HeaderFormat
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 mco_http::header::{Header, HeaderFormat};
#[derive(Debug, Clone, Copy)]
struct Dnt(bool);
impl Header for Dnt {
fn header_name() -> &'static str {
"DNT"
}
fn parse_header(raw: &[Vec<u8>]) -> mco_http::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(mco_http::Error::Header)
}
}
impl HeaderFormat for Dnt {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.0 {
f.write_str("1")
} else {
f.write_str("0")
}
}
}Modules§
- parsing
- Utility functions for Header implementations.
Macros§
Structs§
- Accept
Acceptheader, defined in RFC7231- Accept
Charset Accept-Charsetheader, defined in RFC7231- Accept
Encoding Accept-Encodingheader, defined in RFC7231- Accept
Language Accept-Languageheader, defined in RFC7231- Accept
Ranges Accept-Rangesheader, defined in RFC7233- Access
Control Allow Credentials Access-Control-Allow-Credentialsheader, part of CORS- Access
Control Allow Headers Access-Control-Allow-Headersheader, part of CORS- Access
Control Allow Methods Access-Control-Allow-Methodsheader, part of CORS- Access
Control Expose Headers Access-Control-Expose-Headersheader, part of CORS- Access
Control MaxAge Access-Control-Max-Ageheader, part of CORS- Access
Control Request Headers Access-Control-Request-Headersheader, part of CORS- Access
Control Request Method Access-Control-Request-Methodheader, part of CORS- Allow
Allowheader, defined in RFC7231- Authorization
Authorizationheader, defined in RFC7235- Basic
- Credential holder for Basic Authentication
- Bearer
- Token holder for Bearer Authentication, most often seen with oauth
- Cache
Control Cache-Controlheader, defined in RFC7234- Connection
Connectionheader, defined in RFC7230- Content
Disposition - A
Content-Dispositionheader, (re)defined in RFC6266 - Content
Encoding Content-Encodingheader, defined in RFC7231- Content
Language Content-Languageheader, defined in RFC7231- Content
Length Content-Lengthheader, defined in RFC7230- Content
Range Content-Rangeheader, defined in RFC7233- Content
Type Content-Typeheader, defined in RFC7231- Cookie
Cookieheader, defined in RFC6265- Date
Dateheader, defined in RFC7231- ETag
ETagheader, defined in RFC7232- Entity
Tag - An entity tag, defined in RFC7232
- Expires
Expiresheader, defined in RFC7234- From
Fromheader, defined in RFC7231- Header
Formatter Deprecated - A wrapper around any Header with a Display impl that calls fmt_header.
- Header
View - Returned with the
HeadersItemsiterator. - Headers
- A map of header fields on requests and responses.
- Headers
Items - An
Iteratorover the fields in aHeadersmap. - Host
- The
Hostheader. - Http
Date - A
time::Timewith HTTP formatting and parsing - IfModified
Since If-Modified-Sinceheader, defined in RFC7232- IfUnmodified
Since If-Unmodified-Sinceheader, defined in RFC7232- Last
Modified Last-Modifiedheader, defined in RFC7232- Link
- The
Linkheader, defined in RFC5988 - Link
Value - A single
link-valueof aLinkheader, based on: RFC5988 - Location
Locationheader, defined in RFC7231- Origin
- The
Originheader. - Prefer
Preferheader, defined in RFC7240- Preference
Applied Preference-Appliedheader, defined in RFC7240- Protocol
- Protocols that appear in the
Upgradeheader field - Quality
- Represents a quality used in quality values.
- Quality
Item - Represents an item with a quality value as defined in RFC7231.
- Referer
Refererheader, defined in RFC7231- Server
Serverheader, defined in RFC7231- SetCookie
Set-Cookieheader, defined RFC6265- Strict
Transport Security StrictTransportSecurityheader, defined in RFC6797- Transfer
Encoding Transfer-Encodingheader, defined in RFC7230- Upgrade
Upgradeheader, defined in RFC7230- User
Agent User-Agentheader, defined in RFC7231
Enums§
- Access
Control Allow Origin - The
Access-Control-Allow-Originresponse header, part of CORS - Byte
Range Spec - Each
Range::Bytesheader can contain one or moreByteRangeSpecs. EachByteRangeSpecdefines a range of bytes to fetch - Cache
Directive CacheControlcontains a list of these directives.- Charset
- A Mime charset.
- Connection
Option - Values that can be in the
Connectionheader. - Content
Range Spec - Content-Range, described in RFC7233
- Disposition
Param - A parameter to the disposition type
- Disposition
Type - The implied disposition of the content of the HTTP body
- Encoding
- A value to represent an encoding used in
Transfer-EncodingorAccept-Encodingheader. - Expect
- The
Expectheader. - IfMatch
If-Matchheader, defined in RFC7232- IfNone
Match If-None-Matchheader, defined in RFC7232- IfRange
If-Rangeheader, defined in RFC7233- Media
Desc - A Media Descriptors Enum based on: https://www.w3.org/TR/html401/types.html#h-6.13
- Pragma
- The
Pragmaheader defined by HTTP/1.0. - Preference
- Prefer contains a list of these preferences.
- Protocol
Name - A protocol name used to identify a spefic protocol. Names are case-sensitive
except for the
WebSocketvalue. - Range
Rangeheader, defined in RFC7233- Range
Unit - Range Units, described in RFC7233
- Referrer
Policy Referrer-Policyheader, part of Referrer Policy- Relation
Type - A Link Relation Type Enum based on: RFC5988
- Vary
Varyheader, defined in RFC7231
Traits§
- Header
- A trait for any object that will represent a header field and value.
- Header
Clone - Internal implementation detail.
- Header
Format - A trait for any object that will represent a header field and value.
- Scheme
- An Authorization scheme to be used in the header.