Soup.Server

g GObject.Object GObject.Object Soup.Server Soup.Server GObject.Object->Soup.Server

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

accept_iostream (stream, local_addr, remote_addr)

add_auth_domain (auth_domain)

add_early_handler (path, callback, *user_data)

add_handler (path, callback, *user_data)

add_websocket_extension (extension_type)

add_websocket_handler (path, origin, protocols, callback, *user_data)

disconnect ()

get_async_context ()

get_listener ()

get_listeners ()

get_port ()

get_uris ()

is_https ()

listen (address, options)

listen_all (port, options)

listen_fd (fd, options)

listen_local (port, options)

listen_socket (socket, options)

pause_message (msg)

quit ()

remove_auth_domain (auth_domain)

remove_handler (path)

remove_websocket_extension (extension_type)

run ()

run_async ()

set_ssl_cert_file (ssl_cert_file, ssl_key_file)

unpause_message (msg)

Virtual Methods

Inherited:

GObject.Object (7)

do_request_aborted (msg, client)

do_request_finished (msg, client)

do_request_read (msg, client)

do_request_started (msg, client)

Properties

Name

Type

Flags

Short Description

async-context

int

d/r/w/co

The GLib.MainContext to dispatch async I/O in deprecated

http-aliases

[str]

r/w

URI schemes that are considered aliases for ‘http’

https-aliases

[str]

r/w

URI schemes that are considered aliases for ‘https’

interface

Soup.Address

d/r/w/co

Address of interface to listen on (Deprecated) deprecated

port

int

d/r/w/co

Port to listen on (Deprecated) deprecated

raw-paths

bool

r/w/co

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

server-header

str

r/w/c

Server header

ssl-cert-file

str

r/w/co

File containing server TLS (aka SSL) certificate deprecated

ssl-key-file

str

r/w/co

File containing server TLS (aka SSL) key deprecated

tls-certificate

Gio.TlsCertificate

r/w/co

Gio.TlsCertificate to use for https

Signals

Inherited:

GObject.Object (1)

Name

Short Description

request-aborted

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).

request-finished

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

request-read

Emitted when the server has successfully read a request.

request-started

Emitted when the server has started reading a new request.

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent

GObject.Object

r

Class Details

class Soup.Server(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

Soup.ServerClass

accept_iostream(stream, local_addr, remote_addr)
Parameters:
Raises:

GLib.Error

Returns:

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

Return type:

bool

Add a new client stream to the self.

New in version 2.50.

add_auth_domain(auth_domain)
Parameters:

auth_domain (Soup.AuthDomain) – a Soup.AuthDomain

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 “100-continue” Expectation, self will reject it before the request body is sent.

add_early_handler(path, callback, *user_data)
Parameters:
  • path (str or None) – the toplevel path for the handler

  • callback (Soup.ServerCallback) – callback to invoke for requests under path

  • user_data (object or None) – data for callback

Adds an “early” handler to self 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.)

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

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 Soup.MessageBody.set_accumulate() on the message’s Soup.Message :request-body to turn off request-body accumulation, and connect to the message’s Soup.Message ::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 Soup.Message ::got-body, or else you can register a non-early handler for path as well. As long as you have not set the Soup.Message :status-code by the time Soup.Message ::got-body is emitted, the non-early handler will be run as well.

New in version 2.50.

add_handler(path, callback, *user_data)
Parameters:
  • path (str or None) – the toplevel path for the handler

  • callback (Soup.ServerCallback) – callback to invoke for requests under path

  • user_data (object or None) – data for callback

Adds a handler to self for requests under path. If path is None 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 Soup.AuthDomain, an early #SoupServerHandler, or a signal handler), callback will be invoked after receiving the request body; the message’s Soup.Message :method, Soup.Message :request-headers, and Soup.Message :request-body fields will be filled in.

After determining what to do with the request, the callback must at a minimum call Soup.Message.set_status() (or Soup.Message.set_status_full()) 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 Soup.Server.pause_message() to tell self to not send the response right away. When the response is ready, call Soup.Server.unpause_message() to cause it to be sent.

