1.21 <SoupServer>

Class: <SoupServer>

It derives from <GObject>.

Server provides a basic implementation of an HTTP server. The recommended usage of this server is for internal use, tasks like a mock server for tests, a private service for IPC, etc. It is not recommended to be exposed to untrusted clients as it may be vulnerable to denial of service attacks or other exploits.

To begin, create a server using Server.new. Add at least one handler by calling Server.add_handler or 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 #f) for the path.)

When a new connection is accepted (or a new request is started on an existing persistent connection), the Server will emit "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 ServerMessage signals.

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

After checking for authorization, Server will look for "early" handlers (added with 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 Server will skip the remaining steps and return the response. If the request headers contain Expect: 100-continue and no status code has been set, Server will return a 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 not-found to the client.

Otherwise (assuming no previous step assigned a status to the message) any "normal" handlers (added with 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), Server will attempt to validate the WebSocket handshake, filling in the response and setting a status of switching-protocols or bad-request accordingly.

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

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

Once the server is set up, make one or more calls to Server.listen, Server.listen_local, or Server.listen_all to tell it where to listen for connections. (All ports on a Server 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 Servers, or else check the passed-in URI in the handler function.).

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

Fields

parent-instance

Properties

Property: server:raw-paths self

If #t, percent-encoding in the Request-URI path will not be automatically decoded.

Its value is a boolean.

It can only be set when the object is made, with #:raw-paths.

Property: server:server-header self

Server header.

If non-#f, the value to use for the "Server" header on ServerMessages processed by this server.

The Server header is the server equivalent of the User-Agent header, and provides information about the server and its components. It contains a list of one or more product tokens, separated by whitespace, with the most significant product token coming first. The tokens must be brief, ASCII, and mostly alphanumeric (although "-", "_", and "." are also allowed), and may optionally include a "/" followed by a version string. You may also put comments, enclosed in parentheses, between or after the tokens.

Some HTTP server implementations intentionally do not use version numbers in their Server header, so that installations running older versions of the server don’t end up advertising their vulnerability to specific security holes.

As with Session:user_agent, if you set a Server:server-header property that has trailing whitespace, Server will append its own product token (eg, libsoup/2.3.2) to the end of the header for you.

Its value is a string, or #f.

Set it with (set! (server:server-header self) value).

Property: server:tls-auth-mode self

A <GTlsAuthenticationMode> for SSL/TLS client authentication.

Its value is a <GTlsAuthenticationMode> (or, to set it, one of its symbols).

Set it with (set! (server:tls-auth-mode self) value).

Property: server:tls-certificate self

A [class@Gio.TlsCertificate[] that has a tls-certificate:private-key set.

If this is set, then the server will be able to speak https in addition to (or instead of) plain http.

Its value is a <GTlsCertificate>, or #f.

Set it with (set! (server:tls-certificate self) value).

Property: server:tls-database self

A <GTlsDatabase> to use for validating SSL/TLS client certificates.

Its value is a <GTlsDatabase>, or #f.

Set it with (set! (server:tls-database self) value).

Procedures

Method: server:accept-iostream? self stream [local-addr] [remote-addr] ⇒ %return
Method: accept-iostream? self stream [local-addr] [remote-addr] ⇒ %return

Adds a new client stream to the self.

self

Passed: a <SoupServer>.

a <SoupServer>

stream

Passed: a <GIOStream>.

a <GIOStream>

local-addr

Passed, or left out: a <GSocketAddress>, or #f.

the local <GSocketAddress> associated with the stream

remote-addr

Passed, or left out: a <GSocketAddress>, or #f.

the remote <GSocketAddress> associated with the stream

%return

Returned: a boolean.

#t on success, #f if the stream could not be accepted or any other error occurred (in which case error will be set).

Method: server:add-auth-domain self auth-domain
Method: add-auth-domain self auth-domain

Adds an authentication domain to self.

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), self will automatically reject the request with an appropriate status (401 Unauthorized or 407 Proxy Authentication Required). If the request used the SoupServer:100-continue Expectation, self will reject it before the request body is sent.

self

Passed: a <SoupServer>.

a <SoupServer>

auth-domain

Passed: a <SoupAuthDomain>.

a <SoupAuthDomain>

Method: server:add-early-handler self path callback
Method: add-early-handler self path callback

Adds an "early" handler to self for requests prefixed by 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.)

For requests under path (that have not already been assigned a status code by a AuthDomain or a signal handler), callback will be invoked after receiving the request headers, but before receiving the request body; the message’s method and request-headers properties will be set.

Early handlers are generally used for processing requests with request bodies in a streaming fashion. If you determine that the request will contain a message body, normally you would call MessageBody.set_accumulate on the message’s request-body to turn off request-body accumulation, and connect to the message’s "got-chunk" signal to process each chunk as it comes in.

