Parsing and Processing URLs

The url structure contains procedures to parse and unparse URLs. Until now, only the parsing of HTTP URLs is implemented.

4.1  Server Records

A server value describes path prefixes of the form user:password@host:port. These are frequently used as the initial prefix of URLs describing Internet resources.

(make-server user password host port)     --->     server         (procedure) 
(server? thing)     --->     boolean         (procedure) 
(server-user server)     --->     string-or-#f         (procedure) 
(server-password server)     --->     string-or-#f         (procedure) 
(server-host server)     --->     string-or-#f         (procedure) 
(server-port server)     --->     string-or-#f         (procedure) 
Make-server creates a new server record. Each slot is a decoded string or #f. (Port is also a string.)

server? is the corresponding predicate, server-user, server-password, server-host and server-port are the correspondig selectors.

(parse-server path default)     --->     server         (procedure) 
(server->string server)     --->     string         (procedure) 
Parse-server parses a URI path path (a list representing a path, not a string) into a server value. Default values are taken from the server default except for the host. The values are unescaped and stored into a server record that is returned. Fatal-syntax-error is called, if the specified path has no initial to slashes (i.e., it starts with `//...').

server->string just does the inverse job: it unparses server into a string. The elements of the record are escaped before they are put together.

Example:

> (define default (make-server "andreas"  "se ret" "www.sf.net" "80"))
> (server->string default)
"andreas:se%20ret@www.sf.net:80"
> (parse-server '("" "" "foo%20bar@www.scsh.net" "docu" "index.html") 
                default)
'#server
> (server->string ##)
"foo%20bar:se%20ret@www.scsh.net:80"

For details about escaping and unescaping see Chapter 3.

4.2  HTTP URLs

(make-http-url server path search frag-id)     --->     http-url         (procedure) 
(http-url? thing)     --->     boolean         (procedure) 
(http-url-server http-url)     --->     server         (procedure) 
(http-url-path http-url)     --->     list         (procedure) 
(http-url-search http-url)     --->     string-or-#f         (procedure) 
(http-url-fragment-identifier http-url)     --->     string-or-#f         (procedure) 
Make-http-url creates a new httpd-url record. Server is a record, containing the initial part of the address (like anonymous@clark.lcs.mit.edu:80). Path contains the URL's URI path ( a list). These elements are in raw, unescaped format. To convert them back to a string, use (uri-path->uri (map escape-uri pathlist)). Search and frag-id are the last two parts of the URL. (See Chapter 3 about parts of an URI.)

Http-url? is the predicate for HTTP URL values, and http-url-server, http-url-path, http-url-search and http-url-fragment-identifier are the corresponding selectors.

(parse-http-url path search frag-id)     --->     http-url         (procedure) 
(http-url->string http-url)     --->     string         (procedure) 
This constructs an HTTP URL record from a URI path (a list of path components), a search, and a frag-id component.

Http-url->string just does the inverse job. It converts an HTTP URL record into a string.

Note: The URI parser parse-uri maps a string to four parts: scheme, path, search and frag-id (see Section 3.2 for details). If scheme is http, then the other three parts can be passed to parse-http-url, which parses them into a http-url record. All strings come back from the URI parser encoded. Search and frag-id are left that way; this parser decodes the path elements. The first two list elements of the path indicating the leading double-slash are omitted.

The following procedure combines the jobs of parse-uri and parse-http-url:

(parse-http-url-string string)     --->     http-url         (procedure) 
This parses an HTTP URL and returns the corresponding URL value; it calls fatal-syntax-error if the URL string doesn't have an http scheme.