sark-http 0.26.2

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::borrow::Cow;

/// Canonical standard field-name vocabulary shared by owned headers and H1.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub(in crate::field) enum Known {
    Accept,
    AcceptCharset,
    AcceptEncoding,
    AcceptLanguage,
    AcceptRanges,
    AccessControlAllowCredentials,
    AccessControlAllowHeaders,
    AccessControlAllowMethods,
    AccessControlAllowOrigin,
    AccessControlExposeHeaders,
    AccessControlMaxAge,
    AccessControlRequestHeaders,
    AccessControlRequestMethod,
    Age,
    Allow,
    AltSvc,
    Authorization,
    CacheControl,
    CacheStatus,
    CdnCacheControl,
    Connection,
    ContentDisposition,
    ContentEncoding,
    ContentLanguage,
    ContentLength,
    ContentLocation,
    ContentRange,
    ContentSecurityPolicy,
    ContentSecurityPolicyReportOnly,
    ContentType,
    Cookie,
    Date,
    Dnt,
    Etag,
    Expect,
    Expires,
    Forwarded,
    From,
    Host,
    IfMatch,
    IfModifiedSince,
    IfNoneMatch,
    IfRange,
    IfUnmodifiedSince,
    KeepAlive,
    LastModified,
    Link,
    Location,
    MaxForwards,
    Origin,
    Pragma,
    ProxyAuthenticate,
    ProxyAuthorization,
    ProxyConnection,
    PublicKeyPins,
    PublicKeyPinsReportOnly,
    Range,
    Referer,
    ReferrerPolicy,
    Refresh,
    RetryAfter,
    SecWebSocketAccept,
    SecWebSocketExtensions,
    SecWebSocketKey,
    SecWebSocketProtocol,
    SecWebSocketVersion,
    Server,
    SetCookie,
    StrictTransportSecurity,
    Te,
    Trailer,
    TransferEncoding,
    Upgrade,
    UpgradeInsecureRequests,
    UserAgent,
    Vary,
    Via,
    Warning,
    WwwAuthenticate,
    XContentTypeOptions,
    XDnsPrefetchControl,
    XFrameOptions,
    XXssProtection,
}

