2021-04-19 04:29:38 +02:00
|
|
|
//! Pre-defined `HeaderName`s, traits for parsing and conversion, and other header utility methods.
|
2019-02-07 22:24:24 +01:00
|
|
|
|
2019-08-13 19:48:11 +02:00
|
|
|
use percent_encoding::{AsciiSet, CONTROLS};
|
2019-02-07 22:24:24 +01:00
|
|
|
|
2021-04-19 04:29:38 +02:00
|
|
|
// re-export from http except header map related items
|
|
|
|
pub use http::header::{
|
|
|
|
HeaderName, HeaderValue, InvalidHeaderName, InvalidHeaderValue, ToStrError,
|
|
|
|
};
|
|
|
|
|
|
|
|
// re-export const header names
|
|
|
|
pub use http::header::{
|
|
|
|
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
|
|
|
|
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
|
|
|
|
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
|
|
|
|
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
|
|
|
|
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC,
|
|
|
|
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING,
|
|
|
|
CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE,
|
|
|
|
CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE,
|
|
|
|
DATE, DNT, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH,
|
|
|
|
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
|
|
|
|
LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA, PROXY_AUTHENTICATE,
|
|
|
|
PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY, RANGE, REFERER,
|
|
|
|
REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
|
|
|
|
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
|
|
|
|
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
|
|
|
|
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA,
|
|
|
|
WARNING, WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL,
|
|
|
|
X_FRAME_OPTIONS, X_XSS_PROTECTION,
|
|
|
|
};
|
2019-02-07 22:24:24 +01:00
|
|
|
|
2021-11-28 20:23:29 +01:00
|
|
|
use crate::{error::ParseError, HttpMessage};
|
2019-02-07 22:24:24 +01:00
|
|
|
|
2021-02-09 23:59:17 +01:00
|
|
|
mod as_name;
|
2021-01-15 03:11:10 +01:00
|
|
|
mod into_pair;
|
|
|
|
mod into_value;
|
2021-11-29 03:22:47 +01:00
|
|
|
pub mod map;
|
2019-02-07 22:24:24 +01:00
|
|
|
mod shared;
|
2021-11-28 20:23:29 +01:00
|
|
|
mod utils;
|
2021-01-15 03:11:10 +01:00
|
|
|
|
2019-02-07 22:24:24 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub use self::shared::*;
|
|
|
|
|
2021-02-09 23:59:17 +01:00
|
|
|
pub use self::as_name::AsHeaderName;
|
2021-01-15 03:11:10 +01:00
|
|
|
pub use self::into_pair::IntoHeaderPair;
|
|
|
|
pub use self::into_value::IntoHeaderValue;
|
2021-11-29 03:22:47 +01:00
|
|
|
pub use self::map::HeaderMap;
|
2021-11-28 20:23:29 +01:00
|
|
|
pub use self::utils::{
|
|
|
|
fmt_comma_delimited, from_comma_delimited, from_one_raw_str, http_percent_encode,
|
|
|
|
};
|
2019-04-07 00:02:02 +02:00
|
|
|
|
2021-11-29 03:22:47 +01:00
|
|
|
/// An interface for types that already represent a valid header.
|
2021-01-15 03:11:10 +01:00
|
|
|
pub trait Header: IntoHeaderValue {
|
2019-02-07 22:24:24 +01:00
|
|
|
/// Returns the name of the header field
|
|
|
|
fn name() -> HeaderName;
|
|
|
|
|
|
|
|
/// Parse a header
|
2021-12-02 14:59:25 +01:00
|
|
|
fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError>;
|
2019-02-07 22:24:24 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
2019-04-07 00:02:02 +02:00
|
|
|
impl From<http::HeaderMap> for HeaderMap {
|
2021-02-09 23:59:17 +01:00
|
|
|
fn from(mut map: http::HeaderMap) -> HeaderMap {
|
|
|
|
HeaderMap::from_drain(map.drain())
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 06:38:50 +01:00
|
|
|
/// This encode set is used for HTTP header values and is defined at
|
2021-12-02 04:45:04 +01:00
|
|
|
/// <https://datatracker.ietf.org/doc/html/rfc5987#section-3.2>.
|
2019-08-13 19:48:11 +02:00
|
|
|
pub(crate) const HTTP_VALUE: &AsciiSet = &CONTROLS
|
|
|
|
.add(b' ')
|
|
|
|
.add(b'"')
|
|
|
|
.add(b'%')
|
|
|
|
.add(b'\'')
|
|
|
|
.add(b'(')
|
|
|
|
.add(b')')
|
|
|
|
.add(b'*')
|
|
|
|
.add(b',')
|
|
|
|
.add(b'/')
|
|
|
|
.add(b':')
|
|
|
|
.add(b';')
|
|
|
|
.add(b'<')
|
|
|
|
.add(b'-')
|
|
|
|
.add(b'>')
|
|
|
|
.add(b'?')
|
|
|
|
.add(b'[')
|
|
|
|
.add(b'\\')
|
|
|
|
.add(b']')
|
|
|
|
.add(b'{')
|
|
|
|
.add(b'}');
|