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 [[email protected]_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 [[email protected]_message] to tell this to not send the response right away. When the response is ready, call [[email protected]_message] to cause it to be sent.

To send the response body a bit at a time using "chunked" encoding, first call [[email protected]_encoding] to set soup_encoding_chunked on the response-headers. Then call [[email protected]] (or [ [email protected]_bytes])) to append each chunk as it becomes ready, and [[email protected]_message] 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 [[email protected]] 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




2022 vala-language.org