Server

#SoupServer implements a simple HTTP server.

(The following documentation describes the current #SoupServer API, available in <application>libsoup</application> 2.48 and later. See the section "<link linkend="soup-server-old-api">The Old SoupServer Listening API</link>" in the server how-to documentation for details on the older #SoupServer API.)

To begin, create a server using soup_server_new(). Add at least one handler by calling soup_server_add_handler() or soup_server_add_early_handler(); the handler will be called to process any requests underneath the path you pass. (If you want all requests to go to the same handler, just pass "/" (or %NULL) for the path.)

When a new connection is accepted (or a new request is started on an existing persistent connection), the #SoupServer will emit #SoupServer::request-started and then begin processing the request as described below, but note that once the message is assigned a #SoupMessage:status-code, then callbacks after that point will be skipped. Note also that it is not defined when the callbacks happen relative to various #SoupMessage signals.

Once the headers have been read, #SoupServer will check if there is a #SoupAuthDomain (qv) covering the Request-URI; if so, and if the message does not contain suitable authorization, then the #SoupAuthDomain will set a status of %SOUP_STATUS_UNAUTHORIZED on the message.

After checking for authorization, #SoupServer will look for "early" handlers (added with soup_server_add_early_handler()) matching the Request-URI. If one is found, it will be run; in particular, this can be used to connect to signals to do a streaming read of the request body.

(At this point, if the request headers contain "<literal>Expect: 100-continue</literal>", and a status code has been set, then #SoupServer will skip the remaining steps and return the response. If the request headers contain "<literal>Expect: 100-continue</literal>" and no status code has been set, #SoupServer will return a %SOUP_STATUS_CONTINUE status before continuing.)

The server will then read in the response body (if present). At this point, if there are no handlers at all defined for the Request-URI, then the server will return %SOUP_STATUS_NOT_FOUND to the client.

Otherwise (assuming no previous step assigned a status to the message) any "normal" handlers (added with soup_server_add_handler()) for the message's Request-URI will be run.

Then, if the path has a WebSocket handler registered (and has not yet been assigned a status), #SoupServer will attempt to validate the WebSocket handshake, filling in the response and setting a status of %SOUP_STATUS_SWITCHING_PROTOCOLS or %SOUP_STATUS_BAD_REQUEST accordingly.

If the message still has no status code at this point (and has not been paused with soup_server_pause_message()), then it will be given a status of %SOUP_STATUS_INTERNAL_SERVER_ERROR (because at least one handler ran, but returned without assigning a status).

Finally, the server will emit #SoupServer::request-finished (or #SoupServer::request-aborted if an I/O error occurred before handling was completed).

If you want to handle the special "*" URI (eg, "OPTIONS *"), you must explicitly register a handler for "*"; the default handler will not be used for that case.

If you want to process https connections in addition to (or instead of) http connections, you can either set the %SOUP_SERVER_TLS_CERTIFICATE property when creating the server, or else call soup_server_set_ssl_certificate() after creating it.

Once the server is set up, make one or more calls to soup_server_listen(), soup_server_listen_local(), or soup_server_listen_all() to tell it where to listen for connections. (All ports on a #SoupServer use the same handlers; if you need to handle some ports differently, such as returning different data for http and https, you'll need to create multiple #SoupServers, or else check the passed-in URI in the handler function.).

#SoupServer will begin processing connections as soon as you return to (or start) the main loop for the current thread-default #GMainContext.

class Server : ObjectG {}

Constructors

this
this(SoupServer* soupServer, bool ownedRef)

Sets our main struct and passes it to the parent class.

Members

Functions

acceptIostream
bool acceptIostream(IOStream stream, SocketAddress localAddr, SocketAddress remoteAddr)

Add a new client stream to the @server.

addAuthDomain
void addAuthDomain(AuthDomain authDomain)

Adds an authentication domain to @server. Each auth domain will have the chance to require authentication for each request that comes in; normally auth domains will require authentication for requests on certain paths that they have been set up to watch, or that meet other criteria set by the caller. If an auth domain determines that a request requires authentication (and the request doesn't contain authentication), @server will automatically reject the request with an appropriate status (401 Unauthorized or 407 Proxy Authentication Required). If the request used the "100-continue" Expectation, @server will reject it before the request body is sent.

addEarlyHandler
void addEarlyHandler(string path, SoupServerCallback callback, void* userData, GDestroyNotify destroy)

Adds an "early" handler to @server for requests under @path. Note that "normal" and "early" handlers are matched up together, so if you add a normal handler for "/foo" and an early handler for "/foo/bar", then a request to "/foo/bar" (or any path below it) will run only the early handler. (But if you add both handlers at the same path, then both will get run.)

addHandler
void addHandler(string path, SoupServerCallback callback, void* userData, GDestroyNotify destroy)

Adds a handler to @server 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.)

addOnRequestAborted
gulong addOnRequestAborted(void delegate(Message, ClientContext, Server) dlg, ConnectFlags connectFlags)

Emitted when processing has failed for a message; this could mean either that it could not be read (if #SoupServer::request_read has not been emitted for it yet), or that the response could not be written back (if #SoupServer::request_read has been emitted but #SoupServer::request_finished has not been).

addOnRequestFinished
gulong addOnRequestFinished(void delegate(Message, ClientContext, Server) dlg, ConnectFlags connectFlags)

Emitted when the server has finished writing a response to a request.

addOnRequestRead
gulong addOnRequestRead(void delegate(Message, ClientContext, Server) dlg, ConnectFlags connectFlags)

Emitted when the server has successfully read a request. @message will have all of its request-side information filled in, and if the message was authenticated, @client will have information about that. This signal is emitted before any (non-early) handlers are called for the message, and if it sets the message's #status_code, then normal handler processing will be skipped.

addOnRequestStarted
gulong addOnRequestStarted(void delegate(Message, ClientContext, Server) dlg, ConnectFlags connectFlags)

Emitted when the server has started reading a new request. @message will be completely blank; not even the Request-Line will have been read yet. About the only thing you can usefully do with it is connect to its signals.

addWebsocketExtension
void addWebsocketExtension(GType extensionType)

Add support for a WebSocket extension of the given @extension_type. When a WebSocket client requests an extension of @extension_type, a new #SoupWebsocketExtension of type @extension_type will be created to handle the request.

addWebsocketHandler
void addWebsocketHandler(string path, string origin, string[] protocols, SoupServerWebsocketCallback callback, void* userData, GDestroyNotify destroy)

Adds a WebSocket handler to @server 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.)

disconnect
void disconnect()

Closes and frees @server's listening sockets. If you are using the old #SoupServer APIs, this also includes the effect of soup_server_quit().

getAsyncContext
MainContext getAsyncContext()

Gets @server's async_context, if you are using the old API. (With the new API, the server runs in the thread's thread-default #GMainContext, regardless of what this method returns.)

getListener
Socket getListener()

Gets @server's listening socket, if you are using the old API.

getListeners
ListSG getListeners()

Gets @server's list of listening sockets.

getPort
uint getPort()

Gets the TCP port that @server is listening on, if you are using the old API.

getServerStruct
SoupServer* getServerStruct(bool transferOwnership)

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

getUris
ListSG getUris()

Gets a list of URIs corresponding to the interfaces @server is listening on. These will contain IP addresses, not hostnames, and will also indicate whether the given listener is http or https.

isHttps
bool isHttps()

Checks whether @server is capable of https.

listen
bool listen(SocketAddress address, SoupServerListenOptions options)

This attempts to set up @server to listen for connections on @address.

listenAll
bool listenAll(uint port, SoupServerListenOptions options)

This attempts to set up @server to listen for connections on all interfaces on the system. (That is, it listens on the addresses <literal>0.0.0.0</literal> and/or <literal>::</literal>, depending on whether @options includes %SOUP_SERVER_LISTEN_IPV4_ONLY, %SOUP_SERVER_LISTEN_IPV6_ONLY, or neither.) If @port is specified, @server will listen on that port. If it is 0, @server will find an unused port to listen on. (In that case, you can use soup_server_get_uris() to find out what port it ended up choosing.)

listenFd
bool listenFd(int fd, SoupServerListenOptions options)

This attempts to set up @server to listen for connections on @fd.

listenLocal
bool listenLocal(uint port, SoupServerListenOptions options)

This attempts to set up @server to listen for connections on "localhost" (that is, <literal>127.0.0.1</literal> and/or <literal>::1</literal>, depending on whether @options includes %SOUP_SERVER_LISTEN_IPV4_ONLY, %SOUP_SERVER_LISTEN_IPV6_ONLY, or neither). If @port is specified, @server will listen on that port. If it is 0, @server will find an unused port to listen on. (In that case, you can use soup_server_get_uris() to find out what port it ended up choosing.)

listenSocket
bool listenSocket(GIOSocket socket, SoupServerListenOptions options)

This attempts to set up @server to listen for connections on @socket.

pauseMessage
void pauseMessage(Message msg)

Pauses I/O on @msg. This can be used when you need to return from the server handler without having the full response ready yet. Use soup_server_unpause_message() to resume I/O.

quit
void quit()

Stops processing for @server, if you are using the old API. Call this to clean up after soup_server_run_async(), or to terminate a call to soup_server_run().

removeAuthDomain
void removeAuthDomain(AuthDomain authDomain)

Removes @auth_domain from @server.

removeHandler
void removeHandler(string path)

Removes all handlers (early and normal) registered at @path.

removeWebsocketExtension
void removeWebsocketExtension(GType extensionType)

Removes support for WebSocket extension of type @extension_type (or any subclass of @extension_type) from @server. You can also remove extensions enabled by default from the server at construct time by using the %SOUP_SERVER_REMOVE_WEBSOCKET_EXTENSION property.

run
void run()

Starts @server, if you are using the old API, causing it to listen for and process incoming connections. Unlike soup_server_run_async(), this creates a #GMainLoop and runs it, and it will not return until someone calls soup_server_quit() to stop the server.

runAsync
void runAsync()

Starts @server, if you are using the old API, causing it to listen for and process incoming connections.

setSslCertFile
bool setSslCertFile(string sslCertFile, string sslKeyFile)

Sets @server 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).

unpauseMessage
void unpauseMessage(Message msg)

Resumes I/O on @msg. Use this to resume after calling soup_server_pause_message(), or after adding a new chunk to a chunked response.

Static functions

getType
GType getType()

Variables

soupServer
SoupServer* soupServer;

the main Gtk struct

Meta