The URL system is the foundation of CL-HTTP's resource management.
Every HTTP resource is represented as a CLOS object in a class hierarchy
rooted at url:url. URLs are interned in a global table,
exported to make them accessible via HTTP, and dispatched to response
functions based on their class and translation method. This chapter
documents the URL class hierarchy, interning protocol, export types,
accessors, search parsing, and pathname translation.
Manual Index | Prev: Your First Application | Next: HTTP Server
Every URL in CL-HTTP is a CLOS instance. The class hierarchy
determines what kind of resource a URL represents and what HTTP methods
it supports. The root classes are url:uri (Uniform
Resource Identifier) and url:url (Uniform Resource
Locator). Protocol-specific URL classes inherit from url:url
and incorporate mixins that provide additional capabilities such as
authentication, caching, and expiration control.
The following indented list shows the primary URL class hierarchy.
Classes marked with (M) are mixins that can be combined
with the concrete URL classes.
url:uri
url:url
url:http-url
url:http-path ; directories
url:http-minimum-object ; base for objects with name + extension
url:http-object ; standard HTTP objects (files)
url:http-computed-url ; :computed export type
url:http-search ; :search export type
url:http-searchable-object ; searchable static objects (e.g., image maps)
url:http-form ; :html-form POST handler
url:http-computed-form ; :computed-form GET + POST
url:http-template-object ; :shtml-file server-parsed HTML
url:http-virtual-object ; objects without pathname slots
url:http-dynamic-form ; COMLINK dynamic forms
url:http-client-script ; client-side scripts
url:https-url
url:https-path
url:https-minimum-object
url:https-object
url:https-computed-url
url:https-search
url:https-searchable-object
url:https-form
url:https-computed-form
url:https-template-object
url:https-virtual-object
url:https-dynamic-form
url:https-client-script
url:ftp-url
url:ftp-directory
url:ftp-pathname
url:gopher-url
url:gopher-path
url:gopher-object
url:gopher-search
url:news-url
url:news-group
url:news-article
url:wais-url
url:wais-search
url:wais-doc
url:telnet-url
url:rtsp-url
url:rtsp-directory
url:rtsp-pathname
url:mailto-url
url:mail-address
Mixins are incorporated into the URL class hierarchy through
multiple inheritance. They provide orthogonal capabilities that apply
across different URL types. The most important mixins are already woven
into the standard http-url class; others are incorporated
when a URL is specialized during export.
url:computed-url-mixin
response-function slot and a
header-function slot. Inherited by all URL classes that
compute dynamic responses: http-computed-url,
http-search, http-form, and their HTTPS
counterparts.
url:form-processing-mixin
computed-url-mixin with a server
slot for form POST handling. Inherited by http-form and
http-computed-form.
url:expiration-mixin
expiration-function and
max-age-function slots. Already incorporated into
http-url. Controls the Expires and
Cache-Control: max-age headers.
url:secure-subnets-mixin
secure-subnets, read-subnets,
and write-subnets slots. Already incorporated into
http-url. Controls subnet-based access restrictions.
url:authentication-mixin
authentication-realm and
capabilities slots. Already incorporated into
http-url. Controls user authentication requirements.
url:content-language-mixin
languages slot. Already incorporated
into http-url. Controls the
Content-Language header.
url:http-cache-control-mixin
directives slot for HTTP/1.1 cache
control. Already incorporated into http-url. Controls
the Cache-Control response header.
url:documentation-mixin
url:uri and thus available
on all URLs.
url:server-timeout-mixin
life-time and timeout slots
for per-URL connection timeout control. Already incorporated into
http-url.
url:translation-method-mixin
http-url.
url:reverse-proxy-mixin
forwardings and superior slots
for reverse proxy URL classes.
url:template-mixin
template-parameters,
template-lock, and template-update-time
slots for server-parsed HTML (SHTML) templates.
url:file-data-caching-mixin
data-universe,
dc-revalidation-interval, and dc-wired-p
slots for the data cache subsystem that keeps file contents in memory.
Every HTTP URL class has an HTTPS counterpart that inherits from
both the HTTP class and url:https-url. For example,
url:https-computed-form inherits from both
url:http-computed-form and url:https-form.
The HTTPS classes use the https scheme (port 443) and
inherit the secure-protocol-url mixin. When exporting a
URL, the system automatically selects the correct HTTPS class when the
URL string specifies the https:// scheme.
URLs are interned in a global hash table, analogous to how symbols
are interned in packages. The function url:intern-url
creates or retrieves URL objects. Interning ensures that each URL
string maps to exactly one CLOS object, enabling identity comparison
with eq and efficient property storage on URL objects.
url:intern-url url &key (if-does-not-exist :create) (start 0) end => url, newly-interned-p
Top-level function for interning a URL (Universal Resource
Locator). URL is a string containing the URL to intern.
The :if-does-not-exist keyword controls behavior when the
URL is not already interned:
:create (default)
:soft
nil. Never creates a new URL.
:error
url:no-interned-url-found.
:uninterned
The intern operation automatically escapes search URL suffixes when
creating URLs to ensure valid URL strings. The variable
url:*escape-search-urls* can be bound to nil
around the call to suppress this escaping.
The start and end keyword arguments allow
interning a substring of the URL string, avoiding the need to copy
substrings before interning.
Examples:
;; Create or retrieve a URL object
(url:intern-url "http://www.example.com/index.html")
;; Soft lookup -- returns nil if not found
(url:intern-url "http://www.example.com/missing.html"
:if-does-not-exist :soft)
;; Error if not found
(url:intern-url "http://www.example.com/required.html"
:if-does-not-exist :error)
url:unintern-url url => boolean
Removes url from the global URL table.
url may be either a URL object or a string. Returns
t if the URL was found and removed, nil
otherwise.
(url:unintern-url "http://www.example.com/old-page.html")
url:url-interned-p url => boolean
url:map-url-table function
function with each interned URL object. Useful
for iterating over all known URLs.
url:gc-url-table
http:export-url is the primary interface for making
URLs accessible via the HTTP server. Exporting a URL registers it for
HTTP service and configures how requests to that URL are handled. The
export type determines what class of URL object is created and what
response methods apply.
http:export-url url export-type &rest args => url
URL is either a string or an interned URL object.
EXPORT-TYPE is a keyword specifying the kind of resource.
If url is a string, it is first interned with
url:intern-url. Additional keyword arguments in
args configure the exported URL.
A URL may be re-exported with a different export type; doing so reverts the URL to its origin class and re-specializes it.
http:define-url-exports &body entries
A convenience macro that expands into multiple calls to
http:export-url. Each entry in entries is a
list (url export-type &rest args).
(http:define-url-exports (#u"/index.html" :html-file :pathname "http:www;index.html") (#u"/style.css" :css-file :pathname "http:www;style.css") (#u"/search?" :search :response-function #'search-handler))
The #u reader macro constructs a fully qualified URL
string by merging a relative URI against the local host's default
context. This makes export files portable across hosts.
;; Simple form -- merges against local host
#u"/path/to/resource.html"
;; Extended form -- override host and port
#u("/path/to/resource.html" :host "example.com" :port 8080)
These export types serve the contents of a file. They require URLs
that are objects (i.e., have a name and extension). All accept
:pathname to specify the file, plus the common keyword
arguments described in Section 4.3.9.
:html-file (&key pathname)
text/html; charset=utf-8.
Recognizes extensions .html and .htm.
:text-file (&key pathname)
text/plain; charset=utf-8.
Recognizes extensions .text, .txt,
.lisp, .c, .h,
.sh, .script, .bib.
:lisp-file (&key pathname)
text/plain; charset=utf-8.
Recognizes extensions .lisp, .lsp,
.cl, .asd.
:xml-file (&key pathname)
text/xml. Recognizes extensions
.xml, .rdf, .daml,
.opml.
:css-file (&key pathname)
text/css. Recognizes
extension .css.
:rtf-file (&key pathname)
:mark-down-file (&key pathname)
text/markdown.
Recognizes extension .md.
:markdown-html-file (&key pathname)
text/html. The converted HTML is cached alongside the
source and regenerated only when the source changes.
:gif-image (&key pathname)
image/gif.
:jpeg-image (&key pathname)
image/jpeg. Recognizes
extensions .jpeg, .jpe, .jpg.
:png-image (&key pathname)
image/png.
:x-bitmap-image (&key pathname)
image/x-xbitmap.
:pict-image (&key pathname)
:tiff-image (&key pathname)
:ico-image (&key pathname)
:basic-audio (&key pathname)
:aiff-audio (&key pathname)
:wav-audio (&key pathname)
:real-audio (&key pathname)
:talk-audio (&key pathname)
:mpeg-video (&key pathname)
:quicktime-video (&key pathname)
:pdf-file (&key pathname)
application/pdf.
:postscript-file (&key pathname)
:word-file (&key pathname)
:power-point-file (&key pathname)
:excel-file (&key pathname)
:binhex-file (&key pathname)
:stuffit-file (&key pathname)
:compressed-file (&key pathname)
:mac-binary-file (&key pathname)
:shockwave-file (&key pathname)
:vrml-world (&key pathname)
:java-file (&key pathname)
:java-binary (&key pathname)
:java-script-file (&key pathname)
:tcl-file (&key pathname)
:visual-basic-script-file (&key pathname)
Example: Exporting static files
(http:export-url #u"/index.html"
:html-file
:pathname "http:www;index.html"
:expiration '(:interval 86400)
:language '(:en))
(http:export-url #u"/images/logo.png"
:png-image
:pathname "http:www;images;logo.png")
Directory export types serve all files within a directory whose
content type matches the export category. These apply to URL paths
(directories), not objects. They accept :pathname,
:immediate-export, :recache, and
:recursive-p.
:directory (&key pathname immediate-export recache recursive-p)
:html-directory (&key pathname immediate-export recache recursive-p)
:text-directory (&key pathname immediate-export recache recursive-p)
:lisp-directory (&key pathname immediate-export recache recursive-p)
:image-directory (&key pathname immediate-export recache recursive-p)
:audio-directory (&key pathname immediate-export recache recursive-p)
:video-directory (&key pathname immediate-export recache recursive-p)
:world-directory (&key pathname immediate-export recache recursive-p)
:application-directory (&key pathname immediate-export recache recursive-p)
When :recursive-p is non-null, all subdirectories are
also exported. When http:*auto-export* is non-null, new
files added to an exported directory are automatically exported on
demand. When :immediate-export is non-null, all files are
exported at definition time rather than on first access.
A directory-style list in HTML is the default content returned for
the GET method on a URL directory path. This is customized via the
:directory-writer keyword, which provides a function
called with (url stream) that returns non-null when it
handles the response.
http:write-indexed-directory-listing is a built-in
directory writer that serves the contents of an index.html
file found in the directory.
Example: Exporting a directory hierarchy
(http:export-url #u"/docs/"
:directory
:pathname "http:www;docs;"
:recursive-p t
:immediate-export t
:expiration '(:interval 3600))
Redirect export types instruct the client to request the resource from another location. They work for all HTTP methods including GET and HEAD.
:redirect (&key alternate-urls)
301 Moved Permanently redirect. The
exported URL can be either an HTTP object or an HTTP path.
:alternate-urls is a list of one or more target URL
strings.
:temporary-redirect (&key alternate-urls)
302 Found (temporary) redirect.
:alternate-urls is a list of target URL strings.
(http:export-url #u"/old-page.html"
:redirect
:alternate-urls '("/new-page.html"))
Computed export types use Lisp functions to generate responses dynamically. These are the primary mechanism for building web applications in CL-HTTP.
:computed (&key response-function header-function)
(url stream) and
is responsible for returning content to the client, typically within
http:with-successful-response. An optional
:pathname may be provided if the computation references a
file. The URL class is url:http-computed-url.
Example:
(defun respond-to-hello (url stream)
(http:with-successful-response (stream :html)
(html:with-html-document (:stream stream)
(html:with-document-body (:stream stream)
(html:with-paragraph (:stream stream)
(write-string "Hello, World!" stream))))))
(http:export-url #u"/hello.html"
:computed
:response-function #'respond-to-hello)
:html-form (&key response-function pathname durable-form-values server)
:pathname.
Responds to POST by calling response-function with
(url stream form-alist). The form-alist is
an alist of (query-keyword raw-value) pairs. When a query
has multiple values, they are aggregated into a list. Use
http:bind-query-values to bind form values to local
variables. When :durable-form-values is non-null, query
values are copied strings rather than indirect arrays into a volatile
buffer. The URL class is url:http-form.
Example:
(defun respond-to-form (url stream form-alist)
(http:bind-query-values (name email)
(url stream form-alist)
(http:with-successful-response (stream :html)
(html:with-html-document (:stream stream)
(html:with-document-body (:stream stream)
(html:with-paragraph (:stream stream)
(format stream "Thank you, ~A (~A)." name email)))))))
(http:export-url #u"/feedback.html"
:html-form
:pathname "http:www;feedback.html"
:response-function #'respond-to-form)
:computed-form (&key form-function response-function header-function durable-form-values server)
:computed and
:html-form. The form-function is called with
(url stream) to generate the HTML form for the GET method.
The response-function handles POST with the same signature
as :html-form. This handles content types
application/x-www-form-urlencoded and
application/json. The URL class is
url:http-computed-form.
Note: :html-computed-form is an older synonym for
:computed-form.
:search (&key response-function search-parser search-writer search-database header-function)
? suffix. The response function is called with
(url stream) and accesses search parameters via
url:search-keys and the search database via
url:search-database. The URL must end with ?
so the system can compose the right combination of classes.
See Section 4.5 for details on search
parsers and writers. The URL class is url:http-search.
Example:
(defun respond-to-search (url stream)
(let ((keys (url:search-keys url)))
(http:with-successful-response (stream :html)
(html:with-html-document (:stream stream)
(html:with-document-body (:stream stream)
(html:with-paragraph (:stream stream)
(format stream "Search terms: ~{~A~^, ~}" keys)))))))
(http:export-url #u"/search?"
:search
:response-function #'respond-to-search)
:image-map (&key pathname export-type map-format map-pathname search-parser search-writer header-function)
:pathname is served for GET requests without a search
suffix. When the client clicks on the image (sending coordinates via
?), the coordinates are looked up in the map file
specified by :map-pathname. :map-format is
either :cern or :ncsa.
:script (&key script header-function)
html:define-script. The URL class is
url:http-client-script.
:shtml-file (&key pathname header-function character-set)
<!--# and -->. The server
evaluates these directives and inserts computed results. Predefined
directives include include (file inclusion) and
eval (action evaluation). New actions are defined with
http:define-shtml-action. The URL class is
url:http-template-object.
:reverse-proxy (&key proxy-forwardings)
:proxy-forwardings is a list of
(domain-name port protocol) specifications identifying
origin servers. Requests to this URL and all inferior URLs are
forwarded to the origin servers. Only the most general URL needs to be
exported; inferior URLs are forwarded automatically.
These keyword arguments are accepted by most or all export types,
applied via :before and :after methods on
http:export-url.
:pathname pathname-designator
"http:www;index.html").
:response-function function-designator
(url stream) for :computed
and :search; (url stream form-alist) for
:html-form.
:header-function function-designator
(url) to compute response headers for
the HEAD method. Returns
(content-type content-length last-modification version charset public allow).
:expiration expiration-spec
Expires header sent to clients.
Accepted values:
:no-expiration-header -- no Expires header is issued.
:never -- expires one year from now.
(:interval seconds) -- expires now + seconds.
(:time universal-time) -- expires at the given time.
(:function function-spec) -- function-spec is called with (url) and returns a universal time or nil.
:cache-control directive-plist
Cache-Control directives as a property list.
Example: (:max-age 3600 :public t).
:authentication-realm realm
http:intern-realm. Requires users to authenticate to
access the URL.
:capabilities access-control
http:intern-access-control. Specifies which groups or
users may access the resource.
:read-subnets subnet-spec
:write-subnets subnet-spec
:language language-keywords
(:en) or
(:en-us :fr)) conforming to RFC 1766. Controls the
Content-Language header.
:character-set charset-keyword
:us-ascii, :iso-8859-1,
:utf-8, :utf-16, and others registered with
IANA.
:keywords keyword-list
:documentation string
:directory-writer function-designator
(url stream) that generates a custom directory listing.
Returns non-null if it handles the response.
:life-time milliseconds-or-function
(url)
and returns an integer or nil for the global default.
:timeout sixtieths-or-function
(url).
:property-list plist
URL objects provide a rich set of accessor generic functions for reading and modifying their attributes. These are the primary accessors used in response functions and server internals.
url:name-string url &optional compute-p => string
"http://www.example.com:80/path/file.html"). When
compute-p is non-null, the name string is recomputed from
the parsed URL components rather than returned from the cache.
url:coerce-url-string url &optional escaped-p => string
escaped-p controls whether special characters are escaped
in the result.
url:display-string url => string
url:relative-name-string url => string
url:name-string-without-extension url => string
url:name-string-without-search-suffix url &optional include-suffix-delimiter-p => string
?) removed.
url:scheme url => string
"http",
"https", "ftp").
url:protocol url => keyword
:http,
:https, :ftp).
url:host-string url => string
url:host-object url => host
url:port url => integer
url:host-port url => integer
url:port in
most contexts).
url:standard-port url => integer
url:path url => list
url:object url => string-or-nil
url:extension url => string-or-nil
url:translated-pathname url => pathname
url:cached-pathname url => pathname-or-nil
url:translation-method url => keyword-or-nil
:html-file,
:computed), or nil if the URL has not been exported.
url:response-function url => function-or-nil
url:form-function url => function-or-nil
:computed-form URLs.
url:header-function url => function-or-nil
url:expiration-universal-time url => universal-time-or-nil
:expires argument
for http:with-successful-response or
http:with-conditional-get-response. Returns nil if no
expiration is defined.
url:max-age-seconds url => integer-or-nil
url:response-cache-control-directives url => plist
http:with-successful-response or
http:with-conditional-get-response. Automatically
includes proxy revalidation directives when the URL has subnet
security or user authentication.
url:cache-control-directives url => plist
url:secure-subnets url => list
url:read-subnets url => list
url:write-subnets url => list
url:authentication-realm url => realm-or-nil
url:capabilities url => access-control-or-nil
url:search-keys url => list
? suffix. The format depends on the search parser: either
a list of strings (standard parser) or an alist of
(keyword value) pairs (query-alist parser).
url:search-database url => object-or-nil
url:search-parent url => url-or-nil
url:search-parser url => function
url:search-writer url => function
url:url-superior url => url
url. If url is the root URL, returns itself.
url:map-url-inferiors function url
function with each inferior URL registered under
url.
url:local-url-p url => boolean
url:root-url-p url => boolean
url:languages url => list
url:character-set url => keyword-or-nil
url:content-type-keyword url => keyword
url:copy-mode url => keyword
:text, :binary, or :crlf.
url:alternate-urls url => list
url:keywords url => list
url:url-cpu-time url => integer
url:url-elapsed-time url => integer
url:url-n-requests url => integer
url:average-response-times url => cpu-average, elapsed-average
url:clear-response-times url
Search parsers control how the query string following the
? in a URL is parsed into Lisp data structures accessible
via url:search-keys. CL-HTTP provides several predefined
parsers and allows custom parsers to be supplied at export time via the
:search-parser keyword.
url:standard-parse-search-info string &optional (start 0) (end (length string)) => search-keys
:search-parser is specified at export time.
url:parse-search-info url-string &optional (start 0) (end (length url-string)) => search-keys
+ as the item delimiter. Returns a list of unescaped
strings.
For example, the URL /search?common+lisp+http yields
("common" "lisp" "http").
url:parse-search-info-as-query-alist string &optional (start 0) (end (length string)) => search-alist
& as the field delimiter. Returns an alist of
(keyword value) pairs. This is the appropriate parser when
an HTML form returns data to a search URL via the GET method.
For example, the URL
/search?name=John+Doe&lang=lisp yields
((:name "John Doe") (:lang "lisp")).
Note: The total URL length is limited to 1024 characters, making
this method unsuitable for large form submissions. Use POST
(:html-form) for large data.
url:parse-search-info-as-xml-safe-query-alist string &optional (start 0) (end (length string)) => search-alist
parse-search-info-as-query-alist but uses
semicolon (;) instead of ampersand (&) as
the field delimiter. Recommended for HTML 4.0 and XHTML documents
because it avoids interactions with SGML's use of ampersand in entity
references.
Search writers serialize parsed search keys back into URL query string format. They are the inverse of the parsers.
url:standard-write-search-info
url:write-search-info search-keys &optional (stream *standard-output*) (escape-p *escape-search-urls*)
+-delimited list.
url:write-search-info-as-query-alist
&-delimited
name=value pairs.
url:write-search-info-as-xml-safe-query-alist
;-delimited
name=value pairs.
A custom search parser is a function accepting
(url-string start end) where start and
end delimit the search suffix (after the ?).
The parser returns the parsed representation, which is stored in
url:search-keys.
;; A custom parser that splits on commas
(defun parse-comma-search (url-string &optional (start 0) (end (length url-string)))
(loop for s = start then (1+ e)
while (< s end)
for e = (or (position #\, url-string :start s :end end) end)
collect (subseq url-string s e)))
(http:export-url #u"/tag-search?"
:search
:response-function #'respond-to-tags
:search-parser #'parse-comma-search)
CL-HTTP provides functions for translating between file system pathnames and URL strings, and for accessing the file system location mapped to a URL.
url:pathname-file-url-string pathname &optional force-directory-p => string
file:// URL string for the given pathname.
The pathname's host component is used for the URL host; if the host is
unspecific or nil, the local host domain name is used. When
force-directory-p is non-null, the pathname is treated as
a directory even if it has a name component.
(url:pathname-file-url-string #p"/var/www/index.html") => "file://hostname/var/www/index.html"
url:file-url-pathname url => pathname
file:// URL.
Works on both file-directory and
file-pathname URL objects.
url:translated-pathname url => pathname
url:make-url-string
url:local-context-string url => string
url:directory-context-string url => string
/).
url:canonicalize-url url-string => string
url:url-equal url1 url2 => boolean
url:host-match-p url host => boolean
Manual Index | Prev: Your First Application | Next: HTTP Server