Reference Manual | Prev: Chapter 18. Server-Parsed HTML and CGI | Next: Appendix A. Quick Reference
CL-HTTP's URL export system is extensible. You can define new export types to serve new MIME content types, implement new response strategies, or wrap existing functionality with application-specific behavior.
Use http:define-url-export-type to register a new export
type with its MIME content type and file extension:
(http:define-url-export-type :shockwave-file ;; export type keyword :dir ;; primary file extension (:application :x-director) ;; MIME content type :copy-mode :binary ;; binary or text transfer :alternate-extensions (:dcr)) ;; other file extensions
This macro automatically defines the standard methods
(write-document, write-document-headers,
export-url) for the new type. The :copy-mode
controls whether the content is transferred as binary or text (with CRLF
canonicalization).
When no corresponding export type is required and you only need to
register a MIME type, use
http::define-mime-content-type.
For export types that need custom behavior beyond what
define-url-export-type provides, write methods on three
generic functions:
export-url (url translation &rest args)
(http:export-url #u"..." :my-type ...).
write-document-headers (url translation stream)
write-document (url translation stream)
post-document method instead.
;; Define a custom export type for JSON API endpoints
(defmethod http:export-url ((url url:http-computed-url)
(translation (eql :json-api))
&rest args)
(destructuring-bind (&key response-function
expiration &allow-other-keys) args
(setf (url:translation-method url) translation)
(setf (url:response-function url) response-function)
(when expiration
(url:initialize-expiration url expiration))
url))
(defmethod http:write-document ((url url:http-computed-url)
(translation (eql :json-api))
stream)
(http:with-successful-response
(stream :json
:cache-control '(:no-cache t))
(funcall (url:response-function url) url stream)))
URL classes define the behavior of URL objects. CL-HTTP provides a
class hierarchy rooted at url:url with specialized
subclasses for different URL schemes and content types. Application code
can subclass these for domain-specific behavior.
Key classes in the hierarchy:
url:url
url:http-url
url:http-object
url:http-path
url:http-computed-url
url:http-form
url:http-search
url:http-template-object
(defclass url:my-api-url
(url:http-computed-url
url:expiration-mixin
url:secure-subnets-mixin)
((api-version :initform 1
:initarg :api-version
:accessor url:api-version))
(:documentation "A URL class for versioned API endpoints."))
The URL class can then be specified via the :url-class
argument to define-url-export-type, or by specializing
export-url methods.
CL-HTTP uses Gray streams (CLOS-based streams) for all I/O. The stream class hierarchy wraps the underlying socket with layers for byte counting, chunked transfer encoding, and character encoding.
The stream class hierarchy (per-port, defined in
tcp-stream.lisp):
cl-http-socket-stream
unsigned-byte 8) and character element types on the
same stream. Tracks bytes transmitted and received.
cl-http-chunk-transfer-socket-stream
HTTP requires switching between binary and character modes on the
same stream (binary for images, character for HTML). CL-HTTP's stream
classes support this via mode-switching methods. The macros
with-binary-stream and with-text-stream
switch the stream mode within their scope:
;; Write mixed binary and character content (http:with-successful-response (stream :html) ;; Stream is in character mode for HTML (write-string "<html>" stream) ...)
For performance, stream implementations should use internal byte
buffers with bulk read-sequence/write-sequence
operations rather than per-character delegation to the underlying
stream. The SBCL and LispWorks ports both use buffered I/O with pooled
buffers (via defresource) to minimize allocation on the
hot path.
CL-HTTP's architecture supports extension beyond HTTP. The URL system already handles multiple schemes (HTTP, HTTPS, FTP, mailto, Gopher, WAIS), and new protocols can be added by:
The HTTP/2 implementation (RFC 9113) demonstrates this pattern. It adds a reactor-based connection multiplexer, HPACK header compression, stream prioritization, and flow control while reusing the existing URL dispatch and response function infrastructure.
CL-HTTP's package-based portability model makes porting to a new Common Lisp implementation a well-defined task. The portable code (approximately 80,000 lines) is never modified — a port implements the platform-specific contracts that the portable code expects.
Two packages define the portability interface:
IPC — Internal TCP Package
tcp-stream.lisp.
WWW-UTILS — Portability Bridge
IPC and
RESOURCES, then exports a unified interface for: process
management, file operations, conditions, stream operations, resource
pooling, and MOP introspection. Defined across package.lisp,
tcp-stream.lisp, unix.lisp, and the portable
http:mcl;server;www-utils.lisp.
The HTTP package uses WWW-UTILS and gets
all platform-specific functionality through package inheritance. It
never contains conditionalized code.
| File | Package | Purpose |
|---|---|---|
package.lisp | IPC, WWW-UTILS | Import native APIs, export unified interface |
tcp-stream.lisp | IPC, WWW-UTILS | Streams, conditions, DNS, connections |
unix.lisp | WWW-UTILS | Threading, file ops, atomics, utilities |
tcp-interface.lisp | HTTP | Service management, process pool |
utf-8.lisp | IPC | Character encoding |
ssl.lisp | IPC | SSL/TLS initialization and stream wrapping |
x509.lisp | HTTP | X.509 certificate handling |
sysdcl.lisp | CL-USER | System definition with loading order |
pre-cl-http.lisp | CL-USER | Features, translations, system definition macro |
start.lisp | CL-USER | Startup script |
Every port must map the target Lisp's native socket exceptions to
CL-HTTP's condition hierarchy (all in the WWW-UTILS
package):
network-error
domain-resolver-error
local-network-error
network-resources-exhausted
unknown-address
unknown-host-name
remote-network-error
bad-connection-state
connection-closed
connection-lost
host-stopped-responding
connection-error
connection-refused
host-not-responding
protocol-timeout
network-parse-error
Wrapper functions around native socket calls catch implementation-specific exceptions and re-signal them as CL-HTTP conditions.
make-process, process-run-function,
process-wait, make-lock,
with-lock-held, and atomic operations to the target Lisp's
threading primitives.
stream-unread-char with correct byte count adjustment.
class-slots,
class-direct-subclasses, generic-function-methods)
to the target Lisp's Meta-Object Protocol package.
The recommended approach, proven during the SBCL port:
http:lw;) as the most complete reference.
WWW-UTILS and IPC
packages must also be exported by the new port. Missing exports cause
undefined function errors deep in the portable code.
| Port | Directory | Status |
|---|---|---|
| Genera | http:lispm; | Released |
| LispWorks | http:lw; | Released (reference) |
| SBCL | http:sbcl; | Released (v1.4.2) |
| MCL | http:mcl; | Released (portable utils) |
| CMU CL | http:cmucl; | Beta |
| ACL | http:acl; | Released |
| SCL | http:scl; | Beta |
The logging system is designed for extensibility via CLOS mixins. See Chapter 9, Section 9.6 for the mixin architecture and instructions on defining custom log classes.