alice
library
manual.

Alice Project

The Url structure


________ Synopsis ____________________________________________________

    signature URL
    structure Url : URL

This structure provides functions for parsing URLs, extracting URL constituents, constructing URLs from constituents and resolving relative URLs. Absolute and relative URLs are supported. URL parsing conforms to RFCs 1738 and 1808 with one exception: We allow a URL to contain a single-letter device constituent before the path, which is a mostly-conservative common extension. Device letters enable the embedding of Windows-style path and file names within the set of URLs.

See also: OS.Path, HASHABLE, ORDERED


________ Import ______________________________________________________

    import structure Url from "x-alice:/lib/system/Url"
    import signature URL from "x-alice:/lib/system/URL-sig"

________ Interface ___________________________________________________

    signature URL =
    sig
	eqtype url
	type t = url

	type scheme = string option
	type authority = string option
	type device = char option
	type path = string list
	type query = string option
	type fragment = string option

	exception Malformed
	exception NotLocal

	val empty : url
	val setScheme : url * scheme -> url
	val setAuthority : url * authority -> url
	val setDevice : url * device -> url
	val makeAbsolutePath : url -> url
	val makeRelativePath : url -> url
	val setPath : url * path -> url
	val setQuery : url * query -> url
	val setFragment : url * fragment -> url

	val getScheme : url -> scheme
	val getAuthority : url -> authority
	val getDevice : url -> device
	val isAbsolutePath : url -> bool
	val getPath : url -> path
	val getQuery : url -> query
	val getFragment : url -> fragment

	val fromString : string -> url
	val toString : url -> string
	val toStringRaw : url -> string
	val toLocalFile : url -> string
	val isAbsolute : url -> bool
	val resolve : url -> url -> url
	val equal : url * url -> bool
	val compare : url * url -> order
	val hash : url -> int
    end

________ Description _________________________________________________

type url
type t = url

The type of parsed URLs. Values of this type represent absolute as well as relative URLs. The equivalence test using = is reliable for absolute URLs only. URL values are always normalized.

type scheme = string option
type authority = string option
type device = char option
type path = string list
type query = string option
type fragment = string option

The types of the respective URL constituents. Option types are used to indicate the presence or absence of optional indiviual constituents. Device letters are always normalized to their lower-case equivalent, that is, are always in the range #"a" ... #"z" if present. The absent path is equivalent to the empty path, represented by nil. Path constituents can contain empty strings. The last element of a path constituent being the empty string represents the absence of a file name constituent (that is, the string representation of the path constituent ends in a slash). Constituents use no encoding except for the query, which has to encode #"=" and #"&". The only URL constituent for which there is no explicit type defined is the flag whether the path constituent is absolute or relative, that is, whether its string representation starts with a slash or not.

exception Malformed

indicates that a string is not a well-formed URL in string representation, or that a URL constituent has no well-formed string representation.

exception NotLocal

raised by toLocalFile to indicate that a URL does not have a local file name equivalent.

empty

represents the empty URL, which has all constituents absent resp. empty. Its string representation is the empty string.

setScheme (url, x)
setAuthority (url, x)
setDevice (url, x)
setQuery (url, x)

return a URL with the corresponding constituent replaced. If x is SOME _, this causes the constituent to be present in the result, if it is NONE, the constituent is absent in the result. Raise Malformed if x is not a valid value for the constituent.

makeAbsolutePath url
makeRelativePath url

return a URL equivalent to url except that its path constituent is absolute resp. relative.

setPath (url, x)
setFragment (url, x)

return a URL with the corresponding constituent replaced. If x is SOME _, this causes the constituent to be present in the result, if it is NONE, the constituent is absent in the result.

getScheme url
getAuthority url
getDevice url
getPath url
getQuery url
getFragment url

return the corresponding constituents of url. For optional constituents, return SOME _ if the constituent is present, NONE otherwise.

isAbsolutePath url

returns true if the path constituent of url represents an absolute path, that is, a path whose string representation starts with a slash, false otherwise.

fromString s

parses s as a URL in string representation, raising Malformed if it is not well-formed. The resulting URL is normalized and returned.

toString url

converts url into its string representation. Characters within constituents are encoded as required by RFC 1738.

toStringRaw url

converts url into its string representation, without encoding any characters. Should only be used to construct messages and if the URL is known not to contain any control characters.

toLocalFile url

if possible, converts url to a local file name, else raises the NotLocal exception. This operation

isAbsolute url

returns true if url represents an absolute URL, false otherwise. An URL is absolute if at least one of scheme or device is present or if the path constituent is an absolute path or starts with ".", ".." or the character #"~".

resolve baseUrl relUrl

resolves relUrl with respect to baseUrl and returns the resulting URL. baseUrl should be an absolute URL, although this is not required.

equal (url1, url2)

returns true if url1 and url2 represent the same URL, false otherwise. Is identical to

url1 = url2
compare (url1, url2)

is equivalent to

String.compare (toString url1, toString url2)
hash url

returns a hash value for url.



last modified 2007/Mar/30 17:10