QAbstractHttpServer Class | Qt HTTP Server | Qt 6.12.0
Description: API to subclass to implement an HTTP server.
Keywords: class

- Qt 6.12
- Qt HTTP Server
- C++ Classes
- QAbstractHttpServer
- Qt 6.12.0 Reference Documentation

# QAbstractHttpServer Class

API to subclass to implement an HTTP server. More...

Header: | `#include <QAbstractHttpServer>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS HttpServer)` `target_link_libraries(mytarget PRIVATE Qt6::HttpServer)`
qmake: | `QT += httpserver`
Since: | Qt 6.4
Inherits: | QObject
Inherited By: | QHttpServer

- List of all members, including inherited members

## Public Functions

| QAbstractHttpServer(QObject *parent = nullptr)
virtual | ~QAbstractHttpServer() override
`(since 6.8)` void | addWebSocketUpgradeVerifier(const QObject *context, Handler &&func)
bool | bind(QLocalServer *server)
bool | bind(QTcpServer *server)
`(since 6.9)` QHttpServerConfiguration | configuration() const
bool | hasPendingWebSocketConnections() const
`(since 6.8)` QHttp2Configuration | http2Configuration() const
QList<QLocalServer *> | localServers() const
std::unique_ptr<QWebSocket> | nextPendingWebSocketConnection()
QList<quint16> | serverPorts() const
QList<QTcpServer *> | servers() const
`(since 6.9)` void | setConfiguration(const QHttpServerConfiguration &config)
`(since 6.8)` void | setHttp2Configuration(const QHttp2Configuration &configuration)

## Signals

void | newWebSocketConnection()

## Protected Functions

virtual bool | handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) = 0
virtual void | missingHandler(const QHttpServerRequest &request, QHttpServerResponder &responder) = 0

## Detailed Description

Subclass this class and override handleRequest() and missingHandler() to create an HTTP server. Use bind() to start listening to all the incoming connections to a server.

This is a low level API, see QHttpServer for a higher level API to implement an HTTP server.

## Member Function Documentation

### `[explicit]` QAbstractHttpServer::QAbstractHttpServer(QObject *parent = nullptr)

Creates an instance of QAbstractHttpServer with the parent parent.

### `[override virtual noexcept]` QAbstractHttpServer::~QAbstractHttpServer()

Destroys an instance of QAbstractHttpServer.

### `[since 6.8]` template <typename Handler> void QAbstractHttpServer::addWebSocketUpgradeVerifier(const QObject *context, Handler &&func)

Adds a callback function func that verifies incoming WebSocket upgrades. It is called using the provided context object. An upgrade succeeds if at least one registered callback returns Accept and no prior callback has returned Deny. If no callbacks are registered, or all return PassToNext, the missingHandler() function is called. Callbacks are executed in the order they were registered, and they cannot call this function themselves.

Note: The func has to implement the signature `QHttpServerWebSocketUpgradeResponse (*)(const QHttpServerRequest &)`.

     server.addWebSocketUpgradeVerifier(
             &server, [](const QHttpServerRequest &request) {
                 if (request.url().path() == "/allowed"_L1)
                     return QHttpServerWebSocketUpgradeResponse::accept();
                 else
                     return QHttpServerWebSocketUpgradeResponse::passToNext();
             });

This function was introduced in Qt 6.8.

See also QHttpServerRequest, QHttpServerWebSocketUpgradeResponse, hasPendingWebSocketConnections(), nextPendingWebSocketConnection(), newWebSocketConnection(), and missingHandler().

### bool QAbstractHttpServer::bind(QLocalServer *server)

Bind the given QLocalServer server, over which the transmission happens, to the HTTP server. It is possible to call this function multiple times with different instances of server to handle multiple connections.

After calling this function, every new connection will be handled and forwarded by the HTTP server.

It is the user's responsibility to call QLocalServer::listen() on the server before calling this function. If server is not listening, nothing will happen and `false` will be returned.

If the server is nullptr false is returned.

If successful the server will be parented to this HTTP server and `true` is returned.

See also QLocalServer and QLocalServer::listen().

