rfc-6570

URI Template

J. Gregorio, R. Fielding, M. Hadley, M. Nottingham, D. Orchard
date2012-03 streamIETF wgnon working group statusPROPOSED STANDARD pages34 canonicalhttps://www.rfc-editor.org/rfc/rfc6570 doi10.17487/RFC6570 errataview
A URI Template is a compact sequence of characters for describing a range of Uniform Resource Identifiers through variable expansion. This specification defines the URI Template syntax and the process for expanding a URI Template into a URI reference, along with guidelines for the use of URI Templates on the Internet. [STANDARDS-TRACK]

Extracted elements (29)

design-rationale §1.3

Operator characters were chosen to match their roles as reserved characters in URI generic syntax (".", "/", ";", "?", "&", "#"), so the single-character operators correspond to the delimiter they introduce. This makes Level 1-3 templates readable without knowing the spec.

http

design-rationale §2.4.1

Prefix modifiers count Unicode code points (not octets) to avoid splitting multi-octet UTF-8 sequences or pct-encoded triplets that represent a single code point. This ensures the truncated value is always a valid Unicode string.

http

design-rationale §2.2

The operator characters "=", ",", "!", "@", and "|" are reserved for future extensions and not currently defined. The "$" and "()" characters are explicitly excluded from operator use so that macro languages can apply macro substitution to a string before it is processed as a URI Template.

http

design-rationale §1.2

The specification defines Level 1 as the default simple string expansion (no operator, single variable) to maintain compatibility with prior implementations in WSDL, WADL, and OpenSearch that only implemented this default type. Level 2-4 features are additive extensions.

http

design-rationale §1.3

URI Templates include composite-value operators (explode, named parameters) to describe how resource identifiers are commonly allocated, not merely to generate URIs from changed values. This allows templates to serve as machine-readable structural descriptions of URI spaces.

http

interoperability-note §1.1

A URI Template is also an IRI template because the template string may contain the broader set of IRI characters. The result of template processing can be transformed to an IRI by following the process in RFC 3987 Section 3.2.

http

interoperability-note §1.4

URI Templates are not URIs: they do not identify a resource, are not parsed as URIs, and SHOULD NOT be used where a URI is expected unless the template will be expanded prior to use. Distinct field, element, or attribute names should differentiate protocol elements carrying a URI Template from those expecting a URI reference.

http

interoperability-note §1.4

Variable matching (using a template in reverse to extract variable values from a formed URI) only works reliably when template expressions are delimited by the beginning/end of the URI or by characters that cannot be part of the expansion. In general, regular expression languages are better suited for variable matching.

http

normative-requirement §2 RECOMMENDED

A template processor limited to lower-level templates MAY exclude ABNF rules for higher levels, but it is RECOMMENDED that all parsers implement the full Level 4 syntax so that unsupported levels can be properly identified to the end user.

http

normative-requirement §3 MUST

Each variable's value MUST be formed prior to template expansion. The expansion process scans the template left-to-right, copying literals and replacing each expression with its expansion.

http

normative-requirement §3 SHOULD

If a template processor encounters a character sequence outside an expression that does not match the URI-Template grammar, processing SHOULD cease, the result SHOULD contain the expanded part followed by the remainder unexpanded, and the error SHOULD be indicated to the invoking application.

http

normative-requirement §1.6 SHOULD

If a value is provided by a user (e.g., via data-entry), the string SHOULD be normalized as NFC (Normalization Form C: Canonical Decomposition followed by Canonical Composition) prior to being used in expansions by a template processor.

http

normative-requirement §3.2.1 MUST

If a variable appears more than once in an expression or within multiple expressions of a URI Template, the value of that variable MUST remain static throughout the expansion process.

http

normative-requirement §3 SHOULD

If an error is encountered inside an expression (unknown operator, unsupported modifier, disallowed character), the unprocessed parts SHOULD be copied to the result unexpanded, processing of the remainder of the template SHOULD continue, and the error location and type SHOULD be indicated to the invoking application.

