Janet 1.42.0-dev-7fd75f3 Documentation
(Other Versions: 1.42.0 1.41.2 1.41.1 1.40.1 1.40.0 1.39.1 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.

The http module supports custom stream transports via the :stream-factory parameter, allowing HTTP to work over different underlying protocols (e.g., TLS, QUIC, or custom transports). The stream factory should accept (host port &opt opts) and return a Janet stream that supports :read, :write, and :close methods.

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)

Client with Custom Stream (e.g., HTTPS)

(import spork/http)
(import jossl/tls)

# Use TLS for HTTPS
(def response (http/request "GET" "https://www.example.com"
                           :stream-factory tls/connect))
(def body (http/read-body response))
(print body)

Reference

http/cookie-grammar http/cookies http/download http/logger http/make-response-stream http/middleware http/open-stream http/query-string-grammar http/read-body http/read-request http/read-response http/request http/request-peg http/resolve-url 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/download function source
(download url dest &keys opts)

Download an HTTP resource to a destination path, file, buffer, or sink.
Supports automatic redirect following, chunked transfer encoding,
and incremental chunk streaming without buffering entire responses.

Arguments:
* `url` - The URL to download
* `dest` - Target file path (string), buffer, core/file or stream, or sink function (fn [chunk])

Options:
* `:headers` - Table of HTTP headers
* `:max-redirects` - Maximum redirects to follow (default 10)
* `:stream-factory` - Custom stream factory function
* `:stream-opts` - Options passed to stream-factory

http/logger function source
(logger nextmw)

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

http/make-response-stream function source
(make-response-stream conn head &opt url method)

Create a streaming HTTP response object from a connection and parsed header.

http/middleware function source
(middleware x)

Coerce any type to http middleware

http/open-stream function source
(open-stream url &keys {:headers headers :stream-opts stream-opts :max-redirects max-redirects :body body :method method :stream-factory stream-factory})

Open an HTTP request and return an open response stream.
The stream implements :status, :message, :headers, :url, :head-size,
:read, :blocks, :lines, and :close.
Automatically follows HTTP 3xx redirects up to :max-redirects (default 10).

Options:
* `:method` - HTTP method string (default "GET")
* `:body` - Request body content
* `:headers` - Request headers table
* `:stream-factory` - Function to create connection stream. Defaults to net/connect.
* `:stream-opts` - Options table passed to stream-factory
* `:max-redirects` - Maximum number of 3xx redirects to follow (default 10)

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 &opt sink)

Given a request/response table, read the HTTP body from the connection.
If sink is provided (or (in req :sink)), streams chunks to sink (function,
file, buffer, or stream). Otherwise 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 opts)

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.
* `:url` - final resolved URL.
* `:body` - Bytes of the response body (nil if streaming to custom sink or method is HEAD).

Options:
* `:body` - Request body content
* `:headers` - Request headers table
* `:stream-factory` - Function to create connection stream. Defaults to net/connect.
 Signature: (stream-factory host port stream-opts)
* `:stream-opts` - Options table passed to stream-factory
* `:sink` - Target to stream response body into (function, file, or buffer)
* `:stream` - If true, returns the open response stream directly without consuming body
* `:max-redirects` - Maximum number of 3xx redirects to follow (default 0)

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

http/resolve-url function source
(resolve-url base-url location)

Resolve a redirect location against a base URL per RFC 9110.

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 scheme, domain, port, and path. Supports both http:// and https:// protocols. Returns [scheme host port path].