Skip to content
FutureBlitz

    Install

    Import std.net.http.server

    HttpServer

    std.net.http.server

    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 RFC 9112 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.

    Examples

    Async Function Hello(request: HttpRequest): HttpResponse
        Local name = request.Param("name") ?? "you"
        Return HttpResponse.Of(Body.Text("Hello, {name}!"))
    End Function
    
    Local server = HttpServer()
    server.Get("/hello/:name", Function(request: HttpRequest) => Hello(request))
    Try server.Listen("127.0.0.1", 8080)
    // Serve answers requests, GET /hello/Ada with "Hello, Ada!", until Stop.
    server.Serve()

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

    • 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 answers a 503 before it does. 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.

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