Server

open class Server(pointer: <Error class: unknown class><<Error class: unknown class>>)

A HTTP server.

#SoupServer implements a simple HTTP server.

To begin, create a server using ctor@Server.new. Add at least one handler by calling method@Server.add_handler or method@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 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_STATUS_UNAUTHORIZED on the message.

After checking for authorization, #SoupServer will look for "early" handlers (added with method@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 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_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 method@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 method@ServerMessage.pause), 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 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 method@Server.listen, method@Server.listen_local, or method@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 struct@GLib.MainContext.

Skipped during bindings generation

  • method raw-paths: Property has no getter nor setter

  • method server-header: Property has no getter nor setter

  • method tls-certificate: Property TypeInfo of getter and setter do not match

  • method tls-database: Property TypeInfo of getter and setter do not match

  • constructor new: Varargs parameter is not supported

Constructors

Link copied to clipboard
constructor(pointer: <Error class: unknown class><<Error class: unknown class>>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val soupServerPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard
open var tlsAuthMode: <Error class: unknown class>

A enum@Gio.TlsAuthenticationMode for SSL/TLS client authentication.

Functions

Link copied to clipboard
open fun acceptIostream(stream: <Error class: unknown class>, localAddr: <Error class: unknown class>? = null, remoteAddr: <Error class: unknown class>? = null): <Error class: unknown class><Boolean>

Adds a new client stream to the @server.

Link copied to clipboard
open fun addAuthDomain(authDomain: AuthDomain)

Adds an authentication domain to @server.

Link copied to clipboard
open fun addEarlyHandler(path: String? = null, callback: ServerCallback)

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

Link copied to clipboard
open fun addHandler(path: String? = null, callback: ServerCallback)

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

Link copied to clipboard
open fun addWebsocketExtension(extensionType: <Error class: unknown class>)

Add support for a WebSocket extension of the given @extension_type.

Link copied to clipboard
open fun addWebsocketHandler(path: String? = null, origin: String? = null, protocols: List<String>? = null, callback: ServerWebsocketCallback)

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

Link copied to clipboard
fun connectRequestAborted(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: (message: ServerMessage) -> Unit): <Error class: unknown class>

Emitted when processing has failed for a message.

Link copied to clipboard
fun connectRequestFinished(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: (message: ServerMessage) -> Unit): <Error class: unknown class>

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

Link copied to clipboard
fun connectRequestRead(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: (message: ServerMessage) -> Unit): <Error class: unknown class>

Emitted when the server has successfully read a request.

Link copied to clipboard
fun connectRequestStarted(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: (message: ServerMessage) -> Unit): <Error class: unknown class>

Emitted when the server has started reading a new request.

Link copied to clipboard
open fun disconnect()

Closes and frees @server's listening sockets.

Link copied to clipboard
open fun getListeners(): <Error class: unknown class>

Gets @server's list of listening sockets.

Link copied to clipboard
open fun getTlsCertificate(): <Error class: unknown class>?

Gets the @server SSL/TLS certificate.

Link copied to clipboard
open fun getTlsDatabase(): <Error class: unknown class>?

Gets the @server SSL/TLS database.

Link copied to clipboard
open fun getUris(): <Error class: unknown class>

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

Link copied to clipboard
open fun isHttps(): Boolean

Checks whether @server is capable of https.

Link copied to clipboard
open fun listen(address: <Error class: unknown class>, options: ServerListenOptions): <Error class: unknown class><Boolean>

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

Link copied to clipboard
open fun listenAll(port: <Error class: unknown class>, options: ServerListenOptions): <Error class: unknown class><Boolean>

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

Link copied to clipboard
open fun listenLocal(port: <Error class: unknown class>, options: ServerListenOptions): <Error class: unknown class><Boolean>

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

Link copied to clipboard
open fun listenSocket(socket: <Error class: unknown class>, options: ServerListenOptions): <Error class: unknown class><Boolean>

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

Link copied to clipboard

Pauses I/O on @msg.

Link copied to clipboard
open fun removeAuthDomain(authDomain: AuthDomain)

Removes @auth_domain from @server.

Link copied to clipboard
open fun removeHandler(path: String)

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

Link copied to clipboard
open fun removeWebsocketExtension(extensionType: <Error class: unknown class>)

Removes support for WebSocket extension of type @extension_type (or any subclass of

Link copied to clipboard
open fun setTlsCertificate(certificate: <Error class: unknown class>)

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

Link copied to clipboard
open fun setTlsDatabase(tlsDatabase: <Error class: unknown class>)

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

Link copied to clipboard

Resumes I/O on @msg.