To send the response body a bit at a time using “chunked” encoding, first call Soup.MessageHeaders.set_encoding() to set Soup.Encoding.CHUNKED on the Soup.Message :response-headers. Then call Soup.MessageBody.append() (or Soup.MessageBody.append_buffer()) to append each chunk as it becomes ready, and Soup.Server.unpause_message() 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 Soup.MessageBody.complete() to indicate that no more chunks are coming.

add_websocket_extension(extension_type)
Parameters:

extension_type (GObject.GType) – a GObject.GType

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

You can also add support for a WebSocket extension to the server at construct time by using the Soup.SERVER_ADD_WEBSOCKET_EXTENSION property. Note that Soup.WebsocketExtensionDeflate is supported by default, use Soup.Server.remove_websocket_extension() if you want to disable it.

New in version 2.68.

add_websocket_handler(path, origin, protocols, callback, *user_data)
Parameters:
  • path (str or None) – the toplevel path for the handler

  • origin (str or None) – the origin of the connection

  • protocols ([str] or None) – the protocols supported by this handler

  • callback (Soup.ServerWebsocketCallback) – callback to invoke for successful WebSocket requests under path

  • user_data (object or None) – data for callback

Adds a WebSocket handler to self for requests under path. (If path is None 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-None, then only requests containing a matching “Origin” header will be accepted. If protocols is non-None, 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 (possibly calling soup_server_check_websocket_handshake() one or more times), and setting a failure status code if the handshake should be rejected.

disconnect()

Closes and frees self's listening sockets. If you are using the old Soup.Server APIs, this also includes the effect of Soup.Server.quit().

Note that if there are currently requests in progress on self, that they will continue to be processed if self's GLib.MainContext is still running.

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

get_async_context()
Returns:

self's GLib.MainContext, which may be None

Return type:

GLib.MainContext or None

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

This does not add a ref to the context, so you will need to ref it yourself if you want it to outlive its server.

Deprecated since version ???: If you are using Soup.Server.listen(), etc, then the server listens on the thread-default GLib.MainContext, and this property is ignored.

get_listener()
Returns:

the listening socket.

Return type:

Soup.Socket

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

You should treat this socket as read-only; writing to it or modifiying it may cause self to malfunction.

Deprecated since version ???: If you are using Soup.Server.listen(), etc, then use Soup.Server.get_listeners() to get a list of all listening sockets, but note that that function returns Gio.Sockets, not Soup.Sockets.

get_listeners()
Returns:

a list of listening sockets.

Return type:

[Gio.Socket]

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.

(Beware that in contrast to the old Soup.Server.get_listener(), this function returns Gio.Sockets, not Soup.Sockets.)

get_port()
Returns:

the port self is listening on.

Return type:

int

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

Deprecated since version ???: If you are using Soup.Server.listen(), etc, then use Soup.Server.get_uris() to get a list of all listening addresses.

get_uris()
Returns:

a list of Soup.URIs, which you must free when you are done with it.

Return type:

[Soup.URI]

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 Soup.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.

New in version 2.48.

is_https()
Returns:

True if self is configured to serve https.

Return type:

bool

Checks whether self is capable of https.

In order for a server to run https, you must call Soup.Server.set_ssl_cert_file(), or set the Soup.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 True indicates that the Soup.Server serves https exclusively. If you are using Soup.Server.listen(), etc, then a True return value merely indicates that the server is able to do https, regardless of whether it actually currently is or not. Use Soup.Server.get_uris() to see if it currently has any https listeners.

listen(address, options)
Parameters:
Raises:

GLib.Error

Returns:

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

Return type:

bool

This attempts to set up self to listen for connections on address.

If options includes Soup.ServerListenOptions.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 GLib.MainContext is run.

Note that Soup.Server never makes use of dual IPv4/IPv6 sockets; if address is an IPv6 address, it will only accept IPv6 connections. You must configure IPv4 listening separately.

New in version 2.48.

listen_all(port, options)
Parameters:
Raises:

GLib.Error

Returns:

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

Return type:

bool

This 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 Soup.ServerListenOptions.IPV4_ONLY, Soup.ServerListenOptions.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 Soup.Server.get_uris() to find out what port it ended up choosing.)

See Soup.Server.listen() for more details.

