http

HTTP client and server. Included by default (the server needs tcp, which the http feature brings with it). Exclude with --no-default-features for WASM or minimal builds: a program that names http.get, http.request, http.request_once or http.serve then does not check (the error names the cargo feature http), while the module’s types remain. A URL is a url.Url, in a request the client sends and in one the server gets.

Types

type Method {
  GET,
  POST,
  PUT,
  PATCH,
  DELETE,
  HEAD,
  OPTIONS,
}

type Request {
  method: Method,
  url: url.Url,
  headers: Map(String, String),
  body: Bytes,
  peer: String,
}

type Response {
  status: Int,
  body: Bytes,
  headers: Map(String, String),
}

Method and its variants are members of http: http.Method, http.GET, http.POST, etc.

A body is Bytes, in a request and in a response, sent and received: exactly the bytes that are on the wire, whatever they are. Text is put into a body with bytes.from_string("...") and taken out of one with bytes.to_string(resp.body), a Result: it is Err(InvalidUtf8(offset)) for a body that is no UTF-8.

import bytes
import http
import url

-- Text into a body: what a handler answers.
pub fn hello(_req: http.Request) -> http.Response {
  http.Response { status: 200, body: bytes.from_string("Hello"), headers: #{} }
}

-- Text out of a body: what a client got.
fn main() -> Result((), url.UrlError) {
  let home = url.parse("http://127.0.0.1:8080/")?
  match http.get(home) {
    Ok(resp) -> match bytes.to_string(resp.body) {
      Ok(text) -> println(text)
      Err(e) -> println("the body is no text: {e}")
    }
    Err(e) -> println("{e}")
  }
  Ok(())
}
open in playground

The client takes a url.Url, never a text: a URL is read (url.parse) or built (url.add_segments, url.with_query) by the url module, which checks and escapes it, and the client never reads URL text the program wrote. A value put into a URL with the url module stays one value: a name holding /, ?, #, & or .. asks for no other resource.

Summary

NameSignatureDescription
get(Url) -> Result(Response, HttpError)HTTP GET request
request(Method, Url, Bytes, Map(String, String)) -> Result(Response, HttpError)HTTP request with method, URL, body, headers
request_once(Method, Url, Bytes, Map(String, String)) -> Result(Response, HttpError)One HTTP request, whose redirect is the response, not followed
serve(TcpListener, Fn(Request) -> Response) -> ()Serve HTTP on a listener made with tcp.listen, a task per connection

Errors

http.get, http.request and http.request_once return Result(Response, HttpError). Note that a 4xx or 5xx HTTP response is an Ok(Response): an Err is a response that did not arrive whole. That is a failure before its head is there (DNS, connection, TLS, a timeout, a head that is no HTTP), and one after it: a body that is cut short (ClosedEarly), and a body that does not unpack or is larger than 10,000,000 bytes (InvalidResponse). A program for which a status is a failure looks at resp.status. An HttpError is shown as its message, so "{e}" always yields a rendered string.

VariantFieldsMeaning
Connect(msg)Stringno connection was made: refused, no route, no such host. Also a proxy (see Proxies) that cannot be reached, that refuses the tunnel (CONNECT proxy failed: proxy server responded 403/403) or closes without an answer, and a SOCKS proxy, which the client does not speak
Tls(msg)StringTLS handshake / cert failure
Timeout—one of the request’s times passed (10 s to connect, 60 s in all), or SILT_IO_TIMEOUT
InvalidUrl(url)Stringthe URL has a scheme other than http and https, is longer than 65,534 bytes (the transport’s limit), or has a user with a : in it once decoded; no connection is made. Also the location of a redirect that is no URL or has such a scheme, or that leads from https to http, which is not followed. The URL is shown with its user and password hidden
InvalidResponse(msg)Stringwhat the server sent is no HTTP (a status line that is none, a chunk length that is no number, content-length headers that disagree or a length that is no number, a redirect with two locations, a head that is too large, a body that says it is packed and, though all of it arrived, does not unpack: no bytes at all among them), or its body is larger than 10,000,000 bytes. Also a proxy (see Proxies) that answers no HTTP
ClosedEarly—the server went away before the response was complete, head or body, packed or not
TooManyRedirects—http.get or http.request was redirected more than ten times
Other(msg)Stringany other failure: a header name or value of the request that is none, a header given twice (in two cases of its name), a Host or Authorization that is no ASCII text

Which variant a failure is, is decided by the kind of failure the transport reports, never by the wording of its message: the message is only what Connect, Tls, InvalidResponse and Other carry.

The client keeps its connections: a connection that the server keeps open after a response carries the next request to that server, from any task of the program, so a program that asks one server many times connects to it once (req.peer, which the server sees, stays the same). An idle connection is kept for 15 s, at most 3 to one server and 10 in all. A server may close an idle connection at any time. A GET, HEAD, PUT, DELETE or OPTIONS (the methods that may be sent twice) that went out on a kept connection which then ended before a byte of the answer came (over https too) is sent once more, on a new connection. A POST or a PATCH is not: the server may have acted on it before it closed, so it is Err(ClosedEarly). Any other failure is the program’s.

To bound a request by a time of your own, put a task.deadline around it. A request that nobody waits for any more (its deadline passed, its task was stopped) is ended at once, wherever it waits: for the server’s answer, in its body, in a TLS handshake, and holds no thread. So is a connect that waits for a host that does not answer: on Linux it ends at once; elsewhere it may run on to its connect time, at most 10 s, counted until then among the I/O operations that cannot be interrupted (see the I/O pool). The name lookup of a host name cannot be interrupted: it ends by the system’s own time.

Proxies

The client goes through a proxy that the environment names, picked by the URL’s scheme as curl does:

URLProxy
http://http_proxy, else HTTP_PROXY, else all_proxy, else ALL_PROXY
https://https_proxy, else HTTPS_PROXY, else all_proxy, else ALL_PROXY

Each variable holds a proxy’s URL (http://proxy.example:3128). One that is empty counts as not set, and one that holds no proxy’s URL is passed over for the next. So ALL_PROXY is the proxy of a scheme whose own is not set, and with HTTPS_PROXY alone an http:// URL is reached without a proxy. A host that no_proxy (else NO_PROXY) names is reached without one: a list separated by commas (the spaces around an entry do not count), of hosts (localhost, 127.0.0.1, ::1), of endings (.example.com or *.example.com, every host below example.com), of beginnings (10. or 10.*), or *, every host; case does not matter. An entry names a host without its port: 127.0.0.1:9 matches no host. The variables are read once, at the program’s first request.

The client speaks no SOCKS. A socks5://, socks4:// or socks:// URL in the variable that a request’s scheme picks refuses the request, as Err(Connect("ALL_PROXY: a socks proxy is not supported")) (the name of that variable): it is not sent without the proxy, and the later variables are not looked at. A host that no_proxy names is reached as before.

The client asks the proxy for a tunnel (CONNECT host:port) and sends the request through it, for an http:// URL too: the proxy sees the host and the port, not the request. A user and password in the proxy’s URL (http://user:pw@proxy.example:3128) go to the proxy only, as proxy-authorization, and no error shows them. A proxy that cannot be reached, that refuses the tunnel (any answer but 200, such as a 403 or a 407) or that closes without an answer is Err(Connect(_)), its message naming the status (CONNECT proxy failed: proxy server responded 403/403); a server that answers no HTTP is Err(InvalidResponse(_)).

http.get

http.get(u: Url) -> Result(Response, HttpError)

Makes an HTTP GET request. Returns Ok(Response) for a response of any status (4xx and 5xx too) that arrived whole, and Err(HttpError) otherwise: see Errors. A redirect is followed as http.request follows it, up to ten of them: the Response is the last one’s, and nothing in it says where it came from. The names of the response’s headers are in lower case (map.get(resp.headers, "content-type")); a header that came more than once is one entry, its values joined with ", " in the order they came. Set-Cookie is the one header whose values cannot be one list (a cookie’s date has a comma in it): its values are joined with a line end instead, so two Set-Cookie headers give "a=1\nb=2", and string.split(cookies, "\n") gives each cookie. Its body is the bytes the server sent, with one exception:

  • A body with the header Content-Encoding: gzip, written exactly so, is unpacked: resp.body is the content, and content-encoding and content-length, which describe the packed bytes, are not in resp.headers. (Every request says accept-encoding: gzip.) A body in any other coding, GZIP and x-gzip among them, is handed over as it came, with its header. A coding sent in two headers is their values joined, as of any header: two gzip headers are gzip, gzip, and the body is handed over as it came.
  • An answer to HEAD, a 204 and a 304 have no body: resp.body is empty and nothing is read, whatever the headers say. They may say what a GET would get; of those, content-encoding and content-length are not in resp.headers when the answer says gzip.
  • A body of more than 10,000,000 bytes (the limit of what one read from the network may take, as for tcp.read_exact), counted as the program would get it, is Err(InvalidResponse(_)): no part of it is returned.

Every request says who sends it, user-agent: silt/<version> (the version of silt that runs the program).

When called from a task, http.get transparently yields to the scheduler while the request is in flight. No API change is needed — the call site looks the same.

import bytes
import http
import url

fn main() {
  match url.parse("https://api.github.com/users/torvalds") {
    Ok(u) -> match http.get(u) {
      Ok(resp) -> println("Status: {resp.status}, body length: {bytes.length(resp.body)}")
      Err(http.Timeout) -> println("timed out; retry later")
      Err(e) -> println("Network error: {e}")
    }
    Err(e) -> println("not a URL: {e}")
  }
}
open in playground

Compose with url.add_segments, bytes.to_string, json.parse and ? for typed API responses. Since they return different error types, wrap each in a local enum using result.map_err with a variant constructor as a first-class Fn. The user’s name is one path segment, escaped: a name ann/../admin?x=1 asks for /v1/users/ann%2F..%2Fadmin%3Fx=1, one user, as meant.

import bytes
import http
import json
import result
import url

type User {
  name: String,
  id: Int,
}

type FetchError {
  BadUrl(url.UrlError),
  Network(http.HttpError),
  NotText(bytes.BytesError),
  Parse(json.JsonError),
}

-- `api` is the base of the API, `https://api.example.com/v1/`.
fn fetch_user(api: url.Url, name: String) -> Result(User, FetchError) {
  let u = api |> url.add_segments(["users", name]) |> result.map_err(BadUrl)?
  let resp = http.get(u) |> result.map_err(Network)?
  let text = bytes.to_string(resp.body) |> result.map_err(NotText)?
  json.parse(text, User) |> result.map_err(Parse)
}

http.request

http.request(method: Method, u: Url, body: Bytes, headers: Map(String, String)) -> Result(Response, HttpError)

Makes an HTTP request with full control over method, body, and headers. Use this for POST, PUT, DELETE, or any request that needs custom headers.

The body is sent as it is, with its length: bytes.from_string("...") for a text, bytes.empty() for none. What the bytes are is the request’s to say, in a Content-Type header among headers: none is added. A user-agent among headers is sent instead of silt/<version>, and a Host instead of the URL’s host. A Content-Length, a Transfer-Encoding and a Connection among headers are not sent: the body goes whole, with its own length, and the connection is the client’s. A header’s name is the same in any case: two keys of headers that differ only in case (User-Agent and user-agent) are one header given twice, and the request is Err(Other("the header user-agent is given twice")); so is a Host or an Authorization that is no ASCII text (Err(Other("the header host is no ASCII text"))). Nothing is sent then.

The user and password of the URL (url.with_user, url.with_password, which escape them) are sent as authorization: Basic ... (RFC 7617), decoded, unless headers has an authorization of its own, for http.get too; they are never sent in the URL itself. A user that has a colon (us:er) is an InvalidUrl: RFC 7617 has none in it. No error shows them: the URL of an InvalidUrl is shown as url.Url is, with ***@ in their place.

The URL goes out as the Url holds it, in its normal form: escaped (/h%C3%A9llo for /héllo), its . and .. segments taken out, without its fragment, which is no server’s. A URL longer than 65,534 bytes is an InvalidUrl: the transport takes no longer one.

A redirect is followed, up to ten of them (one more is Err(TooManyRedirects)): after a 307 or 308 with the same method and body; after a 303 as a GET without a body and without the Content-Type among headers (a HEAD stays a HEAD); after a 301 or 302 a POST as such a GET, and any other method with the same method and body. The credentials are not sent to where a redirect leads, also when it leads to the same host: the authorization, proxy-authorization and cookie headers, and the user and password of the URL. Every other header among headers is, and so is the body after a 307 or 308, also to another host, as curl and Go send them. A Host among headers goes on only where the scheme, the host and the port stay the same: a request to another names the host it goes to. A header that is left behind stays behind, as the credentials do: a Host dropped on one redirect is not sent again on a later one, also where that one leads back to the first scheme, host and port. A location is resolved against the URL that answered with url.resolve: ?y=2 on its path, //host/x on its scheme, ../x next to its path. An empty location, or one that is a fragment alone (#top), is the URL that answered itself, as RFC 3986 reads it: after a 303 that is a GET of the same URL, and a server that answers it so every time ends in Err(TooManyRedirects). A response without a location is handed back as it is, its status 3xx; one with two Location headers that lead to different places is Err(InvalidResponse("a redirect with two locations")), not followed to either (two that lead to one place, as /ok and http://host/ok from http://host/, are one). A byte of a location that a URL cannot hold (a space, the bytes of é) is escaped first (%20, %C3%A9), as browsers send it. A location that does not resolve to a URL (url.resolve refuses it: escapes that are no UTF-8 text, a host that is none, mailto:a@b), or that resolves to one of a scheme other than http and https (file:///x, ftp://host/x), is not followed: it is Err(InvalidUrl(location)), a user and password in it hidden. So is a redirect from https to http: it would send the headers and the body in clear text, to anyone on the way. A program that means to go there follows the redirect itself, with http.request_once.

Like http.get, this transparently yields to the scheduler when called from a task.

-- `api` is a Url, `https://api.example.com/`.

-- POST with JSON body
let users = api |> url.add_segments(["users"])?
let resp = http.request(
  http.POST,
  users,
  bytes.from_string(json.stringify(#{ "name": "Alice" })),
  #{ "Content-Type": "application/json", "Authorization": "Bearer tok123" },
)?

-- DELETE
let user = api |> url.add_segments(["users", "42"])?
let resp = http.request(http.DELETE, user, bytes.empty(), #{})?

-- GET with a query and custom headers
let data = api |> url.add_segments(["data"])?
let resp = http.request(
  http.GET,
  data |> url.with_query([("page", "2")]),
  bytes.empty(),
  #{ "Accept": "text/plain" },
)?

http.request_once

http.request_once(method: Method, u: Url, body: Bytes, headers: Map(String, String)) -> Result(Response, HttpError)

Sends one request, as http.request sends it (its method, body, headers, and the user and password of the URL as authorization), and returns its response without following a redirect: a 301, 302, 303, 307 or 308 is an Ok(Response) as it came, with its location header as the server wrote it and its body. A program that follows redirects itself sees each one: it can report the chain, stop where it likes, and knows the address that answered last, against which the links of a page are resolved. url.resolve resolves a location as http.request does.

import bytes
import http
import map
import result
import url

-- Where `page` ends after at most `hops` redirects, and the response
-- there; each redirect is printed.
fn fetch(page: url.Url, hops: Int) -> Result((url.Url, http.Response), String) {
  let resp = http.request_once(http.GET, page, bytes.empty(), #{}) |> result.map_err { e -> "{e}" }?
  let redirected = resp.status >= 300 && resp.status < 400
  match (redirected, map.get(resp.headers, "location")) {
    (true, Some(location)) -> {
      when hops > 0 else {
        return Err("too many redirects at {page}")
      }
      let next = url.resolve(page, location) |> result.map_err { e -> "{e}" }?
      println("{resp.status} {page} -> {next}")
      fetch(next, hops - 1)
    }
    _ -> Ok((page, resp))
  }
}

fn main() -> Result((), String) {
  let start = url.parse("http://127.0.0.1:8080/r/1") |> result.map_err { e -> "{e}" }?
  let (last, resp) = fetch(start, 10)?
  -- A link of the page is resolved against where it ended.
  let link = url.resolve(last, "p1.html") |> result.map_err { e -> "{e}" }?
  println("{resp.status} at {last}; its link p1.html is {link}")
  Ok(())
}
open in playground

Like http.get, this transparently yields to the scheduler when called from a task.

http.serve

http.serve(listener: TcpListener, handler: Fn(Request) -> Response) -> ()

Serves HTTP on a listener made with tcp.listen. The address written there decides where the server is reached, and on which port:

tcp.listen(...)The server is reached
"127.0.0.1:8080"from this machine only (loopback). Write this unless you mean otherwise: a development server is then not exposed to the network the machine happens to be on.
"0.0.0.0:8080"on every network interface: from the LAN, and from the internet if the host is routed. For a deployment behind a reverse proxy, or a container whose port is published.
"127.0.0.1:0"on a port the system chooses; tcp.local_port(listener) says which. For tests, and for servers that tell someone else where they are.

The listener is bound when tcp.listen returns, so a client may connect before http.serve runs: its request is queued and answered when the server starts.

http.serve serves until its listener is closed or its task is stopped. To run a server beside other work, make it a helper of task.background or a piece of task.join. There are two ways to stop it (see Stopping a server): tcp.close_listener(listener) frees the port and lets the server finish what it has begun, then http.serve returns (); a stop of its task (the body of the task.background returned, a task.deadline around it passed) ends the server at once and leaves the listener bound, to be served again (see When a program ends).

import bytes
import http
import tcp
import url

fn main() -> Result((), tcp.TcpError) {
  let listener = tcp.listen("127.0.0.1:8080")?
  http.serve(listener) { req ->
    http.Response {
      status: 200,
      body: bytes.from_string("you asked for {url.path(req.url)}"),
      headers: #{},
    }
  }
  Ok(())
}
open in playground

Connections and handlers

Each connection has a task of its own. It reads a request, calls the handler with it, sends what the handler returns, and reads the next request of the connection. So:

  • Requests on different connections are handled concurrently.
  • Requests on one connection are answered one after the other, in their order: a connection is kept after a response (keep-alive), and a client may send several requests without waiting for the answers.
  • A handler that waits (for a channel, a timer, another request: a long poll) holds no thread while it does.

The handler receives a Request and must return a Response whose status is the status of a response: 200 to 599.

What happensWhat the client gets
The handler fails, or returns a status that is none, or headers that give one header twice (two keys that differ in case alone, X-A and x-a)500. The failure is written to stderr as one line (a line end in its text shown as \n) and nothing of it is sent. The connection stays usable.
128 handlers are being called alreadyThe request waits for one to return, in the server’s queue, first come first served: a short burst is answered as the handlers free up. A request that has waited 5 s, or one that finds 1,024 waiting already, is answered 503. A request that would release the handlers that wait is queued like any other: a server whose handlers wait for each other needs a second server, or a timeout in the handler.
The method is none of Method’s (e.g. TRACE)405, with an Allow header that names the seven
The target or the Host makes no URL, or one that two readers could take differently (see req.url below): a Host that is more than a host and a port, or whose port is empty (h.test:); a fragment in the target (/x#top); an escape that is none or no UTF-8 text; a target in absolute form of another scheme than http, with a user or password, or whose host and port are not the Host header’s400; the handler is not called. The connection stays usable.
OPTIONS *, which asks the server and no resource204, with the Allow header of a 405; the handler is not called
The method is HEADthe handler is called; its response is sent without the body, with the length the body has

A client that leaves is not noticed while its handler runs. The server looks at a connection when it reads from it or writes to it. If a client closes its connection while its handler waits (a long poll), the handler goes on waiting: it keeps its place among the 128, and the connection its descriptor, until the handler returns and the response cannot be sent. Clients that come and go without waiting for their answers can so fill every place, and every later request waits its 5 s in the queue and gets 503. A handler that waits needs a limit of its own, for example channel.receive_timeout instead of channel.receive.

Long polls and health checks: serve them on two listeners. Every request to one server takes a place among its 128 handlers, so 128 long polls that wait hold every place, and a health check (GET /health) waits in the queue behind them until one returns, or is answered 503 after 5 s. A request that must be answered at once while others wait goes to a second server on another port, which has 128 places of its own:

import bytes
import http
import task
import tcp

fn text(body: String) -> http.Response {
  http.Response { status: 200, body: bytes.from_string(body), headers: #{} }
}

fn main() -> Result((), tcp.TcpError) {
  let api = tcp.listen("127.0.0.1:8080")?
  let health = tcp.listen("127.0.0.1:8081")?
  let checks = { -> http.serve(health) { _ -> text("up") } }
  -- the long polls, and every other request, are served on 8080
  task.background([checks]) { -> http.serve(api) { _ -> text("done") } }
  Ok(())
}
open in playground

What the server reads and sends

The server speaks HTTP/1.1, and HTTP/1.0 to a client that does (the connection is then closed after the response unless the request says Connection: keep-alive). A request body is given by Content-Length or sent in chunks (Transfer-Encoding: chunked; trailers are read and dropped); req.body is the body’s bytes, whichever way they came. A client that sends Expect: 100-continue is told to go on before its body is read. Connection: close closes the connection after the response.

A response is sent with Content-Length, Date and, where the connection ends with it, Connection: close. Those three, and Transfer-Encoding, are the server’s: headers of these names in Response.headers are left out (the body is sent whole, with its length), and so is a header whose name or value could not stand in one header line. A response sets one cookie for now: Response.headers has one Set-Cookie value, and a value with a line end is left out whole, as any value that could not stand in one header line (a line end would start a cookie, or a header, of its own). A 204 and a 304 are sent with no body and no length, and a 205 (Reset Content, which has no content) with a length of 0 and no body, whatever Response.body holds. The body is the bytes of Response.body, as they are, and what they are is the handler’s to say: a response has the Content-Type that Response.headers gives it, and none if the handler sets none. (The server’s own answers, such as a 405 or a 503, are a word of text and say text/plain; charset=UTF-8.) There is no Server header.

Limits

LimitBeyond it
The head of a request: request line and headers64 KiB431, connection closed
Headers of a request100431, connection closed
The body of a request10,000,000 bytes413, connection closed
The body of a response that http.get or http.request reads (the client’s one limit of size), counted after it is unpacked10,000,000 bytesErr(InvalidResponse("the body is larger than 10000000 bytes")); no more than one byte beyond the limit is read
The bodies, together, of the requests that no handler has yet: those being read (a body counts the bytes of it that have come, as they come, not the length its head announces), those that wait in the queue for a handler, and those about to be handed over256 MiB503, connection closed: the request whose body grows past the room that is left is refused then, and the rest of its body is not read. With every handler holding a request of the largest size besides, the server holds 128 x 10,000,000 bytes more: about 1.5 GB of bodies is the worst case.
A method32 bytes400, connection closed
A line of a chunked body: a chunk’s size and its extensions4 KiB400, connection closed
What of a chunked body is not the body: chunk sizes, extensions, line ends1 MiB400, connection closed. (A body of 10,000,000 bytes in chunks of 100 bytes is within it; a body cut into single bytes is not.)
The trailers of a chunked body64 KiB431, connection closed
Connections open at once, of all the servers of the program together1,024the connection, of any server, that has waited longest for its next request, and has sent nothing of it, is closed without a word to make room for the new one; if none waits so, the one that has waited longest for a request whose head has been coming for more than 5 s is answered 408 and closed (a client sends its head in one round trip: one that trickles it holds no connection for long); if none does, of the responses whose clients take nothing of them and the requests whose clients send nothing more of their bodies, one that has had no piece (16 KiB) since it began, once that is more than 1 s, the oldest first, or else the one that has gone longest without a piece, once that is more than 5 s: a response is closed without a word, a request answered 408 and closed (a client that reads its answer at 30 KB/s or more keeps its connection, measured with a Linux server and a client with its default buffers, whose system lets more of the answer come in steps of about 128 KiB, as does a client that sends its body at that pace; one that reads none, or sends none of its body, holds no connection for long: at the limit one that has had no piece may be closed after a second, so a flood of them has to bring five times as many a second to hold every connection); if there is none of these, no connection more is accepted until one ends (clients wait in the listener’s queue)
Handlers being called128the request waits in the queue
Requests that wait for a handler1,024503 at once
Time a request waits for a handler5 s503
Time for the head of a request, from when the server starts to wait for it. This is also how long a kept connection that sends nothing stays open.30 s408 if part of the request has come, and the connection closed; a connection that sent nothing, or only an empty line, is closed without a word
Time for the body of a request to arrive after its head5 min408, connection closed
Time for a response to be taken by the client5 minconnection closed
The responses, together, of all the servers of the program, that clients have yet to take: each response of which the system did not take all at once, counted whole256 MiBthe connection whose client has taken nothing of its response yet is closed, the oldest first, or else the one whose client has gone longest without taking more of it, until the new one fits (a response larger than that waits alone): a client that reads its answer at 30 KB/s or more (measured as above) keeps it while clients that read nothing come, however much of their answers the system took at the start, and however many of them come a second. So 1,024 clients that ask for a large answer and read none of it make the server hold 256 MiB of answers, not 1,024 of them.
Time the server goes on reading (and dropping) what a client still sends after a refusal or a 408, so that the answer reaches it5 sconnection closed

A request that two readers could take differently is refused with 400, never guessed at: Content-Length together with Transfer-Encoding; Content-Length twice, or not a plain number; a transfer coding other than chunked; a chunk size that is no hexadecimal number or does not fit; a control byte in a chunk’s line or in a trailer; a line of the head that ends in a bare LF, or a CR that ends no line; whitespace before the colon of a header; a header that goes on in the next line; an HTTP/1.1 request without a Host header, and any request with two. After a refusal the connection is closed, and what followed the refused request on it is not served. A # in the target (GET /x#top), which a proxy in front may read as part of the path, and a target in absolute form whose host and port are not those of the Host header are refused with 400 too; the request was read whole, so the connection stays usable.

An Expect header that asks for anything but 100-continue is answered 417, and the connection closed.

What the handler gets

  • req.headers has every name in lower case, however the client wrote it: a header’s name is not case-sensitive, and a lookup asks for one spelling, map.get(req.headers, "content-type"). A header sent more than once is one entry, its values joined with ", " in the order they came (x-a: 1 and x-a: 2 give "1, 2"; set-cookie’s with a line end, as in a response). A header’s value is text: bytes in it that are no UTF-8 reach the handler as U+FFFD, unlike the body, which is exact. (The headers of a Response that http.get or http.request returns are the same: names in lower case, values joined, bytes that are no UTF-8 as U+FFFD.)
  • req.body is Bytes, exactly what arrived: a binary upload is intact. A handler that wants text says so: bytes.to_string(req.body) is Err(InvalidUtf8(offset)) for a body that is no UTF-8. The body of the Response it returns is Bytes in the same way, sent exactly.
  • req.url is the URL the request asks for, a url.Url: http://, the host the request names (its Host header) and the target as sent, in its normal form. Route on url.segments(req.url), the path’s segments decoded (/users/a%20b is ["users", "a b"], %2F a / inside one segment, a / at the end a last ""). Once decoded, a segment may hold a /, a \, a NUL byte, and .. with them: /a/..%2F..%2Fetc%2Fpasswd is ["a", "../../etc/passwd"]. Check a segment before you use it as a file name: a handler that serves files and joins the segments with / serves what is outside its folder too. Read the query with url.query(req.url), its pairs decoded. . and .. are taken out before the handler sees the URL (/a/../b arrives as /b), and so is a needless escape (%7e arrives as ~): the text exactly as the client sent it is not kept, so a signature over the raw request line cannot be checked. url.path(req.url) and url.query_text(req.url) give the path and the query as they stand in the URL, escaped, for a log line. No client sends a fragment, so url.fragment(req.url) is "": a target with a # in it is answered 400. The scheme is always http: the server speaks no TLS (behind a proxy that does, x-forwarded-proto is the handler’s to read). A target in absolute form (GET http://other.example/x HTTP/1.1) is that URL. It is taken of http only (https:// and every other scheme get 400), and its Host header must be one the server takes in a request of its own and name the same host and port, the host in any case and port 80 the same as none (Host: Other.Example:80); otherwise the request gets 400. So url.host(req.url) and the host header never name two hosts. An HTTP/1.0 request without a Host has the host of its target in absolute form, and otherwise the address the client reached (127.0.0.1:8080).
  • req.peer is the address of the client’s end of the connection, as ip:port: 127.0.0.1:53422, and an IPv6 address in brackets, [::1]:53422. An IPv4 client of a listener on [::] is written by its IPv4 address too, not as [::ffff:127.0.0.1]:53422. It is the connection’s, so behind a reverse proxy it is the proxy’s; the client’s own address is then in a header the proxy sets (such as x-forwarded-for). An access log line: io.eprintln("{req.peer} {req.method} {url.path(req.url)}").
  • If the program fails while requests are in flight, it ends as a process: their connections are closed without an answer.

These times are the server’s own, and the only ones that bound a connection’s wait for a request, a body or a client: SILT_IO_TIMEOUT does not. Inside a handler it works as in any task: the handler’s own I/O ends at SILT_IO_TIMEOUT, and a task.deadline that the handler sets ends what it runs. A task.deadline around http.serve itself ends the server when its time is up, as any stop of the task that serves does: the listener stays bound and can be served again.

Every connection that waits for a request has one I/O operation in flight, of the 4,096 a program can have (see concurrency). With at most 1,024 connections open, of all its servers together, a program’s servers leave the rest of it 3,072 of them, however many clients connect and however many servers it has.

The listener while it is served

While http.serve serves a listener, the listener is the server’s alone:

  • tcp.accept on it returns Err(Other("the listener is served by http.serve")) at once. An accept that was already waiting on the listener when the server started returns the same error at that moment: the server has every client.
  • A second http.serve on it is a runtime error.

Stopping a server

tcp.close_listener(listener) stops a server for good, and lets it finish what it has begun (a graceful shutdown):

  • A client that connects afterwards is refused at once (rather than waiting for its own timeout, 60 s for http.request), and the port can be bound again.
  • Every request already in flight is answered: a handler that runs goes on to its end and its response is written, and so are the requests that wait for a handler and one whose bytes have begun to come. Each of these responses says Connection: close, and its connection is closed after it.
  • A kept-alive connection that waits for its next request is closed.
  • http.serve returns () when the last connection has ended.

A stop of the task that serves (the body of the task.background it is a helper of returned, say) ends the server at once and keeps the listener: what is in flight ends as below, and clients that connect wait in the listener for another http.serve or a tcp.accept. A task.deadline around http.serve does the same when its time is up, also while the server drains: a drain that must not last longer than so long is a deadline around http.serve.

import bytes
import http
import task
import tcp
import url

fn hello(_req: http.Request) -> http.Response {
  http.Response { status: 200, body: bytes.from_string("hi"), headers: #{} }
}

fn main() {
  when let Ok(listener) = tcp.listen("127.0.0.1:0") else {
    panic("cannot listen")
  }
  let port = tcp.local_port(listener)
  when let Ok(home) = url.parse("http://127.0.0.1:{port}/") else {
    panic("no URL")
  }
  task.join(
    [
      { -> http.serve(listener, hello) },
      { ->
        match http.get(home) {
          Ok(r) -> println(r.status) -- 200
          Err(e) -> println("{e}")
        }
        -- the server answers what is in flight, and http.serve returns
        tcp.close_listener(listener)
      },
    ],
  )
  match http.get(home) {
    Ok(_) -> println("still served")
    Err(_) -> println("refused") -- refused
  }
}
open in playground

When the task that serves is stopped (or its deadline passes):

  • Its accept is given up. A client that connects afterwards waits in the listener for whoever accepts next (or is refused, if the listener was closed).
  • From the stop on, the server serves no further request on any of its connections, also before they are closed: a request that comes on a connection a client kept gets no answer, and the connection is closed. http.get and http.request send a request that may be sent twice (GET, HEAD, PUT, DELETE, OPTIONS) again on a new connection, to whoever serves the listener then; a POST or PATCH is Err(ClosedEarly).
  • A handler that returns after the stop, before the call that stopped the server has returned, has its response sent with Connection: close. A handler that has not returned by then is answered 503, and its connection is closed; a response that is being sent at that moment is cut off: the client gets less than its Content-Length says. A connection between two requests is closed.
  • After a stop, the listener is the program’s again as soon as the call that stopped the server has returned: tcp.accept on it gets the next client, and another http.serve serves it. A closed listener serves nothing: http.serve on it returns () at once.

An accept that fails (the process has no file descriptor left) is written to stderr once, and the server tries again after 100 ms.

Use pattern matching on (req.method, url.segments(req.url)) for routing:

import bytes
import http
import json
import tcp
import url

type User {
  id: Int,
  name: String,
}

-- A body is bytes: this server answers with text, and says so.
fn reply(status: Int, text: String) -> http.Response {
  http.Response {
    status,
    body: bytes.from_string(text),
    headers: #{ "Content-Type": "text/plain; charset=UTF-8" },
  }
}

fn main() {
  when let Ok(listener) = tcp.listen("127.0.0.1:8080") else {
    panic("cannot listen on port 8080")
  }
  println("Listening on :{tcp.local_port(listener)}")

  http.serve(listener) { req ->
    match (req.method, url.segments(req.url)) {
      (http.GET, []) -> reply(200, "Hello!")

      (http.GET, ["users", id]) -> reply(200, "User {id}")

      (http.POST, ["users"]) -> match bytes.to_string(req.body) {
        Ok(text) -> match json.parse(text, User) {
          Ok(user) -> http.Response {
            status: 201,
            body: bytes.from_string(json.stringify(user)),
            headers: #{ "Content-Type": "application/json" },
          }
          Err(e) -> reply(400, "{e}")
        }
        Err(e) -> reply(400, "{e}")
      }

      _ -> reply(404, "Not found")
    }
  }
}
open in playground