CL-HTTP Chapter 4: URL System


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


4.1 URL Class Hierarchy

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.

4.1.1 Core Class Tree

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

4.1.2 Mixins

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
Provides a 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
Extends computed-url-mixin with a server slot for form POST handling. Inherited by http-form and http-computed-form.
url:expiration-mixin
Provides 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
Provides secure-subnets, read-subnets, and write-subnets slots. Already incorporated into http-url. Controls subnet-based access restrictions.
url:authentication-mixin
Provides authentication-realm and capabilities slots. Already incorporated into http-url. Controls user authentication requirements.
url:content-language-mixin
Provides a languages slot. Already incorporated into http-url. Controls the Content-Language header.
url:http-cache-control-mixin
Provides a directives slot for HTTP/1.1 cache control. Already incorporated into http-url. Controls the Cache-Control response header.
url:documentation-mixin
Allows keywords and a documentation string to be associated with a URL. Inherited by url:uri and thus available on all URLs.
url:server-timeout-mixin
Provides life-time and timeout slots for per-URL connection timeout control. Already incorporated into http-url.
url:translation-method-mixin
Records the export type (translation method) and original class. Allows URLs to revert when re-exported with a different type. Already incorporated into http-url.
url:reverse-proxy-mixin
Provides forwardings and superior slots for reverse proxy URL classes.
url:template-mixin
Provides template-parameters, template-lock, and template-update-time slots for server-parsed HTML (SHTML) templates.
url:file-data-caching-mixin
Provides data-universe, dc-revalidation-interval, and dc-wired-p slots for the data cache subsystem that keeps file contents in memory.

4.1.3 HTTPS Parallels

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.

4.2 Interning URLs

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.

4.2.1 url:intern-url

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)
Returns the interned URL if it exists; otherwise creates and interns a new URL object. The class of the created URL is determined by the URL scheme and path structure.
:soft
Returns the interned URL if it exists; otherwise returns nil. Never creates a new URL.
:error
Returns the interned URL if it exists; otherwise signals url:no-interned-url-found.
:uninterned
Returns a fresh, uninterned URL object regardless of whether an interned one already exists. Useful for temporary URL objects.

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)

4.2.2 url:unintern-url

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")

4.2.3 Related Functions

url:url-interned-p url => boolean
Returns non-null if the URL is interned in the global table.
url:map-url-table function
Calls function with each interned URL object. Useful for iterating over all known URLs.
url:gc-url-table
Garbage collects the URL table, removing URLs that are no longer exported.

4.3 Exporting URLs

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.

4.3.1 http:export-url

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.

4.3.2 http:define-url-exports

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))

4.3.3 The #u Reader Macro

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)

4.3.4 Static File Export Types

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.

Text Files

:html-file (&key pathname)
Serves an HTML file as text/html; charset=utf-8. Recognizes extensions .html and .htm.
:text-file (&key pathname)
Serves a plain text file as text/plain; charset=utf-8. Recognizes extensions .text, .txt, .lisp, .c, .h, .sh, .script, .bib.
:lisp-file (&key pathname)
Serves a Lisp source file as text/plain; charset=utf-8. Recognizes extensions .lisp, .lsp, .cl, .asd.
:xml-file (&key pathname)
Serves an XML file as text/xml. Recognizes extensions .xml, .rdf, .daml, .opml.
:css-file (&key pathname)
Serves a CSS stylesheet as text/css. Recognizes extension .css.
:rtf-file (&key pathname)
Serves a Rich Text Format file.
:mark-down-file (&key pathname)
Serves raw Markdown source as text/markdown. Recognizes extension .md.
:markdown-html-file (&key pathname)
Converts Markdown source to HTML and serves the result as text/html. The converted HTML is cached alongside the source and regenerated only when the source changes.

Image Files

:gif-image (&key pathname)
Serves a GIF image as image/gif.
:jpeg-image (&key pathname)
Serves a JPEG image as image/jpeg. Recognizes extensions .jpeg, .jpe, .jpg.
:png-image (&key pathname)
Serves a PNG image as image/png.
:x-bitmap-image (&key pathname)
Serves an X bitmap image as image/x-xbitmap.
:pict-image (&key pathname)
Serves a PICT image.
:tiff-image (&key pathname)
Serves a TIFF image.
:ico-image (&key pathname)
Serves an ICO image (favicons).

