Import std.net.http.server
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 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
-
A new HttpServer, with no routes, which lets a connection idle for 60 seconds.
-
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.
-
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.
-
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.
-
How long a connection waits for its next request, or for more of one, before it's closed: a minute unless it's set.
-
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. -
Listens at
host's first address andport. -
@PanicsAtCaller Public Method ListenTls(address: SocketAddress, tls: TlsServer) Throws NetErrormethod #Listens at
addressas Listen does, for HTTPS: each connection is TLS, astlsaccepts 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 NetErrormethod #Listens at
host's first address andport, for HTTPS, astlsaccepts connections. -
@PanicsAtCaller Public Async Method ListenAutoAsync(acme: AcmeClient, names: String[], folder: Path, https: SocketAddress, http: SocketAddress) Throws NetError, TimeoutError, IOErrormethod #Listens for HTTPS at
httpswith a certificate fornames, DNS names, whichacmegets from its CA, or whichfolderkeeps from a run before, while it's valid for thosenames. Onhttp, 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.folderkeeps the certificate's key and chain, as certificate.pem, and itsnames, 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 thefolderdo. Panics when it listens already, and as ObtainAsync does for thenames. -
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.
-
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.
-
When automatic HTTPS's certificate expires, or Null without it.
-
Why automatic HTTPS's last renewal failed, which std.log logs too, or Null once one has worked, or without it.
-
Where it listens, the port the OS picked included. Panics before Listen.
-
Takes connections and answers their requests until Stop, and gives once it stops taking them; requests being answered then are answered. Panics before Listen.
-
Stops taking connections, closes those waiting for a request, and has those answering one close once it's answered.
-
Whether it's listening and hasn't been stopped.
Report an issue with this page or with FutureBlitz on GitHub.