To complete the message processing after the full message body has been read, you can either also connect to "got-body", or else you can register a non-early handler for path as well. As long as you have not set the status-code by the time "got-body" is emitted, the non-early handler will be run as well.

self

Passed: a <SoupServer>.

a <SoupServer>

path

Passed: a string, or #f.

the toplevel path for the handler

callback

Passed: a procedure.

callback to invoke for requests under path

See SoupServerCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

data for callback

destroy

Not passed: guile-gi supplies it.

destroy notifier to free user-data

See GDestroyNotify for how it is called.

Method: server:add-handler self path callback
Method: add-handler self path callback

Adds a handler to self for requests prefixed by path.

If path is #f 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.)

For requests under path (that have not already been assigned a status code by a AuthDomain, an early server handler, or a signal handler), callback will be invoked after receiving the request body; the ServerMessage’s method, request-headers, and request-body properties will be set.

After determining what to do with the request, the callback must at a minimum call ServerMessage.set_status on the message to set the response status code. Additionally, it may set response headers and/or fill in the response body.

If the callback cannot fully fill in the response before returning (eg, if it needs to wait for information from a database, or another network server), it should call ServerMessage.pause to tell self to not send the response right away. When the response is ready, call ServerMessage.unpause to cause it to be sent.

To send the response body a bit at a time using "chunked" encoding, first call MessageHeaders.set_encoding to set chunked on the response-headers. Then call MessageBody.append (or MessageBody.append_bytes)) to append each chunk as it becomes ready, and ServerMessage.unpause to make sure it’s running. (The server will automatically pause the message if it is using chunked encoding but no more chunks are available.) When you are done, call MessageBody.complete to indicate that no more chunks are coming.

self

Passed: a <SoupServer>.

a <SoupServer>

path

Passed: a string, or #f.

the toplevel path for the handler

callback

Passed: a procedure.

callback to invoke for requests under path

See SoupServerCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

data for callback

destroy

Not passed: guile-gi supplies it.

destroy notifier to free user-data

See GDestroyNotify for how it is called.

Method: server:add-websocket-extension self extension-type
Method: add-websocket-extension self extension-type

Add support for a WebSocket extension of the given extension-type.

When a WebSocket client requests an extension of extension-type, a new WebsocketExtension of type extension-type will be created to handle the request.

Note that WebsocketExtensionDeflate is supported by default, use Server.remove_websocket_extension if you want to disable it.

self

Passed: a <SoupServer>.

a <SoupServer>

extension-type

Passed: a class, or a GType number.

a GType

Method: server:add-websocket-handler self path origin protocols callback
Method: add-websocket-handler self path origin protocols callback

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

If path is #f or "/", then this will be the default handler for all requests that don’t have a more specific handler.

When a path has a WebSocket handler registered, self will check incoming requests for WebSocket handshakes after all other handlers have run (unless some earlier handler has already set a status code on the message), and update the request’s status, response headers, and response body accordingly.

If origin is non-#f, then only requests containing a matching "Origin" header will be accepted. If protocols is non-#f, then only requests containing a compatible "Sec-WebSocket-Protocols" header will be accepted. More complicated requirements can be handled by adding a normal handler to path, and having it perform whatever checks are needed and setting a failure status code if the handshake should be rejected.

self

Passed: a <SoupServer>.

a <SoupServer>

path

Passed: a string, or #f.

the toplevel path for the handler

origin

Passed: a string, or #f.

the origin of the connection

protocols

Passed: a list or vector, each a string, or #f.

the protocols supported by this handler

callback

Passed: a procedure.

callback to invoke for successful WebSocket requests under path

See SoupServerWebsocketCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

data for callback

destroy

Not passed: guile-gi supplies it.

destroy notifier to free user-data

See GDestroyNotify for how it is called.

Method: server:disconnect self
Method: disconnect self

Closes and frees self’s listening sockets.

Note that if there are currently requests in progress on self, that they will continue to be processed if self’s <GMainContext> is still running.

You can call Server.listen, etc, after calling this function if you want to start listening again.

self

Passed: a <SoupServer>.

a <SoupServer>

Method: server:get-listeners self ⇒ %return
Method: get-listeners self ⇒ %return

Gets self’s list of listening sockets.

You should treat these sockets as read-only; writing to or modifiying any of these sockets may cause self to malfunction.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a list, each a <GSocket>.

a list of listening sockets.

Method: server:get-tls-auth-mode self ⇒ %return
Method: get-tls-auth-mode self ⇒ %return

Gets the self SSL/TLS client authentication mode.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a <GTlsAuthenticationMode>.

a <GTlsAuthenticationMode>

Method: server:get-tls-certificate self ⇒ %return
Method: get-tls-certificate self ⇒ %return

Gets the self SSL/TLS certificate.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a <GTlsCertificate>, or #f.

