masque_uri_template (masque v0.7.0)

View Source

Generic URI-template engine used by MASQUE protocol facades (UDP, TCP, IP). Implements the subset of RFC 6570 that RFC 9298 and RFC 9484 require: literal segments, Level-1 name path placeholders, and the Level-3 form operator for query strings.

Two entry shapes. parse_absolute/1 accepts only absolute URI templates (scheme + authority + path), used by the CONNECT-IP client where RFC 9484 section 3 requires an absolute template. parse_pattern/1 accepts either a path+query pattern or an absolute URI (in which case only the path+query portion is used), used by servers that match against the :path pseudo-header and by legacy UDP/TCP callers that have always accepted path-only templates.

The parsed result is a template() record that can be fed to expand/2 and match/2. Variable names are atoms; variable values are always returned as binaries (percent-decoded).

Summary

Functions

Expand a template with Vars. Returns a binary: the full URI for absolute templates, just the path+query for patterns.

Match a request :path against a template. Returns the captured variables on success.

Parse an absolute URI template (scheme://authority/path). Returns {error, not_absolute} if the template has no scheme or authority.

Parse a path+query pattern, or the path+query portion of an absolute URI. Non-absolute inputs are accepted as-is.

Types

parse_error/0

-type parse_error() :: not_absolute | bad_template | bad_segment.

segment/0

-type segment() :: {literal, binary()} | {var, atom()} | {query, [atom()]}.

template/0

-opaque template()

vars/0

-type vars() :: #{atom() => binary() | iodata() | integer() | atom()}.

Functions

authority(Tpl)

-spec authority(template()) -> binary() | undefined.

expand(Tpl, Vars)

-spec expand(template(), vars()) -> binary().

Expand a template with Vars. Returns a binary: the full URI for absolute templates, just the path+query for patterns.

is_absolute(Tpl)

-spec is_absolute(template()) -> boolean().

match(Tpl, Path)

-spec match(template(), binary()) -> {ok, vars()} | {error, no_match | bad_pct}.

Match a request :path against a template. Returns the captured variables on success.

parse_absolute(Bin)

-spec parse_absolute(binary()) -> {ok, template()} | {error, parse_error()}.

Parse an absolute URI template (scheme://authority/path). Returns {error, not_absolute} if the template has no scheme or authority.

parse_pattern(Bin)

-spec parse_pattern(binary()) -> {ok, template()} | {error, parse_error()}.

Parse a path+query pattern, or the path+query portion of an absolute URI. Non-absolute inputs are accepted as-is.