Why there isn't QHttpServerRequest copy constructor?
-
I'm playing with QHttpServer.
I use asynchronous processing, so my request handler returns
QFuture<QHttpServerResponse>
.QFuture
is created byQtConcurrent::run
. ButQtConcurrent::run
creates a copy of its arguments. AndQHttpServerRequest
doesn't have copy constructor so I cannot use it as an argument.I came out with some workarounds to solve this, but they are not as nice as if
QHttpServerRequest
had copy constructor.Is there any reason why
QHttpServerRequest
isn't copy-constructible?There is a sample of the code to demonstrate, what I think:
// QT += httpserver concurrent #include <QCoreApplication> #include <QHttpServer> #include <QtConcurrent> QHttpServerResponse responder1() { return "Hello"; } QHttpServerResponse responder2(const QString & arg) { return "Hello with arg: " + arg; } QHttpServerResponse responder3(const QHttpServerRequest & request) { return "Hello to ip address: " + request.remoteAddress().toString(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QHttpServer server; // this works server.route("/r1", [](){return QtConcurrent::run(responder1);}); server.route("/r2/<arg>", [](const QString & arg){return QtConcurrent::run(responder2, arg);}); //this doesn't work server.route("/r3", [](const QHttpServerRequest & request){return QtConcurrent::run(responder3, request);}); server.listen(QHostAddress::Any, 2222); return a.exec(); }
This code uses simple functions to create responses but in my real code, I use
QMetaObject::invokeMethod
to move computation to a different thread. So I really need to useQtConcurrent::run
. -
Hi,
For the why it's not copyable, that's a question you should bring to the module developers.
That said, based on the examples of the module, it should work if you use a const reference to forward your request further.
-
@SGaist Thanks for your reply!
Contacting module developers seems like a good idea. That's why I write this post. I was hoping they read it.
How exactly should I use a const reference to forward your request further?
My functionresponder3
has a const reference argument. -
I somehow missed that.
Taking a look at things again, my understanding is that when calling
QtConcurrent::run
as you do, there is a mandatory copy happening which is what triggers your issue.However, you can "cheat" the system.
QtConcurrent::run
can also take a lambda so you could do something like the following:server.route("/r3", [] (const QHttpServerRequest & request) { auto lambda = [&request]() -> QHttpServerResponse { return responder3(request); }; return QtConcurrent::run(lambda); });
-
Hi
Like this you can skip the helper/workaround function:
server.route("/r2", [] (const QHttpServerRequest & request) { QFuture<QHttpServerResponse> future = QtConcurrent::run([&request] () { QString response = QString("Hello to ip address: %1\n").arg(request.remoteAddress().toString()); return QHttpServerResponse(response, QHttpServerResponder::StatusCode::Ok); }); return future; });