a <GTlsCertificate> or #f

Method: server:get-tls-database self ⇒ %return
Method: get-tls-database self ⇒ %return

Gets the self SSL/TLS database.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a <GTlsDatabase>, or #f.

a <GTlsDatabase>

Method: server:get-uris self ⇒ %return
Method: get-uris self ⇒ %return

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

These will contain IP addresses, not hostnames, and will also indicate whether the given listener is http or https.

Note that if you used Server.listen_all the returned URIs will use the addresses 0.0.0.0 and ::, rather than actually returning separate URIs for each interface on the system.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a list, each a <GUri>.

a list of <GUri>, which you must free with each element with GLib.Uri.unref when you are done with it.

Method: server:is-https? self ⇒ %return
Method: is-https? self ⇒ %return

Checks whether self is capable of https.

In order for a server to run https, you must call Server.set_tls_certificate, or set the Server:tls-certificate property, to provide it with a certificate to use.

If you are using the deprecated single-listener APIs, then a return value of #t indicates that the Server serves https exclusively. If you are using Server.listen, etc, then a #t return value merely indicates that the server is able to do https, regardless of whether it actually currently is or not. Use Server.get_uris to see if it currently has any https listeners.

self

Passed: a <SoupServer>.

a <SoupServer>

%return

Returned: a boolean.

#t if self is configured to serve https.

Method: server:listen-all? self port options ⇒ %return
Method: listen-all? self port options ⇒ %return

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

