add_handler


Description:

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

Adds a handler to this for requests under 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 AuthDomain, an early SoupServerHandler, or a signal handler), callback will be invoked after receiving the request body; the message's method, SoupMessage:request-headerss, and SoupMessage:request-body fields will be filled in.

After determining what to do with the request, the callback must at a minimum call set_status (or set_status_full) 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 pause_message to tell this to not send the response right away. When the response is ready, call unpause_message to cause it to be sent.

To send the response body a bit at a time using "chunked" encoding, first call set_encoding to set CHUNKED on the SoupMessage:response-headerss. Then call append (or append_buffer) to append each chunk as it becomes ready, and unpause_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 complete to indicate that no more chunks are coming.

Example: Simple server, sync:

public class NoodleSoupServer : Soup.Server {
private int access_counter = 0;

public NoodleSoupServer () {
Object (port: 8088);
assert (this != null);

// Links:
// http://localhost:8088/about.html
// http://localhost:8088/index.html
// http://localhost:8088/
this.add_handler ("/about.html", about_handler);
this.add_handler ("/index.html", root_handler);
this.add_handler ("/", root_handler);

// Links:
// http://localhost:8088/*
this.add_handler (null, default_handler);
}

private static void root_handler (Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) {
string html_head = "<head><title>Index</title></head>";
string html_body = "<body><h1>Index:</h1></body>";
msg.set_response ("text/html", Soup.MemoryUse.COPY, "<html>%s%s</html>".printf (html_head, html_body).data);
}

private static void about_handler (Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) {
string html_head = "<head><title>About</title></head>";
string html_body = "<body><h1>About:</h1></body>";
msg.set_response ("text/html", Soup.MemoryUse.COPY, "<html>%s%s</html>".printf (html_head, html_body).data);
}

private static void default_handler (Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) {
NoodleSoupServer self = server as NoodleSoupServer;
assert (self != null);

if (msg.uri.get_path () == "/foo.html") {
// http://localhost:8088/foo.html
string html_head = "<head><title>Default</title></head>";
string html_body = "<body><h1>Default:</h1><p>%s</p><p>%u</p></body>".printf (msg.uri.to_string (false), ++self.access_counter);
msg.set_response ("text/html", Soup.MemoryUse.COPY, "<html>%s%s</html>".printf (html_head, html_body).data);
} else {
// 404:
msg.set_response ("text/html", Soup.MemoryUse.COPY, "<html><head><title>404</title></head><body><h1>404</h1></body></html>".data);
msg.status_code = 404;
}
}

public static int main (string[] args) {
NoodleSoupServer server = new NoodleSoupServer ();
server.run ();
return 0;
}
}

valac --pkg libsoup-2.4 simple-server-sync.vala

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