Janet 1.39.1-e9c6678 Documentation
(Other Versions: 1.38.0 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

http

The http module is an HTTP/1.1 parser, server and client module. It proves a simple server implementation, client, support for chunked encoding. The http module is also non-blocking, so a single thread can run many clients, servers, and connections at once.

Examples

Server

(import spork/http)

(defn handler
 [req]
 (def method (get req :method))
 (case method
  "GET" {:status 200 :body (get req :path)}
  "POST" {:status 400 :body (http/read-body req)}
  {:status 404}))

(http/server handler "127.0.0.1" "9000")

Client

(import spork/http)

(def response (http/request "GET" "http://www.example.com"))
(def body (http/read-body response))
(print body)

Reference

http/cookie-grammar http/cookies http/logger http/middleware http/query-string-grammar http/read-body http/read-request http/read-response http/request http/request-peg http/response-peg http/router http/send-response http/server http/server-handler http/status-messages http/url-grammar


http/cookie-grammar core/peg source
Grammar to parse a cookie header to a series of keys and values.

http/cookies function source
(cookies nextmw)

Parses cookies into the table under :cookies key

http/logger function source
(logger nextmw)

Creates a logging middleware. The logger middleware prints URL route, return status, and elapsed request time.

http/middleware function source
(middleware x)

Coerce any type to http middleware

http/query-string-grammar core/peg source
Grammar that parses a query string (sans url path and ? character) and returns a table.

http/read-body function source
(read-body req)

Given a request, read the HTTP body from the connection. Returns the body as a buffer. If the request has no body, returns nil.

http/read-request function source
(read-request conn buf &opt no-query)

Read an HTTP request header from a connection. Returns a table with the following keys:
* `:headers` - table mapping header names to header values. Header names are lowercase.
* `:connection` - the connection stream for the header.
* `:buffer` - the buffer instance that may contain extra bytes.
* `:head-size` - the number of bytes used by the header.
* `:method` - the HTTP method used.
* `:path` - the path of the resource requested.

The following keys are also present, but omitted if the user passes a truthy parameter to `no-query`.
* `:route` - path of the resource requested without query string.
* `:query-string` - segment of HTTP path after first ? character.
* `:query` - the query string parsed into a table. Supports a single string value
 for every string key, and any query parameters that aren't given a value are mapped to true.

Note that data is read in chunks and any data after the header terminator is
stored in `:buffer`.

http/read-response function source
(read-response conn buf)

Read an HTTP response header from a connection. Returns a table with the following keys:
* `:headers` - table mapping header names to header values. Header names are lowercase.
* `:connection` - the connection stream for the header.
* `:buffer` - the buffer instance that may contain extra bytes.
* `:head-size` - the number of bytes used by the header.
* `:status` - the HTTP status code.
* `:message` - the HTTP status message.

Note that data is read in chunks and any data after the header terminator is
stored in `:buffer`.

http/request function source
(request method url &keys {:body body :headers headers})

Make an HTTP request to a server.
Returns a table containing response information.
* `:head-size` - number of bytes in the http header
* `:headers` - table mapping header names to header values. Header names are lowercase.
* `:connection` - the connection stream for the header.
* `:buffer` - the buffer instance that may contain extra bytes.
* `:status` - HTTP status code as an integer.
* `:message` - HTTP status message.
* `:body` - Bytes of the response body.

http/request-peg core/peg source
PEG for parsing HTTP requests

http/response-peg core/peg source
PEG for parsing HTTP responses

http/router function source
(router routes)

Creates a router middleware. A router will dispatch to different routes based on the URL path.

http/send-response function source
(send-response conn response &opt buf)

Send an HTTP response over a connection. Will automatically use chunked
encoding if body is not a byte sequence. `response` should be a table
with the following keys:

* `:headers` - optional headers to write
* `:status` - integer status code to write
* `:body` - optional byte sequence or iterable (for chunked body)
 for returning contents. The iterable can be lazy, i.e. for streaming
 data.

http/server function source
(server handler &opt host port)

Makes a simple http server. By default it binds to 0.0.0.0:8000, returns a new server stream. Simply wraps http/server-handler with a net/server.

http/server-handler function source
(server-handler conn handler)

A simple connection handler for an HTTP server.
When a connection is accepted. Call this with a handler
function to handle the connect. The handler will be called
with one argument, the request table, which will contain the
following keys:
* `:head-size` - number of bytes in the http header.
* `:headers` - table mapping header names to header values.
* `:connection` - the connection stream for the header.
* `:buffer` - the buffer instance that may contain extra bytes.
* `:path` - HTTP path.
* `:method` - HTTP method, as a string.

http/status-messages struct source
Mapping of HTTP status codes to their status message.

http/url-grammar core/peg source
Grammar to parse a URL into domain, port, and path triplet. Only supports the http:// protocol.