QAbstractHttpServer Class

子类以实现 HTTP 服务器的 API。更多

头文件: #include <QAbstractHttpServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
Qt 6.4
继承 QObject
继承于

QHttpServer

公共函数

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)

信号

保护函数

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

详细说明

子类化该类并覆盖handleRequest() 和missingHandler() 以创建 HTTP 服务器。使用bind() 开始监听服务器的所有传入连接。

这是一个低级应用程序接口,请参阅QHttpServer 以获取实现 HTTP 服务器的高级应用程序接口。

成员函数文档

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

使用父parent 创建 QAbstractHttpServer 实例。

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

销毁QAbstractHttpServer 的一个实例。

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

添加一个回调函数func ,用于验证传入的 WebSocket 升级。该函数使用提供的context 对象调用。如果至少有一个已注册的回调函数返回Accept ,且之前没有回调函数返回Deny ,则升级成功。如果没有注册回调,或所有回调都返回PassToNext ,则调用missingHandler() 函数。回调将按照注册的顺序执行,它们本身不能调用此函数。

注意: func 必须实现签名QHttpServerWebSocketUpgradeResponse (*)(const QHttpServerRequest &)

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

该函数在 Qt 6.8 中引入。

另请参阅 QHttpServerRequest,QHttpServerWebSocketUpgradeResponse,hasPendingWebSocketConnections(),nextPendingWebSocketConnection(),newWebSocketConnection() 和missingHandler() 。

bool QAbstractHttpServer::bind(QLocalServer *server)

将给定的QLocalServer server (通过它进行传输)绑定到 HTTP 服务器。可以使用server 的不同实例多次调用此函数,以处理多个连接。

调用此函数后,HTTP 服务器将处理和转发每个连接。

用户有责任在调用此函数前在server 上调用QLocalServer::listen() 。如果server 没有监听,则不会发生任何事情,并将返回false

如果server 为 nullptr,则返回 false。

如果调用成功,server 将成为该 HTTP 服务器的父节点,并返回true

另请参阅 QLocalServerQLocalServer::listen()。

bool QAbstractHttpServer::bind(QTcpServer *server)

将指定的 TCPserver (传输通过该 TCP 进行)绑定到 HTTP 服务器。可以使用不同的 TCPserver 实例多次调用此函数,以处理多个连接和端口,例如 SSL 和非加密连接。

调用此函数后,HTTP 服务器将处理和转发每个连接。

用户有责任在调用此函数前在server 上调用QTcpServer::listen() 。如果server 没有监听,则不会发生任何事情,并将返回false

如果成功,server 将成为该 HTTP 服务器的父节点,并返回true

要使用 HTTP 2,请绑定到QSslServer ,其中QSslConfiguration::setAllowedNextProtocols() 已被调用,参数为{ QSslConfiguration::ALPNProtocolHTTP2 }

另请参阅 QTcpServerQTcpServer::listen() 和QSslConfiguration::setAllowedNextProtocols()。

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

返回此服务器的常规配置参数。

此函数在 Qt 6.9 中引入。

另请参阅 setConfiguration()。

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

重载此函数以处理每个传入的request ,方法是检查request 并将适当的响应发送回responder 。如果request 处理成功,则返回true 。如果此方法返回false ,则随后将调用missingHandler() 。

bool QAbstractHttpServer::hasPendingWebSocketConnections() const

如果服务器有待处理的 WebSocket 连接,则返回true ;否则返回false

另请参阅 newWebSocketConnection()、nextPendingWebSocketConnection() 和addWebSocketUpgradeVerifier()。

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

返回服务器的 HTTP/2 配置参数。

此函数在 Qt 6.8 中引入。

另请参阅 setHttp2Configuration().

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

返回此 HTTP 服务器的本地服务器。

另请参阅 serverPorts()。

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

重载此函数可处理handleRequest() 未处理的每个传入request 。每当handleRequest() 返回false ,或有 WebSocket 升级尝试,但没有连接到newWebSocketConnection() 或没有匹配的 WebSocket 校验器时,都会调用此函数。requestresponder 参数与调用handleRequest() 时使用的参数相同。

另请参阅 handleRequest() 和addWebSocketUpgradeVerifier()。

[signal] void QAbstractHttpServer::newWebSocketConnection()

每次有新的 WebSocket 连接时都会发出该信号。

另请参阅 hasPendingWebSocketConnections()、nextPendingWebSocketConnection() 和addWebSocketUpgradeVerifier()。

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

以已连接QWebSocket 对象的形式返回下一个待处理连接。如果在没有待处理连接时调用此函数,则返回nullptr

注意: 返回的QWebSocket 对象不能在其他线程中使用。

另请参阅 newWebSocketConnection()、hasPendingWebSocketConnections() 和addWebSocketUpgradeVerifier()。

QList<quint16> QAbstractHttpServer::serverPorts() const

返回QAbstractHttpServer 实例正在监听的端口列表。

此函数的保证与QObject::children 相同,最新添加的服务器是向量中的最后一个条目。

另请参见 servers()。

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

返回 HTTP 服务器将处理的 TCP 和 SSL 服务器连接。

另请参阅 serverPorts()。

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

将此服务器的常规配置参数设置为config

注: 新配置将应用于已建立的连接和所有下一个连接。

此函数在 Qt 6.9 中引入。

另请参阅 configuration() 。

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

设置服务器的 HTTP/2 配置参数。

下一个 HTTP/2 连接将使用给定的configuration

此函数在 Qt 6.8 中引入。

另请参阅 http2Configuration() 。

© 2025 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.