impl Known {
    pub(in crate::field) const fn as_str<'a>(&self) -> &'a str {
        match self {
            Self::Accept => "accept",
            Self::AcceptCharset => "accept-charset",
            Self::AcceptEncoding => "accept-encoding",
            Self::AcceptLanguage => "accept-language",
            Self::AcceptRanges => "accept-ranges",
            Self::AccessControlAllowCredentials => "access-control-allow-credentials",
            Self::AccessControlAllowHeaders => "access-control-allow-headers",
            Self::AccessControlAllowMethods => "access-control-allow-methods",
            Self::AccessControlAllowOrigin => "access-control-allow-origin",
            Self::AccessControlExposeHeaders => "access-control-expose-headers",
            Self::AccessControlMaxAge => "access-control-max-age",
            Self::AccessControlRequestHeaders => "access-control-request-headers",
            Self::AccessControlRequestMethod => "access-control-request-method",
            Self::Age => "age",
            Self::Allow => "allow",
            Self::AltSvc => "alt-svc",
            Self::Authorization => "authorization",
            Self::CacheControl => "cache-control",
            Self::CacheStatus => "cache-status",
            Self::CdnCacheControl => "cdn-cache-control",
            Self::Connection => "connection",
            Self::ContentDisposition => "content-disposition",
            Self::ContentEncoding => "content-encoding",
            Self::ContentLanguage => "content-language",
            Self::ContentLength => "content-length",
            Self::ContentLocation => "content-location",
            Self::ContentRange => "content-range",
            Self::ContentSecurityPolicy => "content-security-policy",
            Self::ContentSecurityPolicyReportOnly => "content-security-policy-report-only",
            Self::ContentType => "content-type",
            Self::Cookie => "cookie",
            Self::Date => "date",
            Self::Dnt => "dnt",
            Self::Etag => "etag",
            Self::Expect => "expect",
            Self::Expires => "expires",
            Self::Forwarded => "forwarded",
            Self::From => "from",
            Self::Host => "host",
            Self::IfMatch => "if-match",
            Self::IfModifiedSince => "if-modified-since",
            Self::IfNoneMatch => "if-none-match",
            Self::IfRange => "if-range",
            Self::IfUnmodifiedSince => "if-unmodified-since",
            Self::KeepAlive => "keep-alive",
            Self::LastModified => "last-modified",
            Self::Link => "link",
            Self::Location => "location",
            Self::MaxForwards => "max-forwards",
            Self::Origin => "origin",
            Self::Pragma => "pragma",
            Self::ProxyAuthenticate => "proxy-authenticate",
            Self::ProxyAuthorization => "proxy-authorization",
            Self::ProxyConnection => "proxy-connection",
            Self::PublicKeyPins => "public-key-pins",
            Self::PublicKeyPinsReportOnly => "public-key-pins-report-only",
            Self::Range => "range",
            Self::Referer => "referer",
            Self::ReferrerPolicy => "referrer-policy",
            Self::Refresh => "refresh",
            Self::RetryAfter => "retry-after",
            Self::SecWebSocketAccept => "sec-websocket-accept",
            Self::SecWebSocketExtensions => "sec-websocket-extensions",
            Self::SecWebSocketKey => "sec-websocket-key",
            Self::SecWebSocketProtocol => "sec-websocket-protocol",
            Self::SecWebSocketVersion => "sec-websocket-version",
            Self::Server => "server",
            Self::SetCookie => "set-cookie",
            Self::StrictTransportSecurity => "strict-transport-security",
            Self::Te => "te",
            Self::Trailer => "trailer",
            Self::TransferEncoding => "transfer-encoding",
            Self::Upgrade => "upgrade",
            Self::UpgradeInsecureRequests => "upgrade-insecure-requests",
            Self::UserAgent => "user-agent",
            Self::Vary => "vary",
            Self::Via => "via",
            Self::Warning => "warning",
            Self::WwwAuthenticate => "www-authenticate",
            Self::XContentTypeOptions => "x-content-type-options",
            Self::XDnsPrefetchControl => "x-dns-prefetch-control",
            Self::XFrameOptions => "x-frame-options",
            Self::XXssProtection => "x-xss-protection",
        }
    }

