Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

HTTP

Source: lib/http.x

Guide: Sockets and HTTP

Blocking HTTP/1.1 routing and worker-pool service.

Include it with #include "http.x".

Primary API

FunctionSummary
Http.failRaises an expected HTTP error before any response bytes are sent.
HttpRequest.jsonParses the request body as JSON.
HttpRequest.readReads one bounded request; clean EOF returns NULL.
HttpResponse.bytesReplaces the body with arbitrary bytes, including embedded NUL.
HttpResponse.headerSets a response header.
HttpResponse.jsonReplaces the body with JSON.
HttpResponse.newCreates an empty 200 response.
HttpResponse.statusSets the response status and returns the response.
HttpResponse.textReplaces the body with UTF-8 text.
HttpResponse.writeWrites a complete response and flushes once.
HttpServer.closeStops and releases native resources.
HttpServer.newCreates a server for router and binds its port immediately.
HttpServer.portReports the bound port.
HttpServer.serveRuns on the caller until stop; closes the queue and joins every worker.
HttpServer.startStarts serving on a joined background Thread.
HttpServer.stopStops accepting and joins the background acceptor when one was started.
Router.deleteRegisters fn for DELETE requests that match p, as Router.route does.
Router.dispatchCalls the first matching handler; unmatched paths raise 404 or 405.
Router.filesServes files below directory, with index.html for directories.
Router.getRegisters fn for GET requests that match p, as Router.route does.
Router.newCreates a router with no routes.
Router.postRegisters fn for POST requests that match p, as Router.route does.
Router.putRegisters fn for PUT requests that match p, as Router.route does.
Router.routeRegisters a handler for method requests whose path matches pattern.

Http

Http.fail

void Http.fail(int status, String reason)

Raises an expected HTTP error before any response bytes are sent.

Raises: <http-fail> with status and reason details.

Source: lib/http.x

HttpRequest

HttpRequest.json

Var HttpRequest.json(HttpRequest req)

Parses the request body as JSON.

Source: lib/http.x

HttpRequest.read

HttpRequest HttpRequest.read(File input, int body_limit)

Reads one bounded request; clean EOF returns NULL.

Raises: <http-fail> with a 4xx status for a malformed, oversized, or unsupported request, including an invalid percent escape in the URL, or <io-fail> when reading input fails.

Source: lib/http.x

HttpResponse

HttpResponse.bytes

HttpResponse HttpResponse.bytes(HttpResponse res, Block bytes)

Replaces the body with arbitrary bytes, including embedded NUL.

Source: lib/http.x

HttpResponse.header

HttpResponse HttpResponse.header(HttpResponse res, String name, String value)

Sets a response header.

Raises: <bad-arg> for a name that is not an HTTP token or a value that contains CR or LF.

Source: lib/http.x

HttpResponse.json

HttpResponse HttpResponse.json(HttpResponse res, Var value)

Replaces the body with JSON.

Source: lib/http.x

HttpResponse.new

HttpResponse HttpResponse.new(void)

Creates an empty 200 response.

Source: lib/http.x

HttpResponse.status

HttpResponse HttpResponse.status(HttpResponse res, int code)

Sets the response status and returns the response.

Source: lib/http.x

HttpResponse.text

HttpResponse HttpResponse.text(HttpResponse res, String text)

Replaces the body with UTF-8 text.

Source: lib/http.x

HttpResponse.write

void HttpResponse.write(HttpResponse res, File output, int keep_alive)

Writes a complete response and flushes once.

Source: lib/http.x

HttpServer

HttpServer.close

void HttpServer.close(HttpServer s)

Stops and releases native resources. Call after synchronous serve returns. Repeated close is harmless.

Source: lib/http.x

HttpServer.new

HttpServer HttpServer.new(Router router, Map @options)

Creates a server for router and binds its port immediately. Port 0 selects an available port. Keyword options use Symbol keys. Splice an existing Map with @options; omitted options use the server defaults:

  • host, the address to bind, defaults to "::";
  • port defaults to 8080;
  • workers, the number of worker threads, defaults to 8;
  • idle-ms, each connection’s socket timeout in milliseconds, defaults to 5000;
  • body-limit, the largest accepted request body in bytes, defaults to 1048576;
  • logger, the Logger that records requests and errors, defaults to a new <info> logger that writes to standard error.