New in version 2.48.

listen_fd(fd, options)
Parameters:
Raises:

GLib.Error

Returns:

True on success, False if an error occurred (in which case error will be set).

Return type:

bool

This attempts to set up self to listen for connections on fd.

See Soup.Server.listen() for more details.

Note that self will close fd when you free it or call Soup.Server.disconnect().

New in version 2.48.

listen_local(port, options)
Parameters:
Raises:

GLib.Error

Returns:

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

Return type:

bool

This 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 Soup.ServerListenOptions.IPV4_ONLY, Soup.ServerListenOptions.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 Soup.Server.get_uris() to find out what port it ended up choosing.)

See Soup.Server.listen() for more details.

New in version 2.48.

listen_socket(socket, options)
Parameters:
Raises:

GLib.Error

Returns:

True on success, False if an error occurred (in which case error will be set).

Return type:

bool

This attempts to set up self to listen for connections on socket.

See Soup.Server.listen() for more details.

New in version 2.48.

pause_message(msg)
Parameters:

msg (Soup.Message) – a Soup.Message associated with self.

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.

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

quit()

Stops processing for self, 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().

Note that messages currently in progress will continue to be handled, if the main loop associated with the server is resumed or kept running.

self is still in a working state after this call; you can start and stop a server as many times as you want.

Deprecated since version ???: When using Soup.Server.listen(), etc, the server will always listen for connections, and will process them whenever the thread-default GLib.MainContext is running.

remove_auth_domain(auth_domain)
Parameters:

auth_domain (Soup.AuthDomain) – a Soup.AuthDomain

Removes auth_domain from self.

remove_handler(path)
Parameters:

path (str) – the toplevel path for the handler

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

remove_websocket_extension(extension_type)
Parameters:

extension_type (GObject.GType) – a GObject.GType

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

New in version 2.68.

run()

Starts self, if you are using the old API, causing it to listen for and process incoming connections. Unlike Soup.Server.run_async(), this creates a GLib.MainLoop and runs it, and it will not return until someone calls Soup.Server.quit() to stop the server.

Deprecated since version ???: When using Soup.Server.listen(), etc, the server will always listen for connections, and will process them whenever the thread-default GLib.MainContext is running.

run_async()

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

The server runs in self's GLib.MainContext. It will not actually perform any processing unless the appropriate main loop is running. In the simple case where you did not set the server’s Soup.SERVER_ASYNC_CONTEXT property, this means the server will run whenever the glib main loop is running.

Deprecated since version ???: When using Soup.Server.listen(), etc, the server will always listen for connections, and will process them whenever the thread-default GLib.MainContext is running.

set_ssl_cert_file(ssl_cert_file, ssl_key_file)
Parameters:
  • ssl_cert_file (str) – path to a file containing a PEM-encoded SSL/TLS certificate.

  • ssl_key_file (str) – path to a file containing a PEM-encoded private key.

Raises:

GLib.Error

Returns:

success or failure.

Return type:

bool

Sets self 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).

Alternatively, you can set the Soup.Server :tls-certificate property at construction time, if you already have a Gio.TlsCertificate.

New in version 2.48.

unpause_message(msg)
Parameters:

msg (Soup.Message) – a Soup.Message associated with self.

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.

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

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

do_request_aborted(msg, client) virtual
Parameters:
do_request_finished(msg, client) virtual
Parameters:
do_request_read(msg, client) virtual
Parameters:
do_request_started(msg, client) virtual
Parameters:

Signal Details

Soup.Server.signals.request_aborted(server, message, client)
Signal Name:

request-aborted

Flags:

RUN_FIRST

Parameters:

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).

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 #SoupServer::request_started.

Soup.Server.signals.request_finished(server, message, client)
Signal Name:

request-finished

Flags:

RUN_FIRST

Parameters:

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

Soup.Server.signals.request_read(server, message, client)
Signal Name:

request-read

Flags:

RUN_FIRST

Parameters:

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.

Soup.Server.signals.request_started(server, message, client)
Signal Name:

request-started

Flags:

RUN_FIRST

