diff --git a/src/Mod/Web/App/Server.cpp b/src/Mod/Web/App/Server.cpp index 84b0fa11d6..8e085da508 100644 --- a/src/Mod/Web/App/Server.cpp +++ b/src/Mod/Web/App/Server.cpp @@ -112,6 +112,13 @@ const QByteArray& ServerEvent::request() const AppServer::AppServer(QObject* parent) : QTcpServer(parent) { + PyObject* mod = PyImport_ImportModule("__main__"); + if (mod) { + module = mod; + } + else { + throw Py::RuntimeError("Cannot load __main__ module"); + } } void AppServer::incomingConnection(qintptr socket) @@ -147,12 +154,35 @@ void AppServer::customEvent(QEvent* e) QByteArray msg = ev->request(); QTcpSocket* socket = ev->socket(); - std::string str = runPython(msg); + std::string str; + if (msg.startsWith("GET ")) { + msg = QByteArray("GET = ") + msg.mid(4); + str = runPython(msg); + if (str == "None") { + str = getRequest(str); + } + } + else { + str = runPython(msg); + } socket->write(str.c_str()); socket->close(); } +std::string AppServer::getRequest(const std::string& str) const +{ + try { + Base::PyGILStateLocker lock; + Py::Object attr = module.getAttr(std::string("GET")); + return attr.as_string(); + } + catch (Py::Exception &e) { + e.clear(); + return str; + } +} + std::string AppServer::runPython(const QByteArray& msg) { std::string str; diff --git a/src/Mod/Web/App/Server.h b/src/Mod/Web/App/Server.h index e03002051c..8307b1eeaa 100644 --- a/src/Mod/Web/App/Server.h +++ b/src/Mod/Web/App/Server.h @@ -89,12 +89,14 @@ public: protected: void customEvent(QEvent* e); + std::string getRequest(const std::string&) const; private Q_SLOTS: void readClient(); void discardClient(); private: + Py::Object module; }; }