pub struct Header {
pub name: Cow<'static, str>,
pub value: Cow<'static, str>,
}Expand description
An HTTP header name/value pair.
Use the associated string constants (e.g. Header::_CONTENT_TYPE) for header names.
Header::get_header_list returns the standard set of response headers (client hints,
CORS, cache control, security headers) that should be included on every response.
Name and value are Cow<'static, str> (18.0.0; String before): the
built-in names and fixed values are borrowed &'static strs, so building
and cloning the standard header set allocates nothing. Construct with
Header::new (or .into() on each field); read through Deref<Target = str> (&*header.value, header.value.trim(), header.value == "x").
Fields§
§name: Cow<'static, str>Header name, e.g. "Content-Type".
value: Cow<'static, str>Header value, e.g. "application/json".
Implementations§
Source§impl Header
impl Header
pub const _ACCESS_CONTROL_REQUEST_METHOD: &'static str = "Access-Control-Request-Method"
pub const _ACCESS_CONTROL_REQUEST_HEADERS: &'static str = "Access-Control-Request-Headers"
pub const _ACCESS_CONTROL_ALLOW_ORIGIN: &'static str = "Access-Control-Allow-Origin"
pub const _ACCESS_CONTROL_ALLOW_METHODS: &'static str = "Access-Control-Allow-Methods"
pub const _ACCESS_CONTROL_ALLOW_HEADERS: &'static str = "Access-Control-Allow-Headers"
pub const _ACCESS_CONTROL_ALLOW_CREDENTIALS: &'static str = "Access-Control-Allow-Credentials"
pub const _ACCESS_CONTROL_MAX_AGE: &'static str = "Access-Control-Max-Age"
pub const _ACCESS_CONTROL_EXPOSE_HEADERS: &'static str = "Access-Control-Expose-Headers"
pub const _ACCEPT: &'static str = "Accept"
pub const _ACCEPT_RANGES: &'static str = "Accept-Ranges"
pub const _ACCEPT_CH: &'static str = "Accept-CH"
pub const _ACCEPT_ENCODING: &'static str = "Accept-Encoding"
pub const _ACCEPT_LANGUAGE: &'static str = "Accept-Language"
pub const _ACCEPT_PATCH: &'static str = "Accept-Patch"
pub const _ACCEPT_POST: &'static str = "Accept-Post"
pub const _AGE: &'static str = "Age"
pub const _ALLOW: &'static str = "Allow"
pub const _ALT_SVC: &'static str = "Alt-Svc"
pub const _AUTHORIZATION: &'static str = "Authorization"
pub const _CACHE_CONTROL: &'static str = "Cache-Control"
pub const _CONNECTION: &'static str = "Connection"
pub const _CLEAR_SITE_DATA: &'static str = "Clear-Site-Data"
pub const _CONTENT_TYPE: &'static str = "Content-Type"
pub const _CONTENT_LENGTH: &'static str = "Content-Length"
pub const _CONTENT_RANGE: &'static str = "Content-Range"
pub const _CONTENT_DISPOSITION: &'static str = "Content-Disposition"
pub const _CONTENT_ENCODING: &'static str = "Content-Encoding"
pub const _CONTENT_LANGUAGE: &'static str = "Content-Language"
pub const _CONTENT_LOCATION: &'static str = "Content-Location"
pub const _CONTENT_SECURITY_POLICY: &'static str = "Content-Security-Policy"
pub const _CONTENT_SECURITY_POLICY_REPORT_ONLY: &'static str = "Content-Security-Policy-Report-Only"
pub const _COOKIE: &'static str = "Cookie"
pub const _CROSS_ORIGIN_EMBEDDER_POLICY: &'static str = "Cross-Origin-Embedder-Policy"
pub const _CROSS_ORIGIN_OPENER_POLICY: &'static str = "Cross-Origin-Opener-Policy"
pub const _CROSS_ORIGIN_RESOURCE_POLICY: &'static str = "Cross-Origin-Resource-Policy"
pub const _DATE: &'static str = "Date"
pub const _DATE_UNIX_EPOCH_NANOS: &'static str = "Date-Unix-Epoch-Nanos"
pub const _DEVICE_MEMORY: &'static str = "Device-Memory"
pub const _DIGEST: &'static str = "Digest"
pub const _DOWNLINK: &'static str = "Downlink"
pub const _EARLY_DATA: &'static str = "Early-Data"
pub const _ECT: &'static str = "ECT"
pub const _ETAG: &'static str = "ETag"
pub const _EXPECT: &'static str = "Expect"
pub const _EXPIRES: &'static str = "Expires"
pub const _FEATURE_POLICY: &'static str = "Feature-Policy"
pub const _PERMISSIONS_POLICY: &'static str = "Permissions-Policy"
pub const _FORWARDED: &'static str = "Forwarded"
pub const _FROM: &'static str = "From"
pub const _HOST: &'static str = "Host"
pub const _IF_MATCH: &'static str = "If-Match"
pub const _IF_MODIFIED_SINCE: &'static str = "If-Modified-Since"
pub const _IF_NONE_MATCH: &'static str = "If-None-Match"
pub const _IF_RANGE: &'static str = "If-Range"
pub const _IF_UNMODIFIED_SINCE: &'static str = "If-Unmodified-Since"
pub const _LAST_MODIFIED: &'static str = "Last-Modified"
pub const _LAST_MODIFIED_UNIX_EPOCH_NANOS: &'static str = "Last-Modified-Unix-Epoch-Nanos"
pub const _LINK: &'static str = "Link"
pub const _LOCATION: &'static str = "Location"
pub const _MAX_FORWARDS: &'static str = "Max-Forwards"
pub const _NEL: &'static str = "NEL"
pub const _ORIGIN: &'static str = "Origin"
pub const _PROXY_AUTHENTICATE: &'static str = "Proxy-Authenticate"
pub const _PROXY_AUTHORIZATION: &'static str = "Proxy-Authorization"
pub const _RANGE: &'static str = "Range"
pub const _REFERER: &'static str = "Referer"
pub const _REFERRER_POLICY: &'static str = "Referrer-Policy"
pub const _RETRY_AFTER: &'static str = "Retry-After"
pub const _RTT: &'static str = "RTT"
pub const _SAVE_DATA: &'static str = "Save-Data"
pub const _SEC_CH_UA: &'static str = "Sec-CH-UA"
pub const _SEC_CH_UA_ARCH: &'static str = "Sec-CH-UA-Arch"
pub const _SEC_CH_UA_BITNESS: &'static str = "Sec-CH-UA-Bitness"
pub const _SEC_CH_UA_FULL_VERSION_LIST: &'static str = "Sec-CH-UA-Full-Version-List"
pub const _SEC_CH_UA_MOBILE: &'static str = "Sec-CH-UA-Mobile"
pub const _SEC_CH_UA_MODEL: &'static str = "Sec-CH-UA-Model"
pub const _SEC_CH_UA_PLATFORM: &'static str = "Sec-CH-UA-Platform"
pub const _SEC_CH_UA_PLATFORM_VERSION: &'static str = "Sec-CH-UA-Platform-Version"
pub const _SEC_FETCH_DEST: &'static str = "Sec-Fetch-Dest"
pub const _SEC_FETCH_MODE: &'static str = "Sec-Fetch-Mode"
pub const _SEC_FETCH_SITE: &'static str = "Sec-Fetch-Site"
pub const _SEC_FETCH_USER: &'static str = "Sec-Fetch-User"
pub const _SEC_GPC: &'static str = "Sec-GPC"
pub const _SERVER: &'static str = "Server"
pub const _SERVER_TIMING: &'static str = "Server-Timing"
pub const _SERVICE_WORKER_NAVIGATION_PRELOAD: &'static str = "Service-Worker-Navigation-Preload"
pub const _SET_COOKIE: &'static str = "Set-Cookie"
pub const _SOURCE_MAP: &'static str = "SourceMap"
pub const _STRICT_TRANSPORT_SECURITY: &'static str = "Strict-Transport-Security"
pub const _TE: &'static str = "TE"
pub const _TIMING_ALLOW_ORIGIN: &'static str = "Timing-Allow-Origin"
pub const _TRAILER: &'static str = "Trailer"
pub const _TRANSFER_ENCODING: &'static str = "Transfer-Encoding"
pub const _UPGRADE: &'static str = "Upgrade"
pub const _UPGRADE_INSECURE_REQUESTS: &'static str = "Upgrade-Insecure-Requests"
pub const _USER_AGENT: &'static str = "User-Agent"
pub const _VARY: &'static str = "Vary"
pub const _VIA: &'static str = "Via"
pub const _WANT_DIGEST: &'static str = "Want-Digest"
pub const _WWW_AUTHENTICATE: &'static str = "WWW-Authenticate"
pub const _X_CONTENT_TYPE_OPTIONS: &'static str = "X-Content-Type-Options"
pub const _X_CONTENT_TYPE_OPTIONS_VALUE_NOSNIFF: &'static str = "nosniff"
pub const _X_FRAME_OPTIONS: &'static str = "X-Frame-Options"
pub const _X_FRAME_OPTIONS_VALUE_DENY: &'static str = "DENY"
pub const _X_FRAME_OPTIONS_VALUE_SAME_ORIGIN: &'static str = "SAMEORIGIN"
pub const _STRICT_TRANSPORT_SECURITY_VALUE_DEFAULT: &'static str = "max-age=31536000; includeSubDomains"
pub const _REFERRER_POLICY_VALUE_DEFAULT: &'static str = "strict-origin-when-cross-origin"
pub const _PERMISSIONS_POLICY_VALUE_DEFAULT: &'static str = "geolocation=(), microphone=(), camera=()"
pub const _CONTENT_SECURITY_POLICY_VALUE_DEFAULT: &'static str = "default-src 'self'"
pub const NAME_VALUE_SEPARATOR: &'static str = ": "
pub const _DO_NOT_STORE_CACHE: &'static str = "no-store, no-cache, private, max-age=0, must-revalidate, proxy-revalidate"
Sourcepub fn new(
name: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> Header
pub fn new( name: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>, ) -> Header
A header from anything that converts into Cow<'static, str>: a
&'static str (borrowed, no allocation) or a String (owned).
use rust_web_server::header::Header;
let h = Header::new(Header::_CONTENT_TYPE, format!("text/{}", "plain"));
assert_eq!("text/plain", h.value);pub fn as_string(&self) -> String
Sourcepub fn single_line(s: &str) -> Cow<'_, str>
pub fn single_line(s: &str) -> Cow<'_, str>
s safe to write on one header line: CR and LF become spaces
(borrowed when there are none). Every HTTP/1.1 writer passes header
names, values and the reason phrase through it, so a value carrying
user input (a redirect target, an upstream header) can’t end the line
and inject headers or a second response (#47).
Sourcepub fn get_header_list_with_config(
request: &Request,
config: &ServerConfig,
) -> Vec<Header>
pub fn get_header_list_with_config( request: &Request, config: &ServerConfig, ) -> Vec<Header>
Build the standard response header list using settings from
[ServerConfig].
Prefer this method inside [App::execute] and any call-site that has
an App-level config available. It avoids reading environment variables
at request time, which enables proper test isolation via
[App::with_config].
Sourcepub fn get_header_list(request: &Request) -> Vec<Header>
pub fn get_header_list(request: &Request) -> Vec<Header>
Build the standard response header list, reading CORS and CSP settings from environment variables.
This is the legacy entry point retained for call-sites that do not yet
have access to a [ServerConfig] (e.g. error helpers in
src/extract/mod.rs). For all call-sites inside [App::execute] use
[get_header_list_with_config] instead.