Qt HTTP Server

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 Limitations & Security .

概述

Qt HTTP Server provides building blocks for embedding a lightweight HTTP server based on RFC 2616 in an application. There are classes for the messages sent and received, and for the various parts of an HTTP server.

An HTTP server can be created by subclassing the QAbstractHttpServer class and overriding the handleRequest () 函数。 QAbstractHttpServer class provides functions for listening to incoming ports or binding to an existing QTcpServer . Dispatching to callables based on incoming URLs can be simplified by using the QHttpServerRouter 类。

This can be simplified even further by using the QHttpServer 类。 QHttpServer 类是子类化的 QAbstractHttpServer and defines an overloaded route function to bind callables to different incoming URLs, and an afterRequest () function to process the response further.

Runtime logging can be configured as described here .

Limitations & Security

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 QAbstractHttpServer and its subclasses to a QSslServer object, providing Transport Layer Security handling. This can also be achieved by calling the QAbstractHttpServer::sslSetup () function before calling listen ().

使用模块

使用 Qt 模块需要直接 (或透过其它依赖) 链接到模块库。一些构建工具对此有提供专门支持,包括 CMake 和 qmake。

构建采用 CMake

使用 find_package() 命令以在 Qt6 包中定位所需的模块组件:

find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
					

另请参阅 构建采用 CMake 概述。

采用 qmake 构建

要配置采用 qmake 构建模块,把模块作为 Qt 变量的值添加到工程 .pro 文件中:

QT += httpserver
					

许可

Qt HTTP Server is available under commercial licenses from Qt 公司 。此外,它是可用的根据 GNU GPL (一般公共许可) 第 3 版 。见 Qt 许可 进一步了解细节。

参考

范例

模块提供下列 范例 作为 API 使用指南。