Http Server¶
| Spice | |
|---|---|
HttpServer struct¶
A blocking HTTP/1.1 server, built on the plain TCP sockets of "std/net/socket".
Requests are served one at a time on the thread that drives the server: run keeps serving until stop is called, while handleNextRequest serves a single connection and hands control back, so the caller can weave the server into a loop of its own. Every connection is answered with Connection: close and closed afterwards.
Routing is exact-match on the path of the request target, so "/a" and "/a/" are different routes and the query string never takes part in the match. A path that is registered for some other method answers 405 instead of 404, and a HEAD request falls back to the GET route of the same path, with the body dropped again afterwards.
The server speaks plain http by default. Call useTls with a certificate and private key (before start) to serve https instead - every connection is then TLS-handshaked (see "std/net/tls") before its request is read and its response is written.
Note on handlers: a handler is stored in a Lambda (see "std/type/lambda"), which relocates the captures of the lambda onto the heap with a shallow copy. A handler may therefore capture plain values, but it must not capture a String, a container or anything else that owns memory, since only the owner itself would free it. Fixed content belongs into serve, and anything else into a global or into the request itself.
Fields¶
| Name | Type | Description |
|---|---|---|
port |
unsigned short |
Port the server listens on |
serverIdent |
String |
Value of the Server field of every response |
maxMessageSize |
unsigned long |
Upper bound on the size of a request |
timeoutMillis |
unsigned long |
Per read/write timeout of a connection, 0 to block indefinitely |
Constructors¶
ctor¶
| Spice | |
|---|---|
Constructs a server for the given port. Nothing is bound yet - that happens in start.
Parameters
| Name | Type | Description |
|---|---|---|
port |
unsigned short |
Port to listen on (default: HTTP_PORT_FALLBACK) |
dtor¶
| Spice | |
|---|---|
Closes the listening socket if the server is still up
Methods¶
useTls¶
| Spice | |
|---|---|
Switches the server from plain http to https: every connection accepted from here on is TLS-handshaked with the given certificate before its request is read.
Must be called before start, since the listening socket itself is unaffected - only what happens to a connection right after it is accepted changes.
Parameters
| Name | Type | Description |
|---|---|---|
certFile |
string |
Path to a PEM file with the server's certificate (chain) |
keyFile |
string |
Path to a PEM file with the private key matching the certificate |
Returns: Result<bool> — true once TLS is configured, or an error describing why it could not be
route¶
| Spice | |
|---|---|
Registers a handler for a method and path
Parameters
| Name | Type | Description |
|---|---|---|
method |
HttpMethod |
Method the route answers |
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
get¶
| Spice | |
|---|---|
Registers a GET handler for a path. HEAD requests for the same path are answered from it as well.
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
post¶
| Spice | |
|---|---|
Registers a POST handler for a path
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
put¶
| Spice | |
|---|---|
Registers a PUT handler for a path
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
patch¶
| Spice | |
|---|---|
Registers a PATCH handler for a path
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
delete¶
| Spice | |
|---|---|
Registers a DELETE handler for a path
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
serve¶
| Spice | |
|---|---|
Registers a route that answers a path with a fixed piece of content
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
content |
string |
Content to respond with |
contentType |
string |
Media type of the content (default: CONTENT_TYPE_HTML) |
serve¶
| Spice | |
|---|---|
Registers a route that answers a path with a fixed piece of content
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path the route answers, matched exactly |
content |
const String& |
Content to respond with |
contentType |
string |
Media type of the content (default: CONTENT_TYPE_HTML) |
setNotFoundHandler¶
| Spice | |
|---|---|
Registers the handler that answers requests no route matches. Without one, unmatched requests are answered with a plain 404.
Parameters
| Name | Type | Description |
|---|---|---|
handler |
p(const HttpRequest&, HttpResponse&) |
Handler that fills in the response |
start¶
| Spice | |
|---|---|
Binds the configured port and starts listening on it
Parameters
| Name | Type | Description |
|---|---|---|
connectionBacklog |
int |
Maximum number of connections that may wait in the accept queue (default: DEFAULT_CONNECTION_BACKLOG) |
Returns: Result<bool> — true once the server listens, or an error describing why it could not
handleNextRequest¶
| Spice | |
|---|---|
Accepts a single connection, answers the request on it and closes it again.
A request that cannot be parsed is answered with 400 rather than being dropped silently, so a confused client learns what happened.
Returns: Result<bool> — true if a request was served, or an error if the connection could not be accepted
run¶
| Spice | |
|---|---|
Serves connections until stop is called or a connection cannot be accepted.
The stop flag is checked between connections, so a stop from another thread takes effect once the connection that is currently being served is done.
Returns: Result<bool> — true if the loop ended through stop, or an error if a connection could not be accepted
stop¶
| Spice | |
|---|---|
Asks a running run loop to end once the connection it is serving is done
isRunning¶
| Spice | |
|---|---|
Checks whether a run loop is currently serving
Returns: bool
isListening¶
| Spice | |
|---|---|
Checks whether the server holds a listening socket
Returns: bool
close¶
| Spice | |
|---|---|
Closes the listening socket. The server can be started again afterwards.
Returns: Closing the socket was successful or not