IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QHttpServer Class

QHttpServer is a simplified API for QAbstractHttpServer and QHttpServerRouter.

This class was introduced in Qt 6.4.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QHttpServer Class

  • Header: QHttpServer

  • Since: Qt 6.4

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS HttpServer)

    target_link_libraries(mytarget PRIVATE Qt6::HttpServer)

  • qmake: QT += httpserver

  • Inherits: QAbstractHttpServer

Detailed Description

 
Sélectionnez
QHttpServer server;

server.route("/", [] () {
    return "hello world";
});
server.listen();

Member Type Documentation

 

[alias] QHttpServer::MissingHandler

Type alias for std::function<void(const QHttpServerRequest &request, QHttpServerResponder &&responder)>.

Member Function Documentation

 

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

Creates an instance of QHttpServer with parent parent.

[virtual] QHttpServer::~QHttpServer()

Destroys a QHttpServer.

void QHttpServer::afterRequest(ViewHandler &&viewHandler)

Register a function to be run after each request.

The viewHandler argument can be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. The callable can take one or two optional arguments: QHttpServerResponse && and const QHttpServerRequest &. If both are given, they can be in either order.

Examples:

 
Sélectionnez
QHttpServer server;

// Valid:
server.afterRequest([] (QHttpServerResponse &amp;&amp;resp, const QHttpServerRequest &amp;request) {
    return std::move(resp);
}
server.afterRequest([] (const QHttpServerRequest &amp;request, QHttpServerResponse &amp;&amp;resp) {
    return std::move(resp);
}
server.afterRequest([] (QHttpServerResponse &amp;&amp;resp) { return std::move(resp); }

// Invalid (compile time error):
// resp must be passed by universal reference
server.afterRequest([] (QHttpServerResponse &amp;resp, const QHttpServerRequest &amp;request) {
    return std::move(resp);
}
// request must be passed by const reference
server.afterRequest([] (QHttpServerResponse &amp;&amp;resp, QHttpServerRequest &amp;request) {
    return std::move(resp);
}

bool QHttpServer::route(Args &&... args)

This function is just a wrapper to simplify the router API.

This function takes variadic arguments args. The last argument is a callback (ViewHandler). The remaining arguments are used to create a new Rule (the default is QHttpServerRouterRule). This is in turn added to the QHttpServerRouter. It returns true if a new rule is created, otherwise it returns false.

ViewHandler can be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. The callable can take two optional special arguments: const QHttpServerRequest& and QHttpServerResponder&&. These special arguments must be the last in the parameter list, but in any order, and there can be none, one, or both of them present.

Examples:

 
Sélectionnez
QHttpServer server;

// Valid:
server.route("test", [] (const int page) { return ""; });
server.route("test", [] (const int page, const QHttpServerRequest &amp;request) { return ""; });
server.route("test", [] (QHttpServerResponder &amp;&amp;responder) { return ""; });

// Invalid (compile time error):
server.route("test", [] (const QHttpServerRequest &amp;request, const int page) { return ""; }); // request must be last
server.route("test", [] (QHttpServerRequest &amp;request) { return ""; });      // request must be passed by const reference
server.route("test", [] (QHttpServerResponder &amp;responder) { return ""; });  // responder must be passed by universal reference

The request handler may return QFuture<QHttpServerResponse> if asynchronous processing is desired:

 
Sélectionnez
server.route("/feature/", [] (int id) {
    return QtConcurrent::run([] () {
        return QHttpServerResponse("the future is coming");
    });
});
See Also

QHttpServerRouter *QHttpServer::router()

Returns the router object.

void QHttpServer::setMissingHandler(QHttpServer::MissingHandler handler)

Set a handler to call for unhandled paths.

The invocable passed as handler will be invoked for each request that cannot be handled by any of registered route handlers. Passing a default-constructed std::function resets the handler to the default one that produces replies with status 404 Not Found.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+