34
35:- module(uri,
36 [ uri_components/2, 37 uri_data/3, 38 uri_data/4, 39
40 uri_normalized/2, 41 iri_normalized/2, 42 uri_normalized_iri/2, 43 uri_normalized/3, 44 iri_normalized/3, 45 uri_normalized_iri/3, 46 uri_resolve/3, 47 uri_is_global/1, 48 uri_query_components/2, 49 uri_authority_components/2, 50 uri_authority_data/3, 51 52 uri_encoded/3, 53 uri_file_name/2, 54 uri_iri/2 55 ]). 56:- use_foreign_library(foreign(uri)).
99uri_data(scheme, uri_components(S, _, _, _, _), S).
100uri_data(authority, uri_components(_, A, _, _, _), A).
101uri_data(path, uri_components(_, _, P, _, _), P).
102uri_data(search, uri_components(_, _, _, S, _), S).
103uri_data(fragment, uri_components(_, _, _, _, F), F).
109uri_data(scheme, uri_components(_, A, P, Q, F), S,
110 uri_components(S, A, P, Q, F)).
111uri_data(authority, uri_components(S, _, P, Q, F), A,
112 uri_components(S, A, P, Q, F)).
113uri_data(path, uri_components(S, A, _, Q, F), P,
114 uri_components(S, A, P, Q, F)).
115uri_data(search, uri_components(S, A, P, _, F), Q,
116 uri_components(S, A, P, Q, F)).
117uri_data(fragment, uri_components(S, A, P, Q, _), F,
118 uri_components(S, A, P, Q, F)).
230uri_authority_data(user, uri_authority(U, _, _, _), U).
231uri_authority_data(password, uri_authority(_, P, _, _), P).
232uri_authority_data(host, uri_authority(_, _, H, _), H).
233uri_authority_data(port, uri_authority(_, _, _, P), P).
272uri_file_name(URI, FileName) :-
273 nonvar(URI),
274 !,
275 uri_components(URI, Components),
276 uri_data(scheme, Components, File), File == file,
277 ( uri_data(authority, Components, '')
278 -> true
279 ; uri_data(authority, Components, localhost)
280 ),
281 uri_data(path, Components, FileNameEnc),
282 uri_encoded(path, FileName0, FileNameEnc),
283 delete_leading_slash(FileName0, FileName).
284uri_file_name(URI, FileName) :-
285 nonvar(FileName),
286 !,
287 absolute_file_name(FileName, Path0),
288 ensure_leading_slash(Path0, Path),
289 uri_encoded(path, Path, PathEnc),
290 uri_data(scheme, Components, file),
291 uri_data(authority, Components, ''),
292 uri_data(path, Components, PathEnc),
293 uri_components(URI, Components).
302ensure_leading_slash(Path, SlashPath) :-
303 ( sub_atom(Path, 0, _, _, /)
304 -> SlashPath = Path
305 ; atom_concat(/, Path, SlashPath)
306 ).
307
308:- if(current_prolog_flag(windows, true)). 309delete_leading_slash(Path, WinPath) :-
310 atom_concat(/, WinPath, Path),
311 is_absolute_file_name(WinPath),
312 !.
313:- endif. 314delete_leading_slash(Path, Path).
315
316
317 320
321:- multifile sandbox:safe_primitive/1. 322
323sandbox:safe_primitive(uri:uri_components(_,_)).
324sandbox:safe_primitive(uri:uri_normalized(_,_)).
325sandbox:safe_primitive(uri:iri_normalized(_,_)).
326sandbox:safe_primitive(uri:uri_normalized_iri(_,_)).
327sandbox:safe_primitive(uri:uri_normalized(_,_,_)).
328sandbox:safe_primitive(uri:iri_normalized(_,_,_)).
329sandbox:safe_primitive(uri:uri_normalized_iri(_,_,_)).
330sandbox:safe_primitive(uri:uri_resolve(_,_,_)).
331sandbox:safe_primitive(uri:uri_is_global(_)).
332sandbox:safe_primitive(uri:uri_query_components(_,_)).
333sandbox:safe_primitive(uri:uri_authority_components(_,_)).
334sandbox:safe_primitive(uri:uri_encoded(_,_,_)).
335sandbox:safe_primitive(uri:uri_iri(_,_))
Process URIs
This library provides high-performance C-based primitives for manipulating URIs. We decided for a C-based implementation for the much better performance on raw character manipulation. Notably, URI handling primitives are used in time-critical parts of RDF processing. This implementation is based on RFC-3986:
The URI processing in this library is rather liberal. That is, we break URIs according to the rules, but we do not validate that the components are valid. Also, percent-decoding for IRIs is liberal. It first tries UTF-8; then ISO-Latin-1 and finally accepts %-characters verbatim.
Earlier experience has shown that strict enforcement of the URI syntax results in many errors that are accepted by many other web-document processing tools. */