Qt HTTP Server supports building HTTP server functionality into an application. Common use cases are exposing the application's functionality through REST APIs, or making devices in a trusted environment configurable also via HTTP. The limitations are described in 局限性和安全性 .
Qt HTTP Server provides building blocks for embedding a lightweight HTTP server based on RFC 2616 and RFC 9113 in an application. There are classes for the messages sent and received, and for the various parts of an HTTP server.
The QHttpServer 类拥有 route () function to bind callables to different incoming URLs. These callables can take as arguments an extensible collection of different copyable types that are parsed from the URL. Types supported are most numeric types, QString , QByteArray ,和 QUrl . Optionally the callables can also take QHttpServerRequest and QHttpServerResponder objects as arguments. The QHttpServerRequest class contains all the information of an incoming request, and is needed to get the body () from a POST HTTP request. The callables either return a QHttpServerResponse object or respond using the QHttpServerResponder 自变量。 QHttpServerResponse class contains a complete response and has numerous constructors for different types, while the QHttpServerResponder has various methods for writing back to the client.
The
QHttpServer
class also has an
addAfterRequestHandler
() function to process a
QHttpServerResponse
further, and a
setMissingHandler
() function to override the default behavior of returning
404 Not Found
when no routes are matched. From the
QAbstractHttpServer
class it inherits a
bind
() function to bind to a listening
QTcpServer
,
QSslServer
,或
QLocalServer
.
An HTTP server can also be created by subclassing the QAbstractHttpServer 类和覆写 handleRequest () 和 missingHandler () 函数。
Runtime logging can be configured as described here .
Qt HTTP Server does not have many of the more advanced features and optimizations that general-purpose HTTP servers have. It also has not seen the same scrutiny regarding various attack vectors over the network. Use Qt HTTP Server, therefore, only for local connections or in a trusted network, and do not expose the ports to the internet.
You can add HTTPS support as a basic security measure, though. If Qt is compiled with support for TLS, you can bind the HTTP server to a QSslServer object, providing Transport Layer Security handling.
The QSslSocket::SupportedFeature::ServerSideAlpn feature from the active TLS backend is needed for HTTP/2 support. To check if a backend supports this, use QSslSocket::isFeatureSupported .
使用 Qt 模块需要直接 (或透过其它依赖) 链接到模块库。一些构建工具对此有提供专门支持,包括 CMake 和 qmake。
使用
find_package()
命令以在 Qt6 包中定位所需的模块组件:
find_package(Qt6 REQUIRED COMPONENTS HttpServer) target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
另请参阅 构建采用 CMake 概述。
要配置采用 qmake 构建模块,把模块作为 Qt 变量的值添加到工程 .pro 文件中:
QT += httpserver
Qt HTTP Server is available under commercial licenses from Qt 公司 。此外,它是可用的根据 GNU GPL (一般公共许可) 第 3 版 。见 Qt 许可 进一步了解细节。
模块提供下列 范例 作为 API 使用指南。