Module uri

This module provides a couple of simple utilities to deal with the generation and parsing of uniform resource identifiers (URIs). The predicates make_query/4, make_spec/4 and make_uri/4 allow constructing and deconstructing queries, specs and URIs. The predicates work bidirectional without loss of data.

Example:
?- make_query(X, Y, Z, 'a%3Db=c%26d&e').
X = 'a=b',
Y = 'c&d',
Z = e

The predicates make_query/4, make_spec/4 and make_uri/4 do a minimal encoding. For the parameter name and the parameter value the characters '#%=&\' will be encoded. For the hash the characters '%\' will be encoded. For the spec the characters '?#%\' will be encoded. If used in the other direction the predicates will perform decoding of the corresponding components.

An URI is relative when it neither contains a scheme nor an authority, and if the path is relative. The predicate is_relative_uri/1 checks whether an URI is relative. The predicate follow_uri/3 can be used to resolve and relativize URIs. Contrary to the Java URL class, it is agnostic to the scheme of the URIs and will allow any scheme. In these predicates the path component is handled by the corresponding routines from system/file.

Example:
?- follow_uri('file:/foo/bar/baz?jack#jill', 
   X, 'file:/foo/tip/tap?fix#fox').
X = '../tip/tap?fix#fox'

The predicate canonical_uri/2 can be used to canonize URIs. For the file protocol the path component is handled by the corresponding routine from the module file. For other protocols the routine uses puny code from the module domain and accesses the server for redirects.

The predicate uri_encode/2 can be used to encode and decode URIs. The predicate uri_encode/2 will percent encode characters above 0x7F. As a result the URI will only contain ASCII. If used in the other direction the predicate will first decode and then minimal encode again.

The following URI predicates are provided:

make_query(N, V, R, Q):
If N, V or R is a variable then the predicate succeeds when N, V and R unify with the first parameter name, the first parameter value and the rest query of the query Q. Otherwise the predicates succeeds when Q unifies with the constructed query.
make_spec(E, A, P, S):
If E, A or P is a variable then the predicate succeeds when E, A and P unify with the scheme, authority and path respectively of the spec S. Otherwise the predicate suc-ceeds when S unifies with the constructed spec.
make_uri(S, Q, H, U):
If S, Q or H is a variable then the predicate succeeds when S, Q and H unify with the spec, query and hash respectively of the URI U. Otherwise the predicate succeeds when U unifies with the constructed URI.
is_relative_uri(U):
The predicate succeeds when the URI U is a relative URI.
follow_uri(B, R, A):
If R is a variable then the predicate succeeds when R unifies with the relative or abso-lute URI that leads from B to A. Otherwise the predicate succeeds when A unifies with the URI that results from B by following R.
canonical_uri(U, C):
The predicate succeeds when C unifies with canonical URI of U.
uri_encode(T, E):
If T is a variable then the predicate succeeds when T unifies with the URI decode of E. Otherwise the predicate succeeds when E unifies with the URI encode of T.
make_link(S, P, H, L):
If L is a variable, then the predicate succeeds in L with the URI composed of the spec S, the parameters P and the hash H. Otherwise the predicate succeeds in decompos-ing the URI.

Kommentare