ServerMessage

class ServerMessage(**properties: Any)

Superclasses: Object

An HTTP server request and response pair.

A SoupServerMessage represents an HTTP message that is being sent or received on a Server.

Server will create SoupServerMessage’s automatically for incoming requests, which your application will receive via handlers.

Note that libsoup’s terminology here does not quite match the HTTP specification: in RFC 2616, an “HTTP-message” is either a Request, or a Response. In libsoup, a ServerMessage combines both the request and the response.

Methods

class ServerMessage
get_http_version() HTTPVersion

Get the HTTP version of msg.

get_local_address() SocketAddress | None

Retrieves the SocketAddress associated with the local end of a connection.

get_method() str

Get the HTTP method of msg.

get_reason_phrase() str | None

Get the HTTP reason phrase of msg.

get_remote_address() SocketAddress | None

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

get_remote_host() str | None

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

get_request_body() MessageBody

Get the request body of msg.

get_request_headers() MessageHeaders

Get the request headers of msg.

get_response_body() MessageBody

Get the response body of msg.

get_response_headers() MessageHeaders

Get the response headers of msg.

get_socket() Socket | None

Retrieves the Socket that msg 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 (eg, by using weak references), so that you do not get fooled when the allocator reuses the memory address of a previously-destroyed socket to represent a new socket.

get_status() int

Get the HTTP status code of msg.

get_tls_peer_certificate() TlsCertificate | None

Gets the peer’s TlsCertificate associated with msg’s connection. Note that this is not set yet during the emission of SoupServerMessage::accept-certificate signal.

Added in version 3.2.

get_tls_peer_certificate_errors() TlsCertificateFlags

Gets the errors associated with validating msg’s TLS peer certificate. Note that this is not set yet during the emission of SoupServerMessage::accept-certificate signal.

Added in version 3.2.

get_uri() Uri

Get msg’s URI.

is_options_ping() bool

Gets if msg represents an OPTIONS message with the path *.

pause() None

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 unpause to resume I/O.

Added in version 3.2.

set_http_version(version: HTTPVersion) None

Set the HTTP version of msg.

Parameters:

version – a HTTPVersion

set_redirect(status_code: int, redirect_uri: str) None

Sets msg’s status_code to status_code and adds a Location header pointing to redirect_uri. Use this from a Server when you want to redirect the client to another URI.

redirect_uri can be a relative URI, in which case it is interpreted relative to msg’s current URI. In particular, if redirect_uri is just a path, it will replace the path and query of msg’s URI.

Parameters:
  • status_code – a 3xx status code

  • redirect_uri – the URI to redirect msg to

set_response(content_type: str | None, resp_use: MemoryUse, resp_body: Sequence[int] | None = None) None

Convenience function to set the response body of a ServerMessage. If content_type is None, the response body must be empty as well.

Parameters:
  • content_type – MIME Content-Type of the body

  • resp_use – a MemoryUse describing how to handle resp_body

  • resp_body – a data buffer containing the body of the message response.

set_status(status_code: int, reason_phrase: str | None = None) None

Sets msg’s status code to status_code.

If status_code is a known value and reason_phrase is None, the reason_phrase will be set automatically.

Parameters:
  • status_code – an HTTP status code

  • reason_phrase – a reason phrase

unpause() None

Resumes I/O on msg.

Use this to resume after calling pause, or after adding a new chunk to a chunked response. I/O won’t actually resume until you return to the main loop.

Added in version 3.2.

Properties

class ServerMessage
props.tls_peer_certificate: TlsCertificate

The peer’s TlsCertificate associated with the message

Added in version 3.2.

props.tls_peer_certificate_errors: TlsCertificateFlags

The verification errors on ServerMessage:tls-peer-certificate

Added in version 3.2.

Signals

class ServerMessage.signals
accept_certificate(tls_peer_certificate: TlsCertificate, tls_peer_errors: TlsCertificateFlags) bool

Emitted during the msg’s connection TLS handshake after client TLS certificate has been received. You can return True to accept tls_certificate despite tls_errors.

Parameters:
  • tls_peer_certificate – the peer’s TlsCertificate

  • tls_peer_errors – the tls errors of tls_certificate

connected() None

Emitted when the msg’s socket is connected and the TLS handshake completed.

disconnected() None

Emitted when the msg’s socket is disconnected.

finished() None

Emitted when all HTTP processing is finished for a message. (After wrote_body).

got_body() None

Emitted after receiving the complete request body.

got_chunk(chunk: Bytes) None

Emitted after receiving a chunk of a message body.

Note that “chunk” in this context means any subpiece of the body, not necessarily the specific HTTP 1.1 chunks sent by the other side.

Parameters:

chunk – the just-read chunk

got_headers() None

Emitted after receiving the Request-Line and request headers.

wrote_body() None

Emitted immediately after writing the complete response body for a message.

wrote_body_data(chunk_size: int) None

Emitted immediately after writing a portion of the message body to the network.

Parameters:

chunk_size – the number of bytes written

wrote_chunk() None

Emitted immediately after writing a body chunk for a message.

Note that this signal is not parallel to got_chunk; it is emitted only when a complete chunk (added with append or append_bytes has been written. To get more useful continuous progress information, use wrote_body_data.

wrote_headers() None

Emitted immediately after writing the response headers for a message.

wrote_informational() None

Emitted immediately after writing a 1xx (Informational) response.