Skip to content

ddn.net.uri

RFC 3986 URI implementation with normalization, reference resolution, percent-encoding helpers, IDNA/Punycode hostname support, and RFC 6570 URI Templates.

Module Declaration

module ddn.net.uri;

Classes

UriException

class UriException : Exception

Base exception type for URI-related errors.


UriParseException

class UriParseException : UriException

Exception type for URI parsing/validation errors.


UriTemplateException

class UriTemplateException : UriException

Exception type for RFC 6570 URI Template parsing/expansion errors.


Enums

Component

enum Component

URI component selector used to apply component-specific rules (notably percent-encoding).

Member Description
Scheme Scheme component
UserInfo Authority userinfo subcomponent
Host Authority host subcomponent
Path Path component
Query Query component (without leading ?)
Fragment Fragment component (without leading #)

Structs

Authority

struct Authority

Authority component per RFC 3986 (userinfo@host:port).

The host is stored without square brackets even for IP-literals; brackets are added when formatting with toString().

Fields

Field Type Description
userinfo string Optional userinfo (empty if absent)
host string Hostname or IP-literal value (IPv6/IPvFuture stored without brackets)
port int Port number (-1 means absent)

Methods

hasUserInfo
bool hasUserInfo() const;
hasPort
bool hasPort() const;
toString
string toString() const;

Serialize the authority, adding brackets for IP-literals.


URI

struct URI

Generic Uniform Resource Identifier per RFC 3986.

Represents either an absolute URI (has a non-empty scheme) or a relative reference.

Fields

URI stores the parsed components directly:

  • scheme
  • authority (guarded by hasAuthority)
  • path
  • query (guarded by hasQueryComponent)
  • fragment (guarded by hasFragmentComponent)

Construction & parsing

parse
static URI parse(string s);
static URI parse(string s, UriOptions opt);

Parse an absolute URI or relative reference.

parseStrict
static URI parseStrict(string s);

Parse s as a URI applying additional strict validations.

Introspection

isAbsolute
bool isAbsolute() const;

True if this reference is an absolute URI (has a non-empty scheme).

hasAuthority
bool hasAuthority() const;

True if an authority component is present (i.e. the reference used the // form).

hasQueryComponent
bool hasQueryComponent() const;
hasFragmentComponent
bool hasFragmentComponent() const;

Serialization

toString
string toString() const;

Serialize this URI per RFC 3986 generic syntax.

Normalization

normalize
URI normalize() const;
URI normalize(UriOptions opt) const;

Normalize per RFC 3986 Section 6 (scheme/host case-folding, dot-segment removal, percent-encoding normalization, default port elision).

normalizeIDNA
URI normalizeIDNA() const;
URI normalizeIDNA(UriOptions opt) const;

Like normalize(), but also converts the host (when present and not an IP-literal) to ASCII using IDNA ToASCII.

hostUnicode
string hostUnicode() const;

Return the Unicode form of the host using IDNA ToUnicode (if applicable).

Reference resolution

resolve
URI resolve(const URI reference) const;

Resolve a reference against this base URI per RFC 3986 ยง5.2.


URL

struct URL

Convenience wrapper for hierarchical absolute URIs that have an authority.

Construction

parse
static URL parse(string s);

Parses and enforces that the URI is absolute and has an authority.

Methods / properties

toString
string toString() const;
scheme
@property string scheme() const;
@property void scheme(string value);
authority
@property ref Authority authority() return;
@property void authority(Authority value);
path
@property string path() const;
@property void path(string value);
query
@property string query() const;
@property void query(string value);
fragment
@property string fragment() const;
@property void fragment(string value);
host
@property string host() const;
@property void host(string value);
port
@property int port() const;
@property void port(int value);
userinfo
@property string userinfo() const;
@property void userinfo(string value);
hostUnicode
@property string hostUnicode() const;

URN

struct URN

Uniform Resource Name wrapper (scheme == "urn").

Construction

parse
static URN parse(string s);

Methods / properties

nss
@property string nss() const;
@property void nss(string value);

Namespace Specific String (NSS) accessors.

toString
string toString() const;

UriOptions

struct UriOptions

Options controlling per-call parsing/normalization strictness.

Field Type Meaning
strictParsing bool Enable stricter parsing validations
idnaStrict bool Stricter IDNA label validation during IDNA conversion
allowNonAsciiHost bool Allow non-ASCII bytes in reg-name during parsing

IdnaOptions

struct IdnaOptions

Options controlling IDNA processing.

Field Type Meaning
enableMappings bool Apply basic UTS #46-like compatibility mappings
strict bool Reject prohibited code points and additional label constraints

UriTemplate

struct UriTemplate

RFC 6570 URI Template (template string parsed into literal/expression parts).

Methods

parse
static UriTemplate parse(string templateString);

Parse an RFC 6570 template string.

expand
string expand(const UriTemplateVars vars) const;

Expand this template using a variable scope.


UriTemplateVars

struct UriTemplateVars

Expansion scope for URI Templates (maps variable names to values).

Methods

set
void set(string name, UriTemplateValue value);
contains
bool contains(string name) const;
get
Nullable!(const(UriTemplateValue)) get(string name) const;

UriTemplateValue

struct UriTemplateValue

URI Template value model (scalar, list, or map).

Common constructors:

static UriTemplateValue fromScalar(string s);
static UriTemplateValue fromList(string[] items);
static UriTemplateValue fromMap(Tuple!(string, string)[] pairs);
static UriTemplateValue fromAA(string[string] aa);

Functions

pctEncodeTo

void pctEncodeTo(R)(string s, Component comp, ref R dst);

Write percent-encoded representation of s for component comp to dst.


pctEncode

string pctEncode(string s, Component comp);

Percent-encode s according to RFC 3986 rules for a specific component.


pctDecode

string pctDecode(string s);

Decode %HH triplets in s byte-wise (invalid/incomplete escapes are preserved).


pctNormalizeTo

void pctNormalizeTo(R)(string s, Component comp, ref R dst);

Normalize percent-escapes: decode escapes that map to unreserved characters and uppercase hex digits in remaining escapes.


pctNormalize

string pctNormalize(string s, Component comp);

Allocating wrapper around pctNormalizeTo.


isPctAllowed

bool isPctAllowed(dchar c, Component comp);

Returns true if code point c is allowed to appear unescaped (literal ASCII byte) in comp.


idnaToASCII

string idnaToASCII(string domain);
string idnaToASCII(string domain, IdnaOptions opt);

Convert a domain name to its ASCII form using IDNA ToASCII (basic version).


idnaToUnicode

string idnaToUnicode(string asciiDomain);
string idnaToUnicode(string asciiDomain, IdnaOptions opt);

Convert an ASCII domain containing Punycode labels to its Unicode form (IDNA ToUnicode subset).


punycodeEncodeLabel

string punycodeEncodeLabel(string label);

Encode a single DNS label to Punycode (without adding the xn-- prefix).


punycodeDecodeLabel

string punycodeDecodeLabel(string ascii);

Decode a single Punycode label (without the xn-- prefix).