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)).
227uri_authority_data(user, uri_authority(U, _, _, _), U).
228uri_authority_data(password, uri_authority(_, P, _, _), P).
229uri_authority_data(host, uri_authority(_, _, H, _), H).
230uri_authority_data(port, uri_authority(_, _, _, P), P).
268uri_file_name(URI, FileName) :-
269 nonvar(URI),
270 !,
271 uri_components(URI, Components),
272 uri_data(scheme, Components, File), File == file,
273 ( uri_data(authority, Components, '')
274 -> true
275 ; uri_data(authority, Components, localhost)
276 ),
277 uri_data(path, Components, FileNameEnc),
278 uri_encoded(path, FileName0, FileNameEnc),
279 delete_leading_slash(FileName0, FileName).
280uri_file_name(URI, FileName) :-
281 nonvar(FileName),
282 !,
283 absolute_file_name(FileName, Path0),
284 ensure_leading_slash(Path0, Path),
285 uri_encoded(path, Path, PathEnc),
286 uri_data(scheme, Components, file),
287 uri_data(authority, Components, ''),
288 uri_data(path, Components, PathEnc),
289 uri_components(URI, Components).
298ensure_leading_slash(Path, SlashPath) :-
299 ( sub_atom(Path, 0, _, _, /)
300 -> SlashPath = Path
301 ; atom_concat(/, Path, SlashPath)
302 ).
303
304:- if(current_prolog_flag(windows, true)). 305delete_leading_slash(Path, WinPath) :-
306 atom_concat(/, WinPath, Path),
307 is_absolute_file_name(WinPath),
308 !.
309:- endif. 310delete_leading_slash(Path, Path).
311
312
313 316
317:- multifile sandbox:safe_primitive/1. 318
319sandbox:safe_primitive(uri:uri_components(_,_)).
320sandbox:safe_primitive(uri:uri_normalized(_,_)).
321sandbox:safe_primitive(uri:iri_normalized(_,_)).
322sandbox:safe_primitive(uri:uri_normalized_iri(_,_)).
323sandbox:safe_primitive(uri:uri_normalized(_,_,_)).
324sandbox:safe_primitive(uri:iri_normalized(_,_,_)).
325sandbox:safe_primitive(uri:uri_normalized_iri(_,_,_)).
326sandbox:safe_primitive(uri:uri_resolve(_,_,_)).
327sandbox:safe_primitive(uri:uri_is_global(_)).
328sandbox:safe_primitive(uri:uri_query_components(_,_)).
329sandbox:safe_primitive(uri:uri_authority_components(_,_)).
330sandbox:safe_primitive(uri:uri_encoded(_,_,_)).
331sandbox: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. */