Audio Files

:basic-audio (&key pathname)
Serves basic audio.
:aiff-audio (&key pathname)
Serves AIFF audio.
:wav-audio (&key pathname)
Serves WAV audio.
:real-audio (&key pathname)
Serves RealAudio.
:talk-audio (&key pathname)
Serves MacinTalk speech synthesis audio.

Video Files

:mpeg-video (&key pathname)
Serves MPEG video.
:quicktime-video (&key pathname)
Serves QuickTime video.

Application Files

:pdf-file (&key pathname)
Serves a PDF document as application/pdf.
:postscript-file (&key pathname)
Serves a PostScript file.
:word-file (&key pathname)
Serves a Microsoft Word document.
:power-point-file (&key pathname)
Serves a Microsoft PowerPoint file.
:excel-file (&key pathname)
Serves a Microsoft Excel file.
:binhex-file (&key pathname)
Serves a BinHex-encoded file.
:stuffit-file (&key pathname)
Serves a StuffIt archive.
:compressed-file (&key pathname)
Serves a compressed file.
:mac-binary-file (&key pathname)
Serves a MacBinary file.
:shockwave-file (&key pathname)
Serves a Shockwave file.
:vrml-world (&key pathname)
Serves a VRML world description.

Java and Scripting Files

:java-file (&key pathname)
Serves Java source code.
:java-binary (&key pathname)
Serves compiled Java class files (applet bytecode).
:java-script-file (&key pathname)
Serves JavaScript source.
:tcl-file (&key pathname)
Serves a Tcl script.
:visual-basic-script-file (&key pathname)
Serves a Visual Basic Script file.

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")

4.3.5 Directory Export Types

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)
Exports all files whose resource type is known.
:html-directory (&key pathname immediate-export recache recursive-p)
Exports only HTML files within the directory.
:text-directory (&key pathname immediate-export recache recursive-p)
Exports only text files.
:lisp-directory (&key pathname immediate-export recache recursive-p)
Exports only Lisp files.
:image-directory (&key pathname immediate-export recache recursive-p)
Exports only image files.
:audio-directory (&key pathname immediate-export recache recursive-p)
Exports only audio files.
:video-directory (&key pathname immediate-export recache recursive-p)
Exports only video files.
:world-directory (&key pathname immediate-export recache recursive-p)
Exports only VRML world files.
:application-directory (&key pathname immediate-export recache recursive-p)
Exports only application files.

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))

4.3.6 Redirect Export Types

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)
Issues a 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)
Issues a 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"))

4.3.7 Computed Export Types

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)
The response function is called with (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)
Responds to GET by serving the contents of :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)
A combination of :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)
Exports a search URL that handles queries passed via the ? 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)
Exports an image as a server-side image map. The image in :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)
Exports a client-side script defined with html:define-script. The URL class is url:http-client-script.
:shtml-file (&key pathname header-function character-set)
Exports a server-parsed HTML file containing SHTML directives delimited by <!--# 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.

4.3.8 Reverse Proxy Export Type

:reverse-proxy (&key proxy-forwardings)
Configures a URL as a reverse proxy entry point. :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.

4.3.9 Common Keyword Arguments

These keyword arguments are accepted by most or all export types, applied via :before and :after methods on http:export-url.

