-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathlib.rs
More file actions
143 lines (136 loc) · 3.73 KB
/
lib.rs
File metadata and controls
143 lines (136 loc) · 3.73 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(all(test, feature = "nightly"), feature(test))]
#![doc(html_root_url = "https://docs.rs/headers/0.3.8")]
//! # Typed HTTP Headers
//!
//! 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
//!
//! ## 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`.
//!
//! ```
//! extern crate http;
//! extern crate headers;
//!
//! use 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));
//! }
//! }
//! ```
//!
//! Another example; the `x-real-ip` is a header set by some reverse proxies,
//! this sets the IP address where the client send the request from.
//!
//! ```
//! extern crate headers;
//!
//! use headers::{Header, HeaderName, HeaderValue};
//! use std::net::IpAddr;
//!
//! struct RealIp(IpAddr);
//!
//! static REAL_IP: HeaderName = HeaderName::from_static("x-real-ip");
//!
//! impl Header for RealIp {
//! fn name() -> &'static HeaderName {
//! &REAL_IP
//! }
//!
//! 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)?;
//!
//! let real_ip_str =
//! std::str::from_utf8(value.as_bytes()).map_err(|_| headers::Error::invalid())?;
//! let real_ip = real_ip_str.parse().map_err(|_| headers::Error::invalid())?;
//!
//! Ok(RealIp(real_ip))
//! }
//!
//! fn encode<E>(&self, values: &mut E)
//! where
//! E: Extend<HeaderValue>,
//! {
//! let value = HeaderValue::from_str(&self.0.to_string());
//!
//! values.extend(std::iter::once(value.unwrap()));
//! }
//! }
//! ```
extern crate base64;
#[macro_use]
extern crate bitflags;
extern crate bytes;
extern crate headers_core;
extern crate http;
extern crate httpdate;
extern crate mime;
extern crate sha1;
#[cfg(all(test, feature = "nightly"))]
extern crate test;
pub use headers_core::{Error, Header};
#[doc(hidden)]
pub use http::HeaderMap;
#[doc(hidden)]
pub use http::header::{HeaderName, HeaderValue};
#[macro_use]
mod util;
mod common;
mod map_ext;
pub use self::common::*;
pub use self::map_ext::HeaderMapExt;