### bool QAbstractHttpServer::bind(QTcpServer *server)

Bind the given TCP server, over which the transmission happens, to the HTTP server. It is possible to call this function multiple times with different instances of TCP server to handle multiple connections and ports, for example both SSL and non-encrypted connections.

After calling this function, every new connection will be handled and forwarded by the HTTP server.

It is the user's responsibility to call QTcpServer::listen() on the server before calling this function. If server is not listening, nothing will happen and `false` will be returned.

If successful the server will be parented to this HTTP server and `true` is returned.

To allow usage of HTTP 2, bind to a QSslServer where QSslConfiguration::setAllowedNextProtocols() has been called with the arguments `{ QSslConfiguration::ALPNProtocolHTTP2 }`.

See also QTcpServer, QTcpServer::listen(), and QSslConfiguration::setAllowedNextProtocols().

### `[since 6.9]` QHttpServerConfiguration QAbstractHttpServer::configuration() const

Returns this server's general configuration parameters.

This function was introduced in Qt 6.9.

See also setConfiguration().

### `[pure virtual protected]` bool QAbstractHttpServer::handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder)

Override this function to handle each incoming request, by examining the request and sending the appropriate response back to responder. Return `true` if the request was handled successfully. If this method returns `false`, missingHandler() will be called afterwards.

### bool QAbstractHttpServer::hasPendingWebSocketConnections() const

Returns `true` if the server has pending WebSocket connections; otherwise returns `false`.

See also newWebSocketConnection(), nextPendingWebSocketConnection(), and addWebSocketUpgradeVerifier().

### `[since 6.8]` QHttp2Configuration QAbstractHttpServer::http2Configuration() const

Returns server's HTTP/2 configuration parameters.

This function was introduced in Qt 6.8.

See also setHttp2Configuration().

### QList<QLocalServer *> QAbstractHttpServer::localServers() const

Returns the local servers of this HTTP server.

See also serverPorts().

### `[pure virtual protected]` void QAbstractHttpServer::missingHandler(const QHttpServerRequest &request, QHttpServerResponder &responder)

Override this function to handle each incoming request that was not handled by handleRequest(). This function is called whenever handleRequest() returns `false`, or if there is a WebSocket upgrade attempt and either there are no connections to newWebSocketConnection() or there are no matching WebSocket verifiers. The request and responder parameters are the same as handleRequest() was called with.

See also handleRequest() and addWebSocketUpgradeVerifier().

### `[signal]` void QAbstractHttpServer::newWebSocketConnection()

This signal is emitted every time a new WebSocket connection is available.

See also hasPendingWebSocketConnections(), nextPendingWebSocketConnection(), and addWebSocketUpgradeVerifier().

### std::unique_ptr<QWebSocket> QAbstractHttpServer::nextPendingWebSocketConnection()

Returns the next pending connection as a connected QWebSocket object. `nullptr` is returned if this function is called when there are no pending connections.

Note: The returned QWebSocket object cannot be used from another thread.

See also newWebSocketConnection(), hasPendingWebSocketConnections(), and addWebSocketUpgradeVerifier().

### QList<quint16> QAbstractHttpServer::serverPorts() const

Returns the list of ports this instance of QAbstractHttpServer is listening to.

This function has the same guarantee as QObject::children, the latest server added is the last entry in the vector.

See also servers().

### QList<QTcpServer *> QAbstractHttpServer::servers() const

Returns the TCP and SSL servers this HTTP server will handle connections from.

See also serverPorts().

### `[since 6.9]` void QAbstractHttpServer::setConfiguration(const QHttpServerConfiguration &config)

Sets this server's general configuration parameters to config.

Note: The new configuration will be applied both to already established connections and all next connections.

This function was introduced in Qt 6.9.

See also configuration().

### `[since 6.8]` void QAbstractHttpServer::setHttp2Configuration(const QHttp2Configuration &configuration)

Sets server's HTTP/2 configuration parameters.

The next HTTP/2 connection will use the given configuration.

This function was introduced in Qt 6.8.

See also http2Configuration().