The drakma-async Reference Manual

This is the drakma-async Reference Manual, version 0.1.5, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Feb 26 16:19:05 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 drakma-async

An asynchronous port of the Drakma HTTP client.

Author

Andrew Danger Lyon <>

License

MIT

Version

0.1.5

Dependencies
  • cl-async-ssl (system).
  • cl-async-future (system).
  • alexandria (system).
  • flexi-streams (system).
  • fast-http (system).
  • fast-io (system).
  • drakma (system).
Source

drakma-async.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


3.1 Lisp


3.1.1 drakma-async/drakma-async.asd

Source

drakma-async.asd.

Parent Component

drakma-async (system).

ASDF Systems

drakma-async.


3.1.2 drakma-async/package.lisp

Source

drakma-async.asd.

Parent Component

drakma-async (system).

Packages

drakma-async.


3.1.3 drakma-async/util.lisp

Dependency

package.lisp (file).

Source

drakma-async.asd.

Parent Component

drakma-async (system).

Internals

3.1.4 drakma-async/http-stream.lisp

Dependency

util.lisp (file).

Source

drakma-async.asd.

Parent Component

drakma-async (system).

Public Interface
Internals

3.1.5 drakma-async/rewrite.lisp

Dependency

http-stream.lisp (file).

Source

drakma-async.asd.

Parent Component

drakma-async (system).

Internals

3.1.6 drakma-async/drakma.lisp

Dependency

rewrite.lisp (file).

Source

drakma-async.asd.

Parent Component

drakma-async (system).

Public Interface

http-request (function).


4 Packages

Packages are listed by definition order.


4.1 drakma-async

Source

package.lisp.

Nickname

das

Use List
  • alexandria.
  • chunga.
  • cl-async-future.
  • common-lisp.
  • drakma.
  • flexi-streams.
  • puri.
Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


5.1 Public Interface


5.1.1 Ordinary functions

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 vhost additional-headers redirect auto-referer keep-alive close external-format-out external-format-in force-binary want-stream stream preserve-uri read-timeout write-timeout)

This function wraps drakma’s new http-request-async function so you don’t have to deal with the intricacies. For full documentation on this function, refer to the docs for drakma:http-request; this library aims to be API compatible with drakma.

This function returns a cl-async future, which is finished with the following values of the request (once it returns):
(body status headers uri stream must-close status-text)

This means drakma-async is a prime candidate for using the cl-async future macros: http://orthecreedence.github.com/cl-async/future#nicer-syntax

Package

drakma-async.

Source

drakma.lisp.


5.1.2 Conditions

Condition: http-eof

Triggered when an HTTP peer closes the connection.

Package

drakma-async.

Source

http-stream.lisp.

Direct superclasses

event-error.

Condition: http-timeout

Triggered when an HTTP connection times out.

Package

drakma-async.

Source

http-stream.lisp.

Direct superclasses

event-error.


5.2 Internals


5.2.1 Special variables

Special Variable: *test-data*
Package

drakma-async.

Source

http-stream.lisp.


5.2.2 Macros

Macro: rewrite-http-request (defun-form)

This macro automates the conversion from drakma:http-request to be asynchronous (http-request-async). It does this by performing recursive tree searches on the main http-request defun and replacing certain pieces with async-ready code.

It was built because I had to keep porting new versions of drakma over by hand. This is all fine and dandy, but why do it by hand when you can automate it. Now I can copy and paste http-request from any drakma version into a (rewrite-http-request ...) macro and all is well in async land.

Package

drakma-async.

Source

rewrite.lisp.


5.2.3 Ordinary functions

Function: get-underlying-socket (stream)

Given a stream (of type flexi, chunga, or async-stream), grab the underlying socket (or return nil).

Package

drakma-async.

Source

http-stream.lisp.

Function: http-request-async (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 no-proxy-domains proxy-basic-authorization real-host vhost additional-headers redirect auto-referer keep-alive close external-format-out external-format-in force-binary want-stream stream preserve-uri connection-timeout)

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.

Package

drakma-async.

Source

rewrite.lisp.

Function: http-request-complete-stream (host port request-cb event-cb &key ssl stream read-timeout write-timeout)

Open a TCP stream to the given uri, determine when a full response has been returned from the host, and then fire the complete callback, at which point the response can be read from the stream.

Package

drakma-async.

Source

http-stream.lisp.

Function: tree-search-replace (tree search-item replace-fn &key rewrite-child-fn)

Replace the first instance of search-item found by searching recursively through tree. If found, calls the replace-fn on the found item, which is then injected back into the tree at the same spot search-item was found.

search-item supports wildcards, as explained in the wildcard-equal function.

Package

drakma-async.

Source

util.lisp.

Function: wildcard-equal (form wildcard-item &key wildcard-marker rest-marker)

A recursive equality function that allows some pieces of data to be missing or unspecified while performing comparisons:

(wildcard-equal ’(1 2 3) ’(1 :* 3)) => t (wildcard-equal ’(1 (3 4) 3) ’(1 :* 3)) => t (wildcard-equal ’(1 2 3 4) ’(1 2 :...)) => t (wildcard-equal ’(1 (2 3) 4) ’(1 (2 :*) 4)) => t (wildcard-equal ’(1 (2 3 4 5) 4) ’(1 (2 :...) 4)) => t

So :* means any item can match, :... means all of the remaining items can match. :* and :... can be changed to your liking via :wildcard-marker and :rest-marker respectively.

Package

drakma-async.

Source

util.lisp.


Appendix A Indexes


A.1 Concepts


A.3 Variables

Jump to:   *  
S  
Index Entry  Section

*
*test-data*: Private special variables

S
Special Variable, *test-data*: Private special variables