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() command to locate the needed module components in the Qt6 package:

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 使用指南。