Expand description
Strict, trust-aware extraction of HTTP request metadata.
This crate operates primarily on http types and does not require a web
framework, Tower, tracing, or OpenTelemetry. An opt-in axum feature adds
only transport-peer extraction from Axum request extensions. Forwarding
fields are never trusted implicitly.
The crate root exposes small synchronous extractors for coherent field
responsibilities. Applications compose only the functions they need at
their framework boundary; this crate does not impose a request-context
aggregate or an asynchronous adapter. Cargo features gate field-specific
APIs; disabling default features leaves the shared Header helpers and
Error available, and enabling a feature exposes only its documented API
and dependencies.
§http-extract
Strict, synchronous extraction of HTTP request metadata using http types.
The default API is framework-independent; an optional axum feature adds
socket-peer extraction from ConnectInfo request extensions.
The crate keeps transport facts separate from Header assertions. Values from
Forwarded, X-Forwarded-*, and provider-specific client-IP fields are raw and
untrusted until the deployment establishes an explicit proxy trust boundary.
§Install
Default features provide all common extractors:
[dependencies]
http-extract = "0.1"Select only the API families an application uses:
[dependencies]
http-extract = { version = "0.1", default-features = false, features = [
"authority",
"content-type",
] }§API
Header functions contain the parsing logic. Matching Request functions are
convenience wrappers that delegate through request.headers().
| Feature/API family | Purpose |
|---|---|
api_key | X-API-Key, then Api-Key fallback |
authority | URI authority and strict Host extraction |
authorization | Raw Authorization plus lightweight Bearer/Basic scheme routing |
client_ip | Socket-peer helpers and default/custom Header selection |
client_ip_headers | Common provider and proxy client-IP fields |
content_type | Strict Content-Type parsing into mime::Mime |
forwarded | RFC 7239 Forwarded for= IP chains |
request_id | X-Request-Id, then Request-Id fallback |
x_forwarded | X-Forwarded-For and X-Forwarded-Proto parsing |
header | Strict singular-field helpers and append-without-replace utility |
See the feature and API map for exact function names and return types.
§Client IP boundary
extract_client_ip checks these Header sources in order:
- RFC 7239
Forwarded; X-Forwarded-For;X-Real-IP;CF-Connecting-IP.
This standard-first order is a library convention, not an RFC-defined
precedence or trust policy. extract_client_ip_with_headers accepts an
explicit ordered slice of ClientIpHeader values for deployment-specific
selection. A malformed first-present source fails instead of falling through.
Both functions return raw Header assertions. Security-sensitive consumers such
as rate limiters should first verify the socket peer and use only a Header that
a trusted proxy overwrites. extract_peer_ip returns an out-of-band socket
peer; with the optional axum feature, extract_axum_peer_address and
extract_axum_peer_ip read Axum’s ConnectInfo<SocketAddr> extension.
Read the client IP trust boundary before using a Header-derived IP for authorization, rate limiting, or auditing.
§Features
Default features are api-key, authority, authorization, client-ip,
client-ip-headers, content-type, forwarded, request-id, and
x-forwarded.
client-ipenables its Header parsing dependencies;content-typeenables the optionalmimedependency;- non-default
axumenablesclient-ipand the optional Axum dependency; --no-default-featuresleaves onlyErrorand the generic Header helpers.
The default normal dependency tree does not include Axum, Tower, Tokio, tracing, or OpenTelemetry. See the feature guide for copyable configurations.
§Errors and sensitive values
Missing optional metadata returns Ok(None). Duplicate, non-text, and invalid
fields return the crate-wide Error; errors identify only the field name and
category, never its value.
Authorization credentials and API keys are returned only by their explicit extractors. Do not log or echo those values, cookies, request bodies, complete query strings, or raw forwarding fields.
§Standards
The crate implements narrow extraction behavior, not complete protocol or authentication implementations:
- HTTP Semantics, RFC 9110, including field semantics, authority, and Content-Type;
- Forwarded, RFC 7239, limited to
continuous
for=IP chains and subject to its security considerations; - Bearer, RFC 6750 Section 2.1 and Basic, RFC 7617 Section 2, used only for lightweight scheme recognition without authentication or Basic decoding.
X-Forwarded-* and provider-specific client-IP fields are de facto or vendor
conventions, not IETF standards. See
standards and compatibility
for the supported boundary.
§Axum example
The runnable Axum example demonstrates peer extraction, request metadata, client-IP selection, error handling, and safe observable output:
cargo run --example axum-demo --features axumThe full guide is available in the docs mdBook sources. The crate declares Rust 1.96.0 and is licensed under MIT or Apache-2.0.
Structs§
- Header
Map - A specialized multimap for header names and values.
- Header
Name - Represents an HTTP header field name
- Header
Value - Represents an HTTP header field value.
- Request
- Represents an HTTP request.
Enums§
- Client
IpHeader - A supported client IP Header and its parsing rule.
- Error
- An error produced while extracting request metadata.
Constants§
- API_KEY
- The fallback
Api-Keyfield name. - BASIC_
SCHEME - The Basic scheme.
- BEARER_
SCHEME - The Bearer scheme.
- CF_
CONNECTING_ IP - The
CF-Connecting-IPfield name. - CLIENT_
IP_ HEADERS - The default Header lookup order used by
extract_client_ip. - CLOUDFRONT_
VIEWER_ ADDRESS - The
CloudFront-Viewer-Addressfield name. - FLY_
CLIENT_ IP - The
Fly-Client-IPfield name. - FORWARDED
- The standardized
Forwardedfield name from RFC 7239, Section 4. - REQUEST_
ID - The fallback
Request-Idfield name. - SCHEME_
SEPARATOR - The space character that separates the scheme from the credentials.
- TRUE_
CLIENT_ IP - The
True-Client-IPfield name. - X_
API_ KEY - The preferred
X-API-Keyfield name. - X_
ENVOY_ EXTERNAL_ ADDRESS - The
X-Envoy-External-Addressfield name. - X_
FORWARDED_ FOR - The de facto, non-IETF
X-Forwarded-Forfield name. - X_
FORWARDED_ PROTO - The de facto, non-IETF
X-Forwarded-Protofield name. - X_
REAL_ IP - The
X-Real-IPfield name. - X_
REQUEST_ ID - The preferred
X-Request-Idfield name.
Functions§
- append_
header_ value - Append one already validated field value without replacing existing values.
- extract_
axum_ peer_ address - Extract the Axum transport peer stored in a request extension.
- extract_
axum_ peer_ ip - Extract the Axum socket peer IP stored in a request extension.
- extract_
client_ ip - Extract a raw client IP assertion using the default field order.
- extract_
client_ ip_ with_ headers - Extract a raw client IP assertion using caller-defined fields and order.
- extract_
header_ api_ key - Extract an API key from request fields.
- extract_
header_ authority - Extract a strict, singular, syntactically valid
Hostauthority. - extract_
header_ authorization - Extract the singular
Authorizationfield as text. - extract_
header_ basic_ credentials - Extract raw Basic credentials from the
Authorizationfield. - extract_
header_ bearer_ token - Extract raw Bearer credentials from the
Authorizationfield. - extract_
header_ cf_ connecting_ ip - Extract the raw, untrusted IP asserted by
CF-Connecting-IP. - extract_
header_ cloudfront_ viewer_ address - Extract an untrusted client IP from AWS CloudFront’s
CloudFront-Viewer-AddressIP:portvalue. - extract_
header_ content_ type - Extract and parse a singular
Content-Typefield as a media type. - extract_
header_ fly_ client_ ip - Extract the raw, untrusted IP asserted by
Fly-Client-IP. - extract_
header_ forwarded_ for - Extract RFC 7239
Forwardedfor=values as an untrusted IP chain. - extract_
header_ request_ id - Extract a request ID from request fields.
- extract_
header_ true_ client_ ip - Extract an untrusted client IP asserted by
True-Client-IP. - extract_
header_ x_ envoy_ external_ address - Extract an untrusted client IP asserted by Envoy’s
X-Envoy-External-Address. - extract_
header_ x_ forwarded_ for - Extract all
X-Forwarded-Forfield lines as an untrusted asserted IP chain. - extract_
header_ x_ forwarded_ proto - Extract all
X-Forwarded-Protofield lines as untrusted protocol tokens. - extract_
header_ x_ real_ ip - Extract the raw, untrusted IP asserted by
X-Real-IP. - extract_
peer_ address - Return a transport peer address supplied out-of-band by an adapter.
- extract_
peer_ ip - Return the IP address of a transport peer supplied out-of-band by an adapter.
- extract_
request_ api_ key - Extract an API key from a complete request.
- extract_
request_ authority - Extract the authority from a complete request.
- extract_
request_ authorization - Extract the raw
Authorizationfield from a complete request. - extract_
request_ basic_ credentials - Extract raw Basic credentials from a complete request.
- extract_
request_ bearer_ token - Extract raw Bearer credentials from a complete request.
- extract_
request_ cf_ connecting_ ip - Extract
CF-Connecting-IPfrom a complete request. - extract_
request_ cloudfront_ viewer_ address - Extract
CloudFront-Viewer-Addressfrom a complete request. - extract_
request_ content_ type - Extract and parse
Content-Typefrom a complete request. - extract_
request_ fly_ client_ ip - Extract
Fly-Client-IPfrom a complete request. - extract_
request_ forwarded_ for - Extract the untrusted
Forwardedfor=IP chain from a complete request. - extract_
request_ request_ id - Extract a request ID from a complete request.
- extract_
request_ true_ client_ ip - Extract
True-Client-IPfrom a complete request. - extract_
request_ x_ envoy_ external_ address - Extract
X-Envoy-External-Addressfrom a complete request. - extract_
request_ x_ forwarded_ for - Extract the untrusted
X-Forwarded-Forchain from a complete request. - extract_
request_ x_ forwarded_ proto - Extract untrusted
X-Forwarded-Prototokens from a complete request. - extract_
request_ x_ real_ ip - Extract
X-Real-IPfrom a complete request. - extract_
rightmost_ forwarded - Extract the rightmost
Forwardedfor=IP address from a header. - extract_
rightmost_ x_ forwarded_ for - Extract the rightmost
X-Forwarded-ForIP address from a header. - extract_
single_ header_ text - Extract a singular field as text without silently discarding invalid bytes.
- extract_
single_ header_ value - Extract a field value only when the field has at most one field line.