http-url
A focused builder for HTTP/HTTPS URLs, powered by the url crate.
http-url does not reimplement URL parsing. Parsing, percent-encoding,
IDNA normalization and relative-reference resolution are all delegated to
url (the WHATWG URL Standard implementation). What this crate adds on top
is:
- a fluent
HttpUrlBuilderfor assembling URLs programmatically, - an
HttpUrlvalue type that enforces the "scheme ishttporhttps" invariant, with a typedSchemeaccessor.
Component access (host, path, query, fragment, …) is provided directly by
url::Url via HttpUrl::as_url; this crate does not re-wrap those
getters.
Features
- Fluent HTTP/HTTPS URL builder
HttpUrlas a typed, scheme-restricted newtype overurl::Urlno_std+allocsupport
Quick Start
[]
= "0.2"
use ;
// Build
let http_url = builder
.scheme
.host
.add_path_segment
.add_path_segment
.add_query_parameter
.build
.unwrap;
assert_eq!;
// Parse (delegated to the `url` crate). Only http/https are accepted.
let http_url = parse.unwrap;
assert_eq!;
// Reach the full url::Url API for component access.
let url = http_url.as_url;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Builder API
builder
.scheme // Scheme::Http | Scheme::Https
.username // optional
.password // optional
.host // domain, IPv4, or [IPv6]
.port // optional, uses scheme default otherwise
.remove_port // revert to scheme default
.add_path_segment // decoded segment (auto-encoded)
.add_path_segment
.add_path_segments // multiple segments at once
.set_path // replace entire path (decoded)
.add_query_parameter // decoded form
.remove_query_parameter // remove all with this name
.clear_query_parameters // remove all
.fragment // decoded fragment
.remove_fragment // clear fragment
.build_url // -> url::Url
.unwrap;
// or .build() -> HttpUrl
All components are stored in decoded form on the builder and handed to
url::Url on build() / build_url(), which performs percent-encoding and
normalization.
Modifying an existing URL
A builder can be started from a parsed URL — a string, an url::Url, or an
existing HttpUrl — and then modified:
use ;
// From a string
let url = parse
.unwrap
.add_path_segment
.add_query_parameter
.build
.unwrap;
assert_eq!;
// From an existing HttpUrl
let url = parse.unwrap;
let url2 = url.new_builder
.add_path_segment
.build
.unwrap;
assert_eq!;
url Crate Interop
url is a required dependency, and HttpUrl is a thin newtype over
url::Url. Use the conversion methods to move between the two:
use HttpUrl;
// url::Url → HttpUrl (fallible, only http/https)
let u = parse.unwrap;
let http_url = try_from.unwrap;
assert_eq!;
// HttpUrl → url::Url (infallible, move)
let url: Url = http_url.into;
assert_eq!;
Feature Flags
| Feature | Default | Description |
|---|---|---|
std |
✅ | Enables std::error::Error impl via thiserror |
no_std
The crate works without std, requiring only alloc:
[]
= { = "0.2", = false }
The underlying url crate is itself no_std + alloc compatible.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.