drakma-v2.0.3/0000755000000000000000000000000013025750121011633 5ustar rootrootdrakma-v2.0.3/packages.lisp0000644000000000000000000000555313025750121014312 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/packages.lisp,v 1.22 2008/01/14 01:57:01 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :cl-user) (defpackage :drakma (:use :cl :flexi-streams :chunga) ;; the variable defined in the ASDF system definition (:shadow #:syntax-error #:parameter-error) (:export #:*drakma-version* #:*allow-dotless-cookie-domains-p* #:*body-format-function* #:*remove-duplicate-cookies-p* #:*default-http-proxy* #:*no-proxy-domains* #:*drakma-default-external-format* #:*header-stream* #:*ignore-unparseable-cookie-dates-p* #:*text-content-types* #:cookie #:cookie-error #:cookie-error-cookie #:cookie-date-parse-error #:cookie-domain #:cookie-expires #:cookie-http-only-p #:cookie-jar #:cookie-jar-cookies #:cookie-name #:cookie-path #:cookie-securep #:cookie-value #:cookie= #:delete-old-cookies #:decode-stream #:drakma-condition #:drakma-error #:drakma-warning #:get-content-type #:header-value #:http-request #:parameter-error #:parameter-present-p #:parameter-value #:parse-cookie-date #:read-tokens-and-parameters #:split-tokens #:syntax-error #:url-encode)) drakma-v2.0.3/CHANGELOG0000644000000000000000000002226613025750121013055 0ustar rootrootVersion 2.0.3 2016-12-19 Allegro CL Express SSL fix (Chris Riesbeck) Use force-ssl, verify and other SSL settings when using proxy (lsxvdqe) verify, ca-path, ca-directory and max-depth for CL+SSL (lsxvdqe) Using host from :additional-headers when specified (Azamat S. Kalimoulline) Version 2.0.2 2015-10-08 Support SNI with cl+ssl. (Paul M. Rodriguez) Version 2.0.1 2015-07-10 Revert "Use quri instead of puri" (Hans Hübner) Merge pull request #56 from tmccombs/test-op (Hans Hübner) Add test-op to ASDF for drakma and drakma-test systems. (Thayne McCombs) Version 2.0.0 2015-07-10 Use quri instead of puri for URIs. (Thayne McCombs) Version 1.3.15 2015-07-07 Refactor time zone parsing into regular expression (Hans Huebner) Update README.md (Hans Hübner) make-ssl-stream: declare additional variables ignorable (Mark David) Version 1.3.14 2015-03-14 Update documentation (Hans Huebner) Add DECODE-CONTENT option (Thayne McCombs) Version 1.3.13 2015-03-11 Workaround for ABCL streams bug More information here: http://abcl.org/trac/ticket/377 (Elias Martenson) Fix code to generate version number in HTML docs (Hans Huebner) Version 1.3.12 2015-01-15 Preserve octet element-type with :force-binary t. (Jan Moringen) Version 1.3.11 2014-11-28 Update support info (Hans Huebner) Do not escape URI twice. (Orivej Desh) Version 1.3.10 2014-09-16 Implement gzip and deflate decoding for responses. (Thayne McCombs) Fix error in example (reported by David Vázquez Púa, closes #44) (Hans Huebner) Version 1.3.9 2014-05-01 Added SSL/TLS support for mocl (Wukix Inc) reindent and refactor cond into if (Hans Huebner) Version 1.3.8 2014-02-25 Determine version number from asdf package (Hans Huebner) Silence warning on LispWorks (Hans Huebner) All symbols from puri are prefixed with puri:. (Kilian Sprotte) Version 1.3.7 2013-11-21 Dummy without any functional changes Version 1.3.6 Documentation fixes (thanks to sarvid and stassats for the report) Version 1.3.5 Ignore incoming Content-Length header when chunking is on Make POST requests use external-format-out for multipart/form-data. (Raymond Wiker) Fixed the link to the SBCL documentation pertaining to "defining constants" (Aaron France) Added *NO-PROXY-DOMAINS* special variable (Aaron France) Version 1.3.4 Add *default-http-proxy* special variable (Tomas Zellerin) Version 1.3.3 Change handling of empty and missing Location headers (Paul M. Rodriguez) Version 1.3.2 Redirect to GET only for POST requests (Vsevolod Dyomkin) Version 1.3.1 2013-03-23 When redirecting from POST to GET, clear the FORM-DATA flag Add trivial test suite (Anton Vodonosov) Version 1.3.0 2012-12-28 Redirect HTTP 302 and 303 using GET (Orivej Desh) Add URL-ENCODER keyword argument Move documentation to XML format and update docstrings Version 1.2.9 2012-10-18 Fix bug with Content-Length computation (Manabu Takayama) Add REAL-HOST keyword argument (Orivej Desh) Version 1.2.8 2012-09-12 fix the computation of request's Content-Length (Manabu Takayama) Version 1.2.7 2012-08-16 Support :REPORT method (Cyrus Harmon) Make PRESERVE-URI work better - PURI:URI mangles paths with encoded &'s. Version 1.2.6 2012-03-03 Enable timeouts for more implementations (Francisco Vides Fernández) Export URL-ENCODE (suggested by Rob Blackwell) Fix incorrect range header syntax Version 1.2.5 2012-01-30 use cl-ppcre:split instead of split-string to fix bug with GET parameter handling (thanks to Rob Blackwell) use :nodelay :if-supported (Anton Vodonosov) Allow specification of client certificate (all platforms) Add arguments that allow validation of server certificate Version 1.2.4 2011-08-31 Make sure GET parameters are always URL-encoded Add :RANGE keyword argument (Hans Huebner) Better handling of optional filenames when uploading (Stas Boukarev) Don't funcall symbols that aren't FBOUNDP (Fare Rideau) Allow disabling of SSL when building (Marko Kocic) Version 1.2.3 2010-08-05 Fix UPDATE-COOKIES (Vsevolod Dyomkin) Fix typo in documentation HTML (Walter Rader) Version 1.2.2 2010-07-10 Make sure pathless URIs work (Hans Huebner, Manuel Odendahl) Version 1.2.1 2010-05-19 Fix a couple of typos (thanks to Stelian Ionescu, Giovanni Gigante, and Zach Beane) Version 1.2.0 2010-05-19 Introduced *REMOVE-DUPLICATE-COOKIES-P* (Ryan Davis) Enabled https through a proxy (Bill St. Clair and Dave Lambert) Bugfix for redirect of a request through a proxy (Bill St. Clair) Export PARSE-COOKIE-DATE Safer method to render URIs Allowed for GET/POST parameters without a value (seen on Lotus webservers) Version 1.1.0 2009-12-01 Allowed additional headers to be function designators (suggested by Xiangjun Wu) Be more liberal when parsing cookies (thanks to Andrei Stebakov) Added HTTP method PATCH (thanks to Xiangjun Wu) Don't send GET parameters again when redirecting (reported by Eugene Ossintsev) Solidify feature expressions (thanks to Joshua Taylor) Make SEND-COOKIE-P work for pathless URIs (thanks to Tomo Matsumoto) Version 1.0.0 2009-02-19 Use the new ("binary") version of Chunga Added conditions types Some performance improvements Be more lenient about content length (thanks to Zach Beane and "pix") Added *ALLOW-DOTLESS-COOKIE-DOMAINS-P* (thanks to Daniel Janus) Fix generation of user agent header (bug caught by Chaitanya Gupta) Added DEADLINE parameter for CCL (thanks to Hans Huebner) Fixed bug where READ-BODY returned NIL although TEXTP was true (thanks to Hans Huebner) Version 0.11.5 2008-03-21 Added workaround for CLISP (thanks to Anton Vodonosov) Version 0.11.4 2008-02-13 Improved error detection in MAKE-FORM-DATA-FUNCTION (suggested by Daniel Janus) Version 0.11.3 2008-01-14 The previous change is only needed for Windows Version 0.11.2 2008-01-14 Disable WRITE-TIMEOUT for LW 5.0 if SSL is used (reported by Nico de Jager) Version 0.11.1 2007-10-11 Make Drakma work with AllegroCL's "modern" mode (patch by Ross Jekel) Needs at least Chunga 0.4.1 and FLEXI-STREAMS 0.13.1 Version 0.11.0 2007-10-01 Added *TEXT-CONTENT-TYPES* and *BODY-FORMAT-FUNCTION* (suggested by Peter Eddy) Version 0.10.2 2007-09-29 Fixed bug introduced in latest change... (reported by Ross Jekel) Version 0.10.1 2007-09-25 Use parameters in URI if they weren't used up for the content body (suggested by Jan Rychter) Version 0.10.0 2007-09-18 Added support for "HttpOnly" cookie attribute (due to a bug report by Alexey Goldin) Version 0.9.1 2007-07-12 Improved CL+SSL support (patch by David Lichteblau) Version 0.9.0 2007-06-30 Added reason phrase to return values (patch by Holger Duerer) Version 0.8.0 2007-06-25 In cookie dates, accept time zones different from "GMT" (reported by Didier Verna) Added *ignore-unparseable-cookie-dates-p* Version 0.7.1 2007-06-17 Allow streams or functions as file designators (suggested by Andrei Stebakov) Version 0.7.0 2007-04-07 Switched from trivial-sockets to usocket (patch by Erik Huelsmann) Version 0.6.2 2007-03-09 Fixed release dates (thanks to Jeffrey Cunningham) Version 0.6.1 2007-03-08 Changed SPLIT-STRING so that it doesn't rely on unspecified behaviour (reported by Jianshi Huang) Version 0.6.0 2007-02-08 Make sure stream is closed in case of early errors (thanks to Chris Dean for test data) Robustified cookie parsing Send all outgoing cookies in one fell swoop (for Sun's buggy web server) Deal with empty Location headers Deal with corrupted Content-Type headers Version 0.5.5 2007-02-05 Fixed socket leak in case of redirects (bug report by Chris Dean) Version 0.5.4 2006-12-01 Workaround for servers which send headers after 100 status line (provided by Donavon Keithley) Version 0.5.3 2006-10-11 Set stream element type for binary streams as needed for CLISP (reported by Magnus Henoch) Version 0.5.2 2006-10-08 Adhere to user-provided content length if FORM-DATA is true Version 0.5.1 2006-10-07 Take Content-Encoding header into account (due to a bug report by Gregory Tod) Version 0.5.0 2006-09-25 Fixed bug where body sometimes wasn't read (reported by Ivan Toshkov) Added AUTO-REFERER feature (thanks to Colin Simmonds) Version 0.4.4 2006-09-24 Treat "localhost" special for cookies (reported by Ivan Toshkov) Version 0.4.3 2006-09-24 Circumvent CL+SSL for AllegroCL (suggested by David Lichteblau) Version 0.4.2 2006-09-07 Fixed :OPTIONS* method Version 0.4.1 2006-09-07 Added more methods including :OPTIONS* pseudo method (suggested by Ralf Mattes) Always (except for POST) add parameters to URI query Always read body (unless there's no chunking and no content length) Version 0.4.0 2006-09-05 Added file uploads Added multipart/form-data Added enforced computation of request bodies in RAM Use LF line endings in default external format Version 0.3.1 2006-09-04 Don't use underlying streams of flexi streams anymore Returned streams now have element type OCTET when FORCE-BINARY is true Better default "User-Agent" header for some Lisps Added info about mailing lists Added note about Gentoo Version 0.3.0 2006-09-02 Added client-side chunked encoding and various ways to send the content Version 0.2.0 2006-09-01 Completely re-factored for portability, chunking code is in Chunga now Version 0.1.3 2006-08-30 REQUIRE "comm" before WITH-STREAM-INPUT-BUFFER is used Version 0.1.2 2006-08-27 Notes about SSL and listener font Version 0.1.1 2006-08-27 Note about CL-BASE64 and KMRCL Version 0.1.0 2006-08-27 First public release drakma-v2.0.3/drakma-test.asd0000644000000000000000000000324013025750121014537 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*- ;;; Copyright (c) 2013, Anton Vodonosov. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (defsystem :drakma-test :description "Test suite for drakma" :serial t :version "0.1" :depends-on (:drakma :fiveam) :pathname #P"test/" :components ((:file "drakma-test")) :perform (test-op (o s) (uiop:symbol-call :fiveam '#:run! :drakma))) drakma-v2.0.3/read.lisp0000644000000000000000000001410513025750121013440 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/read.lisp,v 1.17 2008/05/25 11:35:20 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (defun read-status-line (stream &optional log-stream) "Reads one line from STREAM \(using Chunga's READ-LINE*) and interprets it as a HTTP status line. Returns a list of two or three values - the protocol \(HTTP version) as a keyword, the status code as an integer, and optionally the reason phrase." (let* ((*current-error-message* "While reading status line:") (line (or (read-line* stream log-stream) (error 'drakma-simple-error :format-control "No status line - probably network error."))) (first-space-pos (or (position #\Space line :test #'char=) (syntax-error "No space in status line ~S." line))) (second-space-pos (position #\Space line :test #'char= :start (1+ first-space-pos)))) (list (cond ((string-equal line "HTTP/1.0" :end1 first-space-pos) :http/1.0) ((string-equal line "HTTP/1.1" :end1 first-space-pos) :http/1.1) (t (syntax-error "Unknown protocol in ~S." line))) (or (ignore-errors (parse-integer line :start (1+ first-space-pos) :end second-space-pos)) (syntax-error "Status code in ~S is not an integer." line)) (and second-space-pos (subseq line (1+ second-space-pos)))))) (defun get-content-type (headers) "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." (when-let (content-type (header-value :content-type headers)) (with-sequence-from-string (stream content-type) (let* ((*current-error-message* "Corrupted Content-Type header:") (type (read-token stream)) (subtype (and (assert-char stream #\/) (read-token stream))) (parameters (read-name-value-pairs stream))) (values type subtype parameters))))) (defun read-token-and-parameters (stream) "Reads and returns \(as a two-element list) from STREAM a token and an optional list of parameters \(attribute/value pairs) following the token." (skip-whitespace stream) (list (read-token stream) (read-name-value-pairs stream))) (defun skip-more-commas (stream) "Reads and consumes from STREAM any number of commas and whitespace. Returns the following character or NIL in case of END-OF-FILE." (loop while (eql (peek-char* stream nil) #\,) do (read-char* stream) (skip-whitespace stream)) (skip-whitespace stream)) (defun read-tokens-and-parameters (string &key (value-required-p t)) "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." (with-sequence-from-string (stream string) (loop with *current-error-message* = (format nil "While parsing ~S:" string) for first = t then nil for next = (and (skip-whitespace stream) (or first (assert-char stream #\,)) (skip-whitespace stream) (skip-more-commas stream)) for token = (and next (read-token stream)) for parameters = (and token (read-name-value-pairs stream :value-required-p value-required-p)) while token collect (if parameters (cons token parameters) token)))) (defun split-tokens (string) "Splits the string STRING into a list of substrings separated by commas and optional whitespace. Empty substrings are ignored." (loop for old-position = -1 then position for position = (and old-position (position #\, string :test #'char= :start (1+ old-position))) for substring = (and old-position (trim-whitespace (subseq string (1+ old-position) position))) while old-position when (plusp (length substring)) collect substring)) drakma-v2.0.3/drakma.asd0000644000000000000000000000500413025750121013562 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/drakma.asd,v 1.49 2008/05/24 03:21:22 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :cl-user) #+:lispworks (unless (find-symbol "STREAM-WRITE-TIMEOUT" :stream) (pushnew :lw-does-not-have-write-timeout *features*)) (defpackage :drakma-asd (:use :cl :asdf)) (in-package :drakma-asd) (defsystem :drakma :description "Full-featured http/https client based on usocket" :serial t :version "2.0.3" :components ((:file "packages") (:file "specials") (:file "conditions") (:file "util") (:file "read") (:file "cookies") (:file "encoding") (:file "request")) :depends-on (:puri :cl-base64 :chunga :flexi-streams :cl-ppcre #-:drakma-no-chipz :chipz #-:lispworks :usocket #-(or :lispworks (and :allegro (not :allegro-cl-express)) :mocl-ssl :drakma-no-ssl) :cl+ssl) :perform (test-op (o s) (asdf:load-system :drakma-test) (asdf:perform 'asdf:test-op :drakma-test))) drakma-v2.0.3/.pre-release.sh0000755000000000000000000000001513025750121014450 0ustar rootrootcd doc; make drakma-v2.0.3/doc/0000755000000000000000000000000013025750121012400 5ustar rootrootdrakma-v2.0.3/doc/index.xml0000644000000000000000000022717313025750121014245 0ustar rootroot Drakma - A Common Lisp HTTP client 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.

Drakma - A Common Lisp HTTP client

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.

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.

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

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>

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"

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"

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\"}"

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."
#()

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

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

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

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

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

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

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 .

The canonical location for the latest version of Drakma is http://weitz.de/files/drakma.tar.gz.

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 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
=> 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.

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.

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.

string external-format string

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

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.

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.

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

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

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

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.

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.

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.

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.

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

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.

string list

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

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.

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

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. cookie string cookie (or string null) cookie string cookie (or string null) cookie (or integer null) cookie boolean cookie boolean

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.

cookie-jar list cookie-jar cookie-jar

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

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

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.

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

Valid values are:

  • NIL - duplicates will not be removed,
  • T or :KEEP-LAST - for duplicates, only the last cookie value will be kept, based on the order of the response header,
  • :KEEP-FIRST - for duplicates, only the first cookie value will be kept, based on the order of the response header.

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.

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

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

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

cookie-error (or cookie null)

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

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

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

Superclass for all conditions related to Drakma.

Superclass for all errors related to Drakma.

Superclass for all warnings related to Drakma.

drakma-v2.0.3/doc/index.html0000644000000000000000000027112713025750121014407 0ustar rootroot Drakma - A Common Lisp HTTP client

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.3.

The canonical location for the latest version of Drakma is http://weitz.de/files/drakma.tar.gz.

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

drakma-v2.0.3/doc/Makefile0000644000000000000000000000022113025750121014033 0ustar rootroot all: xsltproc --stringparam library-version `perl -ne 'print "$$1\n" if (/:version "(.*)"/)' ../drakma.asd` clixdoc.xsl index.xml > index.html drakma-v2.0.3/doc/clixdoc.xsl0000644000000000000000000004113513025750121014561 0ustar rootroot <xsl:value-of select="clix:title"/>

[]
=>

[]
=>

=>

[]
(setf ( ) new-value) =>

[]
=>
(setf ( ) new-value)

=>
(setf ( ) new-value)

[]


[Constants]

[]

[]

&

  1. #
    1. #
# - Generic function Method Macro Function Generic reader Specialized reader Reader Generic reader Specialized reader Reader Generic readers Specialized readers Readers Generic accessors Specialized accessors Accessors Generic writer Specialized writer Writer Generic accessor Specialized accessor Accessor Generic accessor Specialized accessor Accessor Special variable Standard class Condition type Symbol Constant Constant
drakma-v2.0.3/test/0000755000000000000000000000000013025750121012612 5ustar rootrootdrakma-v2.0.3/test/drakma-test.lisp0000644000000000000000000001232513025750121015722 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*- ;;; Copyright (c) 2013, Anton Vodonosov. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (defpackage :drakma-test (:use :cl :fiveam)) (in-package :drakma-test) (def-suite :drakma) (in-suite :drakma) (test get-google (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "http://google.com/") (is (> (length body-or-stream) 0)) (is (= 200 status-code))))) (test get-google-ssl (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "https://google.com/") (is (> (length body-or-stream) 0)) (is (= 200 status-code))))) (test post-google (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code headers uri stream must-close reason-phrase) (drakma:http-request "http://google.com/" :method :post :parameters '(("a" . "b"))) (declare (ignore headers uri stream must-close)) (is (> (length body-or-stream) 0)) (is (= 405 status-code)) (is (string= "Method Not Allowed" reason-phrase))))) (test post-google-ssl (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code headers uri stream must-close reason-phrase) (drakma:http-request "https://google.com/" :method :post :parameters '(("a" . "b"))) (declare (ignore headers uri stream must-close)) (is (> (length body-or-stream) 0)) (is (= 405 status-code)) (is (string= "Method Not Allowed" reason-phrase))))) (test gzip-content (let ((drakma:*header-stream* *standard-output*) (drakma:*text-content-types* (cons '(nil . "json") drakma:*text-content-types*))) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "http://httpbin.org/gzip" :decode-content t) (is (= 200 status-code)) (is (typep body-or-stream 'string)) (is (search "\"gzipped\": true" body-or-stream))))) (test deflate-content (let ((drakma:*header-stream* *standard-output*) (drakma:*text-content-types* (cons '(nil . "json") drakma:*text-content-types*))) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "http://httpbin.org/deflate" :decode-content t) (is (= 200 status-code)) (is (typep body-or-stream 'string)) (is (search "\"deflated\": true" body-or-stream))))) (test gzip-content-undecoded (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "http://httpbin.org/gzip") (is (= 200 status-code)) (is (typep body-or-stream '(vector flexi-streams:octet))) (is (> (length body-or-stream) 0)) (is (equalp #(#x1f #x8b) (subseq body-or-stream 0 2)))))) (test deflate-content-undecoded (let ((drakma:*header-stream* *standard-output*)) (multiple-value-bind (body-or-stream status-code) (drakma:http-request "http://httpbin.org/deflate") (is (= 200 status-code)) (is (typep body-or-stream '(vector flexi-streams:octet))) (is (> (length body-or-stream) 0)) (is (equalp #x78 (aref body-or-stream 0)))))) (test stream (multiple-value-bind (stream status-code) (drakma:http-request "http://google.com/" :want-stream t) (is (streamp stream)) (is (= 200 status-code)) (is (subtypep (stream-element-type stream) 'character)) (let ((buffer (make-string 1))) (read-sequence buffer stream)))) (test force-binary (multiple-value-bind (stream status-code) (drakma:http-request "http://google.com/" :want-stream t :force-binary t) (is (streamp stream)) (is (= 200 status-code)) (is (subtypep (stream-element-type stream) 'flexi-streams:octet)) (let ((buffer (make-array 1 :element-type 'flexi-streams:octet))) (read-sequence buffer stream)))) drakma-v2.0.3/request.lisp0000644000000000000000000014103613025750121014221 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/request.lisp,v 1.58 2008/05/30 11:30:45 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (defun determine-body-format (headers external-format-in) "The default function used by Drakma to determine how the content body is to be read. See the docstring of *BODY-FORMAT-FUNCTION* for more info." (handler-case (multiple-value-bind (type subtype params) (get-content-type headers) (when (text-content-type-p type subtype) (let* ((charset (parameter-value "charset" params)) (name (cond (charset (as-keyword charset)) (t external-format-in)))) (make-external-format name :eol-style :lf)))) (error (condition) (drakma-warn "Problems determining charset \(falling back to binary):~%~A" condition)))) (defun send-content (content stream &optional external-format-out) "Sends CONTENT to the stream STREAM as part of the request body depending on the type of CONTENT." (when content (cond ((stringp content) (setf (flexi-stream-external-format stream) external-format-out) (write-string content stream) (setf (flexi-stream-external-format stream) +latin-1+)) ((or (arrayp content) (listp content)) (write-sequence content stream)) ((and (streamp content) (input-stream-p content) (open-stream-p content) (subtypep (stream-element-type content) 'octet)) (let ((buf (make-array +buffer-size+ :element-type 'octet))) (loop (let ((pos (read-sequence buf content))) (when (zerop pos) (return)) (write-sequence buf stream :end pos))))) ((pathnamep content) (with-open-file (from content :element-type 'octet) ;; calls itself with a stream now (send-content from stream))) ((or (functionp content) (and (symbolp content) (fboundp content))) (funcall content stream)) (t (parameter-error "Don't know how to send content ~S to server." content))))) (defun make-form-data-function (parameters boundary external-format-out) "Creates and returns a closure which can be used as an argument for SEND-CONTENT to send PARAMETERS as a `multipart/form-data' request body using the boundary BOUNDARY." (lambda (stream) (flet ((crlf () "Sends carriage return and linefeed to STREAM." (write-char #\Return stream) (write-char #\Linefeed stream))) (dolist (name/value parameters) (destructuring-bind (name . value) name/value (when (or (pathnamep value) (streamp value) (functionp value)) (setq value (list value))) (format stream "--~A" boundary) (crlf) (format stream "Content-Disposition: form-data; name=\"~A\"" name) (cond ((stringp value) (crlf) (format stream "Content-Type: text/plain; charset=~a" external-format-out) (crlf) (crlf) (setf (flexi-stream-external-format stream) external-format-out) (format stream "~A" value) (setf (flexi-stream-external-format stream) +latin-1+)) ((and (listp value) (first value) (not (stringp (first value)))) (let* ((file-source (first value)) (filename (or (getf (rest value) :filename) (etypecase file-source (function "user-closure") (file-stream (or (file-namestring file-source) "user-stream")) (stream "user-stream") (pathname (file-namestring file-source))))) (content-type (or (getf (rest value) :content-type) "application/octet-stream"))) (format stream "; filename=\"~A\"" filename) (crlf) (format stream "Content-Type: ~A" content-type) (crlf) (crlf) ;; use SEND-CONTENT to send file as binary data (send-content file-source stream))) (t (parameter-error "Don't know what to do with name/value pair (~S . ~S) in multipart/form-data body." name value))) (crlf))) (format stream "--~A--" boundary) (crlf)))) (defun %read-body (stream element-type) ;; On ABCL, a flexi-stream is not a normal stream. This is caused by ;; a bug in ABCL which is supposedly quite difficult to fix. More ;; details here: http://abcl.org/trac/ticket/377 #-abcl (declare (stream stream)) "Helper function to read from stream into a buffer of element-type, which is returned." (let ((buffer (make-array +buffer-size+ :element-type element-type)) (result (make-array 0 :element-type element-type :adjustable t))) (loop for index = 0 then (+ index pos) for pos = (read-sequence buffer stream) do (adjust-array result (+ index pos)) (replace result buffer :start1 index :end2 pos) while (= pos +buffer-size+)) result)) (defun read-body (stream headers textp &key (decode-content t)) "Reads the message body from the HTTP stream STREAM using the information contained in HEADERS \(as produced by HTTP-REQUEST). If TEXTP is true, the body is assumed to be of content type `text' and will be returned as a string. Otherwise an array of octets \(or NIL for an empty body) is returned. Returns the optional `trailer' HTTP headers of the chunked stream \(if any) as a second value." (let ((content-length (when-let (value (and (not (header-value :transfer-encoding headers)) ;; see RFC 2616, section 4.4, 3. (header-value :content-length headers))) (parse-integer value))) (element-type (if textp #+:lispworks 'lw:simple-char #-:lispworks 'character 'octet))) (values (cond ((eql content-length 0) nil) (content-length (setf (flexi-stream-element-type stream) 'octet) (let ((result (make-array content-length :element-type 'octet))) #+:clisp (setf (flexi-stream-element-type stream) 'octet) (read-sequence result stream) (when (and decode-content (header-value :content-encoding headers)) (setq result (with-input-from-sequence (s result) (%read-body (decode-response-stream headers s) 'octet)))) (when textp (setf result (octets-to-string result :external-format (flexi-stream-external-format stream)) #+:clisp (flexi-stream-element-type stream) #+:clisp element-type)) result)) (t ;; no content length, read until EOF (or end of chunking) #+:clisp (setf (flexi-stream-element-type stream) element-type) (%read-body (decode-flexi-stream headers stream :decode-content decode-content) element-type))) (chunked-input-stream-trailers (flexi-stream-stream stream))))) (defun trivial-uri-path (uri-string) "If the PRESERVE-URI argument is used, the URI needs to be passed to the server in unmodified form. This function returns just the path component of the URI with no URL encoding or other modifications done." (cl-ppcre:regex-replace "[^/]+://[^/]*/?" uri-string "/")) (defun http-request (uri &rest args &key (protocol :http/1.1) (method :get) force-ssl certificate key certificate-password verify (max-depth 10) ca-file ca-directory parameters (url-encoder #'url-encode) content (content-type "application/x-www-form-urlencoded") (content-length nil content-length-provided-p) form-data cookie-jar basic-authorization (user-agent :drakma) (accept "*/*") range (proxy *default-http-proxy*) (no-proxy-domains *no-proxy-domains*) proxy-basic-authorization real-host additional-headers (redirect 5) auto-referer keep-alive (close t) (external-format-out *drakma-default-external-format*) (external-format-in *drakma-default-external-format*) force-binary want-stream stream preserve-uri decode-content ; default to nil for backwards compatibility #+(or abcl clisp lispworks mcl openmcl sbcl) (connection-timeout 20) #+:lispworks (read-timeout 20) #+(and :lispworks (not :lw-does-not-have-write-timeout)) (write-timeout 20 write-timeout-provided-p) #+:openmcl deadline &aux (unparsed-uri (if (stringp uri) (copy-seq uri) (puri:copy-uri uri)))) "Sends a 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 reply \(but see below), the status code as an integer, an alist of the headers 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 URI the reply comes from \(which might be different from the URI the request was sent to in case of redirects), the stream the reply was read from, a generalized boolean which denotes whether the stream should be closed \(and which you can usually ignore), and finally the reason phrase from the status line as a string. PROTOCOL is the HTTP protocol 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 one argument. The argument is the string to encode, 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 \(see http://weitz.de/flexi-streams/) with a chunked stream \(see http://weitz.de/chunga/) 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." #+lispworks (declare (ignore certificate key certificate-password verify max-depth ca-file ca-directory)) (unless (member protocol '(:http/1.0 :http/1.1) :test #'eq) (parameter-error "Don't know how to handle protocol ~S." protocol)) (setq uri (cond ((puri:uri-p uri) (puri:copy-uri uri)) (t (puri:parse-uri uri)))) (unless (member method +known-methods+ :test #'eq) (parameter-error "Don't know how to handle method ~S." method)) (unless (member (puri:uri-scheme uri) '(:http :https) :test #'eq) (parameter-error "Don't know how to handle scheme ~S." (puri:uri-scheme uri))) (when (and close keep-alive) (parameter-error "CLOSE and KEEP-ALIVE must not be both true.")) (when (and form-data (not (member method '(:post :report) :test #'eq))) (parameter-error "FORM-DATA only makes sense with POST requests.")) (when range (unless (and (listp range) (integerp (first range)) (integerp (second range)) (<= (first range) (second range))) (parameter-error "RANGE parameter must be specified as list of two integers, with the second larger or equal to the first"))) ;; supersede PROXY with REAL-HOST (when real-host (setq proxy real-host)) ;; convert PROXY argument to canonical form (when proxy (when (atom proxy) (setq proxy (list proxy 80)))) ;; Ignore the proxy for whitelisted hosts. (when (member (puri:uri-host uri) no-proxy-domains :test #'string=) (setq proxy '())) ;; make sure we don't get :CRLF on Windows (let ((*default-eol-style* :lf) (file-parameters-p (find-if-not (lambda (thing) (or (stringp thing) (null thing))) parameters :key #'cdr)) parameters-used-p) (when (and file-parameters-p (not (eq method :post))) (parameter-error "Don't know how to handle parameters in ~S, as this is not a POST request." parameters)) (when (eq method :post) ;; create content body for POST unless it was provided (unless content ;; mark PARAMETERS argument as used up, so we don't use it ;; again below (setq parameters-used-p t) (cond ((or form-data file-parameters-p) (let ((boundary (format nil "----------~A" (make-random-string)))) (setq content (make-form-data-function parameters boundary external-format-out) content-type (format nil "multipart/form-data; boundary=~A" boundary))) (unless (or file-parameters-p content-length-provided-p) (setq content-length (or content-length t)))) (t (setq content (alist-to-url-encoded-string parameters external-format-out url-encoder) content-type "application/x-www-form-urlencoded"))))) (let ((proxying-https-p (and proxy (not stream) (or force-ssl (eq :https (puri:uri-scheme uri))))) http-stream raw-http-stream must-close done) (unwind-protect (progn (let ((host (or (and proxy (first proxy)) (puri:uri-host uri))) (port (cond (proxy (second proxy)) ((puri:uri-port uri)) (t (default-port uri)))) (use-ssl (and (not proxying-https-p) (or force-ssl (eq (puri:uri-scheme uri) :https))))) #+(and :lispworks5.0 :mswindows (not :lw-does-not-have-write-timeout)) (when use-ssl (when (and write-timeout write-timeout-provided-p) (drakma-warn "Disabling WRITE-TIMEOUT because it doesn't mix well with SSL.")) (setq write-timeout nil)) (setq http-stream (or stream #+:lispworks (comm:open-tcp-stream host port :element-type 'octet :timeout connection-timeout :read-timeout read-timeout #-:lw-does-not-have-write-timeout :write-timeout #-:lw-does-not-have-write-timeout write-timeout :errorp t) #-:lispworks (usocket:socket-stream (usocket:socket-connect host port :element-type 'octet #+:openmcl :deadline #+:openmcl deadline #+(or abcl clisp lispworks mcl openmcl sbcl) :timeout #+(or abcl clisp lispworks mcl openmcl sbcl) connection-timeout :nodelay :if-supported))) raw-http-stream http-stream) #+:openmcl (when deadline ;; it is correct to set the deadline here even though ;; it may have been initialized by SOCKET-CONNECT ;; already - the stream may have been passed in by the ;; user and the user may want to adjust the deadline ;; for every request (setf (ccl:stream-deadline http-stream) deadline)) (labels ((write-http-line (fmt &rest args) (when *header-stream* (format *header-stream* "~?~%" fmt args)) (format http-stream "~?~C~C" fmt args #\Return #\Linefeed)) (write-header (name value-fmt &rest value-args) (write-http-line "~A: ~?" name value-fmt value-args)) (wrap-stream (http-stream) (make-flexi-stream (make-chunked-stream http-stream) :external-format +latin-1+))) (when (and use-ssl ;; don't attach SSL to existing streams (not stream)) #+:lispworks (comm:attach-ssl http-stream :ssl-side :client) #-:lispworks (setq http-stream (make-ssl-stream http-stream :hostname host :certificate certificate :key key :certificate-password certificate-password :verify verify :max-depth max-depth :ca-file ca-file :ca-directory ca-directory))) (cond (stream (setf (flexi-stream-element-type http-stream) #+:lispworks 'lw:simple-char #-:lispworks 'character (flexi-stream-external-format http-stream) +latin-1+)) (t (setq http-stream (wrap-stream http-stream)))) (when proxying-https-p ;; set up a tunnel through the proxy server to the ;; final destination (write-http-line "CONNECT ~A:~:[443~;~:*~A~] HTTP/1.1" (puri:uri-host uri) (puri:uri-port uri)) (write-http-line "Host: ~A:~:[443~;~:*~A~]" (puri:uri-host uri) (puri:uri-port uri)) (write-http-line "") (force-output http-stream) ;; check we get a 200 response before proceeding (unless (eql (second (read-status-line http-stream *header-stream*)) 200) (error "Unable to establish HTTPS tunnel through proxy.")) ;; got a connection; we have to read a blank line, ;; turn on SSL, and then we can transmit (read-line* http-stream) #+:lispworks (comm:attach-ssl raw-http-stream :ssl-side :client) #-:lispworks (setq http-stream (wrap-stream (make-ssl-stream raw-http-stream :hostname host :certificate certificate :key key :certificate-password certificate-password :verify verify :max-depth max-depth :ca-file ca-file :ca-directory ca-directory)))) (when-let (all-get-parameters (and (not preserve-uri) (append (dissect-query (puri:uri-query uri)) (and (not parameters-used-p) parameters)))) (setf (puri:uri-query uri) (alist-to-url-encoded-string all-get-parameters external-format-out url-encoder))) (when (eq method :options*) ;; special pseudo-method (setf method :options (puri:uri-path uri) "*" (puri:uri-query uri) nil)) (write-http-line "~A ~A ~A" (string-upcase method) (if (and preserve-uri (stringp unparsed-uri)) (trivial-uri-path unparsed-uri) (puri:render-uri (if (and proxy (null stream) (not proxying-https-p) (not real-host)) uri (make-instance 'puri:uri :path (puri:uri-path uri) :parsed-path (puri:uri-parsed-path uri) :query (puri:uri-query uri) :escaped t)) nil)) (string-upcase protocol)) (when (not (assoc "Host" additional-headers :test #'string-equal)) (write-header "Host" "~A~@[:~A~]" (puri:uri-host uri) (non-default-port uri))) (when user-agent (write-header "User-Agent" "~A" (user-agent-string user-agent))) (when basic-authorization (write-header "Authorization" "Basic ~A" (base64:string-to-base64-string (format nil "~A:~A" (first basic-authorization) (second basic-authorization))))) (when (and proxy proxy-basic-authorization) (write-header "Proxy-Authorization" "Basic ~A" (base64:string-to-base64-string (format nil "~A:~A" (first proxy-basic-authorization) (second proxy-basic-authorization))))) (when accept (write-header "Accept" "~A" accept)) (when range (write-header "Range" "bytes=~A-~A" (first range) (second range))) (when cookie-jar ;; write all cookies in one fell swoop, so even Sun's ;; web server has a chance to get it (when-let (cookies (loop for cookie in (cookie-jar-cookies cookie-jar) when (send-cookie-p cookie uri force-ssl) collect (cookie-name cookie) and collect (cookie-value cookie))) (write-header "Cookie" "~{~A=~A~^; ~}" cookies))) (when keep-alive (write-header "Connection" "Keep-Alive")) (when close (setq must-close close) (write-header "Connection" "close")) (loop for (name . value) in additional-headers do (write-header name "~A" (cond ((or (functionp value) (and (symbolp value) (fboundp value))) (funcall value)) (t value)))) (when content (when content-type (write-header "Content-Type" "~A" content-type)) (when (or (and (not content-length-provided-p) (stringp content)) (and content-length (not (or (and (integerp content-length) (not (minusp content-length))) (typep content '(or (vector octet) list)) (eq content :continuation))))) ;; CONTENT-LENGTH forces us to compute request body ;; in RAM (setq content (with-output-to-sequence (bin-out) (let ((out (make-flexi-stream bin-out :external-format +latin-1+))) (send-content content out external-format-out))))) (when (and (or (not content-length-provided-p) (eq content-length t)) (typep content '(or (vector octet) list))) (setq content-length (length content))) (cond (content-length (write-header "Content-Length" "~D" content-length)) (t (write-header "Transfer-Encoding" "chunked")))) ;; end of request headers (when *header-stream* (terpri *header-stream*)) (format http-stream "~C~C" #\Return #\Linefeed) (force-output http-stream) (when (and content (null content-length)) (setf (chunked-stream-output-chunking-p (flexi-stream-stream http-stream)) t)) (labels ((finish-request (content &optional continuep) (send-content content http-stream external-format-out) (when continuep (force-output http-stream) (return-from finish-request)) (setf (chunked-stream-output-chunking-p (flexi-stream-stream http-stream)) nil) (finish-output http-stream) (with-character-stream-semantics (multiple-value-bind (server-protocol status-code status-text) ;; loop until status is NOT 100 (loop for (server-protocol status-code status-text) = (read-status-line http-stream *header-stream*) when (= status-code 100) ;; ignore headers sent until non-100 status is seen do (read-http-headers http-stream *header-stream*) until (/= status-code 100) finally (return (values server-protocol status-code status-text))) (let ((headers (read-http-headers http-stream *header-stream*)) body external-format-body) (let ((connections (header-value :connection headers))) (when connections (setq connections (split-tokens connections))) (when (or (member "close" connections :test #'string-equal) (not (or (and (eq protocol :http/1.1) (eq server-protocol :http/1.1)) (member "Keep-Alive" connections :test #'string-equal)))) (setq must-close t))) (when cookie-jar (update-cookies (get-cookies headers uri) cookie-jar)) (when (and redirect (member status-code +redirect-codes+) (header-value :location headers)) (unless (or (eq redirect t) (and (integerp redirect) (plusp redirect))) (cerror "Continue anyway." 'drakma-simple-error :format-control "Status code was ~A, but ~ ~:[REDIRECT is ~S~;redirection limit has been exceeded~]." :format-arguments (list status-code (integerp redirect) redirect))) (when auto-referer (setq additional-headers (set-referer uri additional-headers))) (let* ((location (header-value :location headers)) (new-uri (puri:merge-uris location uri)) ;; can we re-use the stream? (old-server-p (and (string= (puri:uri-host new-uri) (puri:uri-host uri)) (eql (puri:uri-port new-uri) (puri:uri-port uri)) (eq (puri:uri-scheme new-uri) (puri:uri-scheme uri))))) (unless old-server-p (setq must-close t want-stream nil)) ;; try to re-use the stream, but only ;; if the user hasn't opted for a ;; connection which is always secure (let ((re-use-stream (and old-server-p (not must-close) (not force-ssl)))) ;; close stream if we can't re-use it (unless re-use-stream (ignore-errors (close http-stream))) (setq done t) (return-from http-request (let ((method (if (and (member status-code +redirect-to-get-codes+) (member method +redirect-to-get-methods+)) :get method))) (apply #'http-request new-uri :method method :redirect (cond ((integerp redirect) (1- redirect)) (t redirect)) :stream (and re-use-stream http-stream) :additional-headers additional-headers :parameters parameters :preserve-uri t :form-data (if (eq method :get) nil form-data) args)))))) (let ((transfer-encodings (header-value :transfer-encoding headers))) (when transfer-encodings (setq transfer-encodings (split-tokens transfer-encodings))) (when (member "chunked" transfer-encodings :test #'equalp) (setf (chunked-stream-input-chunking-p (flexi-stream-stream http-stream)) t))) (when (setq external-format-body (and (not force-binary) (funcall *body-format-function* headers external-format-in))) (setf (flexi-stream-external-format http-stream) external-format-body)) (when force-binary (setf (flexi-stream-element-type http-stream) 'octet)) (unless (or want-stream (eq method :head)) (let (trailers) (multiple-value-setq (body trailers) (read-body http-stream headers external-format-body :decode-content decode-content)) (when trailers (drakma-warn "Adding trailers from chunked encoding to HTTP headers.") (setq headers (nconc headers trailers))))) (setq done t) (values (if want-stream (decode-flexi-stream headers http-stream :decode-content decode-content) body) status-code headers uri http-stream must-close status-text)))))) (when (eq content :continuation) (return-from http-request #'finish-request)) (finish-request content))))) ;; the cleanup form of the UNWIND-PROTECT above (when (and http-stream (or (not done) (and must-close (not want-stream))) (not (eq content :continuation))) (ignore-errors (close http-stream))))))) drakma-v2.0.3/encoding.lisp0000644000000000000000000001174213025750121014317 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/read.lisp,v 1.17 2008/05/25 11:35:20 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (defgeneric decode-stream (encoding-type stream) (:documentation "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.")) (defmethod decode-stream ((encoding-type t) stream) "Default handler, just return the stream." stream) #-:drakma-no-chipz (defmethod decode-stream ((encoding-type (eql :gzip)) stream) "Decode stream using gzip compression." (chipz:make-decompressing-stream 'chipz:gzip stream)) #-:drakma-no-chipz (defmethod decode-stream ((encoding-type (eql :deflate)) stream) "Decode stream using deflate compression in zlib container." (chipz:make-decompressing-stream 'chipz:zlib stream)) (defmethod decode-stream ((encoding-type (eql :chunked)) (stream chunked-input-stream)) "Decode a chunked stream. Special method for chunked-input-stream that just turns chunking on." (setf (chunked-stream-input-chunking-p stream) t) stream) (defmethod decode-stream ((encoding-type (eql :chunked)) stream) "General decode method for chunked stream. Creates new chunked-stream." (let ((chunk-stream (make-chunked-stream stream))) (decode-stream :chunked chunk-stream))) (defun decode-response-stream (headers stream &key (decode-content t)) "Perform all necessary decodings on stream, from the Transfer-Encoding and Content-Encoding headers. If DECODE-CONTENT is nil, only the Transfer-Encoding headers will be used." (let ((transfer-encodings (header-value :transfer-encoding headers)) (content-encodings (and decode-content (header-value :content-encoding headers)))) (when transfer-encodings (setq transfer-encodings (split-tokens transfer-encodings))) (when content-encodings (setq content-encodings (split-tokens content-encodings))) ;; Reverse, because we need to run decodings in the opposite order ;; they were applied. (let ((encodings (nreverse (nconc content-encodings transfer-encodings)))) (loop for s = stream then (decode-stream encoding s) for encoding-str in encodings for encoding = (intern (string-upcase encoding-str) 'keyword) finally (return s))))) (defun decode-flexi-stream (headers stream &key (decode-content t)) (declare (flexi-input-stream stream)) "Perform all necessary decodings on the internal stream of a flexi-stream. Wrapper around decode-response-stream which preserverves the external format of the flexi-stream. If DECODE-CONTENT is nil, the Content-Encoding header will not be used to determine which decoding mechanisms to use. Most servers use Content-Encoding to identify compression." (let* ((raw-stream (flexi-stream-stream stream)) (result (decode-response-stream headers raw-stream :decode-content decode-content))) (if (eq raw-stream result) stream (make-flexi-stream result :external-format (flexi-stream-external-format stream) :element-type (flexi-stream-element-type stream))))) drakma-v2.0.3/specials.lisp0000644000000000000000000002305713025750121014336 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/specials.lisp,v 1.19 2008/01/14 01:57:02 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (defparameter *drakma-version* #.(asdf:component-version (asdf:find-system :drakma)) "Drakma's version number as a string.") (defmacro define-constant (name value &optional doc) "A version of DEFCONSTANT for, cough, /strict/ CL implementations." ;; See `(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value) ,@(when doc (list doc)))) (define-constant +latin-1+ (make-external-format :latin-1 :eol-style :lf) "Default external format when reading headers.") (define-constant +redirect-codes+ '(301 302 303 307) "A list of all HTTP return codes that redirect us to another URI.") (define-constant +redirect-to-get-codes+ '(302 303) "A list of HTTP return codes that redirect using a GET method (see http://en.wikipedia.org/wiki/Post/Redirect/Get).") (define-constant +known-methods+ '(:copy :delete :get :head :lock :mkcol :move :options :options* :patch :post :propfind :proppatch :put :report :trace :unlock) "The HTTP methods \(including WebDAV methods) Drakma knows.") (define-constant +redirect-to-get-methods+ '(:post) "A list of HTTP methods that should be changed to GET in case of redirect (see http://en.wikipedia.org/wiki/Post/Redirect/Get).") (defconstant +buffer-size+ 8192) (defvar *drakma-default-external-format* ':latin-1 "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).") (defvar *header-stream* nil "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.") (defvar *allow-dotless-cookie-domains-p* nil "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\".") (defvar *ignore-unparseable-cookie-dates-p* nil "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.") (defvar *remove-duplicate-cookies-p* t "Determines how duplicate cookies in the response are handled, defaults to T. Cookies are considered duplicate using COOKIE=. Valid values are: NIL - duplicates will not be removed, T or :KEEP-LAST - for duplicates, only the last cookie value will be kept, based on the order of the response header, :KEEP-FIRST - for duplicates, only the first cookie value will be kept, based on the order of the response header. 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.") (defvar *text-content-types* '(("text" . nil)) "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.") (defvar *body-format-function* 'determine-body-format "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.") (defvar *time-zone-map* ;; list taken from ;; '(("A" . -1) ("ACDT" . -10.5) ("ACST" . -9.5) ("ADT" . 3) ("AEDT" . -11) ("AEST" . -10) ("AKDT" . 8) ("AKST" . 9) ("AST" . 4) ("AWDT" . -9) ("AWST" . -8) ("B" . -2) ("BST" . -1) ("C" . -3) ("CDT" . 5) ("CEDT" . -2) ("CEST" . -2) ("CET" . -1) ("CST" . -10.5) ("CST" . -9.5) ("CST" . 6) ("CXT" . -7) ("D" . -4) ("E" . -5) ("EDT" . 4) ("EEDT" . -3) ("EEST" . -3) ("EET" . -2) ("EST" . -11) ("EST" . -10) ("EST" . 5) ("F" . -6) ("G" . -7) ("GMT" . 0) ("H" . -8) ("HAA" . 3) ("HAC" . 5) ("HADT" . 9) ("HAE" . 4) ("HAP" . 7) ("HAR" . 6) ("HAST" . 10) ("HAT" . 2.5) ("HAY" . 8) ("HNA" . 4) ("HNC" . 6) ("HNE" . 5) ("HNP" . 8) ("HNR" . 7) ("HNT" . 3.5) ("HNY" . 9) ("I" . -9) ("IST" . -1) ("K" . -10) ("L" . -11) ("M" . -12) ("MDT" . 6) ("MESZ" . -2) ("MEZ" . -1) ("MST" . 7) ("N" . 1) ("NDT" . 2.5) ("NFT" . -11.5) ("NST" . 3.5) ("O" . 2) ("P" . 3) ("PDT" . 7) ("PST" . 8) ("Q" . 4) ("R" . 5) ("S" . 6) ("T" . 7) ("U" . 8) ("UTC" . 0) ("V" . 9) ("W" . 10) ("WEDT" . -1) ("WEST" . -1) ("WET" . 0) ("WST" . -9) ("WST" . -8) ("X" . 11) ("Y" . 12) ("Z" . 0)) "An alist which maps time zone abbreviations to Common Lisp timezones.") (defvar *default-http-proxy* nil "HTTP proxy to be used as default. 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).") ;; stuff for Nikodemus Siivola's HYPERDOC ;; see ;; and ;; also used by LW-ADD-ONS (defvar *no-proxy-domains* nil "A list of domains for which a proxy should not be used.") (defvar *hyperdoc-base-uri* "http://weitz.de/drakma/") (let ((exported-symbols-alist (loop for symbol being the external-symbols of :drakma collect (cons symbol (concatenate 'string "#" (string-downcase symbol)))))) (defun hyperdoc-lookup (symbol type) (declare (ignore type)) (cdr (assoc symbol exported-symbols-alist :test #'eq)))) drakma-v2.0.3/cookies.lisp0000644000000000000000000003634113025750121014167 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/cookies.lisp,v 1.15 2008/01/14 01:57:01 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (defclass cookie () ((name :initarg :name :initform (cookie-error nil "A cookie must have a name.") :accessor cookie-name :documentation "The name of the cookie.") (value :initarg :value :initform "" :accessor cookie-value :documentation "The cookie's value.") (domain :initarg :domain :initform (cookie-error nil "A cookie must have a domain.") :accessor cookie-domain :documentation "The domain the cookie is valid for.") (path :initarg :path :initform "/" :accessor cookie-path :documentation "The path prefix the cookie is valid for.") (expires :initarg :expires :initform nil :accessor cookie-expires :documentation "When the cookie expires. A Lisp universal time or NIL.") (securep :initarg :securep :initform nil :accessor cookie-securep :documentation "Whether the cookie must only be transmitted over secure connections.") (http-only-p :initarg :http-only-p :initform nil :accessor cookie-http-only-p :documentation "Whether the cookie should not be accessible from Javascript. This is a Microsoft extension that has been implemented in Firefox as well. See .")) (:documentation "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).")) (defun render-cookie-date (time) "Returns a string representation of the universal time TIME which can be used for cookie headers." (multiple-value-bind (second minute hour date month year weekday) (decode-universal-time time 0) (format nil "~A, ~2,'0d-~2,'0d-~4,'0d ~2,'0d:~2,'0d:~2,'0d GMT" (aref #("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun") weekday) date month year hour minute second))) (defmethod print-object ((cookie cookie) stream) "Prints a representation of COOKIE similar to a `Set-Cookie' header." (print-unreadable-object (cookie stream :type t) (with-slots (name value expires path domain securep http-only-p) cookie (format stream "~A~@[=~A~]~@[; expires=~A~]~@[; path=~A~]~@[; domain=~A~]~@[; secure~]~@[; HttpOnly~]" name (and (plusp (length value)) value) (and expires (render-cookie-date expires)) path domain securep http-only-p)))) (defun normalize-cookie-domain (domain) "Adds a dot at the beginning of the string DOMAIN unless there is already one." (cond ((starts-with-p domain ".") domain) (t (format nil ".~A" domain)))) (defun valid-cookie-domain-p (domain) "Checks if the string DOMAIN contains enough dots to be acceptable. If *ALLOW-DOTLESS-COOKIE-DOMAINS-P* is non-NIL, every domain name is considered acceptable." (or *allow-dotless-cookie-domains-p* (string-equal domain "localhost") (> (count #\. (normalize-cookie-domain domain) :test #'char=) 1))) (defun cookie-domain-matches (domain uri) "Checks if the domain DOMAIN \(a string) matches the \(PURI) URI URI." (ends-with-p (normalize-cookie-domain (puri:uri-host uri)) (normalize-cookie-domain domain))) (defun send-cookie-p (cookie uri force-ssl) "Checks if the cookie COOKIE should be sent to the server depending on the \(PURI) URI URI and the value of FORCE-SSL \(as in HTTP-REQUEST)." (and ;; check domain (cookie-domain-matches (cookie-domain cookie) uri) ;; check path (starts-with-p (or (puri:uri-path uri) "/") (cookie-path cookie)) ;; check expiry date (let ((expires (cookie-expires cookie))) (or (null expires) (> expires (get-universal-time)))) ;; check if connection must be secure (or (null (cookie-securep cookie)) force-ssl (eq (puri:uri-scheme uri) :https)))) (defun check-cookie (cookie) "Checks if the slots of the COOKIE object COOKIE have valid values and raises a corresponding error of type COOKIE-ERROR otherwise." (with-slots (name value domain path expires) cookie (unless (and (stringp name) (plusp (length name))) (cookie-error cookie "Cookie name ~S must be a non-empty string." name)) (unless (stringp value) (cookie-error cookie "Cookie value ~S must be a non-empty string." value)) (unless (valid-cookie-domain-p domain) (cookie-error cookie "Invalid cookie domain ~S." domain)) (unless (and (stringp path) (plusp (length path))) (cookie-error cookie "Cookie path ~S must be a non-empty string." path)) (unless (or (null expires) (and (integerp expires) (plusp expires))) (cookie-error cookie "Cookie expiry ~S should have been NIL or a universal time." expires)))) (defmethod initialize-instance :after ((cookie cookie) &rest initargs) "Check cookie validity after creation." (declare (ignore initargs)) (check-cookie cookie)) (defmethod (setf cookie-name) :after (new-value (cookie cookie)) "Check cookie validity after name change." (declare (ignore new-value)) (check-cookie cookie)) (defmethod (setf cookie-value) :after (new-value (cookie cookie)) "Check cookie validity after value change." (declare (ignore new-value)) (check-cookie cookie)) (defmethod (setf cookie-domain) :after (new-value (cookie cookie)) "Check cookie validity after domain change." (declare (ignore new-value)) (check-cookie cookie)) (defmethod (setf cookie-path) :after (new-value (cookie cookie)) "Check cookie validity after path change." (declare (ignore new-value)) (check-cookie cookie)) (defmethod (setf cookie-expires) :after (new-value (cookie cookie)) "Check cookie validity after expiry change." (declare (ignore new-value)) (check-cookie cookie)) (defun cookie= (cookie1 cookie2) "Returns true if the cookies COOKIE1 and COOKIE2 are equal. Two cookies are considered to be equal if name and path are equal." (and (string= (cookie-name cookie1) (cookie-name cookie2)) (string= (cookie-path cookie1) (cookie-path cookie2)))) (defclass cookie-jar () ((cookies :initarg :cookies :initform nil :accessor cookie-jar-cookies :documentation "A list of the cookies in this cookie jar.")) (:documentation "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.")) (defmethod print-object ((cookie-jar cookie-jar) stream) "Print a cookie jar, showing the number of cookies it contains." (print-unreadable-object (cookie-jar stream :type t :identity t) (format stream "(with ~A cookie~:P)" (length (cookie-jar-cookies cookie-jar))))) (defun parse-cookie-date (string) "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. " ;; it seems like everybody and their sister invents their own format ;; for this, so (as there's no real standard for it) we'll have to ;; make this function more flexible once we come across something ;; new; as an alternative we could use net-telent-date, but it also ;; fails to parse some of the stuff you encounter in the wild; or we ;; could try to employ CL-PPCRE, but that'd add a new dependency ;; without making this code much cleaner (handler-case (let* ((last-space-pos (or (position #\Space string :test #'char= :from-end t) (cookie-date-parse-error "Can't parse cookie date ~S, no space found." string))) (time-zone-string (subseq string (1+ last-space-pos))) (time-zone (interpret-as-time-zone time-zone-string)) second minute hour day month year) (dolist (part (rest (cl-ppcre:split "[ ,-]" (subseq string 0 last-space-pos)))) (when (and day month) (cond ((every #'digit-char-p part) (when year (cookie-date-parse-error "Can't parse cookie date ~S, confused by ~S part." string part)) (setq year (parse-integer part))) ((= (count #\: part :test #'char=) 2) (let ((h-m-s (mapcar #'safe-parse-integer (cl-ppcre:split ":" part)))) (setq hour (first h-m-s) minute (second h-m-s) second (third h-m-s)))) (t (cookie-date-parse-error "Can't parse cookie date ~S, confused by ~S part." string part)))) (cond ((null day) (unless (setq day (safe-parse-integer part)) (setq month (interpret-as-month part)))) ((null month) (setq month (interpret-as-month part))))) (unless (and second minute hour day month year) (cookie-date-parse-error "Can't parse cookie date ~S, component missing." string)) (when (< year 100) (setq year (+ year 2000))) (encode-universal-time second minute hour day month year time-zone)) (cookie-date-parse-error (condition) (cond (*ignore-unparseable-cookie-dates-p* (drakma-warn "~A" condition) nil) (t (error condition)))))) (defun parse-set-cookie (string) "Parses the `Set-Cookie' header line STRING and returns a list of three-element lists where each one contains the name of the cookie, the value of the cookie, and an attribute/value list for the optional cookie parameters." (let ((*current-error-message* (format nil "While parsing cookie header ~S:" string)) result) (dolist (substring (split-set-cookie-string string)) (with-sequence-from-string (stream substring) (let* ((name/value (read-name-value-pair stream :cookie-syntax t)) (parameters (read-name-value-pairs stream :value-required-p nil :cookie-syntax t))) (push (list (car name/value) (cdr name/value) parameters) result)))) (nreverse result))) (defun get-cookies (headers uri) "Returns a list of COOKIE objects corresponding to the `Set-Cookie' header as found in HEADERS \(an alist as returned by HTTP-REQUEST). Collects only cookies which match the domain of the \(PURI) URI URI." (loop with set-cookie-header = (header-value :set-cookie headers) with parsed-cookies = (and set-cookie-header (parse-set-cookie set-cookie-header)) for (name value parameters) in parsed-cookies for expires = (parameter-value "expires" parameters) for domain = (or (parameter-value "domain" parameters) (puri:uri-host uri)) when (and (valid-cookie-domain-p domain) (cookie-domain-matches domain uri)) collect (make-instance 'cookie :name name :value value :path (or (parameter-value "path" parameters) (puri:uri-path uri) "/") :expires (and expires (plusp (length expires)) (parse-cookie-date expires)) :domain domain :securep (not (not (parameter-present-p "secure" parameters))) :http-only-p (not (not (parameter-present-p "HttpOnly" parameters)))) into new-cookies finally (return (ccase *remove-duplicate-cookies-p* ((nil) new-cookies) ((:keep-last t) (delete-duplicates new-cookies :test #'cookie=)) (:keep-first (delete-duplicates new-cookies :test #'cookie= :from-end T)))))) (defun update-cookies (new-cookies cookie-jar) "Updates the cookies in COOKIE-JAR by replacing those which are equal to a cookie in \(the list) NEW-COOKIES with the corresponding `new' cookie and adding those which are really new." (setf (cookie-jar-cookies cookie-jar) (let ((updated-cookies (loop for old-cookie in (cookie-jar-cookies cookie-jar) collect (or (find old-cookie new-cookies :test #'cookie=) old-cookie)))) (union updated-cookies (set-difference new-cookies updated-cookies :test #'cookie=) :test #'cookie=))) cookie-jar) (defun delete-old-cookies (cookie-jar) "Removes all cookies from COOKIE-JAR which have either expired or which don't have an expiry date." (setf (cookie-jar-cookies cookie-jar) (loop with now = (get-universal-time) for cookie in (cookie-jar-cookies cookie-jar) for expires = (cookie-expires cookie) unless (or (null expires) (< expires now)) collect cookie)) cookie-jar) drakma-v2.0.3/README.md0000644000000000000000000000052613025750121013115 0ustar rootroot# DRAKMA - http client written in Common Lisp DRAKMA is a http client written in Common Lisp. Please visit [the documentation site](http://weitz.de/drakma/) for more information. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/edicl/drakma?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) drakma-v2.0.3/util.lisp0000644000000000000000000004013413025750121013503 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: DRAKMA; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/drakma/util.lisp,v 1.36 2008/05/30 11:30:45 edi Exp $ ;;; Copyright (c) 2006-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) #+:lispworks (require "comm") #+:lispworks (eval-when (:compile-toplevel :load-toplevel :execute) (import 'lw:when-let)) #-:lispworks (defmacro when-let ((var expr) &body body) "Evaluates EXPR, binds it to VAR, and executes BODY if VAR has a true value." `(let ((,var ,expr)) (when ,var ,@body))) #+:lispworks (eval-when (:compile-toplevel :load-toplevel :execute) (import 'lw:with-unique-names)) #-:lispworks (defmacro with-unique-names ((&rest bindings) &body body) "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form* Executes a series of forms with each VAR bound to a fresh, uninterned symbol. The uninterned symbol is as if returned by a call to GENSYM with the string denoted by X - or, if X is not supplied, the string denoted by VAR - as argument. The variable bindings created are lexical unless special declarations are specified. The scopes of the name bindings and declarations do not include the Xs. The forms are evaluated in order, and the values of all but the last are discarded \(that is, the body is an implicit PROGN)." ;; reference implementation posted to comp.lang.lisp as ;; by Vebjorn Ljosa - see also ;; `(let ,(mapcar #'(lambda (binding) (check-type binding (or cons symbol)) (if (consp binding) (destructuring-bind (var x) binding (check-type var symbol) `(,var (gensym ,(etypecase x (symbol (symbol-name x)) (character (string x)) (string x))))) `(,binding (gensym ,(symbol-name binding))))) bindings) ,@body)) (defun ends-with-p (seq suffix &key (test #'char-equal)) "Returns true if the sequence SEQ ends with the sequence SUFFIX. Individual elements are compared with TEST." (let ((mismatch (mismatch seq suffix :from-end t :test test))) (or (null mismatch) (= mismatch (- (length seq) (length suffix)))))) (defun starts-with-p (seq prefix &key (test #'char-equal)) "Returns true if the sequence SEQ starts with the sequence PREFIX whereby the elements are compared using TEST." (let ((mismatch (mismatch seq prefix :test test))) (or (null mismatch) (= mismatch (length prefix))))) (defun url-encode (string external-format) "Returns a URL-encoded version of the string STRING using the external format EXTERNAL-FORMAT." (with-output-to-string (out) (loop for octet across (string-to-octets (or string "") :external-format external-format) for char = (code-char octet) do (cond ((or (char<= #\0 char #\9) (char<= #\a char #\z) (char<= #\A char #\Z) (find char "$-_.!*'()," :test #'char=)) (write-char char out)) ((char= char #\Space) (write-char #\+ out)) (t (format out "%~2,'0x" (char-code char))))))) (defun alist-to-url-encoded-string (alist external-format url-encoder) "ALIST is supposed to be an alist of name/value pairs where both names and values are strings \(or, for values, NIL). This function returns a string where this list is represented as for the content type `application/x-www-form-urlencoded', i.e. the values are URL-encoded using the external format EXTERNAL-FORMAT, the pairs are joined with a #\\& character, and each name is separated from its value with a #\\= character. If the value is NIL, no #\\= is used." (with-output-to-string (out) (loop for first = t then nil for (name . value) in alist unless first do (write-char #\& out) do (format out "~A~:[~;=~A~]" (funcall url-encoder name external-format) value (funcall url-encoder value external-format))))) (defun default-port (uri) "Returns the default port number for the \(PURI) URI URI. Works only with the http and https schemes." (ecase (puri:uri-scheme uri) (:http 80) (:https 443))) (defun non-default-port (uri) "If the \(PURI) URI specifies an explicit port number which is different from the default port its scheme, this port number is returned, otherwise NIL." (when-let (port (puri:uri-port uri)) (when (/= port (default-port uri)) port))) (defun user-agent-string (token) "Returns a corresponding user agent string if TOKEN is one of the keywords :DRAKMA, :FIREFOX, :EXPLORER, :OPERA, or :SAFARI. Returns TOKEN itself otherwise." (case token (:drakma (format nil "Drakma/~A (~A~@[ ~A~]; ~A;~@[ ~A;~] http://weitz.de/drakma/)" *drakma-version* (or (lisp-implementation-type) "Common Lisp") (or (lisp-implementation-version) "") (or #-:clisp (software-type) #+(or :win32 :mswindows) "Windows" #-(or :win32 :mswindows) "Unix") (or #-:clisp (software-version)))) (:firefox "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6") (:explorer "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)") (:opera "Opera/9.01 (Windows NT 5.1; U; en)") (:safari "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3") (otherwise token))) (defun header-value (name headers) "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)." (cdr (assoc name headers :test #'eq))) (defun parameter-present-p (name parameters) "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." (assoc name parameters :test #'string-equal)) (defun parameter-value (name parameters) "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." (cdr (parameter-present-p name parameters))) (defun make-random-string (&optional (length 50)) "Generates and returns a random string length LENGTH. The string will consist solely of decimal digits and ASCII letters." (with-output-to-string (s) (dotimes (i length) (write-char (ecase (random 5) ((0 1) (code-char (+ #.(char-code #\a) (random 26)))) ((2 3) (code-char (+ #.(char-code #\A) (random 26)))) ((4) (code-char (+ #.(char-code #\0) (random 10))))) s)))) (defun safe-parse-integer (string) "Like PARSE-INTEGER, but returns NIL instead of signalling an error." (ignore-errors (parse-integer string))) (defun interpret-as-month (string) "Tries to interpret STRING as a string denoting a month and returns the corresponding number of the month. Accepts three-letter abbreviations like \"Feb\" and full month names likes \"February\". Finally, the function also accepts strings representing integers from one to twelve." (or (when-let (pos (position (subseq string 0 (min 3 (length string))) '("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec") :test #'string=)) (1+ pos)) (when-let (num (safe-parse-integer string)) (when (<= 1 num 12) num)))) (defun interpret-as-time-zone (string) "Tries to interpret STRING as a time zone abbreviation which can either be something like \"PST\" or \"GMT\" with an offset like \"GMT-02:00\"." (or (cdr (assoc string *time-zone-map* :test #'string=)) (cl-ppcre:register-groups-bind (sign hours minutes) ("(?:GMT|)\\s*([+-]?)(\\d\\d):?(\\d\\d)" string) (* (if (equal sign "-") 1 -1) (+ (parse-integer hours) (/ (parse-integer minutes) 60)))) (cookie-date-parse-error "Can't interpret ~S as a time zone." string))) (defun set-referer (referer-uri &optional alist) "Returns a fresh copy of the HTTP header list ALIST with the `Referer' header set to REFERER-URI. If REFERER-URI is NIL, the result will be a list of headers without a `Referer' header." (let ((alist-sans-referer (remove "Referer" alist :key #'car :test #'string=))) (cond (referer-uri (acons "Referer" referer-uri alist-sans-referer)) (t alist-sans-referer)))) (defun text-content-type-p (type subtype) "Returns a true value iff the combination of TYPE and SUBTYPE matches an entry of *TEXT-CONTENT-TYPES*. See docstring of *TEXT-CONTENT-TYPES* for more info." (loop for (candidate-type . candidate-subtype) in *text-content-types* thereis (and (or (null candidate-type) (string-equal type candidate-type)) (or (null candidate-subtype) (string-equal subtype candidate-subtype))))) (defmacro with-sequence-from-string ((stream string) &body body) "Kludge to make Chunga tokenizing functionality usable. Works like WITH-INPUT-FROM-STRING, but creates a sequence of octets that works with CHUNGA::PEEK-CHAR* and friends." `(flex:with-input-from-sequence (,stream (map 'list #'char-code ,string)) ,@body)) (defun split-set-cookie-string (string) "Splits the string STRING which is assumed to be the value of a `Set-Cookie' into parts corresponding to individual cookies and returns a list of these parts \(substrings). The string /should/ be split at commas, but heuristical approach is used instead which doesn't split at commas which are followed by what cannot be recognized as the start of the next cookie. This is necessary because servers send headers containing unquoted commas which are not meant as separators." ;; this would of course be a lot easier with CL-PPCRE's SPLIT (let ((cookie-start 0) (string-length (length string)) search-start result) (tagbody ;; at this point we know that COOKIE-START is the start of a new ;; cookie (at the start of the string or behind a comma) next-cookie (setq search-start cookie-start) ;; we reach this point if the last comma didn't separate two ;; cookies or if there was no previous comma skip-comma (unless (< search-start string-length) (return-from split-set-cookie-string (nreverse result))) ;; look is there's a comma (let* ((comma-pos (position #\, string :start search-start)) ;; and if so, look for a #\= behind the comma (equals-pos (and comma-pos (position #\= string :start comma-pos))) ;; check that (except for whitespace) there's only a token ;; (the name of the next cookie) between #\, and #\= (new-cookie-start-p (and equals-pos (every 'token-char-p (trim-whitespace string :start (1+ comma-pos) :end equals-pos))))) (when (and comma-pos (not new-cookie-start-p)) (setq search-start (1+ comma-pos)) (go skip-comma)) (let ((end-pos (or comma-pos string-length))) (push (trim-whitespace (subseq string cookie-start end-pos)) result) (setq cookie-start (1+ end-pos)) (go next-cookie)))))) #-:lispworks (defun make-ssl-stream (http-stream &key certificate key certificate-password verify (max-depth 10) ca-file ca-directory hostname) "Attaches SSL to the stream HTTP-STREAM and returns the SSL stream \(which will not be equal to HTTP-STREAM)." (declare (ignorable http-stream certificate-password max-depth ca-directory hostname)) (check-type verify (member nil :optional :required)) (when (and certificate (not (probe-file certificate))) (error "certificate file ~A not found" certificate)) (when (and key (not (probe-file key))) (error "key file ~A not found" key)) (when (and ca-file (not (probe-file ca-file))) (error "ca file ~A not found" ca-file)) #+(and :allegro (not :allegro-cl-express) (not :drakma-no-ssl)) (socket:make-ssl-client-stream http-stream :certificate certificate :key key :certificate-password certificate-password :verify verify :max-depth max-depth :ca-file ca-file :ca-directory ca-directory) #+(and :mocl-ssl (not :drakma-no-ssl)) (progn (when (or ca-file ca-directory) (warn ":max-depth, :ca-file and :ca-directory arguments not available on this platform")) (rt:start-ssl http-stream :verify verify)) #+(and (or :allegro-cl-express (not :allegro)) (not :mocl-ssl) (not :drakma-no-ssl)) (let ((s http-stream) (ctx (cl+ssl:make-context :verify-depth max-depth :verify-mode (if (eql verify :required) cl+ssl:+ssl-verify-peer+ cl+ssl:+ssl-verify-none+) :verify-location (or (and ca-file ca-directory (list ca-file ca-directory)) ca-file ca-directory :default)))) (cl+ssl:with-global-context (ctx) (cl+ssl:make-ssl-client-stream (cl+ssl:stream-fd s) :hostname hostname :close-callback (lambda () (close s) (cl+ssl:ssl-ctx-free ctx)) :certificate certificate :key key :password certificate-password))) #+:drakma-no-ssl (error "SSL not supported. Remove :drakma-no-ssl from *features* to enable SSL")) (defun dissect-query (query-string) "Accepts a query string as in PURI:URI-QUERY and returns a corresponding alist of name/value pairs." (when query-string (loop for parameter-pair in (cl-ppcre:split "&" query-string) for (name value) = (cl-ppcre:split "=" parameter-pair :limit 2) collect (cons name value)))) drakma-v2.0.3/conditions.lisp0000644000000000000000000001067313025750121014704 0ustar rootroot;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: ODD-STREAMS; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/odd-streams/conditions.lisp,v 1.5 2007/12/31 01:08:45 edi Exp $ ;;; Copyright (c) 2008-2012, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package :drakma) (define-condition drakma-condition (condition) () (:documentation "Superclass for all conditions related to Drakma.")) (define-condition drakma-error (drakma-condition error) () (:documentation "Superclass for all errors related to Drakma.")) (define-condition drakma-simple-error (drakma-error simple-condition) () (:documentation "Like DRAKMA-ERROR but with formatting capabilities.")) (define-condition drakma-warning (drakma-condition warning) () (:documentation "Superclass for all warnings related to Drakma.")) (define-condition drakma-simple-warning (drakma-warning simple-condition) () (:documentation "Like DRAKMA-WARNING but with formatting capabilities.")) (defun drakma-warn (format-control &rest format-arguments) "Signals a warning of type DRAKMA-SIMPLE-WARNING with the provided format control and arguments." (warn 'drakma-simple-warning :format-control format-control :format-arguments format-arguments)) (define-condition parameter-error (drakma-simple-error) () (:documentation "Signalled if a function was called with inconsistent or illegal parameters.")) (defun parameter-error (format-control &rest format-arguments) "Signals an error of type PARAMETER-ERROR with the provided format control and arguments." (error 'parameter-error :format-control format-control :format-arguments format-arguments)) (define-condition syntax-error (drakma-simple-error) () (:documentation "Signalled if Drakma encounters wrong or unknown syntax when reading the reply from the server.")) (defun syntax-error (format-control &rest format-arguments) "Signals an error of type SYNTAX-ERROR with the provided format control and arguments." (error 'syntax-error :format-control format-control :format-arguments format-arguments)) (define-condition cookie-error (drakma-simple-error) ((cookie :initarg :cookie :initform nil :reader cookie-error-cookie :documentation "The COOKIE object that caused this error. Can be NIL in case such an object couldn't be initialized.")) (:documentation "Signalled if someone tries to create a COOKIE object that's not valid.")) (defun cookie-error (cookie format-control &rest format-arguments) "Signals an error of type COOKIE-ERROR with the provided cookie \(can be NIL), format control and arguments." (error 'cookie-error :cookie cookie :format-control format-control :format-arguments format-arguments)) (define-condition cookie-date-parse-error (cookie-error) () (:documentation "Signalled if Drakma tries to parse the date of an incoming cookie header and can't interpret it.")) (defun cookie-date-parse-error (format-control &rest format-arguments) "Signals an error of type COOKIE-DATE-PARSE-ERROR with the provided format control and arguments." (error 'cookie-date-parse-error :format-control format-control :format-arguments format-arguments))