Parameters:

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 #SoupServer::request_read signal. If a response is then sent, the request processing will end with a #SoupServer::request_finished signal. If a network error occurs, the processing will instead end with #SoupServer::request_aborted.

Property Details

Soup.Server.props.async_context
Name:

async-context

Type:

int

Default Value:

None

Flags:

DEPRECATED, READABLE, WRITABLE, CONSTRUCT_ONLY

The server’s GLib.MainContext, if you are using the old API. Servers created using Soup.Server.listen() will listen on the GLib.MainContext that was the thread-default context at the time Soup.Server.listen() was called.

Deprecated since version ???: The new API uses the thread-default GLib.MainContext rather than having an explicitly-specified one.

Soup.Server.props.http_aliases
Name:

http-aliases

Type:

[str]

Default Value:

[]

Flags:

READABLE, WRITABLE

A None-terminated array of URI schemes that should be considered to be aliases for “http”. Eg, if this included "dav", than a URI of dav://example.com/path would be treated identically to http://example.com/path. In particular, this is needed in cases where a client sends requests with absolute URIs, where those URIs do not use “http:”.

The default value is an array containing the single element "*", a special value which means that any scheme except “https” is considered to be an alias for “http”.

See also Soup.Server :https-aliases.

New in version 2.44.

Soup.Server.props.https_aliases
Name:

https-aliases

Type:

[str]

Default Value:

[]

Flags:

READABLE, WRITABLE

A comma-delimited list of URI schemes that should be considered to be aliases for “https”. See Soup.Server :http-aliases for more information.

The default value is None, meaning that no URI schemes are considered aliases for “https”.

New in version 2.44.

Soup.Server.props.interface
Name:

interface

Type:

Soup.Address

Default Value:

None

Flags:

DEPRECATED, READABLE, WRITABLE, CONSTRUCT_ONLY

The address of the network interface the server is listening on, if you are using the old Soup.Server API. (This will not be set if you use Soup.Server.listen(), etc.)

Deprecated since version ???: Soup.Servers can listen on multiple interfaces at once now. Use Soup.Server.listen(), etc, to listen on an interface, and Soup.Server.get_uris() to see what addresses are being listened on.

Soup.Server.props.port
Name:

port

Type:

int

Default Value:

0

Flags:

DEPRECATED, READABLE, WRITABLE, CONSTRUCT_ONLY

The port the server is listening on, if you are using the old Soup.Server API. (This will not be set if you use Soup.Server.listen(), etc.)

Deprecated since version ???: Soup.Servers can listen on multiple interfaces at once now. Use Soup.Server.listen(), etc, to listen on a port, and Soup.Server.get_uris() to see what ports are being listened on.

Soup.Server.props.raw_paths
Name:

raw-paths

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

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

Soup.Server.props.server_header
Name:

server-header

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT

If non-None, the value to use for the “Server” header on Soup.Message s 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 #SoupSession:user_agent, if you set a #SoupServer:server_header property that has trailing whitespace, Soup.Server will append its own product token (eg, “libsoup/2.3.2”) to the end of the header for you.

Soup.Server.props.ssl_cert_file
Name:

ssl-cert-file

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

Path to a file containing a PEM-encoded certificate.

If you set this property and Soup.Server :ssl-key-file at construct time, then soup_server_new() will try to read the files; if it cannot, it will return None, with no explicit indication of what went wrong (and logging a warning with newer versions of glib, since returning None from a constructor is illegal).

Deprecated since version ???: use Soup.Server :tls-certificate or soup_server_set_ssl_certificate().

Soup.Server.props.ssl_key_file
Name:

ssl-key-file

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

Path to a file containing a PEM-encoded private key. See Soup.Server :ssl-cert-file for more information about how this is used.

Deprecated since version ???: use Soup.Server :tls-certificate or soup_server_set_ssl_certificate().

Soup.Server.props.tls_certificate
Name:

tls-certificate

Type:

Gio.TlsCertificate

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

A Gio.TlsCertificate that has a Gio.TlsCertificate :private-key set. If this is set, then the server will be able to speak https in addition to (or instead of) plain http.

Alternatively, you can call Soup.Server.set_ssl_cert_file() to have Soup.Server read in a a certificate from a file.

New in version 2.38.