http_encoding_headers
A Rust library for handling HTTP Accept-Encoding and Content-Encoding headers with support for common compression algorithms and custom encodings.
Features
- Parse and generate Accept-Encoding headers with quality values
- Parse and generate Content-Encoding headers
- Support for common encodings: gzip, deflate, br, zstd, and more
- Support for custom/unknown encodings via
Encoding::Custom
- Integration with
http
andheaders
crates for encoding/decoding. Can optionally be turned off.
Examples
Basic Accept-Encoding Usage
use ;
// Encode Accept-Encoding header
let encodings = vec!;
let header_value = encode_header_value.unwrap;
// Result: "gzip, deflate;q=0.8, br;q=0.6"
// Decode Accept-Encoding header
let parsed = decode_header_value.unwrap;
// Result: [(Gzip, 1.0), (Deflate, 0.8), (Br, 0.6)]
AcceptEncoding with Sorting and Preferred Encoding
use ;
let encodings = vec!;
let mut accept_encoding = new.unwrap;
// Find preferred encoding (highest quality)
let preferred = accept_encoding.preferred; // Some(Br)
// Find preferred encoding (highest quality) which is also in a list of allowed encodings
let preferred_allowed = accept_encoding.preferred_allowed;
// Sort in descending order (highest quality first)
accept_encoding.sort_descending;
// Sort in ascending order (lowest quality first)
accept_encoding.sort_ascending;
Content-Encoding Usage
use ;
use ;
use HeaderMap;
// Create Content-Encoding header
let content_encoding = new;
// Use with HTTP HeaderMap
let mut headers = new;
headers.typed_insert;
// Decode from header values
let header_values = vec!;
let decoded = decode.unwrap;
Running Examples
To see comprehensive examples in action:
# Basic usage examples
# AcceptEncoding specific examples
# Encode/decode focused examples
# Axum integration example
Axum Integration Example
The axum_integration.rs example demonstrates real-world usage with the Axum web framework:
use TypedHeader;
use AcceptEncoding;
// AcceptEncoding can be injected directly as a TypedHeader parameter
async
The axum example includes:
- TypedHeader extraction for AcceptEncoding
- Content negotiation algorithms
- Setting Content-Encoding response headers
- Multiple handler patterns and JSON responses
License
MIT