:pathname pathname-designator
The file system pathname for the resource. Required for static file export types. May use logical pathnames (e.g., "http:www;index.html").
:response-function function-designator
A function that computes the response. The signature depends on the export type: (url stream) for :computed and :search; (url stream form-alist) for :html-form.
:header-function function-designator
Called with (url) to compute response headers for the HEAD method. Returns (content-type content-length last-modification version charset public allow).
:expiration expiration-spec
Controls the Expires header sent to clients. Accepted values:
:cache-control directive-plist
HTTP/1.1 Cache-Control directives as a property list. Example: (:max-age 3600 :public t).
:authentication-realm realm
An authentication realm obtained via http:intern-realm. Requires users to authenticate to access the URL.
:capabilities access-control
A URL access control obtained via http:intern-access-control. Specifies which groups or users may access the resource.
:read-subnets subnet-spec
A list of IP addresses (with 0 as wildcard) that may perform read operations (GET, HEAD, POST, OPTIONS, TRACE).
:write-subnets subnet-spec
A list of IP addresses that may perform write operations (PUT, DELETE). Write access presumes read access.
:language language-keywords
A list of language keywords (e.g., (:en) or (:en-us :fr)) conforming to RFC 1766. Controls the Content-Language header.
:character-set charset-keyword
Character set for text content types. Valid values include :us-ascii, :iso-8859-1, :utf-8, :utf-16, and others registered with IANA.
:keywords keyword-list
A list of keywords associated with the URL for documentation and search purposes.
:documentation string
A descriptive string for the URL.
:directory-writer function-designator
For path (directory) URLs only. A function called with (url stream) that generates a custom directory listing. Returns non-null if it handles the response.
:life-time milliseconds-or-function
Maximum lifetime for an HTTP connection serving this URL. An integer is milliseconds; a function is called with (url) and returns an integer or nil for the global default.
:timeout sixtieths-or-function
Maximum idle time before an HTTP connection is scavengeable, in 60ths of a second. An integer, nil, or a function of (url).
:property-list plist
Additional properties stored on the URL as a property list.

4.4 URL Accessors

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.

4.4.1 Identity and Naming