    pub(in crate::field) fn from_bytes(name: &[u8]) -> Option<Self> {
        let mut lower = [0_u8; 35];
        let bytes = lower.get_mut(..name.len())?;
        for (out, byte) in bytes.iter_mut().zip(name) {
            *out = byte.to_ascii_lowercase();
        }
        match &*bytes {
            b"accept" => Some(Self::Accept),
            b"accept-charset" => Some(Self::AcceptCharset),
            b"accept-encoding" => Some(Self::AcceptEncoding),
            b"accept-language" => Some(Self::AcceptLanguage),
            b"accept-ranges" => Some(Self::AcceptRanges),
            b"access-control-allow-credentials" => Some(Self::AccessControlAllowCredentials),
            b"access-control-allow-headers" => Some(Self::AccessControlAllowHeaders),
            b"access-control-allow-methods" => Some(Self::AccessControlAllowMethods),
            b"access-control-allow-origin" => Some(Self::AccessControlAllowOrigin),
            b"access-control-expose-headers" => Some(Self::AccessControlExposeHeaders),
            b"access-control-max-age" => Some(Self::AccessControlMaxAge),
            b"access-control-request-headers" => Some(Self::AccessControlRequestHeaders),
            b"access-control-request-method" => Some(Self::AccessControlRequestMethod),
            b"age" => Some(Self::Age),
            b"allow" => Some(Self::Allow),
            b"alt-svc" => Some(Self::AltSvc),
            b"authorization" => Some(Self::Authorization),
            b"cache-control" => Some(Self::CacheControl),
            b"cache-status" => Some(Self::CacheStatus),
            b"cdn-cache-control" => Some(Self::CdnCacheControl),
            b"connection" => Some(Self::Connection),
            b"content-disposition" => Some(Self::ContentDisposition),
            b"content-encoding" => Some(Self::ContentEncoding),
            b"content-language" => Some(Self::ContentLanguage),
            b"content-length" => Some(Self::ContentLength),
            b"content-location" => Some(Self::ContentLocation),
            b"content-range" => Some(Self::ContentRange),
            b"content-security-policy" => Some(Self::ContentSecurityPolicy),
            b"content-security-policy-report-only" => Some(Self::ContentSecurityPolicyReportOnly),
            b"content-type" => Some(Self::ContentType),
            b"cookie" => Some(Self::Cookie),
            b"date" => Some(Self::Date),
            b"dnt" => Some(Self::Dnt),
            b"etag" => Some(Self::Etag),
            b"expect" => Some(Self::Expect),
            b"expires" => Some(Self::Expires),
            b"forwarded" => Some(Self::Forwarded),
            b"from" => Some(Self::From),
            b"host" => Some(Self::Host),
            b"if-match" => Some(Self::IfMatch),
            b"if-modified-since" => Some(Self::IfModifiedSince),
            b"if-none-match" => Some(Self::IfNoneMatch),
            b"if-range" => Some(Self::IfRange),
            b"if-unmodified-since" => Some(Self::IfUnmodifiedSince),
            b"keep-alive" => Some(Self::KeepAlive),
            b"last-modified" => Some(Self::LastModified),
            b"link" => Some(Self::Link),
            b"location" => Some(Self::Location),
            b"max-forwards" => Some(Self::MaxForwards),
            b"origin" => Some(Self::Origin),
            b"pragma" => Some(Self::Pragma),
            b"proxy-authenticate" => Some(Self::ProxyAuthenticate),
            b"proxy-authorization" => Some(Self::ProxyAuthorization),
            b"proxy-connection" => Some(Self::ProxyConnection),
            b"public-key-pins" => Some(Self::PublicKeyPins),
            b"public-key-pins-report-only" => Some(Self::PublicKeyPinsReportOnly),
            b"range" => Some(Self::Range),
            b"referer" => Some(Self::Referer),
            b"referrer-policy" => Some(Self::ReferrerPolicy),
            b"refresh" => Some(Self::Refresh),
            b"retry-after" => Some(Self::RetryAfter),
            b"sec-websocket-accept" => Some(Self::SecWebSocketAccept),
            b"sec-websocket-extensions" => Some(Self::SecWebSocketExtensions),
            b"sec-websocket-key" => Some(Self::SecWebSocketKey),
            b"sec-websocket-protocol" => Some(Self::SecWebSocketProtocol),
            b"sec-websocket-version" => Some(Self::SecWebSocketVersion),
            b"server" => Some(Self::Server),
            b"set-cookie" => Some(Self::SetCookie),
            b"strict-transport-security" => Some(Self::StrictTransportSecurity),
            b"te" => Some(Self::Te),
            b"trailer" => Some(Self::Trailer),
            b"transfer-encoding" => Some(Self::TransferEncoding),
            b"upgrade" => Some(Self::Upgrade),
            b"upgrade-insecure-requests" => Some(Self::UpgradeInsecureRequests),
            b"user-agent" => Some(Self::UserAgent),
            b"vary" => Some(Self::Vary),
            b"via" => Some(Self::Via),
            b"warning" => Some(Self::Warning),
            b"www-authenticate" => Some(Self::WwwAuthenticate),
            b"x-content-type-options" => Some(Self::XContentTypeOptions),
            b"x-dns-prefetch-control" => Some(Self::XDnsPrefetchControl),
            b"x-frame-options" => Some(Self::XFrameOptions),
            b"x-xss-protection" => Some(Self::XXssProtection),
            _ => None,
        }
    }
}

const _: () = assert!(core::mem::size_of::<Known>() == 1);
const _: () = assert!(core::mem::size_of::<Option<Known>>() == 1);

impl<'field> From<Known> for Cow<'field, str> {
    fn from(name: Known) -> Self {
        Self::Borrowed(name.as_str())
    }
}