diff --git a/src/Mod/Web/App/AppWeb.cpp b/src/Mod/Web/App/AppWeb.cpp index f064885f29..bfb4d892bc 100644 --- a/src/Mod/Web/App/AppWeb.cpp +++ b/src/Mod/Web/App/AppWeb.cpp @@ -129,19 +129,12 @@ private: throw Py::OverflowError("port number is lower than 0"); } - QTcpServer server; + AppServer server(true); if (server.listen(QHostAddress(QString::fromLatin1(addr)), port)) { bool ok = server.waitForNewConnection(timeout); QTcpSocket* socket = server.nextPendingConnection(); if (socket) { socket->waitForReadyRead(); - if (socket->bytesAvailable()) { - QByteArray request = socket->readAll(); - std::string str = AppServer::runPython(request); - socket->write(str.c_str()); - socket->waitForBytesWritten(); - socket->close(); - } } server.close(); diff --git a/src/Mod/Web/App/Server.cpp b/src/Mod/Web/App/Server.cpp index 8e085da508..41a980a85f 100644 --- a/src/Mod/Web/App/Server.cpp +++ b/src/Mod/Web/App/Server.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "Server.h" #include @@ -109,8 +110,9 @@ const QByteArray& ServerEvent::request() const // ---------------------------------------------------------------------------- -AppServer::AppServer(QObject* parent) +AppServer::AppServer( bool direct, QObject* parent) : QTcpServer(parent) + , direct(direct) { PyObject* mod = PyImport_ImportModule("__main__"); if (mod) { @@ -127,6 +129,7 @@ void AppServer::incomingConnection(qintptr socket) connect(s, SIGNAL(readyRead()), this, SLOT(readClient())); connect(s, SIGNAL(disconnected()), this, SLOT(discardClient())); s->setSocketDescriptor(socket); + addPendingConnection(s); } void AppServer::readClient() @@ -134,7 +137,13 @@ void AppServer::readClient() QTcpSocket* socket = (QTcpSocket*)sender(); if (socket->bytesAvailable() > 0) { QByteArray request = socket->readAll(); - QCoreApplication::postEvent(this, new ServerEvent(socket, request)); + std::unique_ptr event(std::make_unique(socket, request)); + if (direct) { + customEvent(event.get()); + } + else { + QCoreApplication::postEvent(this, event.release()); + } } // if (socket->state() == QTcpSocket::UnconnectedState) { // //mark the socket for deletion but do not destroy immediately @@ -154,6 +163,15 @@ void AppServer::customEvent(QEvent* e) QByteArray msg = ev->request(); QTcpSocket* socket = ev->socket(); + std::string str = handleRequest(msg); + socket->write(str.c_str()); + if (direct) + socket->waitForBytesWritten(); + socket->close(); +} + +std::string AppServer::handleRequest(QByteArray msg) +{ std::string str; if (msg.startsWith("GET ")) { msg = QByteArray("GET = ") + msg.mid(4); @@ -166,8 +184,7 @@ void AppServer::customEvent(QEvent* e) str = runPython(msg); } - socket->write(str.c_str()); - socket->close(); + return str; } std::string AppServer::getRequest(const std::string& str) const diff --git a/src/Mod/Web/App/Server.h b/src/Mod/Web/App/Server.h index 8307b1eeaa..6dd913b1e8 100644 --- a/src/Mod/Web/App/Server.h +++ b/src/Mod/Web/App/Server.h @@ -82,13 +82,15 @@ class AppServer : public QTcpServer Q_OBJECT public: - AppServer(QObject* parent = 0); - static std::string runPython(const QByteArray&); - - void incomingConnection(qintptr socket); + AppServer(bool direct = false, QObject* parent = nullptr); protected: + void incomingConnection(qintptr socket); void customEvent(QEvent* e); + +private: + std::string handleRequest(QByteArray); + static std::string runPython(const QByteArray&); std::string getRequest(const std::string&) const; private Q_SLOTS: @@ -96,6 +98,7 @@ private Q_SLOTS: void discardClient(); private: + bool direct; Py::Object module; };