url:name-string url &optional compute-p => string
Returns the canonical name string for the URL (e.g., "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
Returns a URL string from a URL object or string. When escaped-p controls whether special characters are escaped in the result.
url:display-string url => string
Returns a human-readable display string for the URL, typically the title. Defaults to the name string if no display string has been set.
url:relative-name-string url => string
Returns the URL string relative to the server's local context (i.e., without the scheme and host portion).
url:name-string-without-extension url => string
Returns the name string with the file extension removed.
url:name-string-without-search-suffix url &optional include-suffix-delimiter-p => string
Returns the name string with the search suffix (everything after ?) removed.

4.4.2 URL Components

url:scheme url => string
Returns the scheme string (e.g., "http", "https", "ftp").
url:protocol url => keyword
Returns the protocol keyword (e.g., :http, :https, :ftp).
url:host-string url => string
Returns the hostname portion of the URL.
url:host-object url => host
Returns the host object associated with the URL.
url:port url => integer
Returns the port number.
url:host-port url => integer
Returns the port number (same as url:port in most contexts).
url:standard-port url => integer
Returns the default port for the URL's protocol (80 for HTTP, 443 for HTTPS, 21 for FTP, etc.).
url:path url => list
Returns the path component as a list of directory name strings.
url:object url => string-or-nil
Returns the object (file) name, or nil for path URLs.
url:extension url => string-or-nil
Returns the file extension, or nil.

4.4.3 Resource Attributes

url:translated-pathname url => pathname
Returns the file system pathname that the URL maps to. The pathname is computed from the path, object, and extension components and cached.
url:cached-pathname url => pathname-or-nil
Returns the cached pathname, or nil if none has been computed yet.
url:translation-method url => keyword-or-nil
Returns the export type keyword (e.g., :html-file, :computed), or nil if the URL has not been exported.
url:response-function url => function-or-nil
Returns the response function for computed URL types.
url:form-function url => function-or-nil
Returns the form-generating function for :computed-form URLs.
url:header-function url => function-or-nil
Returns the header function for HEAD method responses.
url:expiration-universal-time url => universal-time-or-nil
Returns the universal time when the URL's data becomes invalid. This is the standard way to compute the :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
Returns the number of seconds until the URL's data becomes invalid.
url:response-cache-control-directives url => plist
Returns cache control directives as a property list for use with 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
Returns the raw cache control directives stored on the URL.

4.4.4 Security Attributes

url:secure-subnets url => list
Returns the merged list of secure subnets (both read and write).
url:read-subnets url => list
Returns the list of subnets allowed read access.
url:write-subnets url => list
Returns the list of subnets allowed write access.
url:authentication-realm url => realm-or-nil
Returns the authentication realm for the URL.
url:capabilities url => access-control-or-nil
Returns the access control capabilities for the URL.

4.4.5 Search Attributes

url:search-keys url => list
Returns the parsed search parameters from the URL's ? 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
Returns the search database associated with the URL.
url:search-parent url => url-or-nil
Returns the parent URL of a search URL (the URL without the search suffix).
url:search-parser url => function
Returns the search parser function for the URL.
url:search-writer url => function
Returns the search writer function for the URL.

4.4.6 Hierarchy Navigation

url:url-superior url => url
Returns the URL that is the immediate directory superior of url. If url is the root URL, returns itself.
url:map-url-inferiors function url
Calls function with each inferior URL registered under url.
url:local-url-p url => boolean
Returns non-null if the URL refers to the local host.
url:root-url-p url => boolean
Returns non-null if the URL is a root URL (no path beyond the host).

4.4.7 Content Attributes

url:languages url => list
Returns the list of language keywords for the URL.
url:character-set url => keyword-or-nil
Returns the character set keyword for the URL.
url:content-type-keyword url => keyword
Returns a keyword denoting the MIME content type.
url:copy-mode url => keyword
Returns the copy mode for transferring the URL's data: :text, :binary, or :crlf.
url:alternate-urls url => list
Returns the list of alternate URLs (for redirect types and content negotiation).
url:keywords url => list
Returns the documentation keywords associated with the URL.

4.4.8 Performance Metering

url:url-cpu-time url => integer
Returns the cumulative CPU time spent serving this URL.
url:url-elapsed-time url => integer
Returns the cumulative elapsed time spent serving this URL.
url:url-n-requests url => integer
Returns the number of requests served for this URL.
url:average-response-times url => cpu-average, elapsed-average
Returns the average CPU and elapsed times per request.
url:clear-response-times url
Resets the metering counters for the URL.

4.5 Search Parsers

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.

4.5.1 Predefined Parsers

url:standard-parse-search-info string &optional (start 0) (end (length string)) => search-keys
The default parser. Examines the search info to determine whether it encodes form data or list data, then dispatches to the appropriate specialized parser. This is the parser used when no :search-parser is specified at export time.
url:parse-search-info url-string &optional (start 0) (end (length url-string)) => search-keys
Parses the search component using standard search rules. Uses + 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
Parses the search component using URL-encoded form rules with & 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
Like 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.

4.5.2 Predefined Writers

Search writers serialize parsed search keys back into URL query string format. They are the inverse of the parsers.

url:standard-write-search-info
The default writer. Dispatches to the appropriate specialized writer based on the format of the search keys.
url:write-search-info search-keys &optional (stream *standard-output*) (escape-p *escape-search-urls*)
Writes search keys as a +-delimited list.
url:write-search-info-as-query-alist
Writes search keys as &-delimited name=value pairs.
url:write-search-info-as-xml-safe-query-alist
Writes search keys as ;-delimited name=value pairs.

4.5.3 Custom Parsers

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)

4.6 URL Translation

CL-HTTP provides functions for translating between file system pathnames and URL strings, and for accessing the file system location mapped to a URL.

4.6.1 Pathname to URL

url:pathname-file-url-string pathname &optional force-directory-p => string
Returns a 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"

4.6.2 URL to Pathname

url:file-url-pathname url => pathname
Returns a Common Lisp pathname for a file:// URL. Works on both file-directory and file-pathname URL objects.
url:translated-pathname url => pathname
Returns the file system pathname that an HTTP URL maps to, based on the URL's path, object, and extension components. This is the pathname that the server uses to locate the file when serving static content. The result is cached on the URL object.

4.6.3 URL String Construction

url:make-url-string
Constructs a URL string from individual components.
url:local-context-string url => string
Returns the local context string for the URL (scheme + host + port).
url:directory-context-string url => string
Returns the directory context string (everything up to and including the last /).
url:canonicalize-url url-string => string
Returns a canonical form of the URL string with consistent scheme, host, and port formatting.

4.6.4 URL Comparison

url:url-equal url1 url2 => boolean
Returns non-null if two URLs are equivalent. Compares by name string after canonicalization.
url:host-match-p url host => boolean
Returns non-null if the URL's host matches the given host.

Manual Index | Prev: Your First Application | Next: HTTP Server