Expand description
HTTP header types
The module provides HeaderName
, HeaderMap
, and a number of types
used for interacting with HeaderMap
. These types allow representing both
HTTP/1 and HTTP/2 headers.
§HeaderName
The HeaderName
type represents both standard header names as well as
custom header names. The type handles the case insensitive nature of header
names and is used as the key portion of HeaderMap
. Header names are
normalized to lower case. In other words, when creating a HeaderName
with
a string, even if upper case characters are included, when getting a string
representation of the HeaderName
, it will be all lower case. This allows
for faster HeaderMap
comparison operations.
The internal representation is optimized to efficiently handle the cases
most commonly encountered when working with HTTP. Standard header names are
special cased and are represented internally as an enum. Short custom
headers will be stored directly in the HeaderName
struct and will not
incur any allocation overhead, however longer strings will require an
allocation for storage.
§Limitations
HeaderName
has a max length of 32,768 for header names. Attempting to
parse longer names will result in a panic.
§HeaderMap
HeaderMap
is a map structure of header names highly optimized for use
cases common with HTTP. It is a multimap structure, where each header name
may have multiple associated header values. Given this, some of the APIs
diverge from HashMap
.
§Overview
Just like HashMap
in Rust’s stdlib, HeaderMap
is based on Robin Hood
hashing. This algorithm tends to reduce the worst case search times in the
table and enables high load factors without seriously affecting performance.
Internally, keys and values are stored in vectors. As such, each insertion
will not incur allocation overhead. However, once the underlying vector
storage is full, a larger vector must be allocated and all values copied.
§Deterministic ordering
Unlike Rust’s HashMap
, values in HeaderMap
are deterministically
ordered. Roughly, values are ordered by insertion. This means that a
function that deterministically operates on a header map can rely on the
iteration order to remain consistent across processes and platforms.
§Adaptive hashing
HeaderMap
uses an adaptive hashing strategy in order to efficiently handle
most common cases. All standard headers have statically computed hash values
which removes the need to perform any hashing of these headers at runtime.
The default hash function emphasizes performance over robustness. However,
HeaderMap
detects high collision rates and switches to a secure hash
function in those events. The threshold is set such that only denial of
service attacks should trigger it.
§Limitations
HeaderMap
can store a maximum of 32,768 headers (header name / value
pairs). Attempting to insert more will result in a panic.
Structs§
- Drain
- A drain iterator for
HeaderMap
. - GetAll
- A view to all values stored in a single entry.
- Header
Map - A set of HTTP headers
- Header
Name - Represents an HTTP header field name
- Header
Value - Represents an HTTP header field value.
- Into
Iter - An owning iterator over the entries of a
HeaderMap
. - Invalid
Header Name - A possible error when converting a
HeaderName
from another type. - Invalid
Header Value - A possible error when converting a
HeaderValue
from a string or byte slice. - Iter
HeaderMap
entry iterator.- IterMut
HeaderMap
mutable entry iterator- Keys
- An iterator over
HeaderMap
keys. - MaxSize
Reached - Error returned when max capacity of
HeaderMap
is exceeded - Occupied
Entry - A view into a single occupied location in a
HeaderMap
. - ToStr
Error - A possible error when converting a
HeaderValue
to a string representation. - Vacant
Entry - A view into a single empty location in a
HeaderMap
. - Value
Drain - An drain iterator of all values associated with a single header name.
- Value
Iter - An iterator of all values associated with a single header name.
- Value
Iter Mut - A mutable iterator of all values associated with a single header name.
- Values
HeaderMap
value iterator.- Values
Mut HeaderMap
mutable value iterator
Enums§
- Entry
- A view into a single location in a
HeaderMap
, which may be vacant or occupied.
Constants§
- ACCEPT
- Advertises which content types the client is able to understand.
- ACCEPT_
CHARSET - Advertises which character set the client is able to understand.
- ACCEPT_
ENCODING - Advertises which content encoding the client is able to understand.
- ACCEPT_
LANGUAGE - Advertises which languages the client is able to understand.
- ACCEPT_
RANGES - Marker used by the server to advertise partial request support.
- ACCESS_
CONTROL_ ALLOW_ CREDENTIALS - Preflight response indicating if the response to the request can be exposed to the page.
- ACCESS_
CONTROL_ ALLOW_ HEADERS - Preflight response indicating permitted HTTP headers.
- ACCESS_
CONTROL_ ALLOW_ METHODS - Preflight header response indicating permitted access methods.
- ACCESS_
CONTROL_ ALLOW_ ORIGIN - Indicates whether the response can be shared with resources with the given origin.
- ACCESS_
CONTROL_ EXPOSE_ HEADERS - Indicates which headers can be exposed as part of the response by listing their names.
- ACCESS_
CONTROL_ MAX_ AGE - Indicates how long the results of a preflight request can be cached.
- ACCESS_
CONTROL_ REQUEST_ HEADERS - Informs the server which HTTP headers will be used when an actual request is made.
- ACCESS_
CONTROL_ REQUEST_ METHOD - Informs the server know which HTTP method will be used when the actual request is made.
- AGE
- Indicates the time in seconds the object has been in a proxy cache.
- ALLOW
- Lists the set of methods support by a resource.
- ALT_SVC
- Advertises the availability of alternate services to clients.
- AUTHORIZATION
- Contains the credentials to authenticate a user agent with a server.
- CACHE_
CONTROL - Specifies directives for caching mechanisms in both requests and responses.
- CACHE_
STATUS - Indicates how caches have handled a response and its corresponding request.
- CDN_
CACHE_ CONTROL - Specifies directives that allow origin servers to control the behavior of CDN caches interposed between them and clients separately from other caches that might handle the response.
- CONNECTION
- Controls whether or not the network connection stays open after the current transaction finishes.
- CONTENT_
DISPOSITION - Indicates if the content is expected to be displayed inline.
- CONTENT_
ENCODING - Used to compress the media-type.
- CONTENT_
LANGUAGE - Used to describe the languages intended for the audience.
- CONTENT_
LENGTH - Indicates the size of the entity-body.
- CONTENT_
LOCATION - Indicates an alternate location for the returned data.
- CONTENT_
RANGE - Indicates where in a full body message a partial message belongs.
- CONTENT_
SECURITY_ POLICY - Allows controlling resources the user agent is allowed to load for a given page.
- CONTENT_
SECURITY_ POLICY_ REPORT_ ONLY - Allows experimenting with policies by monitoring their effects.
- CONTENT_
TYPE - Used to indicate the media type of the resource.
- COOKIE
- Contains stored HTTP cookies previously sent by the server with the Set-Cookie header.
- DATE
- Contains the date and time at which the message was originated.
- DNT
- Indicates the client’s tracking preference.
- ETAG
- Identifier for a specific version of a resource.
- EXPECT
- Indicates expectations that need to be fulfilled by the server in order to properly handle the request.
- EXPIRES
- Contains the date/time after which the response is considered stale.
- FORWARDED
- Contains information from the client-facing side of proxy servers that is altered or lost when a proxy is involved in the path of the request.
- FROM
- Contains an Internet email address for a human user who controls the requesting user agent.
- HOST
- Specifies the domain name of the server and (optionally) the TCP port number on which the server is listening.
- IF_
MATCH - Makes a request conditional based on the E-Tag.
- IF_
MODIFIED_ SINCE - Makes a request conditional based on the modification date.
- IF_
NONE_ MATCH - Makes a request conditional based on the E-Tag.
- IF_
RANGE - Makes a request conditional based on range.
- IF_
UNMODIFIED_ SINCE - Makes the request conditional based on the last modification date.
- LAST_
MODIFIED - The Last-Modified header contains the date and time when the origin believes the resource was last modified.
- LINK
- Allows the server to point an interested client to another resource containing metadata about the requested resource.
- LOCATION
- Indicates the URL to redirect a page to.
- MAX_
FORWARDS - Indicates the max number of intermediaries the request should be sent through.
- ORIGIN
- Indicates where a fetch originates from.
- PRAGMA
- HTTP/1.0 header usually used for backwards compatibility.
- PROXY_
AUTHENTICATE - Defines the authentication method that should be used to gain access to a proxy.
- PROXY_
AUTHORIZATION - Contains the credentials to authenticate a user agent to a proxy server.
- PUBLIC_
KEY_ PINS - Associates a specific cryptographic public key with a certain server.
- PUBLIC_
KEY_ PINS_ REPORT_ ONLY - Sends reports of pinning violation to the report-uri specified in the header.
- RANGE
- Indicates the part of a document that the server should return.
- REFERER
- Contains the address of the previous web page from which a link to the currently requested page was followed.
- REFERRER_
POLICY - Governs which referrer information should be included with requests made.
- REFRESH
- Informs the web browser that the current page or frame should be refreshed.
- RETRY_
AFTER - The Retry-After response HTTP header indicates how long the user agent should wait before making a follow-up request. There are two main cases this header is used:
- SEC_
WEBSOCKET_ ACCEPT - The |Sec-WebSocket-Accept| header field is used in the WebSocket opening handshake. It is sent from the server to the client to confirm that the server is willing to initiate the WebSocket connection.
- SEC_
WEBSOCKET_ EXTENSIONS - The |Sec-WebSocket-Extensions| header field is used in the WebSocket opening handshake. It is initially sent from the client to the server, and then subsequently sent from the server to the client, to agree on a set of protocol-level extensions to use for the duration of the connection.
- SEC_
WEBSOCKET_ KEY - The |Sec-WebSocket-Key| header field is used in the WebSocket opening handshake. It is sent from the client to the server to provide part of the information used by the server to prove that it received a valid WebSocket opening handshake. This helps ensure that the server does not accept connections from non-WebSocket clients (e.g., HTTP clients) that are being abused to send data to unsuspecting WebSocket servers.
- SEC_
WEBSOCKET_ PROTOCOL - The |Sec-WebSocket-Protocol| header field is used in the WebSocket opening handshake. It is sent from the client to the server and back from the server to the client to confirm the subprotocol of the connection. This enables scripts to both select a subprotocol and be sure that the server agreed to serve that subprotocol.
- SEC_
WEBSOCKET_ VERSION - The |Sec-WebSocket-Version| header field is used in the WebSocket opening handshake. It is sent from the client to the server to indicate the protocol version of the connection. This enables servers to correctly interpret the opening handshake and subsequent data being sent from the data, and close the connection if the server cannot interpret that data in a safe manner.
- SERVER
- Contains information about the software used by the origin server to handle the request.
- SET_
COOKIE - Used to send cookies from the server to the user agent.
- STRICT_
TRANSPORT_ SECURITY - Tells the client to communicate with HTTPS instead of using HTTP.
- TE
- Informs the server of transfer encodings willing to be accepted as part of the response.
- TRAILER
- Allows the sender to include additional fields at the end of chunked messages.
- TRANSFER_
ENCODING - Specifies the form of encoding used to safely transfer the entity to the client.
- UPGRADE
- Used as part of the exchange to upgrade the protocol.
- UPGRADE_
INSECURE_ REQUESTS - Sends a signal to the server expressing the client’s preference for an encrypted and authenticated response.
- USER_
AGENT - Contains a string that allows identifying the requesting client’s software.
- VARY
- Determines how to match future requests with cached responses.
- VIA
- Added by proxies to track routing.
- WARNING
- General HTTP header contains information about possible problems with the status of the message.
- WWW_
AUTHENTICATE - Defines the authentication method that should be used to gain access to a resource.
- X_
CONTENT_ TYPE_ OPTIONS - Marker used by the server to indicate that the MIME types advertised in
the
content-type
headers should not be changed and be followed. - X_
DNS_ PREFETCH_ CONTROL - Controls DNS prefetching.
- X_
FRAME_ OPTIONS - Indicates whether or not a browser should be allowed to render a page in a frame.
- X_
XSS_ PROTECTION - Stop pages from loading when an XSS attack is detected.
Traits§
- AsHeader
Name - A marker trait used to identify values that can be used as search keys
to a
HeaderMap
. - Into
Header Name - A marker trait used to identify values that can be used as insert keys
to a
HeaderMap
.