## SoupServer

SoupServer — HTTP server

## Functions

SoupServer * | soup_server_new ()
gboolean | soup_server_is_https ()
guint | soup_server_get_port ()
SoupSocket * | soup_server_get_listener ()
void | soup_server_run ()
void | soup_server_run_async ()
void | soup_server_quit ()
void | soup_server_disconnect ()
GMainContext * | soup_server_get_async_context ()
void | (*SoupServerCallback) ()
void | soup_server_add_handler ()
void | soup_server_remove_handler ()
SoupSocket * | soup_client_context_get_socket ()
SoupAddress * | soup_client_context_get_address ()
const char * | soup_client_context_get_host ()
SoupAuthDomain * | soup_client_context_get_auth_domain ()
const char * | soup_client_context_get_auth_user ()
void | soup_server_add_auth_domain ()
void | soup_server_remove_auth_domain ()
void | soup_server_pause_message ()
void | soup_server_unpause_message ()

## Properties

gpointer | async-context | Read / Write / Construct Only
GStrv | http-aliases | Read / Write
GStrv | https-aliases | Read / Write
SoupAddress * | interface | Read / Write / Construct Only
guint | port | Read / Write / Construct Only
gboolean | raw-paths | Read / Write / Construct Only
gchar * | server-header | Read / Write / Construct
gchar * | ssl-cert-file | Read / Write / Construct Only
gchar * | ssl-key-file | Read / Write / Construct Only
GTlsCertificate * | tls-certificate | Read / Write / Construct Only

## Signals

void | request-aborted | Run First
void | request-finished | Run First
void | request-read | Run First
void | request-started | Run First

## Types and Values

| SoupServer
typedef | SoupClientContext
#define | SOUP_SERVER_PORT
#define | SOUP_SERVER_INTERFACE
#define | SOUP_SERVER_SSL_CERT_FILE
#define | SOUP_SERVER_SSL_KEY_FILE
#define | SOUP_SERVER_TLS_CERTIFICATE
#define | SOUP_SERVER_ASYNC_CONTEXT
#define | SOUP_SERVER_RAW_PATHS
#define | SOUP_SERVER_SERVER_HEADER
#define | SOUP_SERVER_HTTP_ALIASES
#define | SOUP_SERVER_HTTPS_ALIASES

## Object Hierarchy

        GBoxed
        ╰── SoupClientContext
        GObject
        ╰── SoupServer

## Includes

    #include <libsoup/soup.h>

## Description

SoupServer implements a simple HTTP server.

To begin, create a server using `soup_server_new()`. Add at least one handler by calling `soup_server_add_handler()`; the handler will be called to process any requests underneath the path passed to `soup_server_add_handler()`. (If you want all requests to go to the same handler, just pass "/" (or `NULL`) for the path.) Any request that does not match any handler will automatically be returned to the client with a 404 (Not Found) status.

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.

To add authentication to some or all paths, create an appropriate SoupAuthDomain (qv), and add it to the server via `soup_server_add_auth_domain()`. (As with handlers, you must explicitly add "*" to an auth domain if you want it to be covered.)

Additional processing options are available via SoupServer's signals; Connect to “request-started” to be notified every time a new request is being processed. (This gives you a chance to connect to the SoupMessage "got-" signals in case you want to do processing before the body has been fully read.)

