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:


Public Functions

QAbstractHttpServer(QObject *parent = nullptr)
void bind(QTcpServer *server = nullptr)
bool hasPendingWebSocketConnections() const
quint16 listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
std::unique_ptr<QWebSocket> nextPendingWebSocketConnection()
QList<quint16> serverPorts()
QList<QTcpServer *> servers() const
void sslSetup(const QSslCertificate &certificate, const QSslKey &privateKey, QSsl::SslProtocol protocol = QSsl::SecureProtocols)
void sslSetup(const QSslConfiguration &sslConfiguration)


void missingHandler(const QHttpServerRequest &request, QTcpSocket *socket)
void newWebSocketConnection()

Protected Functions

virtual bool handleRequest(const QHttpServerRequest &request, QTcpSocket *socket) = 0

Detailed Description

Subclass this class and override handleRequest() to create an HTTP server. Use listen() or bind() to start listening to incoming connections.

Member Function Documentation

QAbstractHttpServer::QAbstractHttpServer(QObject *parent = nullptr)

Creates an instance of QAbstractHttpServer with the parent parent.

[signal] void QAbstractHttpServer::missingHandler(const QHttpServerRequest &request, QTcpSocket *socket)

This signal is emitted whenever handleRequest() returns false. The request and socket parameters are the same as handleRequest() was called with.

[signal] void QAbstractHttpServer::newWebSocketConnection()

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

See also hasPendingWebSocketConnections() and nextPendingWebSocketConnection().

void QAbstractHttpServer::bind(QTcpServer *server = nullptr)

Bind the HTTP server to given TCP server over which the transmission happens. 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.

If the server is null, then a new, default-constructed TCP server will be constructed, which will be listening on a random port and all interfaces.

The server will be parented to this HTTP server.

See also QTcpServer and QTcpServer::listen().

[pure virtual protected] bool QAbstractHttpServer::handleRequest(const QHttpServerRequest &request, QTcpSocket *socket)

Overload this function to handle each incoming request from socket, by examining the request and sending the appropriate response back to socket. Returns true if the request was handled. Otherwise, returns false. If returning false, the missingHandler() signal will be emitted.

bool QAbstractHttpServer::hasPendingWebSocketConnections() const

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

See also newWebSocketConnection() and nextPendingWebSocketConnection().

quint16 QAbstractHttpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)

Tries to bind a QTcpServer to address and port.

Returns the server port upon success, 0 otherwise.

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() and hasPendingWebSocketConnections().

QList<quint16> QAbstractHttpServer::serverPorts()

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 list of child TCP servers of this HTTP server.

See also serverPorts().

void QAbstractHttpServer::sslSetup(const QSslCertificate &certificate, const QSslKey &privateKey, QSsl::SslProtocol protocol = QSsl::SecureProtocols)

Turns the server into an HTTPS server.

The next listen() call will use the given certificate, privateKey, and protocol.

void QAbstractHttpServer::sslSetup(const QSslConfiguration &sslConfiguration)

Turns the server into an HTTPS server.

The next listen() call will use the given sslConfiguration.

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.