Templated Uri
Standards-compliant URI handling with templating, safety validation, and data classification.
This crate provides comprehensive URI manipulation capabilities designed for HTTP clients
and servers that need type-safe, efficient, and data classification-aware URI handling. It builds
on top of the standard http crate while adding additional safety guarantees, templating
capabilities, and data classification features.
Core Types
The crate centers around several key abstractions:
Uri- Flexible URI type with endpoint and path/query componentsBaseUri- Lightweight type representing scheme, authority, and optional base path (BasePath)TemplatedPathAndQuery- RFC 6570 Level 3 compliant URI templatingUriSafeandUriSafeString- Generic newtype wrapper proving a value is safe for URI components by not containing any reserved characters
Basic Usage
Simple URI Construction
use ;
use ;
// Create an endpoint (scheme + authority only)
let base_uri = from_uri_static;
// Create a path (can be static for zero-allocation)
let path: TargetPathAndQuery = from_static;
// Combine into complete URI
let uri = default.base_uri.path_and_query;
assert_eq!;
Templated URIs
For dynamic URIs with variable components, use the templating system:
use ;
let path = UserPostPath ;
let uri = default
.base_uri
.path_and_query;
URI Safety Guarantees
The UriSafe<T> newtype wraps values that are guaranteed
to contain only URI-safe characters. This prevents common URI injection vulnerabilities:
use UriSafeString;
// This will succeed - encodes unsafe characters into a URI-safe format
let unsafe_string = encode;
assert_eq!;
// This will succeed - contains only safe characters
let safe = try_new.unwrap;
assert_eq!;
// try_new() fails on URI-reserved characters
let unsafe_string = try_new;
assert!;
Built-in safe types include numeric types (u32, u64, etc.), Uuid (with the uuid feature),
IP addresses, and validated UriSafeString instances.
Telemetry Labels
For complex templates, use the label attribute to provide a concise identifier
for telemetry. When present, the label takes precedence over the template string.
use ;
Data Classification
The crate integrates with data_privacy to track data sensitivity levels
in URIs. This is particularly important for compliance and data security:
use Sensitive;
use ;
RFC 6570 Template Compliance
The templating system implements RFC 6570 Level 3 URI Template specification. Supported expansions include:
- Simple string expansion:
{var} - Reserved string expansion:
{+var} - Path segments:
{/var} - Query parameters:
{?var} - Query continuation:
{&var}
Note: Fragment expansion ({#var}) from RFC 6570 is not supported because URI
fragments are stripped by the http crate and ignored by HTTP clients.
Template variables must implement UriParam (except for reserved expansions)
to ensure the resulting URI is valid.
Integration with HTTP Ecosystem
This crate seamlessly integrates with the broader Rust HTTP ecosystem by re-exporting
and building upon the standard http crate types. The resulting Uri can be converted
to an http::Uri for use with HTTP clients
and servers based on hyper like reqwest.