Server

A HTTP server.

#SoupServer implements a simple HTTP server.

To begin, create a server using soup.server.Server.new_. Add at least one handler by calling soup.server.Server.addHandler or soup.server.Server.addEarlyHandler; 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 signal@Server::request-started and then begin processing the request as described below, but note that once the message is assigned a status-code, then callbacks after that point will be skipped. Note also that it is not defined when the callbacks happen relative to various class@ServerMessage signals.

Once the headers have been read, #SoupServer will check if there is a class@AuthDomain (qv) covering the Request-URI; if so, and if the message does not contain suitable authorization, then the class@AuthDomain will set a status of soup.types.Status.Unauthorized on the message.

After checking for authorization, #SoupServer will look for "early" handlers (added with soup.server.Server.addEarlyHandler) 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 `Expect: 100-continue`, and a status code has been set, then #SoupServer will skip the remaining steps and return the response. If the request headers contain `Expect: 100-continue` and no status code has been set, #SoupServer will return a soup.types.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.types.Status.NotFound to the client.

Otherwise (assuming no previous step assigned a status to the message) any "normal" handlers (added with soup.server.Server.addHandler) 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.types.Status.SwitchingProtocols or soup.types.Status.BadRequest accordingly.

If the message still has no status code at this point (and has not been paused with soup.server_message.ServerMessage.pause), then it will be given a status of soup.types.Status.InternalServerError (because at least one handler ran, but returned without assigning a status).

