add_handler


Description:

public void add_handler (string? path, owned ServerCallback callback)

Adds a handler to this for requests prefixed by path.

If path is null or "/", then this will be the default handler for all requests that don't have a more specific handler. (Note though that if you want to handle requests to the special "*" URI, you must explicitly register a handler for "*"; the default handler will not be used for that case.)

For requests under path (that have not already been assigned a status code by a [class@AuthDomain], an early server handler, or a signal handler), callback will be invoked after receiving the request body; the [class@ServerMessage]'s method, request-headers, and request-body properties will be set.

After determining what to do with the request, the callback must at a minimum call [method@ServerMessage.set_status] on the message to set the response status code. Additionally, it may set response headers and/or fill in the response body.

If the callback cannot fully fill in the response before returning (eg, if it needs to wait for information from a database, or another network server), it should call [method@ServerMessage.pause] to tell this to not send the response right away. When the response is ready, call [method@ServerMessage.unpause] to cause it to be sent.

To send the response body a bit at a time using "chunked" encoding, first call [method@MessageHeaders.set_encoding] to set soup_encoding_chunked on the response-headers. Then call [method@MessageBody.append] (or [ method@MessageBody.append_bytes])) to append each chunk as it becomes ready, and [method@ServerMessage.unpause] to make sure it's running. (The server will automatically pause the message if it is using chunked encoding but no more chunks are available.) When you are done, call [ method@MessageBody.complete] to indicate that no more chunks are coming.

Parameters:

this

a Server

path

the toplevel path for the handler

callback

callback to invoke for requests under path

destroy

destroy notifier to free user_data

user_data

data for callback