That is, it listens on the addresses 0.0.0.0 and/or ::, depending on whether options includes ipv4-only, ipv6-only, or neither.) If port is specified, self will listen on that port. If it is 0, self will find an unused port to listen on. (In that case, you can use Server.get_uris to find out what port it ended up choosing.

See Server.listen for more details.

self

Passed: a <SoupServer>.

a <SoupServer>

port

Passed: an unsigned 32-bit integer.

the port to listen on, or 0

options

Passed: a <SoupServerListenOptions>, or a list of its symbols, or one of them.

listening options for this server

%return

Returned: a boolean.

#t on success, #f if port could not be bound or any other error occurred (in which case error will be set).

Method: server:listen-local? self port options ⇒ %return
Method: listen-local? self port options ⇒ %return

Attempts to set up self 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). If port is specified, self will listen on that port. If it is 0, self will find an unused port to listen on. (In that case, you can use Server.get_uris to find out what port it ended up choosing.

See Server.listen for more details.

self

Passed: a <SoupServer>.

a <SoupServer>

port

Passed: an unsigned 32-bit integer.

the port to listen on, or 0

options

Passed: a <SoupServerListenOptions>, or a list of its symbols, or one of them.

listening options for this server

%return

Returned: a boolean.

#t on success, #f if port could not be bound or any other error occurred (in which case error will be set).

Method: server:listen-socket? self socket options ⇒ %return
Method: listen-socket? self socket options ⇒ %return

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

See Server.listen for more details.

self

Passed: a <SoupServer>.

a <SoupServer>

socket

Passed: a <GSocket>.

a listening <GSocket>

options

Passed: a <SoupServerListenOptions>, or a list of its symbols, or one of them.

listening options for this server

%return

Returned: a boolean.

#t on success, #f if an error occurred (in which case error will be set).

Method: server:listen? self address options ⇒ %return
Method: listen? self address options ⇒ %return

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

If options includes https, and self has been configured for TLS, then self will listen for https connections on this port. Otherwise it will listen for plain http.

You may call this method (along with the other "listen" methods) any number of times on a server, if you want to listen on multiple ports, or set up both http and https service.

After calling this method, self will begin accepting and processing connections as soon as the appropriate <GMainContext> is run.

Note that this API does not make use of dual IPv4/IPv6 sockets; if address is an IPv6 address, it will only accept IPv6 connections. You must configure IPv4 listening separately.

self

Passed: a <SoupServer>.

a <SoupServer>

address

Passed: a <GSocketAddress>.

the address of the interface to listen on

options

Passed: a <SoupServerListenOptions>, or a list of its symbols, or one of them.

listening options for this server

%return

Returned: a boolean.

#t on success, #f if address could not be bound or any other error occurred (in which case error will be set).

Method: server:pause-message self msg
Method: pause-message self 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 Server.unpause_message to resume I/O.

This must only be called on a ServerMessage which was created by the Server and are currently doing I/O, such as those passed into a ServerCallback or emitted in a "request-read" signal.

self

Passed: a <SoupServer>.

a <SoupServer>

msg

Passed: a <SoupServerMessage>.

a <SoupServerMessage> associated with self.

Method: server:remove-auth-domain self auth-domain
Method: remove-auth-domain self auth-domain

Removes auth-domain from self.

self

Passed: a <SoupServer>.

a <SoupServer>

auth-domain

Passed: a <SoupAuthDomain>.

a <SoupAuthDomain>

Method: server:remove-handler self path
Method: remove-handler self path

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

self

Passed: a <SoupServer>.

a <SoupServer>

path

Passed: a string.

the toplevel path for the handler

Method: server:remove-websocket-extension self extension-type
Method: remove-websocket-extension self extension-type

Removes support for WebSocket extension of type extension-type (or any subclass of extension-type) from self.

self

Passed: a <SoupServer>.

a <SoupServer>

extension-type

Passed: a class, or a GType number.

a GType

Method: server:set-tls-auth-mode self mode
Method: set-tls-auth-mode self mode

Sets self’s <GTlsAuthenticationMode> to use for SSL/TLS client authentication.

self

Passed: a <SoupServer>.

a <SoupServer>

mode

Passed: a <GTlsAuthenticationMode>, or one of its symbols.

a <GTlsAuthenticationMode>

Method: server:set-tls-certificate self certificate
Method: set-tls-certificate self certificate

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

self

Passed: a <SoupServer>.

a <SoupServer>

certificate

Passed: a <GTlsCertificate>.

a <GTlsCertificate>

Method: server:set-tls-database self tls-database
Method: set-tls-database self tls-database

Sets self’s <GTlsDatabase> to use for validating SSL/TLS client certificates.

self

Passed: a <SoupServer>.

a <SoupServer>

tls-database

Passed: a <GTlsDatabase>.

a <GTlsDatabase>

Method: server:unpause-message self msg
Method: unpause-message self msg

Resumes I/O on msg.

Use this to resume after calling Server.pause_message, or after adding a new chunk to a chunked response.

I/O won’t actually resume until you return to the main loop.

This must only be called on a ServerMessage which was created by the Server and are currently doing I/O, such as those passed into a ServerCallback or emitted in a "request-read" signal.

self

Passed: a <SoupServer>.

a <SoupServer>

msg

Passed: a <SoupServerMessage>.

a <SoupServerMessage> associated with self.

Signals

Signal: server:request-aborted

Connect a handler with (connect object server:request-aborted handler). It is called as (handler self message).

Emitted when processing has failed for a message.

This could mean either that it could not be read (if "request-read" has not been emitted for it yet), or that the response could not be written back (if "request-read" has been emitted but "request-finished" has not been).

message is in an undefined state when this signal is emitted; the signal exists primarily to allow the server to free any state that it may have allocated in "request-started".

self

Passed: a <SoupServer>.

message

Passed: a <SoupServerMessage>.

the message

Signal: server:request-finished

Connect a handler with (connect object server:request-finished handler). It is called as (handler self message).

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

self

Passed: a <SoupServer>.

message

Passed: a <SoupServerMessage>.

the message

Signal: server:request-read

Connect a handler with (connect object server:request-read handler). It is called as (handler self message).

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.

self

Passed: a <SoupServer>.

message

Passed: a <SoupServerMessage>.

the message

Signal: server:request-started

Connect a handler with (connect object server:request-started handler). It is called as (handler self message).

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.

If the request is read successfully, this will eventually be followed by a [signal@Server::request_read signal]. If a response is then sent, the request processing will end with a "request-finished" signal. If a network error occurs, the processing will instead end with "request-aborted".

self

Passed: a <SoupServer>.

message

Passed: a <SoupServerMessage>.

the new message

Virtual methods

Virtual method: server:do-request-aborted implementor self msg

To override it in a new class, give register-type #:vfuncs `((request-aborted . ,procedure)). The procedure is called as (procedure self msg).

implementor

Passed: the class whose implementation to call: <SoupServer>, or a class that derives from it, or its GType number.

self

Passed: a <SoupServer>.

msg

Passed: a <SoupServerMessage>.

Virtual method: server:do-request-finished implementor self msg

To override it in a new class, give register-type #:vfuncs `((request-finished . ,procedure)). The procedure is called as (procedure self msg).

implementor

Passed: the class whose implementation to call: <SoupServer>, or a class that derives from it, or its GType number.

self

Passed: a <SoupServer>.

msg

Passed: a <SoupServerMessage>.

Virtual method: server:do-request-read implementor self msg

To override it in a new class, give register-type #:vfuncs `((request-read . ,procedure)). The procedure is called as (procedure self msg).

implementor

Passed: the class whose implementation to call: <SoupServer>, or a class that derives from it, or its GType number.

self

Passed: a <SoupServer>.

msg

Passed: a <SoupServerMessage>.

Virtual method: server:do-request-started implementor self msg

To override it in a new class, give register-type #:vfuncs `((request-started . ,procedure)). The procedure is called as (procedure self msg).

implementor

Passed: the class whose implementation to call: <SoupServer>, or a class that derives from it, or its GType number.

self

Passed: a <SoupServer>.

msg

Passed: a <SoupServerMessage>.