Finally, the server will emit signal@Server::request-finished (or signal@Server::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 set the property@Server:tls-certificate property.

Once the server is set up, make one or more calls to soup.server.Server.listen, soup.server.Server.listenLocal, or soup.server.Server.listenAll 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 soup.server.Servers, 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 glib.main_context.MainContext.

class Server : ObjectG {}

Members

Functions

acceptIostream
bool acceptIostream(gio.iostream.IOStream stream, gio.socket_address.SocketAddress localAddr, gio.socket_address.SocketAddress remoteAddr)

Adds a new client stream to the server.

addAuthDomain
void addAuthDomain(soup.auth_domain.AuthDomain authDomain)

Adds an authentication domain to server.

addEarlyHandler
void addEarlyHandler(string path, soup.types.ServerCallback callback)

Adds an "early" handler to server for requests prefixed by path.

addHandler
void addHandler(string path, soup.types.ServerCallback callback)

Adds a handler to server for requests prefixed by path.

addWebsocketExtension
void addWebsocketExtension(gobject.types.GType extensionType)

Add support for a WebSocket extension of the given extension_type.

addWebsocketHandler
void addWebsocketHandler(string path, string origin, string[] protocols, soup.types.ServerWebsocketCallback callback)

Adds a WebSocket handler to server for requests prefixed by path.

connectRequestAborted
ulong connectRequestAborted(T callback, Flag!"After" after)

Connect to RequestAborted signal.

connectRequestFinished
ulong connectRequestFinished(T callback, Flag!"After" after)

Connect to RequestFinished signal.

connectRequestRead
ulong connectRequestRead(T callback, Flag!"After" after)

Connect to RequestRead signal.

connectRequestStarted
ulong connectRequestStarted(T callback, Flag!"After" after)

Connect to RequestStarted signal.

disconnect
void disconnect()

Closes and frees server's listening sockets.

getListeners
gio.socket.Socket[] getListeners()

Gets server's list of listening sockets.

getTlsAuthMode
gio.types.TlsAuthenticationMode getTlsAuthMode()

Gets the server SSL/TLS client authentication mode.

getTlsCertificate
gio.tls_certificate.TlsCertificate getTlsCertificate()

Gets the server SSL/TLS certificate.

getTlsDatabase
gio.tls_database.TlsDatabase getTlsDatabase()

Gets the server SSL/TLS database.

getUris
glib.uri.Uri[] getUris()

Gets a list of URIs corresponding to the interfaces server is listening on.

isHttps
bool isHttps()

Checks whether server is capable of https.

listen
bool listen(gio.socket_address.SocketAddress address, soup.types.ServerListenOptions options)

Attempts to set up server to listen for connections on address.

listenAll
bool listenAll(uint port, soup.types.ServerListenOptions options)

Attempts to set up server to listen for connections on all interfaces on the system.

listenLocal
bool listenLocal(uint port, soup.types.ServerListenOptions options)

Attempts to set up server to listen for connections on "localhost".

listenSocket
bool listenSocket(gio.socket.Socket socket, soup.types.ServerListenOptions options)

Attempts to set up server to listen for connections on socket.

pauseMessage
void pauseMessage(soup.server_message.ServerMessage msg)

Pauses I/O on msg.

removeAuthDomain
void removeAuthDomain(soup.auth_domain.AuthDomain authDomain)

Removes auth_domain from server.

removeHandler
void removeHandler(string path)

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

removeWebsocketExtension
void removeWebsocketExtension(gobject.types.GType extensionType)

Removes support for WebSocket extension of type extension_type (or any subclass of extension_type) from server.

setTlsAuthMode
void setTlsAuthMode(gio.types.TlsAuthenticationMode mode)

Sets server's #GTlsAuthenticationMode to use for SSL/TLS client authentication.

setTlsCertificate
void setTlsCertificate(gio.tls_certificate.TlsCertificate certificate)

Sets server up to do https, using the given SSL/TLS certificate.

setTlsDatabase
void setTlsDatabase(gio.tls_database.TlsDatabase tlsDatabase)

Sets server's #GTlsDatabase to use for validating SSL/TLS client certificates.

unpauseMessage
void unpauseMessage(soup.server_message.ServerMessage msg)

Resumes I/O on msg.

Inherited Members

From ObjectG

setGObject
void setGObject(void* cObj, Flag!"Take" take)

Set the GObject of a D ObjectG wrapper.

cPtr
void* cPtr(Flag!"Dup" dup)

Get a pointer to the underlying C object.

ref_
void* ref_(void* gObj)

Calls g_object_ref() on a GObject.

unref
unref(void* gObj)

Calls g_object_unref() on a GObject.

getType
GType getType()

Get the GType of an object.

gType
GType gType [@property getter]

GObject GType property.

self
ObjectG self()

Convenience method to return this cast to a type. For use in D with statements.

getDObject
T getDObject(void* cptr, Flag!"Take" take)

Template to get the D object from a C GObject and cast it to the given D object type.

connectSignalClosure
ulong connectSignalClosure(string signalDetail, DClosure closure, Flag!"After" after)

Connect a D closure to an object signal.

setProperty
void setProperty(string propertyName, T val)

Template for setting a GObject property.

getProperty
T getProperty(string propertyName)

Template for getting a GObject property.

compatControl
size_t compatControl(size_t what, void* data)
bindProperty
gobject.binding.Binding bindProperty(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags)

Creates a binding between source_property on source and target_property on target.

bindPropertyFull
gobject.binding.Binding bindPropertyFull(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags, gobject.closure.Closure transformTo, gobject.closure.Closure transformFrom)

Creates a binding between source_property on source and target_property on target, allowing you to set the transformation functions to be used by the binding.

forceFloating
void forceFloating()

This function is intended for #GObject implementations to re-enforce a floating[floating-ref] object reference. Doing this is seldom required: all #GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling gobject.object.ObjectG.refSink.

freezeNotify
void freezeNotify()

Increases the freeze count on object. If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one #GObject::notify signal is emitted for each property modified while the object is frozen.

getData
void* getData(string key)

Gets a named field from the objects table of associations (see gobject.object.ObjectG.setData).

getProperty
void getProperty(string propertyName, gobject.value.Value value)

Gets a property of an object.

getQdata
void* getQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata.

getv
void getv(string[] names, gobject.value.Value[] values)

Gets n_properties properties for an object. Obtained properties will be set to values. All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.

isFloating
bool isFloating()

Checks whether object has a floating[floating-ref] reference.

notify
void notify(string propertyName)

Emits a "notify" signal for the property property_name on object.

notifyByPspec
void notifyByPspec(gobject.param_spec.ParamSpec pspec)

Emits a "notify" signal for the property specified by pspec on object.

refSink
gobject.object.ObjectG refSink()

Increase the reference count of object, and possibly remove the floating[floating-ref] reference, if object has a floating reference.

runDispose
void runDispose()

Releases all references to other objects. This can be used to break reference cycles.

setData
void setData(string key, void* data)

Each object carries around a table of associations from strings to pointers. This function lets you set an association.

setProperty
void setProperty(string propertyName, gobject.value.Value value)

Sets a property on an object.

stealData
void* stealData(string key)

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.

stealQdata
void* stealQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example:

thawNotify
void thawNotify()

Reverts the effect of a previous call to gobject.object.ObjectG.freezeNotify. The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted.

watchClosure
void watchClosure(gobject.closure.Closure closure)

This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling gobject.closure.Closure.invalidate on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, gobject.object.ObjectG.ref_ and gobject.object.ObjectG.unref are added as marshal guards to the closure, to ensure that an extra reference count is held on object during invocation of the closure. Usually, this function will be called on closures that use this object as closure data.

connectNotify
ulong connectNotify(string detail, T callback, Flag!"After" after)

Connect to Notify signal.