drakma

API Reference

drakma

Full-featured http/https client based on usocket

DRAKMA

  • Variable *DRAKMA-VERSION*
    "2.0.7"
    Drakma's version number as a string.
  • Variable *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).
  • Variable *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.
  • Variable *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".
  • Variable *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.
  • Variable *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.
  • Variable *TEXT-CONTENT-TYPES*
    '(("text"))
    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.
  • Variable *BODY-FORMAT-FUNCTION*
    'drakma::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.
  • Variable *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).
  • Variable *NO-PROXY-DOMAINS*
    nil
    A list of domains for which a proxy should not be used.
  • Condition DRAKMA-CONDITION  (CONDITION)
    Superclass for all conditions related to Drakma.
  • Condition DRAKMA-ERROR  (DRAKMA-CONDITION, ERROR)
    Superclass for all errors related to Drakma.
  • Condition DRAKMA-WARNING  (DRAKMA-CONDITION, WARNING)
    Superclass for all warnings related to Drakma.
  • Condition PARAMETER-ERROR  (DRAKMA-SIMPLE-ERROR)
    Signalled if a function was called with inconsistent or illegal parameters.
  • Function PARAMETER-ERROR (format-control &rest format-arguments)
    Signals an error of type PARAMETER-ERROR with the provided format control and arguments.
  • Condition SYNTAX-ERROR  (DRAKMA-SIMPLE-ERROR)
    Signalled if Drakma encounters wrong or unknown syntax when reading the reply from the server.
  • Function SYNTAX-ERROR (format-control &rest format-arguments)
    Signals an error of type SYNTAX-ERROR with the provided format control and arguments.
  • Condition COOKIE-ERROR  (DRAKMA-SIMPLE-ERROR)
    Signalled if someone tries to create a COOKIE object that's not valid.
  • Function 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.
  • Condition COOKIE-DATE-PARSE-ERROR  (COOKIE-ERROR)
    Signalled if Drakma tries to parse the date of an incoming cookie header and can't interpret it.
  • Function 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.
  • Function URL-ENCODE (string external-format)
    Returns a URL-encoded version of the string STRING using the external format EXTERNAL-FORMAT.
  • Function 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).
  • Function 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.
  • Function 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.
  • Function 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.
  • Function 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.
  • Function SPLIT-TOKENS (string)
    Splits the string STRING into a list of substrings separated by commas and optional whitespace. Empty substrings are ignored.
  • 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 <http://wp.netscape.com/newsref/std/cookie_spec.html> (and at this page <http://msdn2.microsoft.com/en-us/library/ms533046.aspx> for the HttpOnly extension).
    NAME   Accessor: COOKIE-NAME
    The name of the cookie.
    VALUE   Accessor: COOKIE-VALUE
    The cookie's value.
    DOMAIN   Accessor: COOKIE-DOMAIN
    The domain the cookie is valid for.
    PATH   Accessor: COOKIE-PATH
    The path prefix the cookie is valid for.
    EXPIRES   Accessor: COOKIE-EXPIRES
    When the cookie expires. A Lisp universal time or NIL.
    SECUREP   Accessor: COOKIE-SECUREP
    Whether the cookie must only be transmitted over secure connections.
    HTTP-ONLY-P   Accessor: COOKIE-HTTP-ONLY-P
    Whether the cookie should not be accessible from Javascript. This is a Microsoft extension that has been implemented in Firefox as well. See <http://msdn2.microsoft.com/en-us/library/ms533046.aspx>.
  • Method (setf COOKIE-NAME) (new-value (cookie cookie))
    Check cookie validity after name change.
  • Method (setf COOKIE-VALUE) (new-value (cookie cookie))
    Check cookie validity after value change.
  • Method (setf COOKIE-DOMAIN) (new-value (cookie cookie))
    Check cookie validity after domain change.
  • Method (setf COOKIE-PATH) (new-value (cookie cookie))
    Check cookie validity after path change.
  • Method (setf COOKIE-EXPIRES) (new-value (cookie cookie))
    Check cookie validity after expiry change.
  • Function 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.
  • 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.
    COOKIES   Accessor: COOKIE-JAR-COOKIES
    A list of the cookies in this cookie jar.
  • Function 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.
  • Function DELETE-OLD-COOKIES (cookie-jar)
    Removes all cookies from COOKIE-JAR which have either expired or which don't have an expiry date.
  • Generic-Function DECODE-STREAM (encoding-type 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.
  • Method DECODE-STREAM ((encoding-type t) stream)
    Default handler, just return the stream.
  • Method DECODE-STREAM ((encoding-type (eql :gzip)) stream)
    Decode stream using gzip compression.
  • Method DECODE-STREAM ((encoding-type (eql :deflate)) stream)
    Decode stream using deflate compression in zlib container.
  • Method 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.
  • Method DECODE-STREAM ((encoding-type (eql :chunked)) stream)
    General decode method for chunked stream. Creates new chunked-stream.
  • Function 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 (connection-timeout 20) &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.