Drakma - A Common Lisp HTTP client

Abstract

Drakma is a full-featured HTTP client implemented in Common Lisp. It knows how to handle HTTP/1.1 chunking, persistent connections, re-usable sockets, SSL, continuable uploads, file uploads, cookies, and more.

The code comes with a BSD-style license so you can basically do with it whatever you want.

Contents

  1. Abstract
  2. Contents
  3. Examples
    1. Loading Drakma with Quicklisp
    2. Log headers to the REPL output stream
    3. Requesting a page with redirection
    4. Requesting a page containing non-ASCII characters
    5. Requesting binary data
    6. Chunked transfers and HTTPS
    7. Faking a user agent header
    8. Posting data and using cookies
    9. Reusing a connection to a server
    10. Basic Authorization
    11. Reading the response from a stream
    12. Piecemeal assembly of request contents
    13. Partial transfers
  4. Download and Installation
  5. Development and patches
  6. The Drakma dictionary
    1. Requests
    2. Headers
    3. Cookies
    4. Conditions
  7. Symbol index

Examples

Here is a collection of example uses of Drakma to which demonstrate some of its features. In the examples, text is color coded to indicate where it comes from (REPL input, REPL output, HTTP headers sent and HTTP headers received). Headers particularly relevant to the example at hand are shown in bold.

Loading Drakma with Quicklisp

? (ql:quickload :drakma)
To load "drakma":
  Load 1 ASDF system:
    drakma
; Loading "drakma"
To load "cl+ssl":
  Load 1 ASDF system:
    flexi-streams
  Install 8 Quicklisp releases:
    alexandria babel bordeaux-threads cffi cl+ssl
    trivial-features trivial-garbage trivial-gray-streams
...
; Loading "drakma"

(:DRAKMA)

Log headers to the REPL output stream

In some of the following examples, the headers exchanged between Drakma and the HTTP server should be shown, for illustration purposes. This can be achieved like so:

? (setf drakma:*header-stream* *standard-output*)
#<SYNONYM-STREAM to *TERMINAL-IO* #x3020006AC7DD>

Requesting a page with redirection

Request a page. Note how Drakma automatically follows the 301 redirect and how the fourth return value shows the new URI.

? (drakma:http-request "http://lisp.org/")
GET / HTTP/1.1
Host: lisp.org
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 307  Temporary Redirect
Date: Sun, 09 Dec 2012 08:01:56 GMT
Connection: Close
Server: AllegroServe/1.2.65
Transfer-Encoding: chunked
LOCATION: http://lisp.org/index.html

GET /index.html HTTP/1.1
Host: lisp.org
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 200  OK
Date: Sun, 09 Dec 2012 08:01:56 GMT
Connection: Close
Server: AllegroServe/1.2.65
Content-Type: text/html
Content-Length: 459
LAST-MODIFIED: Wed, 26 Oct 2011 02:26:26 GMT

"<HTML>
<HEAD>
  <title>John McCarthy, 1927-2011</title>
  <STYLE type=\"text/css\">
    BODY {text-align: center}
  </STYLE>
</HEAD>
<BODY>
<h1>John McCarthy</h1>
<img src=\"jmccolor.jpg\" alt=\"a picture of John McCarthy, from his website\"/>
<h3>1927-2011</h3>
<br><br>
<a href=\"http://www-formal.stanford.edu/jmc/\">John McCarthy's Home Page</a><br>
<a href=\"http://news.stanford.edu/news/2011/october/john-mccarthy-obit-102511.html\">Obituary</a>
</BODY>
</HTML>
"
200
((:DATE . "Sun, 09 Dec 2012 08:01:56 GMT") (:CONNECTION . "Close") (:SERVER . "AllegroServe/1.2.65")
 (:CONTENT-TYPE . "text/html") (:CONTENT-LENGTH . "459") (:LAST-MODIFIED . "Wed, 26 Oct 2011 02:26:26 GMT"))
#<URI http://lisp.org/index.html>
#<FLEXI-STREAMS:FLEXI-IO-STREAM #x30200155DB1D>
T
" OK"

Requesting a page containing non-ASCII characters

Drakma automatically interprets the 'charset=utf-8' part correctly.

? (subseq (drakma:http-request "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/digraphs.txt") 0 298)
GET /~mgk25/ucs/examples/digraphs.txt HTTP/1.1
Host: www.cl.cam.ac.uk
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:15:04 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Mon, 06 Apr 2009 18:13:43 GMT
ETag: "17cd62-298-466e6dbcd03c0"
Accept-Ranges: bytes
Content-Length: 664
X-UA-Compatible: IE=edge
Connection: close
Content-Type: text/plain; charset=utf-8

"Latin Digraphs and Ligatures in ISO10646-1

A short table of ligatures and digraphs follows. Some of these may not be
ligatures/digraphs in the technical sense, (for example, æ is a seperate
letter in English), but visually they behave that way.

AÆE : U+00C6
aæe : U+00E6
ſßs : U+00DF
IIJJ : U+0132"

Requesting binary data

For non-textual content types, a vector of octets is returned.

