Skip to content
FutureBlitz

    Install

    Import std.net.http.server

    std.net.http.server

    HttpServer: HTTP/1.1 over TCP, or HTTPS, over TLS, whose handlers are Async code on the main thread, so they share its Globals without data races. A request goes through the server's middleware, in the order Use gave it, to the handler of the route that fits its method and path best, through the middleware of the groups that route is in; the response goes back on a connection kept open for the next request, unless the request or the response says to close it. Automatic HTTPS gets the server's certificate from an ACME CA, as Let's Encrypt is, and renews it while the server runs.

    std/net/http/server.fb

    Types

    HttpHandler alias

    Public Alias HttpHandler = Func(HttpRequest): Task<HttpResponse>

    What a route runs: an Async Function of the request, which gives the response.

    std/net/http/server.fb, line 38

    HttpMiddleware alias

    Public Alias HttpMiddleware = Func(HttpRequest, HttpHandler): Task<HttpResponse>

    What runs around a handler: an Async Function of the request and the next handler, which it calls, or doesn't, to give the response.

    std/net/http/server.fb, line 42

    HttpRequest class

    Public Class HttpRequest

    A request a server read: its method, its Verb, as the client wrote it, like "GET" (Method is a keyword); its target, and the target's path, percent-encoded, without dot segments, and query; its headers and body; the path parameters its route bound; and the client's address.

    std/net/http/server.fb, line 47

    • Public ReadOnly Field Verb: String field #

      Its method, as the client wrote it, like "GET".

    • Public ReadOnly Field Target: String field #

      Its target, as the client wrote it.

    • Public ReadOnly Field Path: String field #

      Its target's path, percent-encoded, without dot segments.

    • Public ReadOnly Field Query: QueryParams field #

      Its target's query.

    • Public ReadOnly Field Headers: Headers field #

      Its headers.

    • Public ReadOnly Field Bytes: UInt8[] field #

      Its body's bytes.

    • Public ReadOnly Field Version: String field #

      "HTTP/1.1" or "HTTP/1.0".

    • Public ReadOnly Field RemoteAddress: String field #

      The client's address.

    • Public Property Text: String property #

      The body as text, its UTF-8, with U+FFFD for each byte that doesn't start well-formed UTF-8.

    • Public Method Param(name: String): String? method #

      A path parameter its route bound, percent-decoded, like :id's, or Null when it has none of that name.

    • Public Property Params: Map<String, String> property #

      Each path parameter its route bound, by name.

    • Public Method Form(): QueryParams method #

      A form's fields, when the body is application/x-www-form-urlencoded, as a browser sends them, or none.

    HttpRouter class

    Public Class HttpRouter

    Routes, and the middleware around them: a server's own, or a group's, whose routes' paths start with its prefix and whose middleware runs inside its parent's. A path is segments after a '/' each: text, which the request's segment matches when it's the same once it's percent-decoded, :name, which matches any one segment and binds it to name, or, last, *name, which matches the rest of the path, empty too, and binds it. Of the routes whose paths match a request's, the one whose segments are text first, segment by segment, takes it, and of those its method fits, the first added; a request whose path some route matches but whose method none has is a 405 Method Not Allowed, and one whose path none matches a 404. A HEAD goes to a GET's route when there's no HEAD's, and its response's body isn't sent.

    std/net/http/server.fb, line 215

    • @PanicsAtCaller Public Method Get(path: String, handler: HttpHandler) method #

      Adds a route for GET and the path, which panics unless it's a path as HttpRouter has them, or another route has the method and a path that matches what it matches.

    • @PanicsAtCaller Public Method Post(path: String, handler: HttpHandler) method #

      Adds a route for POST and path, as Get does.

    • @PanicsAtCaller Public Method Put(path: String, handler: HttpHandler) method #

      Adds a route for PUT and path, as Get does.

    • @PanicsAtCaller Public Method Patch(path: String, handler: HttpHandler) method #

      Adds a route for PATCH and path, as Get does.

    • @PanicsAtCaller Public Method Delete(path: String, handler: HttpHandler) method #

      Adds a route for DELETE and path, as Get does.

    • @PanicsAtCaller Public Method Route(method: String, path: String, handler: HttpHandler) method #

      Adds a route for method, any token, like "PROPFIND", as Get does.

    • @PanicsAtCaller Public Method Work(path: String, handler: WorkHandler) method #

      Adds a work route for GET and the path, as Get does, whose handler blocks, computing or waiting, and so runs on one of the server's Workers threads, given a Sendable copy of the request, while the main thread goes on answering others. The server's and groups' middleware runs around it on the main thread, as it does around any route.

    • @PanicsAtCaller Public Method Work(method: String, path: String, handler: WorkHandler) method #

      Adds a work route for method, as Route does.

    • Public Method Use(middleware: HttpMiddleware) method #

      Adds middleware, which runs around each of its routes' handlers, after what Use gave it before, a server's around every request, the ones no route takes too.

    • @PanicsAtCaller Public Method Group(prefix: String): HttpRouter method #

      A group of routes whose paths start with prefix, a path as routes have them, but without a *name, or it panics, and whose middleware runs inside this one's.

    HttpServer class

    Public Class HttpServer Extends HttpRouter

    A server: routes and middleware, as HttpRouter has them, answering the requests that come to where Listen has it listen, or ListenTls over TLS, as Serve runs. A request that breaks HTTP/1.1's rules is answered as wire.fb has it (400, 413, 414, 431, 501, or 505) and its connection closed; an HTTP/1.1 one without a Host, or any with two, is a 400; an Expect other than 100-continue is a 417. A response has its Content-Length and Date set, and a Connection: close when its connection closes after it.

    std/net/http/server.fb, line 464

    • Public New() constructor #

      A new HttpServer, with no routes, which lets a connection idle for 60 seconds.

    • @PanicsAtCaller Public Property MaxBodySize: Int64 property #

      The most bytes a request's body can have, 16 MiB unless it's set, past which it's a 413. Panics when it's set below 0.

    • @PanicsAtCaller Public Property Workers: Int property #

      How many threads its work routes' handlers run on, at most that many at once, the job pool's size, a core fewer than the machine's, unless it's set; the threads start when a work route is first called, and end once Serve has given and the connections it was answering have ended, so a request being answered as Stop comes is answered. Panics when it's set below 1, or once the threads have started.

    • @PanicsAtCaller Public Property MaxQueuedWork: Int property #

      How many work routes' calls wait for a Workers thread at most, 1024 unless it's set, past which a call is a 503 Service Unavailable. Panics when it's set below 1, or once the threads have started.

    • Public Property IdleTimeout: Duration property #

      How long a connection waits for its next request, or for more of one, before it's closed: a minute unless it's set.

    • @PanicsAtCaller Public Method Listen(address: SocketAddress) Throws NetError method #

      Listens at address, whose port 0 has the OS pick one, which LocalAddress tells. Throws a NetError as TcpListener.Listen does. Panics when it listens already.

    • @PanicsAtCaller Public Method Listen(host: String, port: UInt16) Throws NetError method #

      Listens at host's first address and port.

    • @PanicsAtCaller Public Method ListenTls(address: SocketAddress, tls: TlsServer) Throws NetError method #

      Listens at address as Listen does, for HTTPS: each connection is TLS, as tls accepts it, with its certificate chain and key; a handshake that fails, or takes longer than IdleTimeout, closes its connection, and the server goes on.

    • @PanicsAtCaller Public Method ListenTls(host: String, port: UInt16, tls: TlsServer) Throws NetError method #

      Listens at host's first address and port, for HTTPS, as tls accepts connections.

    • @PanicsAtCaller Public Async Method ListenAutoAsync(acme: AcmeClient, names: String[], folder: Path, https: SocketAddress, http: SocketAddress) Throws NetError, TimeoutError, IOError method #

      Listens for HTTPS at https with a certificate for names, DNS names, which acme gets from its CA, or which folder keeps from a run before, while it's valid for those names. On http, where the CA checks HTTP-01's challenges, a server of its own answers them, and redirects every other request to HTTPS, with a 308, once HTTPS listens, and until then answers a 503. folder keeps the certificate's key and chain, as certificate.pem, and its names, each written atomically. Serve renews the certificate as it runs, as RenewAsync does, once less than a third of its lifetime is left, which it checks as it starts, as a kept one can be due, and then every RenewalCheck; connections from then on get the renewed one, and those before keep theirs. Throws what ObtainAsync, Listen, and writing the folder do. Panics when it listens already, and as ObtainAsync does for the names.

    • @PanicsAtCaller Public Async Method RenewAsync() Throws NetError, TimeoutError, IOError method #

      Gets automatic HTTPS's certificate again at once, and gives it to connections from then on. Throws what ObtainAsync and writing the folder do, which RenewalError then says, and the certificate before goes on. Panics without ListenAutoAsync.

    • @PanicsAtCaller Public Property RenewalCheck: Duration property #

      How often automatic HTTPS checks whether its certificate is due to renew: 12 hours unless it's set, which counts from the check before even while a wait is under way. Panics when it's set to 0 or less.

    • Public Property CertificateNotAfter: Timestamp? property #

      When automatic HTTPS's certificate expires, or Null without it.

    • Public Property RenewalError: Error? property #

      Why automatic HTTPS's last renewal failed, which std.log logs too, or Null once one has worked, or without it.

    • @PanicsAtCaller Public Property LocalAddress: SocketAddress property #

      Where it listens, the port the OS picked included. Panics before Listen.

    • @PanicsAtCaller Public Async Method Serve() method #

      Takes connections and answers their requests until Stop, and gives once it stops taking them; requests being answered then are answered. Panics before Listen.

    • Public Method Stop() method #

      Stops taking connections, closes those waiting for a request, and has those answering one close once it's answered.

    • Public Property IsRunning: Bool property #

      Whether it's listening and hasn't been stopped.

    Extends

    HttpResponse extend

    Extend HttpResponse

    No doc comment.

    std/net/http/server.fb, line 131

    • @PanicsAtCaller Public Static Method Of(status: Int, body: Body? = Null): HttpResponse static method #

      A response of status, with body's bytes and its Content-Type, or an empty one. The status is a final response's, from 200 to 599, or it panics: a 1xx's response comes before the one that answers, and a WebSocket's 101 comes from its AcceptAsync.

    • Public Static Method Of(body: Body): HttpResponse static method #

      A 200 with body.

    • @PanicsAtCaller Public Static Method Redirect(url: String, status: Int = 302): HttpResponse static method #

      A redirect to url, which a header's value can hold, a 302 Found unless status says another from 300 to 399, or it panics.

    Report an issue with this page or with FutureBlitz on GitHub.