:- ensure_loaded(common).
cvs_id('$Id: uri.pl,v 1.3 2004/06/11 15:56:46 sandro Exp $').

uri_scheme(URI, Scheme) :-
	once(sub_atom(URI, ColonIndex, 1, _, ':')),
	sub_atom(URI, 0, ColonIndex, _, Scheme).
unit_test( uri_scheme('http://example.com/foo', 'http') ).

uri_host_part(URI, Host) :-
	% assumes Absolute URI (no frag)
	atom_concat('http://', PostScheme, URI),
	(  sub_atom(PostScheme, HostLen, _, _, '/')
	-> sub_atom(PostScheme, 0, HostLen, _, Host)
	;  sub_atom(PostScheme, 0, _, 0, Host)
	).
unit_test( uri_host_part('http://example.com/foo', 'example.com') ).
unit_test( uri_host_part('http://x.y/foo', 'x.y') ).
unit_test( uri_host_part('http://x.y', 'x.y') ).
unit_test( \+ uri_host_part('htxtp://x.y', _) ).

% errr, really this is Post_Host, not just path....
uri_path(URI, Path) :-
	% assumes Absolute URI (no frag)
	atom_concat('http://', PostScheme, URI),
	(  sub_atom(PostScheme, HostLen, _, _, '/')
	-> sub_atom(PostScheme, HostLen, _, 0, Path)
	;  Path='/'
	).
unit_test( uri_path('http://example.com/foo', '/foo') ).
unit_test( uri_path('http://x.y/foo', '/foo') ).
unit_test( uri_path('http://x.y', '/') ).
unit_test( \+ uri_path('htxtp://x.y', _) ).

uri_stem(URI, Stem) :-
	(   once(sub_atom(URI, Pos, 1, _,'#'))
	->  sub_atom(URI, 0, Pos, _, Stem)
	;   Stem = URI
	).
unit_test( uri_stem('http://example.com/foo#bar#baz',
		    'http://example.com/foo')  ).