? (drakma:http-request "https://api.github.com/repos/edicl/drakma/git/tags/tag-does-not-exist")
GET /repos/edicl/drakma/git/tags/tag-does-not-exist HTTP/1.1
Host: api.github.com
User-Agent: Drakma/1.3.0 (SBCL 1.1.1.31.master.2-9fac43f-dirty; Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 404 Not Found
Server: nginx
Date: Fri, 28 Dec 2012 08:37:31 GMT
Content-Type: application/json; charset=utf-8
Connection: close
Status: 404 Not Found
X-GitHub-Media-Type: github.beta
X-RateLimit-Remaining: 48
X-RateLimit-Limit: 60
Content-Length: 23
X-Content-Type-Options: nosniff
Cache-Control: 

#(123 34 109 101 115 115 97 103 101 34 58 34 78 111 116 32 70 111 117 110 100 34 125)
404
((:SERVER . "nginx") (:DATE . "Fri, 28 Dec 2012 08:37:31 GMT") (:CONTENT-TYPE . "application/json; charset=utf-8")
 (:CONNECTION . "close") (:STATUS . "404 Not Found") (:X-GITHUB-MEDIA-TYPE . "github.beta") (:X-RATELIMIT-REMAINING . "48")
 (:X-RATELIMIT-LIMIT . "60") (:CONTENT-LENGTH . "23") (:X-CONTENT-TYPE-OPTIONS . "nosniff") (:CACHE-CONTROL . ""))
#<PURI:URI https://api.github.com/repos/edicl/drakma/git/tags/tag-does-not-exist>
#<FLEXI-STREAMS:FLEXI-IO-STREAM {101C40C043}>
T
"Not Found"
? (flexi-streams:octets-to-string *)
"{\"message\":\"Not Found\"}"

Chunked transfers and HTTPS

Request a page using the HTTPS protocol. Also note that the server uses chunked transfer encoding for its reply

? (ql:quickload :cl-ppcre)
? (cl-ppcre:scan-to-strings "(?s)You have.*your data."
                            (drakma:http-request "https://www.fortify.net/cgi/ssl_2.pl"))
GET /cgi/ssl_2.pl HTTP/1.1
Host: www.fortify.net
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:15:31 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

"You have connected to this web server using the RC4-SHA encryption cipher
 with a key length of 128 bits.
 <p>
 This is a high-grade encryption connection, regarded by most experts as being suitable
 for sending or receiving even the most sensitive or valuable information
 across a network.
 <p>
 In a crude analogy, using this cipher is similar to sending or storing your data inside
 a high quality safe - compared to an export-grade cipher which is similar to using
 a paper envelope to protect your data."
#()

Faking a user agent header

Some servers adapt their behavior according to the Browser that is used. Drakma can claim to be i.e. MS Internet Explorer.

? (cl-ppcre:scan-to-strings "<h4>.*" (drakma:http-request "http://whatsmyuseragent.com/" :user-agent :explorer))
GET / HTTP/1.1
Host: whatsmyuseragent.com
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Accept: */*
Connection: close

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:23:50 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

"<h4>Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)</h4>"
#()

Posting data and using cookies

Drakma can send parameters in a POST request and knows how to deal with cookies. Note how Drakma sends the cookie back in the second request.

? (let ((cookie-jar (make-instance 'drakma:cookie-jar)))
    (drakma:http-request "http://www.phpsecurepages.com/test/test.php"
                         :method :post
                         :parameters '(("entered_login" . "test")
                                       ("entered_password" . "test"))
                         :cookie-jar cookie-jar)
    (drakma:http-request "http://www.phpsecurepages.com/test/test2.php"
                         :cookie-jar cookie-jar)
    (drakma:cookie-jar-cookies cookie-jar))
POST /test/test.php HTTP/1.1
Host: www.phpsecurepages.com
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 40

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:25:13 GMT
Server:  
X-Powered-By: PHP/5.2.17
Set-Cookie: PHPSESSID=vijk3706eojs7n8u5cdpi3ju05; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Powered-By: PleskLin
Content-Length: 4479
Connection: close
Content-Type: text/html

GET /test/test2.php HTTP/1.1
Host: www.phpsecurepages.com
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Cookie: PHPSESSID=vijk3706eojs7n8u5cdpi3ju05
Connection: close

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:25:16 GMT
Server:  
X-Powered-By: PHP/5.2.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Powered-By: PleskLin
Content-Length: 4479
Connection: close
Content-Type: text/html

(#<COOKIE PHPSESSID=vijk3706eojs7n8u5cdpi3ju05; path=/; domain=www.phpsecurepages.com>)

Reusing a connection to a server

Drakma can use a connection to a server for multiple requests.

? (let ((stream (nth-value 4 (drakma:http-request "http://www.lispworks.com/" :close nil))))
    (nth-value 2 (drakma:http-request "http://www.lispworks.com/success-stories/index.html"
                                      :stream stream)))
GET / HTTP/1.1
Host: www.lispworks.com
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:25:56 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/1.0.1c mod_apreq2-20051231/2.6.0 mod_perl/2.0.5 Perl/v5.8.9
Last-Modified: Tue, 20 Nov 2012 12:27:40 GMT
ETag: "336280-28eb-4ceec5c1f4700"
Accept-Ranges: bytes
Content-Length: 10475
Content-Type: text/html

GET /success-stories/index.html HTTP/1.1
Host: www.lispworks.com
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 200 OK
Date: Sun, 09 Dec 2012 08:25:56 GMT
Server: Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/1.0.1c mod_apreq2-20051231/2.6.0 mod_perl/2.0.5 Perl/v5.8.9
Last-Modified: Tue, 20 Nov 2012 12:28:52 GMT
ETag: "336386-2940-4ceec6069e900"
Accept-Ranges: bytes
Content-Length: 10560
Connection: close
Content-Type: text/html

((:DATE . "Sun, 09 Dec 2012 08:25:56 GMT")
 (:SERVER . "Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/1.0.1c mod_apreq2-20051231/2.6.0 mod_perl/2.0.5 Perl/v5.8.9")
 (:LAST-MODIFIED . "Tue, 20 Nov 2012 12:28:52 GMT") (:ETAG . "\"336386-2940-4ceec6069e900\"") (:ACCEPT-RANGES . "bytes")
 (:CONTENT-LENGTH . "10560") (:CONNECTION . "close") (:CONTENT-TYPE . "text/html"))

Basic Authorization

Drakma supports basic authorization. In this example, we use a locally running Hunchentoot server.

? (ql:quickload :hunchentoot-test)
To load "hunchentoot-test":
  Load 4 ASDF systems:
    cl-ppcre cl-who drakma hunchentoot
  Install 1 Quicklisp release:
    hunchentoot
...
; Loading "hunchentoot-test"

(:HUNCHENTOOT-TEST)
? (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242))
#<EASY-ACCEPTOR (host *, port 4242)>
? (nth-value 1 (drakma:http-request "http://localhost:4242/hunchentoot/test/authorization.html"))
GET /hunchentoot/test/authorization.html HTTP/1.1
Host: localhost:4242
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

127.0.0.1 - [2012-12-09 09:27:40] "GET /hunchentoot/test/authorization.html HTTP/1.1" 401 543 "-" "Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)"
HTTP/1.1 401 Authorization Required
Content-Length: 543
Date: Sun, 09 Dec 2012 08:27:40 GMT
Server: Hunchentoot 1.2.5
Connection: Close
Www-Authenticate: Basic realm="Hunchentoot"
Content-Type: text/html; charset=iso-8859-1

401
? (nth-value 1 (drakma:http-request "http://localhost:4242/hunchentoot/test/authorization.html"
                                    :basic-authorization '("nanook" "igloo")))
GET /hunchentoot/test/authorization.html HTTP/1.1
Host: localhost:4242
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Authorization: Basic bmFub29rOmlnbG9v
Accept: */*
Connection: close

127.0.0.1 nanook [2012-12-09 09:28:15] "GET /hunchentoot/test/authorization.html HTTP/1.1" 200 907 "-" "Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)"
HTTP/1.1 200 OK
Content-Length: 907
Date: Sun, 09 Dec 2012 08:28:15 GMT
Server: Hunchentoot 1.2.5
Connection: Close
Content-Type: text/html; charset=utf-8

200

Reading the response from a stream

Drakma can return a stream to the application so that the reply is not completely buffered in memory first.

? (let ((stream (drakma:http-request "https://api.github.com/orgs/edicl/public_members"
                                      :want-stream t)))
    (setf (flexi-streams:flexi-stream-external-format stream) :utf-8)
    (yason:parse stream :object-as :plist))
GET /orgs/edicl/public_members HTTP/1.1
Host: api.github.com
User-Agent: Drakma/1.3.0 (SBCL 1.1.1.31.master.2-9fac43f-dirty; Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 28 Dec 2012 10:27:34 GMT
Content-Type: application/json; charset=utf-8
Connection: close
Status: 200 OK
Last-Modified: Sat, 22 Dec 2012 18:39:14 GMT
X-Content-Type-Options: nosniff
X-RateLimit-Limit: 60
X-GitHub-Media-Type: github.beta
Vary: Accept
Content-Length: 1899
Cache-Control: public, max-age=60, s-maxage=60
ETag: "66a5dd35e79146a53029a1807293f9d3"
X-RateLimit-Remaining: 56

(("type" "User" "repos_url" "https://api.github.com/users/hanshuebner/repos" "followers_url"
  "https://api.github.com/users/hanshuebner/followers" "login" "hanshuebner" "gists_url"
  "https://api.github.com/users/hanshuebner/gists{/gist_id}" "following_url"
  "https://api.github.com/users/hanshuebner/following" "events_url"
  "https://api.github.com/users/hanshuebner/events{/privacy}" "organizations_url"
  "https://api.github.com/users/hanshuebner/orgs" "received_events_url"
  "https://api.github.com/users/hanshuebner/received_events" "url"
  "https://api.github.com/users/hanshuebner" "avatar_url"
  "https://secure.gravatar.com/avatar/280d76aa82179ae04550534649de1e6e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"
  "subscriptions_url" "https://api.github.com/users/hanshuebner/subscriptions" "starred_url"
  "https://api.github.com/users/hanshuebner/starred{/owner}{/repo}" "id" 108751 "gravatar_id"
  "280d76aa82179ae04550534649de1e6e")
 ("type" "User" "repos_url" "https://api.github.com/users/nhabedi/repos" "followers_url"
  "https://api.github.com/users/nhabedi/followers" "login" "nhabedi" "gists_url"
  "https://api.github.com/users/nhabedi/gists{/gist_id}" "following_url"
  "https://api.github.com/users/nhabedi/following" "events_url"
  "https://api.github.com/users/nhabedi/events{/privacy}" "organizations_url"
  "https://api.github.com/users/nhabedi/orgs" "received_events_url"
  "https://api.github.com/users/nhabedi/received_events" "url"
  "https://api.github.com/users/nhabedi" "avatar_url"
  "https://secure.gravatar.com/avatar/24c09c7b0b2c0481283d854bacdd7926?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"
  "subscriptions_url" "https://api.github.com/users/nhabedi/subscriptions" "starred_url"
  "https://api.github.com/users/nhabedi/starred{/owner}{/repo}" "id" 537618 "gravatar_id"
  "24c09c7b0b2c0481283d854bacdd7926"))

Piecemeal assembly of request contents

Request contents can be assembled from various sources, and chunked encoding can be used by request bodies. Many servers do not support chunked encoding for request bodies, though.

? (let ((temp-file (ensure-directories-exist #p"/tmp/quux.txt"))
        (continuation (drakma:http-request "http://localhost:4242/hunchentoot/test/parameter_latin1_post.html"
                                           :method :post
                                           :content :continuation)))
    (funcall continuation "foo=" t)
    (funcall continuation (list (char-code #\z) (char-code #\a)) t)
    (funcall continuation (lambda (stream)
                            (write-char #\p stream)) t)
    (with-open-file (out temp-file
                         :direction :output
                         :if-does-not-exist :create
                         :if-exists :supersede)
      (write-string "p" out))
    (funcall continuation temp-file t)
    (cl-ppcre:scan-to-strings "zappzerapp" (funcall continuation "zerapp")))
POST /hunchentoot/test/parameter_latin1_post.html HTTP/1.1
Host: localhost:4242
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked

127.0.0.1 - [2012-12-09 10:06:44] "POST /hunchentoot/test/parameter_latin1_post.html HTTP/1.1" 200 1312 "-" "Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)"
HTTP/1.1 200 OK
Content-Length: 1312
Date: Sun, 09 Dec 2012 09:06:44 GMT
Server: Hunchentoot 1.2.5
Connection: Close
Last-Modified: Sun, 09 Dec 2012 09:06:44 GMT
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Content-Type: text/html; charset=ISO-8859-1

"zappzerapp"
#()

Partial transfers

Partial transfers of resources are possible.

? (cl-ppcre:regex-replace-all
   "<.*?>"
   (format nil "~A~%~A"
           (drakma:http-request "http://members.shaw.ca/mitb/hunchentoot.html"
                                :range '(998 1034))
           (drakma:http-request "http://members.shaw.ca/mitb/hunchentoot.html"
                                :range '(1213 1249)))
   "")
GET /mitb/hunchentoot.html HTTP/1.1
Host: members.shaw.ca
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close
Range: bytes=998-1034

HTTP/1.1 206 Partial Content
Date: Sun, 09 Dec 2012 09:16:16 GMT
Server: Apache/2.2.20 (Unix) mod_ldap_userdir/1.1.17
Last-Modified: Wed, 14 Mar 2012 23:22:04 GMT
ETag: "3b7eed-3238-4bb3c3e453f00"
Accept-Ranges: bytes
Content-Length: 37
Content-Range: bytes 998-1034/12856
Content-Type: text/html
Connection: close

GET /mitb/hunchentoot.html HTTP/1.1
Host: members.shaw.ca
User-Agent: Drakma/1.3.0 (Clozure Common Lisp Version 1.8-r15286M  (DarwinX8664); Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: */*
Connection: close
Range: bytes=1213-1249

HTTP/1.1 206 Partial Content
Date: Sun, 09 Dec 2012 09:16:16 GMT
Server: Apache/2.2.20 (Unix) mod_ldap_userdir/1.1.17
Last-Modified: Wed, 14 Mar 2012 23:22:04 GMT
ETag: "3b7eed-3238-4bb3c3e453f00"
Accept-Ranges: bytes
Content-Length: 37
Content-Range: bytes 1213-1249/12856
Content-Type: text/html

"DRAKMA (Queen of Cosmic Greed)
HUNCHENTOOT (The Giant Spider)"
T

Download and Installation

Drakma depends on a number of open source libraries, so the preferred method to download, compile and load it is via Quicklisp. Drakma's current version number is 2.0.10.

The canonical location for the latest version of Drakma is https://github.com/edicl/drakma/releases.

Development and patches

The development version of Drakma can be found on github. Please use the github issue tracking system to submit bug reports. Patches are welcome, please use GitHub pull requests. If you want to make a change, please read this first.

The Drakma dictionary

Requests

The HTTP-REQUEST function is the heart of Drakma. It is used to send requests to web servers and will either return the message body of the server's reply or (if the user so wishes) a stream one can read from. The wealth of keyword parameters might look a bit intimidating first, but you will rarely need more than two or three of them - the default behavior of Drakma is (hopefully) designed to do The Right Thing[TM] in most cases.

You can use the *HEADER-STREAM* variable to debug requests handled by Drakma in a way similar to LiveHTTPHeaders.

[Function]

http-request uri

&rest args
&key protocol method force-ssl certificate key certificate-password verify max-depth ca-file ca-directory parameters url-encoder content content-type content-length form-data cookie-jar basic-authorization user-agent accept range proxy proxy-basic-authorization real-host additional-headers redirect auto-referer keep-alive close external-format-out external-format-in force-binary want-stream stream preserve-uri connection-timeout deadline
=> body-or-stream0, status-code1, headers2, uri3, stream4, must-close5, reason-phrase6

Sends an HTTP request to a web server and returns its reply. uri is where the request is sent to, and it is either a string denoting a uniform resource identifier or a PURI:URI object. The scheme of uri must be `http' or `https'. The function returns SEVEN values - the body of the reply0 (but see below), the status code1 as an integer, an alist of the headers2 sent by the server where for each element the car (the name of the header) is a keyword and the cdr (the value of the header) is a string, the uri3 the reply comes from (which might be different from the uri the request was sent to in case of redirects), the stream4 the reply was read from, a generalized boolean5 which denotes whether the stream should be closed (and which you can usually ignore), and finally the reason phrase6 from the status line as a string.

protocol is the HTTP protocol version which is going to be used in the request line. It must be one of the keywords :HTTP/1.0 or :HTTP/1.1.

method is the method used in the request line, a keyword (like :GET or :HEAD) denoting a valid HTTP/1.1 or WebDAV request method, or :REPORT, as described in the Versioning Extensions to WebDAV. Additionally, you can also use the pseudo method :OPTIONS* which is like :OPTIONS but means that an "OPTIONS *" request line will be sent, i.e. the uri's path and query parts will be ignored.

If force-ssl is true, SSL will be attached to the socket stream which connects Drakma with the web server. Usually, you don't have to provide this argument, as SSL will be attached anyway if the scheme of uri is `https'.

certificate is the file name of the PEM encoded client certificate to present to the server when making a SSL connection. key specifies the file name of the PEM encoded private key matching the certificate. certificate-password specifies the pass phrase to use to decrypt the private key.

verify can be specified to force verification of the certificate that is presented by the server in an SSL connection. It can be specified either as NIL if no check should be performed, :OPTIONAL to verify the server's certificate if it presented one or :REQUIRED to verify the server's certificate and fail if an invalid or no certificate was presented.

max-depth can be specified to change the maximum allowed certificate signing depth that is accepted. The default is 10.

ca-file and ca-directory can be specified to set the certificate authority bundle file or directory to use for certificate validation.

The certificate, key, certificate-password, verify, max-depth, ca-file and ca-directory parameters are ignored for non-SSL requests. They are also ignored on LispWorks.

parameters is an alist of name/value pairs (the car and the cdr each being a string) which denotes the parameters which are added to the query part of the URL or (in the case of a POST request) comprise the body of the request. (But see content below.) The values can also be NIL in which case only the name (without an equal sign) is used in the query string. The name/value pairs are URL-encoded using the FLEXI-STREAMS external format external-format-out before they are sent to the server unless form-data is true in which case the POST request body is sent as `multipart/form-data' using external-format-out. The values of the parameters alist can also be pathnames, open binary input streams, unary functions, or lists where the first element is of one of the former types. These values denote files which should be sent as part of the request body. If files are present in parameters, the content type of the request is always `multipart/form-data'. If the value is a list, the part of the list behind the first element is treated as a plist which can be used to specify a content type and/or a filename for the file, i.e. such a value could look like, e.g., (#p"/tmp/my_file.doc" :content-type "application/msword" :filename "upload.doc").

url-encoder specifies a custom URL encoder function which will be used by drakma to URL-encode parameter names and values. It needs to be a function of two arguments. The arguments are the string to encode and the external format to use (as accepted by FLEXI-STREAMS:STRING-TO-OCTETS). The return value must be the URL-encoded string. This can be used if specific encoding rules are required.

content, if not NIL, is used as the request body - parameters is ignored in this case. content can be a string, a sequence of octets, a pathname, an open binary input stream, or a function designator. If content is a sequence, it will be directly sent to the server (using external-format-out in the case of strings). If content is a pathname, the binary contents of the corresponding file will be sent to the server. If content is a stream, everything that can be read from the stream until EOF will be sent to the server. If content is a function designator, the corresponding function will be called with one argument, the stream to the server, to which it should send data.

Finally, content can also be the keyword :CONTINUATION in which case HTTP-REQUEST returns only one value - a `continuation' function. This function has one required argument and one optional argument. The first argument will be interpreted like content above (but it cannot be a keyword), i.e. it will be sent to the server according to its type. If the second argument is true, the continuation function can be called again to send more content, if it is NIL the continuation function returns what HTTP-REQUEST would have returned.

If content is a sequence, Drakma will use LENGTH to determine its length and will use the result for the `Content-Length' header sent to the server. You can overwrite this with the content-length parameter (a non-negative integer) which you can also use for the cases where Drakma can't or won't determine the content length itself. You can also explicitly provide a content-length argument of NIL which will imply that no `Content-Length' header will be sent in any case. If no `Content-Length' header is sent, Drakma will use chunked encoding to send the content body. Note that this will not work with older web servers.

Providing a true content-length argument which is not a non-negative integer means that Drakma /must/ build the request body in RAM and compute the content length even if it would have otherwise used chunked encoding, for example in the case of file uploads.

content-type is the corresponding `Content-Type' header to be sent and will be ignored unless content is provided as well.

Note that a query already contained in uri will always be sent with the request line anyway in addition to other parameters sent by Drakma.

cookie-jar is a cookie jar containing cookies which will potentially be sent to the server (if the domain matches, if they haven't expired, etc.) - this cookie jar will be modified according to the `Set-Cookie' header(s) sent back by the server.

basic-authorization, if not NIL, should be a list of two strings (username and password) which will be sent to the server for basic authorization.

user-agent, if not NIL, denotes which `User-Agent' header will be sent with the request. It can be one of the keywords :DRAKMA, :FIREFOX, :EXPLORER, :OPERA, or :SAFARI which denote the current version of Drakma or, in the latter four cases, a fixed string corresponding to a more or less recent (as of August 2006) version of the corresponding browser. Or it can be a string which is used directly.

accept, if not NIL, specifies the contents of the `Accept' header sent.

range optionally specifies a subrange of the resource to be requested. It must be specified as a list of two integers which indicate the start and (inclusive) end offset of the requested range, in bytes (i.e. octets).

If proxy is not NIL, it should be a string denoting a proxy server through which the request should be sent. Or it can be a list of two values - a string denoting the proxy server and an integer denoting the port to use (which will default to 80 otherwise). Defaults to *default-http-proxy*. proxy-basic-authorization is used like basic-authorization, but for the proxy, and only if proxy is true. If the host portion of the uri is present in the *NO-PROXY-DOMAINS* or the NO-PROXY-DOMAINS list then the proxy setting will be ignored for this request.

If NO-PROXY-DOMAINS is set then it will supersede the *NO-PROXY-DOMAINS* variable. Inserting domains into this list will allow them to ignore the proxy setting.

If real-host is not NIL, request is sent to the denoted host instead of the uri host. When specified, real-host supersedes proxy.

additional-headers is a name/value alist of additional HTTP headers which should be sent with the request. Unlike in parameters, the cdrs can not only be strings but also designators for unary functions (which should in turn return a string) in which case the function is called each time the header is written.

If redirect is not NIL, it must be a non-negative integer or T. If redirect is true, Drakma will follow redirects (return codes 301, 302, 303, or 307) unless redirect is 0. If redirect is an integer, it will be decreased by 1 with each redirect. Furthermore, if auto-referer is true when following redirects, Drakma will populate the `Referer' header with the uri that triggered the redirection, overwriting an existing `Referer' header (in additional-headers) if necessary.

If keep-alive is T, the server will be asked to keep the connection alive, i.e. not to close it after the reply has been sent. (Note that this not necessary if both the client and the server use HTTP 1.1.) If close is T, the server is explicitly asked to close the connection after the reply has been sent. keep-alive and close are obviously mutually exclusive.

If the message body sent by the server has a text content type, Drakma will try to return it as a Lisp string. It'll first check if the `Content-Type' header denotes an encoding to be used, or otherwise it will use the external-format-in argument. The body is decoded using FLEXI-STREAMS. If FLEXI-STREAMS doesn't know the external format, the body is returned as an array of octets. If the body is empty, Drakma will return NIL.

If the message body doesn't have a text content type or if force-binary is true, the body is always returned as an array of octets.

If want-stream is true, the message body is NOT read and instead the (open) socket stream is returned as the first return value. If the sixth value of HTTP-REQUEST is true, the stream should be closed (and not be re-used) after the body has been read. The stream returned is a flexi-stream with a chunked stream as its underlying stream. If you want to read binary data from this stream, read from the underlying stream which you can get with FLEXI-STREAM-STREAM.

Drakma will usually create a new socket connection for each HTTP request. However, you can use the stream argument to provide an open socket stream which should be re-used. stream MUST be a stream returned by a previous invocation of HTTP-REQUEST where the sixth return value wasn't true. Obviously, it must also be connected to the correct server and at the right position (i.e. the message body, if any, must have been read). Drakma will NEVER attach SSL to a stream provided as the stream argument.

connection-timeout is the time (in seconds) Drakma will wait until it considers an attempt to connect to a server as a failure. It is supported only on some platforms (currently abcl, clisp, LispWorks, mcl, openmcl and sbcl). READ-TIMEOUT and WRITE-TIMEOUT are the read and write timeouts (in seconds) for the socket stream to the server. All three timeout arguments can also be NIL (meaning no timeout), and they don't apply if an existing stream is re-used. READ-TIMEOUT argument is only available for LispWorks, WRITE-TIMEOUT is only available for LispWorks 5.0 or higher.

deadline, a time in the future, specifies the time until which the request should be finished. The deadline is specified in internal time units. If the server fails to respond until that time, a COMMUNICATION-DEADLINE-EXPIRED condition is signalled. deadline is only available on CCL 1.2 and later.

If preserve-uri is not NIL, the given uri will not be processed. This means that the uri will be sent as-is to the remote server and it is the responsibility of the client to make sure that all parameters are encoded properly. Note that if this parameter is given, and the request is not a POST with a content-type of `multipart/form-data', parameters will not be used.

If decode-content is not NIL, then the content will automatically be decoded according to any encodings specified in the Content-Encoding header. The actual decoding is done by the decode-stream generic function, and you can implement new methods to support additional encodings. Any encodings in Transfer-Encoding, such as chunking, are always performed.

[Function]
parameter-present-p name parameters => boolean

If parameters is an alist of parameters as returned by, for example, READ-TOKENS-AND-PARAMETERS and name is a string naming a parameter, this function returns the full parameter (name and value) - or NIL if it's not in parameters.

[Function]
parameter-value name parameters => (or string null)

If parameters is an alist of parameters as returned by, for example, READ-TOKENS-AND-PARAMETERS and name is a string naming a parameter, this function returns the value of this parameter - or NIL if it's not in parameters.

[Function]
url-encode string external-format => string

Returns a URL-encoded version of the string string using the external format external-format.

[Function]
decode-stream encoding-type stream => stream

Generic function to decode a stream. This is a generic function which decodes the stream based on the encoding-type. If a response contains one or more transfer or content encodings, then decode-stream is called for each encoding type in the correct order to properly decode the stream to its original content.

encoding-type will be a keyword created by upcasing and interning the encoding type from the header. stream will be the stream that needs to be decoded. decode-stream returns a new stream from which you can read the decoded data.

[Special variable]
*body-format-function*

A function which determines whether the content body returned by the server is text and should be treated as such or not. The function is called after the request headers have been read and it must accept two arguments, headers and external-format-in, where headers is like the third return value of HTTP-REQUEST while external-format-in is the HTTP-REQUEST argument of the same name. It should return NIL if the body should be regarded as binary content, or a FLEXI-STREAMS external format (which will be used to read the body) otherwise.

This function will only be called if the force-binary argument to HTTP-REQUEST is NIL.

The initial value of this variable is a function which uses *TEXT-CONTENT-TYPES* to determine whether the body is text and then proceeds as described in the HTTP-REQUEST documentation entry.

[Special variable]
*default-http-proxy*

HTTP proxy to be used as default for the proxy keyword argument of HTTP-REQUEST. If not NIL, it should be a string denoting a proxy server through which the request should be sent. Or it can be a list of two values - a string denoting the proxy server and an integer denoting the port to use (which will default to 80 otherwise).

[Special variable]
*no-proxy-domains*

A list of domains for which a proxy should not be used.

[Special variable]
*drakma-default-external-format*

The default value for the external format keyword arguments of HTTP-REQUEST. The value of this variable will be interpreted by FLEXI-STREAMS. The initial value is the keyword :LATIN-1. (Note that Drakma binds *DEFAULT-EOL-STYLE* to :LF).

[Special variable]
*header-stream*

If this variable is not NIL, it should be bound to a stream to which incoming and outgoing headers will be written for debugging purposes.

[Special variable]
*text-content-types*

A list of conses which are used by the default value of *BODY-FORMAT-FUNCTION* to decide whether a 'Content-Type' header denotes text content. The car and cdr of each cons should each be a string or NIL. A content type matches one of these entries (and thus denotes text) if the type part is STRING-EQUAL to the car or if the car is NIL and if the subtype part is STRING-EQUAL to the cdr or if the cdr is NIL.

The initial value of this variable is the list

(("text" . nil))
which means that every content type that starts with "text/" is regarded as text, no matter what the subtype is.

Headers

This section assembles a couple of convenience functions which can be used to access information returned as the third value (headers) of HTTP-REQUEST.

Note that if the server sends multiple headers with the same name, these are comprised into one entry by HTTP-REQUEST. The values are separated by commas.

[Function]
get-content-type headers => list

Reads and parses a `Content-Type' header and returns it as three values - the type, the subtype, and an alist (possibly empty) of name/value pairs for the optional parameters. headers is supposed to be an alist of headers as returned by HTTP-REQUEST. Returns NIL if there is no such header amongst headers.

[Function]
header-value name headers => (or string null)

If headers is an alist of headers as returned by HTTP-REQUEST and name is a keyword naming a header, this function returns the corresponding value of this header (or NIL if it's not in headers).

[Function]
read-tokens-and-parameters string &key value-required-p => list

Reads a comma-separated list of tokens from the string string. Each token can be followed by an optional, semicolon-separated list of attribute/value pairs where the attributes are tokens followed by a #\= character and a token or a quoted string. Returned is a list where each element is either a string (for a simple token) or a cons of a string (the token) and an alist (the attribute/value pairs). If value-required-p is NIL, the value part (including the #\= character) of each attribute/value pair is optional.

[Function]
split-tokens string => list

Splits the string string into a list of substrings separated by commas and optional whitespace. Empty substrings are ignored.

Cookies

HTTP-REQUEST can deal with HTTP cookies if it gets a cookie jar, a collection of COOKIE objects, as its cookie-jar argument. Cookies sent by the web server will be added to the cookie jar (or updated) if appropriate and cookies already in the cookie jar will be sent to the server together with the request.

Drakma will never remove cookies from a cookie jar automatically. You have to do it manually using DELETE-OLD-COOKIES.

[Standard class]
cookie

Instances of this class represent HTTP cookies. If you need to create your own cookies, you should use MAKE-INSTANCE with the initargs :NAME, :DOMAIN, :VALUE, :PATH, :EXPIRES, :SECUREP, and :HTTP-ONLY-P all of which are optional except for the first two. The meaning of these initargs and the corresponding accessors should be pretty clear if one looks at the original cookie specification (and at this page for the HttpOnly extension).

? (make-instance 'drakma:cookie
                 :name "Foo" 
                 :value "Bar"
                 :expires (+ (get-universal-time) 3600)
                 :domain ".weitz.de")
#<COOKIE Foo=Bar; expires=Sun, 09-12-2012 20:37:42 GMT; path=/; domain=.weitz.de>

[Function]
parse-cookie-date string => universal-time

Parses a cookie expiry date and returns it as a Lisp universal time. Currently understands the following formats:

"Wed, 06-Feb-2008 21:01:38 GMT"
"Wed, 06-Feb-08 21:01:38 GMT"
"Tue Feb 13 08:00:00 2007 GMT"
"Wednesday, 07-February-2027 08:55:23 GMT"
"Wed, 07-02-2017 10:34:45 GMT"

Instead of "GMT" time zone abbreviations like "CEST" and UTC offsets like "GMT-01:30" are also allowed.

While this function has "cookie" in its name, it might come in handy in other situations as well and it is thus exported as a convenience function.

[Function]
cookie= cookie1 cookie2 => boolean

Returns a true value if the cookies cookie1 and cookie2 are equal. Two cookies are considered to be equal if name and path are equal.

[Generic accessors]
cookie-name cookie => string
(setf (cookie-name cookie) new-value)
cookie-value cookie => (or string null)
(setf (cookie-value cookie) new-value)
cookie-domain cookie => string
(setf (cookie-domain cookie) new-value)
cookie-path cookie => (or string null)
(setf (cookie-path cookie) new-value)
cookie-expires cookie => (or integer null)
(setf (cookie-expires cookie) new-value)
cookie-http-only-p cookie => boolean
(setf (cookie-http-only-p cookie) new-value)
cookie-securep cookie => boolean
(setf (cookie-securep cookie) new-value)

[Standard class]
cookie-jar

An object of this class encapsulates a collection (a list, actually) of COOKIE objects. You create a new cookie jar with (MAKE-INSTANCE 'COOKIE-JAR) where you can optionally provide a list of COOKIE objects with the :COOKIES initarg. The cookies in a cookie jar are accessed with COOKIE-JAR-COOKIES.

[Generic accessors]
cookie-jar-cookies cookie-jar => list
(setf (cookie-jar-cookies cookie-jar) new-value)

[Function]
delete-old-cookies cookie-jar => cookie-jar

Removes all cookies from cookie-jar which have either expired or which don't have an expiry date.

[Special variable]
*allow-dotless-cookie-domains-p*

When this variable is not NIL, cookie domains containing no dots are considered valid. The default is NIL, meaning to disallow such domains except for "localhost".

[Special variable]
*ignore-unparseable-cookie-dates-p*

Whether Drakma is allowed to treat `Expires' dates in cookie headers as non-existent if it can't parse them. If the value of this variable is NIL (which is the default), an error will be signalled instead.

[Special variable]
*remove-duplicate-cookies-p*

Determines how duplicate cookies in the response are handled, defaults to T. Cookies are considered duplicate using COOKIE=.

Valid values are:

Misbehaving servers may send duplicate cookies back in the same Set-Cookie header:

HTTP/1.1 200  OK
Server: My-hand-rolled-server
Date: Wed, 07 Apr 2010 15:12:30 GMT
Connection: Close
Content-Type: text/html
Content-Length: 82
Set-Cookie: a=1; Path=/; Secure, a=2; Path=/; Secure

In this case Drakma has to choose whether cookie "a" has the value "1" or "2". By default, Drakma will choose the last value specified, in this case "2".

By default, Drakma conforms to RFC2109 HTTP State Management Mechanism, section 4.3.3 Cookie Management:

If a user agent receives a Set-Cookie response header whose NAME is the same as a pre-existing cookie, and whose Domain and Path attribute values exactly (string) match those of a pre-existing cookie, the new cookie supersedes the old.

Conditions

This section lists all the condition types that are defined by Drakma.

[Condition type]
cookie-date-parse-error

Signalled if Drakma tries to parse the date of an incoming cookie header and can't interpret it.

[Condition type]
cookie-error

Signalled if someone tries to create a COOKIE object that's not valid.

[Generic function]
cookie-error-cookie cookie-error => (or cookie null)

The COOKIE object that caused this error. Can be NIL in case such an object couldn't be initialized.

[Condition type]
parameter-error

Signalled if a function was called with inconsistent or illegal parameters.

[Condition type]
syntax-error

Signalled if Drakma encounters wrong or unknown syntax when reading the reply from the server.

[Condition type]
drakma-condition

Superclass for all conditions related to Drakma.

[Condition type]
drakma-error

Superclass for all errors related to Drakma.

[Condition type]
drakma-warning

Superclass for all warnings related to Drakma.

Symbol index