QHttpServer is a simplified API for QAbstractHttpServer and QHttpServerRouter . 更多...
| 头: |
#include <QHttpServer>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
|
| qmake: |
QT += httpserver
|
| Since: | Qt 6.4 |
| 继承: | QAbstractHttpServer |
| QHttpServer (QObject * parent = nullptr) | |
| virtual | ~QHttpServer () override |
| void | addAfterRequestHandler (const QObject * receiver , Functor && slot ) |
| void | clearMissingHandler () |
| Rule * | route (const QString & pathPattern , QHttpServerRequest::Methods method , const QObject * receiver , Functor && slot ) |
| Rule * | route (const QString & pathPattern , Functor && handler ) |
| Rule * | route (const QString & pathPattern , QHttpServerRequest::Methods method , Functor && handler ) |
| Rule * | route (const QString & pathPattern , const QObject * receiver , Functor && slot ) |
| QHttpServerRouter * | router () |
| const QHttpServerRouter * | router () const |
| void | setMissingHandler (const QObject * receiver , Functor && slot ) |
QHttpServer allows to create a simple Http server by setting a range of request handlers.
The route function can be used to conveniently add rules to the servers QHttpServerRouter . To register a handler to be called after every request use addAfterRequestHandler and to register a handler for all unhandled requests use setMissingHandler .
Minimal example:
QHttpServer server; server.route("/", [] () { return "hello world"; }); auto tcpserver = new QTcpServer(); if (!tcpserver->listen() || !server.bind(tcpserver)) { delete tcpserver; return -1; } qDebug() << "Listening on port" << tcpserver->serverPort();
[explicit]
QHttpServer::
QHttpServer
(
QObject
*
parent
= nullptr)
Creates an instance of QHttpServer with parent parent .
[override virtual noexcept]
QHttpServer::
~QHttpServer
()
销毁 QHttpServer .
Register a receiver and slot to be called after every request is handled.
The
slot
has to implement the signature
void (*)(const QHttpServerRequest &, QHttpServerResponse &)
.
The slot can also be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. In that case the receiver will be a context object and the handler will be valid until the context object is destroyed.
范例:
QHttpServer server; server.addAfterRequestHandler(&server, [] (const QHttpServerRequest &req, QHttpServerResponse &resp) { resp.write(req.body(), "text/plain"_ba); }
注意:
These handlers won't be called for requests, processed by handlers with
QHttpServerResponder&
自变量。
Resets the handler to the default one that produces replies with status 404 Not Found.
This is a convenience method to add a new
Rule
to the server's
QHttpServerRouter
. The Rule template parameter can be any custom class derived from
QHttpServerRouterRule
.
This function takes a pathPattern 和 method that represent a set of requests and creates a new QHttpServerRouterRule (or custom Rule if specified in the template parameters) that forwards all respective requests to the provided receiver and slot . The rule is added to the router . For details on valid patterns in pathPattern ,见 QHttpServerRouterRule 文档编制。
slot can be a member function pointer of receiver . It can also be a function pointer, a non-mutable lambda, or any other copiable callable with const call operator. In that case receiver has to be a QObject pointer. The rule will be valid for the lifetime duration of the receiver . The receiver must share the same thread affinity as the QHttpServer for the registration to be successful and for the rule to be executed.
The slot can express its response with a return statement. The function has to return QHttpServerResponse or any type that can be converted to QHttpServerResponse . A large range of conversion constructors are available, see QHttpServerResponse .
QHttpServer server; server.route("/test", this, [] () { return ""; });
Alternatively, an optional last function argument
QHttpServerResponder&
can be provided on which the response has to be written. If the response is written to a
QHttpServerResponder&
the function must return
void
.
server.route("/test2", this, [] (QHttpServerResponder &responder) { responder.write(QHttpServerResponder::StatusCode::Forbidden); });
The slot can further have a
const QHttpServerRequest&
as a second to last parameter to get detailed information on the request
server.route("/test3", this, [] (const QHttpServerRequest &request, QHttpServerResponder &responder) { responder.write(request.body(), "text/plain"_ba);});
Finally, the callback can contain an arbitrary amount of copiable parameters that are registered with the QHttpServerRouter::converters . By default, these are most integer types, float, double, QString , QByteArray ,和 QUrl . Additional converters can be registered, see QHttpServerRouter::addConverter . These parameters must have a corresponding placeholder in the pathPattern . For details on placeholders and pathPattern see QHttpServerRouterRule .
QHttpServer server; server.route("/test/<arg>", this, [] (const int page) { return ""; });
This function returns, if successful, a pointer to the newly created Rule, otherwise a
nullptr
. The pointer can be used to set parameters on any custom
QHttpServerRouter
类:
auto rule = server.route<MyRule>("/test", this, [] () {return "";}); rule->setParameter("test");
. This function must not be called from any route callback.
注意:
If a request was processed by a handler accepting
QHttpServerResponder&
as an argument, none of the after request handlers (see
addAfterRequestHandler
) will be called.
Requests are processed sequentially inside the
QHttpServer
's thread by default. The request handler may return
QFuture<QHttpServerResponse>
if asynchronous processing is desired:
server.route("/feature/", [] (int id) { return QtConcurrent::run([] () { return QHttpServerResponse("the future is coming"); }); });
The body of
QFuture
is executed asynchronously, but all the network communication is executed sequentially in the thread the
QHttpServer
belongs to. The
QHttpServerResponder&
special argument is not available for routes returning a
QFuture
.
另请参阅 QHttpServerRouter::addRule and addAfterRequestHandler .
这是重载函数。
Overload of QHttpServer::route to create a Rule for pathPattern and QHttpServerRequest::Method::AnyKnown . All requests are forwarded to handler , which can be a function pointer, a non-mutable lambda, or any other copiable callable with const call operator. The rule will be valid until the QHttpServer 被销毁。
这是重载函数。
Overload of QHttpServer::route to create a Rule for pathPattern and method . All requests are forwarded to handler , which can be a function pointer, a non-mutable lambda, or any other copiable callable with const call operator. The rule will be valid until the QHttpServer 被销毁。
这是重载函数。
Overload of QHttpServer::route to create a Rule for pathPattern and QHttpServerRequest::Method::AnyKnown . All requests are forwarded to receiver and slot .
Returns a pointer to the router object.
Returns a pointer to the constant router object.
Set a handler for unhandled requests.
All unhandled requests will be forwarded to the receiver 's slot .
The
slot
has to implement the signature
void (*)(const QHttpServerRequest &, QHttpServerResponder &)
。
slot
can also be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. In that case the
receiver
will be a context object. The handler will be valid until the receiver object is destroyed.
The default handler replies with status 404: Not Found.