http

normative-requirement §3.2.1 MUST

In all expression types except reserved ("+") and fragment ("#"), a value character of "%" MUST be pct-encoded as "%25" by variable expansion. Reserved and fragment expansions allow "%" as part of pct-encoded triplets to pass through unencoded.

http

normative-requirement §1.6 MUST

When non-ASCII data is pct-encoded for use in a URI reference, a template processor MUST first encode the string as UTF-8 and then pct-encode any octets not allowed in a URI reference.

http, security

protocol-element §2

A URI Template is a string of printable Unicode characters containing zero or more embedded variable expressions, each delimited by matching braces ('{', '}'). The grammar is: URI-Template = *( literals / expression ). Expressions contain an optional operator followed by a comma-separated variable-list.

http

protocol-element §3.2.1

An undefined variable (value is null/undef, or a list/associative array with zero members) is ignored by expansion: if all variables in an expression are undefined, the expression's expansion is the empty string. A zero-length string is defined, not undefined.

http

protocol-element §3.2.9

Form-style query continuation ("&" operator) is used when the template already contains a literal query component with fixed parameters; it appends "&name=value" pairs for each defined variable. This contrasts with "?" which begins a new query component.

http

protocol-element §3.2.7

Path-style parameter expansion (";" operator) appends ";varname" and omits "=" when the variable value is empty. Form-style query expansion ("?" operator) always appends "=" even when the value is empty, following HTML form conventions.

http

protocol-element §3.2.1

Reserved expansion ("+" operator) and fragment expansion ("#" operator) allow the union of (unreserved / reserved / pct-encoded) characters to pass through without pct-encoding. All other operators allow only unreserved characters through without pct-encoding.

http

protocol-element §A

The operator table (Appendix A) defines per-operator behavior: first (string prepended if any variable is defined), sep (separator between expansions), named (whether to include varname), ifemp (appended when value is empty), and allow (U = unreserved only; U+R = unreserved+reserved+pct-encoded).

http

security-consideration §4

If an attacker controls the URI Template itself or controls variable values used with an expression type that allows reserved characters ("+ " or "#"), unanticipated URIs may be crafted. Security depends on who provides the template and values, in what execution context expansion occurs, and where the resulting URIs are used.

security, http

security-consideration §4

In client-side implementations, care ought to be taken to ensure that potentially dangerous URI reference strings such as those beginning with "javascript:" do not appear in the expansion unless both the template and the values are provided by a trusted source. URI Templates in this context have properties similar to HTML forms.

security, http

wire-format §2.4.1

A prefix modifier has the form ":" max-length where max-length = %x31-39 0*3DIGIT (a positive integer less than 10000). The max-length counts Unicode code points from the beginning of the variable's decoded value string, not octets.

http

wire-format §2.2

An expression has the form "{" [ operator ] variable-list "}". Operators are: op-level2 = "+" / "#"; op-level3 = "." / "/" / ";" / "?" / "&"; op-reserve = "=" / "," / "!" / "@" / "|". Absence of an operator means simple string expansion.

http

wire-format §2.1

Literals are defined as any Unicode character except CTL, SP, DQUOTE, "'", unencoded "%", "<", ">", "\\", "^", "`", "{", "|", "}". Characters not allowed in URI syntax are copied as their UTF-8 pct-encoded sequences.

http

wire-format §2.4.2

The explode modifier is the single character "*" appended to a varspec. It signals that the variable should be treated as a composite value (list or associative array) and each member expanded as if it were a separate variable.

http

wire-format §2.3

Variable names (varname) are defined as: varname = varchar *( ["."] varchar ); varchar = ALPHA / DIGIT / "_" / pct-encoded. Variable names are case-sensitive. Pct-encoded triplets within a varname are not decoded and are treated as part of the name.

http