ServerCallback


Description:

[ CCode ( instance_pos = 5.9 ) ]
public delegate void ServerCallback (Server server, Message msg, string path, HashTable<string,string>? query, ClientContext client)

A callback used to handle requests to a Server.

path and query contain the likewise-named components of the Request-URI, subject to certain assumptions. By default, Server decodes all percent-encoding in the URI path, such that "/foo%2Fbar" is treated the same as "/foo/bar". If your server is serving resources in some non-POSIX-filesystem namespace, you may want to distinguish those as two distinct paths. In that case, you can set the SERVER_RAW_PATHS property when creating the Server, and it will leave those characters undecoded. (You may want to call uri_normalize to decode any percent-encoded characters that you aren't handling specially.)

query contains the query component of the Request-URI parsed according to the rules for HTML form handling. Although this is the only commonly-used query string format in HTTP, there is nothing that actually requires that HTTP URIs use that format; if your server needs to use some other format, you can just ignore query, and call get_uri and parse the URI's query field yourself.

See add_handler and add_early_handler for details of what handlers can/should do.

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:

server

the Server

msg

the message being processed

path

the path component of msg's Request-URI

query

the parsed query component of msg's Request-URI

client

additional contextual information about the client

user_data

the data passed to add_handler or add_early_handler.


Namespace: Soup
Package: libsoup-2.4