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
| Function | Summary |
|---|---|
Http.fail | Raises an expected HTTP error before any response bytes are sent. |
HttpRequest.json | Parses the request body as JSON. |
HttpRequest.read | Reads one bounded request; clean EOF returns NULL. |
HttpResponse.bytes | Replaces the body with arbitrary bytes, including embedded NUL. |
HttpResponse.header | Sets a response header. |
HttpResponse.json | Replaces the body with JSON. |
HttpResponse.new | Creates an empty 200 response. |
HttpResponse.status | Sets the response status and returns the response. |
HttpResponse.text | Replaces the body with UTF-8 text. |
HttpResponse.write | Writes a complete response and flushes once. |
HttpServer.close | Stops and releases native resources. |
HttpServer.new | Creates a server for router and binds its port immediately. |
HttpServer.port | Reports the bound port. |
HttpServer.serve | Runs on the caller until stop; closes the queue and joins every worker. |
HttpServer.start | Starts serving on a joined background Thread. |
HttpServer.stop | Stops accepting and joins the background acceptor when one was started. |
Router.delete | Registers fn for DELETE requests that match p, as Router.route does. |
Router.dispatch | Calls the first matching handler; unmatched paths raise 404 or 405. |
Router.files | Serves files below directory, with index.html for directories. |
Router.get | Registers fn for GET requests that match p, as Router.route does. |
Router.new | Creates a router with no routes. |
Router.post | Registers fn for POST requests that match p, as Router.route does. |
Router.put | Registers fn for PUT requests that match p, as Router.route does. |
Router.route | Registers 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"::";portdefaults 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, theLoggerthat 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
| Function | Summary |
|---|---|
HttpRequest.init | Initializes 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
| Type | Kind | Summary |
|---|---|---|
Http | alias | The receiverless owner of Http.fail. |
HttpRequest | class | One request, allocated in the current request Context. |
HttpResponse | class | A buffered response. |
HttpServer | class | A blocking acceptor with a bounded Channel and joined worker Threads. |
Router | class | Routes 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