Once the server is set up, start it processing connections by calling `soup_server_run_async()` or `soup_server_run()`. SoupServer runs via the glib main loop; if you need to have a server that runs in another thread (or merely isn't bound to the default main loop), create a GMainContext for it to use, and set that via the SOUP_SERVER_ASYNC_CONTEXT property.

## Functions

### soup_server_new ()

    SoupServer *
    soup_server_new (const char *optname1,
                     ...);

Creates a new SoupServer.

#### Parameters

optname1 | name of first property to set |
... | value of `optname1` , followed by additional property/value pairs |

#### Returns

a new SoupServer

### soup_server_is_https ()

    gboolean
    soup_server_is_https (SoupServer *server);

Checks whether `server` is running plain http or https.

In order for a server to run https, you must set the `SOUP_SERVER_SSL_CERT_FILE` and `SOUP_SERVER_SSL_KEY_FILE` properties or `SOUP_SERVER_TLS_CERTIFICATE` property to provide it with an SSL certificate to use.

#### Parameters

server | a SoupServer |

#### Returns

`TRUE` if `server` is serving https.

### soup_server_get_port ()

    guint
    soup_server_get_port (SoupServer *server);

Gets the TCP port that `server` is listening on. This is most useful when you did not request a specific port (or explicitly requested `SOUP_ADDRESS_ANY_PORT`).

#### Parameters

server | a SoupServer |

#### Returns

the port `server` is listening on.

### soup_server_get_listener ()

    SoupSocket *
    soup_server_get_listener (SoupServer *server);

Gets `server` 's listening socket. You should treat this as read-only; writing to it or modifiying it may cause `server` to malfunction.

#### Parameters

server | a SoupServer |

#### Returns

the listening socket.

[transfer none]

### soup_server_run ()

    void
    soup_server_run (SoupServer *server);

Starts `server` , 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.

#### Parameters

server | a SoupServer |

### soup_server_run_async ()

    void
    soup_server_run_async (SoupServer *server);

Starts `server` , causing it to listen for and process incoming connections.

The server actually runs in `server` 's GMainContext. 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.

#### Parameters

server | a SoupServer |

### soup_server_quit ()

    void
    soup_server_quit (SoupServer *server);

Stops processing for `server` . Call this to clean up after `soup_server_run_async()`, or to terminate a call to `soup_server_run()`.

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

#### Parameters

server | a SoupServer |

### soup_server_disconnect ()

    void
    soup_server_disconnect (SoupServer *server);

Stops processing for `server` and closes its socket. This implies the effects of `soup_server_quit()`, but additionally closes the listening socket. Note that messages currently in progress will continue to be handled, if the main loop associated with the server is resumed or kept running.

After calling this function, `server` is no longer functional, so it has nearly the same effect as destroying `server` entirely. The function is thus useful mainly for language bindings without explicit control over object lifetime.

#### Parameters

server | a SoupServer |

### soup_server_get_async_context ()

    GMainContext *
    soup_server_get_async_context (SoupServer *server);

Gets `server` 's async_context. 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.

#### Parameters

server | a SoupServer |

#### Returns

`server` 's GMainContext, which may be `NULL`.

[transfer none]

### SoupServerCallback ()

    void
    (*SoupServerCallback) (SoupServer *server,
                           SoupMessage *msg,
                           const char *path,
                           GHashTable *query,
                           SoupClientContext *client,
                           gpointer user_data);

A callback used to handle requests to a SoupServer. The callback will be invoked after receiving the request body; `msg` 's “method”, “request_headers”, and “request_body” fields will be filled in.

`path` and `query` contain the likewise-named components of the Request-URI, subject to certain assumptions. By default, SoupServer decodes all percent-encoding in the URI path, such that "/foo%2Fbar" is treated the same as "/foo/bar". If your server is serving resources in some non-POSIX-filesystem namespace, you may want to distinguish those as two distinct paths. In that case, you can set the `SOUP_SERVER_RAW_PATHS` property when creating the SoupServer, and it will leave those characters undecoded. (You may want to call `soup_uri_normalize()` to decode any percent-encoded characters that you aren't handling specially.)

`query` contains the query component of the Request-URI parsed according to the rules for HTML form handling. Although this is the only commonly-used query string format in HTTP, there is nothing that actually requires that HTTP URIs use that format; if your server needs to use some other format, you can just ignore `query` , and call `soup_message_get_uri()` and parse the URI's query field yourself.

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 `msg` 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 SoupServer 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_message_headers_set_encoding()` to set `SOUP_ENCODING_CHUNKED` on the “response_headers”. Then call `soup_message_body_append()` (or `soup_message_body_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_message_body_complete()` to indicate that no more chunks are coming.

#### Parameters

server | the SoupServer |
msg | the message being processed |
path | the path component of `msg` 's Request-URI |
query | the parsed query component of `msg` 's Request-URI. | [element-type utf8 utf8][allow-none]
client | additional contextual information about the client |
user_data | the data passed to `soup_server_add_handler` |

### soup_server_add_handler ()

    void
    soup_server_add_handler (SoupServer *server,
                             const char *path,
                             SoupServerCallback callback,
                             gpointer user_data,
                             GDestroyNotify destroy);

Adds a handler to `server` for requests under `path` . See the documentation for SoupServerCallback for information about how callbacks should behave.

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.

#### Parameters

server | a SoupServer |
path | the toplevel path for the handler. | [allow-none]
callback | callback to invoke for requests under `path` |
user_data | data for `callback` |
destroy | destroy notifier to free `user_data` |

### soup_server_remove_handler ()

    void
    soup_server_remove_handler (SoupServer *server,
                                const char *path);

Removes the handler registered at `path` .

#### Parameters

server | a SoupServer |
path | the toplevel path for the handler |

### soup_client_context_get_socket ()

    SoupSocket *
    soup_client_context_get_socket (SoupClientContext *client);

Retrieves the SoupSocket that `client` is associated with.

If you are using this method to observe when multiple requests are made on the same persistent HTTP connection (eg, as the ntlm-test test program does), you will need to pay attention to socket destruction as well (either by using weak references, or by connecting to the “disconnected” signal), so that you do not get fooled when the allocator reuses the memory address of a previously-destroyed socket to represent a new socket.

#### Parameters

client | a SoupClientContext |

#### Returns

the SoupSocket that `client` is associated with.

[transfer none]

### soup_client_context_get_address ()

    SoupAddress *
    soup_client_context_get_address (SoupClientContext *client);

Retrieves the SoupAddress associated with the remote end of a connection.

#### Parameters

client | a SoupClientContext |

#### Returns

the SoupAddress associated with the remote end of a connection.

[transfer none]

### soup_client_context_get_host ()

    const char *
    soup_client_context_get_host (SoupClientContext *client);

Retrieves the IP address associated with the remote end of a connection. (If you want the actual hostname, you'll have to call `soup_client_context_get_address()` and then call the appropriate SoupAddress method to resolve it.)

#### Parameters

client | a SoupClientContext |

#### Returns

the IP address associated with the remote end of a connection.

### soup_client_context_get_auth_domain ()

    SoupAuthDomain *
    soup_client_context_get_auth_domain (SoupClientContext *client);

Checks whether the request associated with `client` has been authenticated, and if so returns the SoupAuthDomain that authenticated it.

#### Parameters

client | a SoupClientContext |

#### Returns

a SoupAuthDomain, or `NULL` if the request was not authenticated.

[transfer none][allow-none]

### soup_client_context_get_auth_user ()

    const char *
    soup_client_context_get_auth_user (SoupClientContext *client);

Checks whether the request associated with `client` has been authenticated, and if so returns the username that the client authenticated as.

#### Parameters

client | a SoupClientContext |

#### Returns

the authenticated-as user, or `NULL` if the request was not authenticated.

### soup_server_add_auth_domain ()

    void
    soup_server_add_auth_domain (SoupServer *server,
                                 SoupAuthDomain *auth_domain);

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.

#### Parameters

server | a SoupServer |
auth_domain | a SoupAuthDomain |

### soup_server_remove_auth_domain ()

    void
    soup_server_remove_auth_domain (SoupServer *server,
                                    SoupAuthDomain *auth_domain);

Removes `auth_domain` from `server` .

#### Parameters

server | a SoupServer |
auth_domain | a SoupAuthDomain |

### soup_server_pause_message ()

    void
    soup_server_pause_message (SoupServer *server,
                               SoupMessage *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.

This must only be called on SoupMessages which were created by the SoupServer and are currently doing I/O, such as those passed into a SoupServerCallback or emitted in a “request-read” signal.

#### Parameters

server | a SoupServer |
msg | a SoupMessage associated with `server` . |

### soup_server_unpause_message ()

    void
    soup_server_unpause_message (SoupServer *server,
                                 SoupMessage *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.

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

This must only be called on SoupMessages which were created by the SoupServer and are currently doing I/O, such as those passed into a SoupServerCallback or emitted in a “request-read” signal.

#### Parameters

server | a SoupServer |
msg | a SoupMessage associated with `server` . |

## Types and Values

### SoupServer

    typedef struct _SoupServer SoupServer;

### SoupClientContext

    typedef struct SoupClientContext SoupClientContext;

A SoupClientContext provides additional information about the client making a particular request. In particular, you can use `soup_client_context_get_auth_domain()` and `soup_client_context_get_auth_user()` to determine if HTTP authentication was used successfully.

soup_client_context_get_address() and/or `soup_client_context_get_host()` can be used to get information for logging or debugging purposes. `soup_client_context_get_socket()` may also be of use in some situations (eg, tracking when multiple requests are made on the same connection).

### SOUP_SERVER_PORT

    #define SOUP_SERVER_PORT            "port"

Alias for the “port” property. (The port the server listens on.)

### SOUP_SERVER_INTERFACE

    #define SOUP_SERVER_INTERFACE       "interface"

Alias for the “interface” property. (The address of the network interface the server listens on.)

### SOUP_SERVER_SSL_CERT_FILE

    #define SOUP_SERVER_SSL_CERT_FILE   "ssl-cert-file"

Alias for the “ssl-cert-file” property, qv.

### SOUP_SERVER_SSL_KEY_FILE

    #define SOUP_SERVER_SSL_KEY_FILE    "ssl-key-file"

Alias for the “ssl-key-file” property, qv.

### SOUP_SERVER_TLS_CERTIFICATE

    #define SOUP_SERVER_TLS_CERTIFICATE "tls-certificate"

Alias for the “tls-certificate” property, qv.

### SOUP_SERVER_ASYNC_CONTEXT

    #define SOUP_SERVER_ASYNC_CONTEXT   "async-context"

Alias for the “async-context” property. (The server's GMainContext.)

### SOUP_SERVER_RAW_PATHS

    #define SOUP_SERVER_RAW_PATHS       "raw-paths"

Alias for the “raw-paths” property. (If `TRUE`, percent-encoding in the Request-URI path will not be automatically decoded.)

### SOUP_SERVER_SERVER_HEADER

    #define SOUP_SERVER_SERVER_HEADER   "server-header"

Alias for the “server-header” property, qv.

### SOUP_SERVER_HTTP_ALIASES

    #define SOUP_SERVER_HTTP_ALIASES    "http-aliases"

### SOUP_SERVER_HTTPS_ALIASES

    #define SOUP_SERVER_HTTPS_ALIASES   "https-aliases"

Alias for the “https-aliases” property, qv.

Since 2.44

## Property Details

### The `“async-context”` property

      “async-context”            gpointer

The GMainContext to dispatch async I/O in.

Flags: Read / Write / Construct Only

### The `“http-aliases”` property

      “http-aliases”             GStrv

A `NULL`-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 “https-aliases”.

Flags: Read / Write

Since 2.44

### The `“https-aliases”` property

      “https-aliases”            GStrv

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

The default value is `NULL`, meaning that no URI schemes are considered aliases for "https".

Flags: Read / Write

Since 2.44

### The `“interface”` property

      “interface”                SoupAddress *

Address of interface to listen on.

Flags: Read / Write / Construct Only

### The `“port”` property

      “port”                     guint

Port to listen on.

Flags: Read / Write / Construct Only

Allowed values: <= 65536

Default value: 0

### The `“raw-paths”` property

      “raw-paths”                gboolean

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

Flags: Read / Write / Construct Only

Default value: FALSE

### The `“server-header”` property

      “server-header”            gchar *

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

Flags: Read / Write / Construct

Default value: NULL

### The `“ssl-cert-file”` property

      “ssl-cert-file”            gchar *

Path to a file containing a PEM-encoded certificate. If this and “ssl-key-file” are both set, then the server will speak https rather than plain http.

Alternatively, you can use “tls-certificate” to provide an arbitrary GTlsCertificate.

Flags: Read / Write / Construct Only

Default value: NULL

### The `“ssl-key-file”` property

      “ssl-key-file”             gchar *

Path to a file containing a PEM-encoded private key. If this and “ssl-key-file” are both set, then the server will speak https rather than plain http. Note that you are allowed to set them to the same value, if you have a single file containing both the certificate and the key.

Alternatively, you can use “tls-certificate” to provide an arbitrary GTlsCertificate.

Flags: Read / Write / Construct Only

Default value: NULL

### The `“tls-certificate”` property

      “tls-certificate”          GTlsCertificate *

A GTlsCertificate that has a “private-key” set. If this is set, then the server will speak https rather than plain http.

Alternatively, you can use “ssl-cert-file” and “ssl-key-file” properties, to have SoupServer read in a a certificate from a file.

Flags: Read / Write / Construct Only

## Signal Details

### The `“request-aborted”` signal

    void
    user_function (SoupServer        *server,
                   SoupMessage       *message,
                   SoupClientContext *client,
                   gpointer           user_data)

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

#### Parameters

server | the server |
message | the message |
client | the client context |
user_data | user data set when the signal handler was connected. |

Flags: Run First

### The `“request-finished”` signal

    void
    user_function (SoupServer        *server,
                   SoupMessage       *message,
                   SoupClientContext *client,
                   gpointer           user_data)

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

#### Parameters

server | the server |
message | the message |
client | the client context |
user_data | user data set when the signal handler was connected. |

Flags: Run First

### The `“request-read”` signal

    void
    user_function (SoupServer        *server,
                   SoupMessage       *message,
                   SoupClientContext *client,
                   gpointer           user_data)

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 handlers are called for the message, and if it sets the message's status_code, then normal handler processing will be skipped.

#### Parameters

server | the server |
message | the message |
client | the client context |
user_data | user data set when the signal handler was connected. |

Flags: Run First

### The `“request-started”` signal

    void
    user_function (SoupServer        *server,
                   SoupMessage       *message,
                   SoupClientContext *client,
                   gpointer           user_data)

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 “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”.

#### Parameters

server | the server |
message | the new message |
client | the client context |
user_data | user data set when the signal handler was connected. |

Flags: Run First

## See Also

SoupAuthDomain