https://github.com/orthecreedence/http-parse.git
git clone 'https://github.com/orthecreedence/http-parse.git'
(ql:quickload :http-parse)
http-parse had a good, long life and served many HTTP requests, but it's now time for it to stand aside and let libraries better than itself take its place. fast-http is an incredible library (and is now the core parser used in Wookie).
Use fast-http instead of http-parse. This library is retired.
A pure-lisp library for parsing HTTP requests/responses. Right now the focus for this library is making it useful and easy to use. With time, slower parts of http-parse will be replaced to make it screaming fast.
The purpose of this library is to be able to easily parse incoming HTTP data either synchronously or asynchronously (but it was mainly built for streaming HTTP data asynchronously).
This is a base class extended by http-request and http-response. It holds values that both requests/responses deal with (HTTP version, headers, body).
This class, and those that extend it, are meant to be instiantiated with no parameters and passed into the make-parser function, which fills in all the details which can be read out later.
Accessor for the parsed HTTP version out of the http object.
Accessor for the headers parsed from the HTTP request/response
store-body
specifies whether the HTTP body should be stored (in its entirety)
in the http-body accessor. If this is set after initializing a
parser, it should be done so no later than the header-callback
being fired, or else pieces of the body may be missing.
force-stream
lets the parser know that you want every TCP packet that comes in
to be passed into a body callback as if it was sent via an HTTP chunk. This is
an advanced option, but can be very useful in some cases. For instance, if you
have a server that supports file uploads and a client doesn't know how to chunk
an upload (like every browser ever), your server is going to spin its CPU
and waste memory buffering the entire file and passing it around as a huge array
instead of dealing with it packet by packet. This is a great way to fake HTTP
chunking in your server/client.
Accessor for the full HTTP body from the request/response (although storing of the
body in the http
object must be explicitely asked for by passing :store-body t
into make-parser.
extends http
Holds values specific to an HTTP request (method, resource)
Accessor for the parsed HTTP request method as a keyword: :GET
, :POST
, etc.
Accessor for the resource in the request. Parse with puri.
extends http
Holds values specific to an HTTP response (status, status text).
Accessor for the HTTP response status (integer).
Accessor for the HTTP response status string: OK
, Insufficient Privileges
,
etc.
(defun make-parser (http &key header-callback body-callback multipart-callback finish-callback store-body)
=> closure
This is what you've all been waiting for, folks. This function initializes an HTTP parser (a closure) which can be fed binary data in sequence and will parse an HTTP request/response as it comes in.
It accepts a class of http-request or http-response
as its only required argument. It returns a closure which only has one argument:
a byte array that contains pieces of an HTTP request/response. The pieces must
be in order, but other than that, there is no restriction on how many times the
parser can be called with new data until it is finished. In some cases (older
HTTP versions), the end of an HTTP payload is marked by an EOF on the socket. If
this occurs, you can pass :eof
into the parser instead of a byte array to
signal that it should finish up.
The parser closure returns three values: the http object passed in, a boolean indicating if the headers are finished parsing, and a boolean indicating if the HTTP body has been fully parsed.
make-parser
accepts these callbacks: :header-callback
, :body-callback
,
:multipart-callback
, and :finish-callback
.
:content-length
being present in
the headers) or piece by piece as it is sent in (when the body is chunked).The :store-body
keyword specifies that the parser should store the body (as a
byte array) into the given http object as it is parsed. Otherwise, the
best way to get the body data is via the body-callback.
;; example. anything under my-app is not included.
(let ((http (make-instance 'http-response))
(parser (make-parser http
:header-callback (lambda (headers)
(my-app:got-headers!!! headers))
:body-callback (lambda (bytes)
(my-app:got-body-piece bytes)))))
(loop for http-data = (my-app:get-http-data-from-request-i-sent-out-earlier) do
(multiple-value-bind (http headers-finished-p body-finished-p)
(funcall parser http-data)
(when body-finished-p
(my-app:close-http-stream))
...)))
(lambda (byte-array) ...)
=> http, headers-finished-p, body-finished-p
As noted, if an EOF happens on the socket the HTTP data is coming in on, you may
indicate this to the parser by sending in :eof
instead of the byte array.
(lambda (header-plist) ...)
Headers are in the form '(:host "musio.com" :content-type "text/html" ...)
.
Headers are not reversed, they are passed in the order they occur in the
HTTP payload.
(lambda (byte-array last-chunk-p) ...)
Byte-array is not cumulative, it is just the new data that has been parsed
from the payload. If multiple chunks are parsed at once, their body data is sent
in as one call to the body-callback
. Incomplete chunks are not sent in until
they are completed.
last-chunk-p
is true if the entire body has been processed (if a Content-Length
was specified and all bytes accounted for, or if the body is chunked and the
0-byte chunk has been encountered).
See multipart parser callback definition.
(lambda () ...)
This callback is fired when the HTTP parser is finished parsing the request/response.
(defun make-multipart-parser (headers callback))
=> closure
Returns a parser closure to deal with multipart form data. Data is fed to the
parser in as many chunks as needed (or all at once) and the given callback
will be fired at least once for each form field present in the multipart form
data. If data for a field is spread over multiple chunks, the callback is fired
for each of the chunks, along with a second argument indicating whether the
current chunk is that last for that field.
headers
are all the headers from a parsed HTTP payload, in plist form.
NOTE: If make-multipart-parser
detects that the data being decoded is not in
multipart format (determined by reading the headers), it returns nil
instead
of a closure.
(lambda (field-name field-headers field-meta body-bytes body-complete-p) ...)
This callback is fired for each form field encountered in a multipart request.
The field-name
arg is a string indicating the name of the form field. The
field-headers
arg is a plist containing the headers for that field (generally
this is Content-Disposition
and sometimes Content-Type
for uploads). The
field-meta
arg is a plist of key/value pairs found in the Content-Disposition
header for the field (this is where the field-name
arg comes from, and is also
used to specify the filename of uploaded files). body-bytes
is a byte array
containing all or a chunk of that field's data, and body-complete-p
indicates
whether or not the body-bytes
being sent into the callback is the last bit of
data for that field.
Generally, this callback will be a closure that is able to track the current
field it's operating on and be able to handle the case where body-bytes
is
spread over multiple calls if body-complete-p
is nil
.
Tests are under the http-parse-test
package:
(ql:quickload :http-parse-test)
(http-parse-test:run-tests)
Please report any bugs you find or failing tests to the issues list.
MIT Licensed. Enjoy.