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
Example: Simple server, async:
public class NoodleSoupServer : Soup.Server {
private int access_counter = 0;
public NoodleSoupServer () {
assert (this != null);
// Links:
// http://localhost:8088/*
this.add_handler (null, default_handler);
}
private static void default_handler (Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) {
unowned NoodleSoupServer self = server as NoodleSoupServer;
uint id = self.access_counter++;
print ("Default handler start (%u)\n", id);
// Simulate asynchronous input / time consuming operations:
// See GLib.IOSchedulerJob for time consuming operations
Timeout.add_seconds (0, () => {
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);
// Resumes HTTP I/O on msg:
self.unpause_message (msg);
print ("Default handler end (%u)\n", id);
return false;
}, Priority.DEFAULT);
// Pauses HTTP I/O on msg:
self.pause_message (msg);
}
public static int main (string[] args) {
try {
int port = 8088;
MainLoop loop = new MainLoop ();
NoodleSoupServer server = new NoodleSoupServer ();
server.listen_all (port, 0);
loop.run ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
}
valac --pkg libsoup-2.4 simple-server-async-new.vala
- public bool accept_iostream (IOStream stream, SocketAddress? local_addr, SocketAddress? remote_addr) throws Error
Add a new client stream to the this.
- public void add_auth_domain (AuthDomain auth_domain)
Adds an authentication domain to this
.
- public void add_early_handler (string? path, owned ServerCallback callback)
Adds an "early" handler to this for
requests under path
.
- public void add_handler (string? path, owned ServerCallback callback)
Adds a handler to this for requests
under path
.
- public void add_websocket_extension (Type extension_type)
Add support for a WebSocket extension of the given
extension_type
.
- public void add_websocket_handler (string? path, string? origin, string[]? protocols, owned ServerWebsocketCallback callback)
Adds a WebSocket handler to this for
requests under path
.
- public void disconnect ()
Closes and frees this's listening
sockets.
- public unowned MainContext? get_async_context ()
Gets this's async_context, if you
are using the old API.
- public unowned Socket get_listener ()
Gets this's listening socket, if you
are using the old API.
- public SList<unowned Socket> get_listeners ()
Gets this's list of listening
sockets.
- public uint get_port ()
Gets the TCP port that this is
listening on, if you are using the old API.
- public SList<URI> get_uris ()
Gets a list of URIs corresponding to the interfaces
this is listening on.
- public bool is_https ()
Checks whether this is capable of
https.
- public bool listen (SocketAddress address, ServerListenOptions options) throws Error
This attempts to set up this to
listen for connections on address
.
- public bool listen_all (uint port, ServerListenOptions options) throws Error
This attempts to set up this to
listen for connections on all interfaces on the system.
- public bool listen_fd (int fd, ServerListenOptions options) throws Error
This attempts to set up this to
listen for connections on fd
.
- public bool listen_local (uint port, ServerListenOptions options) throws Error
This attempts to set up this to
listen for connections on "localhost" (that is, 127.0.0.1 and/or :1, depending on whether options
includes
IPV4_ONLY,
IPV6_ONLY, or neither).
- public bool listen_socket (Socket socket, ServerListenOptions options) throws Error
This attempts to set up this to
listen for connections on socket
.
- public void pause_message (Message msg)
Pauses I/O on msg
.
- public void quit ()
Stops processing for this, if you
are using the old API.
- public void remove_auth_domain (AuthDomain auth_domain)
Removes auth_domain
from this
.
- public void remove_handler (string path)
Removes all handlers (early and normal) registered at path
.
- public void remove_websocket_extension (Type extension_type)
Removes support for WebSocket extension of type extension_type
(or any subclass of extension_type
) from this.
- public void run ()
Starts this, if you are using the
old API, causing it to listen for and process incoming connections.
- public void run_async ()
Starts this, if you are using the
old API, causing it to listen for and process incoming connections.
- public bool set_ssl_cert_file (string ssl_cert_file, string ssl_key_file) throws Error
Sets this up to do https, using the
SSL/TLS certificate specified by ssl_cert_file
and ssl_key_file
(which may point to the same file).
- public void unpause_message (Message msg)
Resumes I/O on msg
.