Raises: <bad-arg> for fewer than one worker, an idle timeout below one millisecond, a negative body limit, a NULL logger, or an invalid port, <not-found> when the host does not resolve, or <io-fail> on a host failure while binding, as Listener.bind does.

Source: lib/http.x

HttpServer.port

int HttpServer.port(HttpServer s)

Reports the bound port.

Raises: <bad-arg> for NULL.

Source: lib/http.x

HttpServer.serve

void HttpServer.serve(HttpServer s)

Runs on the caller until stop; closes the queue and joins every worker.

Raises: <bad-arg> for NULL.

Source: lib/http.x

HttpServer.start

void HttpServer.start(HttpServer s)

Starts serving on a joined background Thread.

Raises: <bad-arg> for NULL, <bad-state> when the server is already serving, or the causes of Thread.start.

Source: lib/http.x

HttpServer.stop

void HttpServer.stop(HttpServer s)

Stops accepting and joins the background acceptor when one was started.

Raises: <bad-arg> for NULL.

Source: lib/http.x

Router

Router.delete

Router Router.delete(Router r, String p, Func fn)

Registers fn for DELETE requests that match p, as Router.route does.

Source: lib/http.x

Router.dispatch

void Router.dispatch(Router router, HttpRequest req, HttpResponse res)

Calls the first matching handler; unmatched paths raise 404 or 405. A HEAD request without a matching HEAD route calls the first matching GET handler, and the server then omits the response body.

Source: lib/http.x

Router.files

Router Router.files(Router router, String prefix, Path directory)

Serves files below directory, with index.html for directories.

Source: lib/http.x

Router.get

Router Router.get(Router r, String p, Func fn)

Registers fn for GET requests that match p, as Router.route does.

Source: lib/http.x

Router.new

Router Router.new(void)

Creates a router with no routes.

Source: lib/http.x

Router.post

Router Router.post(Router r, String p, Func fn)

Registers fn for POST requests that match p, as Router.route does.

Source: lib/http.x

Router.put

Router Router.put(Router r, String p, Func fn)

Registers fn for PUT requests that match p, as Router.route does.

Source: lib/http.x

Router.route

Router Router.route(Router r, Symbol method, String pattern, Func handler)

Registers a handler for method requests whose path matches pattern. The pattern is split at /. A :name segment matches any one path segment, and a final *rest segment matches the rest of the path; the handler reads each capture from req.params under its name. The handler takes (HttpRequest req, HttpResponse res) and fills in res. Routes match in registration order. Returns r.

Source: lib/http.x

Advanced and interop API

FunctionSummary
HttpRequest.initInitializes zeroed HttpRequest storage without acquiring resources.

HttpRequest

HttpRequest.init

void HttpRequest.init(HttpRequest value)

Initializes zeroed HttpRequest storage without acquiring resources.

Source: lib/http.x

Public types

TypeKindSummary
HttpaliasThe receiverless owner of Http.fail.
HttpRequestclassOne request, allocated in the current request Context.
HttpResponseclassA buffered response.
HttpServerclassA blocking acceptor with a bounded Channel and joined worker Threads.
RouterclassRoutes are tried in registration order within each method.

Http

typedef Symbol Http

The receiverless owner of Http.fail.

Source: lib/http.x

HttpRequest

class HttpRequest { Symbol method; String path, body; Map query, headers, params; int keep_alive; } *

One request, allocated in the current request Context.

Source: lib/http.x

HttpResponse

class HttpResponse { int code; Map headers; Buffer body; } *

A buffered response. Nothing is sent until the handler returns.

Source: lib/http.x

HttpServer

class HttpServer { Router router; Listener listener; Channel channel; Array threads; Thread acceptor; Logger logger; int workers, idle_ms, body_limit, owns_logger, running; } *

A blocking acceptor with a bounded Channel and joined worker Threads.

Source: lib/http.x

Router

class Router { Array routes; } *

Routes are tried in registration order within each method. A HEAD request without a matching HEAD route uses the first matching GET route.

Source: lib/http.x

Examples