Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add another custom header example #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,48 @@
//! }
//! }
//! ```
//!
//! 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]
Expand Down