Skip to main content

ntex_http/
header.rs

1//! Various http headers
2pub use crate::HeaderMap;
3#[doc(hidden)]
4pub use crate::map::{AsName, Either, GetAll, Iter, Value};
5pub use crate::value::{HeaderValue, InvalidHeaderValue, ToStrError};
6
7pub use http::header::{
8    ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
9    ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS,
10    ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
11    ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC,
12    AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING,
13    CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE, CONTENT_SECURITY_POLICY,
14    CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE, DATE, DNT, ETAG, EXPECT, EXPIRES,
15    FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE,
16    IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA,
17    PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY, RANGE,
18    REFERER, REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT, SEC_WEBSOCKET_EXTENSIONS,
19    SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL, SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE,
20    STRICT_TRANSPORT_SECURITY, TE, TRAILER, TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS,
21    USER_AGENT, VARY, VIA, WARNING, WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS,
22    X_DNS_PREFETCH_CONTROL, X_FRAME_OPTIONS, X_XSS_PROTECTION,
23};
24pub use http::header::{HeaderName, InvalidHeaderName};
25
26/// Represents supported types of content encodings
27#[derive(Copy, Clone, PartialEq, Eq, Debug)]
28pub enum ContentEncoding {
29    /// Automatically select encoding based on encoding negotiation
30    Auto,
31    /// A format using the Brotli algorithm
32    Br,
33    /// A format using the zlib structure with deflate algorithm
34    Deflate,
35    /// Gzip algorithm
36    Gzip,
37    /// Indicates the identity function (i.e. no compression, nor modification)
38    Identity,
39}
40
41impl ContentEncoding {
42    #[inline]
43    /// Is the content compressed?
44    pub fn is_compressed(self) -> bool {
45        !matches!(self, ContentEncoding::Identity | ContentEncoding::Auto)
46    }
47
48    #[inline]
49    /// Convert content encoding to string
50    pub fn as_str(self) -> &'static str {
51        match self {
52            ContentEncoding::Br => "br",
53            ContentEncoding::Gzip => "gzip",
54            ContentEncoding::Deflate => "deflate",
55            ContentEncoding::Identity | ContentEncoding::Auto => "identity",
56        }
57    }
58
59    #[inline]
60    /// default quality value
61    pub fn quality(self) -> f64 {
62        match self {
63            ContentEncoding::Br => 1.1,
64            ContentEncoding::Gzip => 1.0,
65            ContentEncoding::Deflate => 0.9,
66            ContentEncoding::Identity | ContentEncoding::Auto => 0.1,
67        }
68    }
69}
70
71impl<'a> From<&'a str> for ContentEncoding {
72    fn from(s: &'a str) -> ContentEncoding {
73        let s = s.trim();
74
75        if s.eq_ignore_ascii_case("br") {
76            ContentEncoding::Br
77        } else if s.eq_ignore_ascii_case("gzip") {
78            ContentEncoding::Gzip
79        } else if s.eq_ignore_ascii_case("deflate") {
80            ContentEncoding::Deflate
81        } else {
82            ContentEncoding::Identity
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn encoding() {
93        assert!(ContentEncoding::Br.is_compressed());
94        assert!(!ContentEncoding::Identity.is_compressed());
95        assert!(!ContentEncoding::Auto.is_compressed());
96        assert_eq!(format!("{:?}", ContentEncoding::Identity), "Identity");
97    }
98}