diff --git a/libresapi/src/api/ApiServerLocal.cpp b/libresapi/src/api/ApiServerLocal.cpp new file mode 100644 index 000000000..e951c2425 --- /dev/null +++ b/libresapi/src/api/ApiServerLocal.cpp @@ -0,0 +1,74 @@ +#include "ApiServerLocal.h" +#include "JsonStream.h" + +namespace resource_api{ + +ApiServerLocal::ApiServerLocal(ApiServer* server) : QThread(), + mApiServer(server), mLocalServer(this) +{ + start(); + mLocalServer.removeServer(serverName()); +#if QT_VERSION >= 0x050000 + mLocalServer.setSocketOptions(QLocalServer::UserAccessOption); +#endif + connect(&mLocalServer, SIGNAL(newConnection()), this, SLOT(handleConnection())); + mLocalServer.listen(serverName()); +} + +ApiServerLocal::~ApiServerLocal() +{ + mLocalServer.close(); + quit(); +} + +void ApiServerLocal::handleConnection() +{ + new ApiLocalConnectionHandler(mApiServer, mLocalServer.nextPendingConnection()); +} + +ApiLocalConnectionHandler::ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock) : + QThread(), mApiServer(apiServer), mLocalSocket(sock), mState(WAITING_PATH) +{ + sock->moveToThread(this); + connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit())); + connect(this, SIGNAL(finished()), this, SLOT(deleteLater())); + connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest())); + start(); +} + +void ApiLocalConnectionHandler::handleRequest() +{ + switch(mState) + { + case WAITING_PATH: + { + char path[1024]; + mLocalSocket->readLine(path, 1023); + reqPath = path; + mState = WAITING_DATA; + break; + } + case WAITING_DATA: + { + char jsonData[1024]; + mLocalSocket->read(jsonData, 1023); + resource_api::JsonStream reqJson; + reqJson.setJsonString(std::string(jsonData)); + resource_api::Request req(reqJson); + req.setPath(reqPath); + std::string resultString = mApiServer->handleRequest(req); + mLocalSocket->write(resultString.data(), resultString.length()); + mState = WAITING_PATH; + break; + } + } +} + +ApiLocalConnectionHandler::~ApiLocalConnectionHandler() +{ + mLocalSocket->close(); + delete mLocalSocket; +} + + +} // namespace resource_api diff --git a/libresapi/src/api/ApiServerLocal.h b/libresapi/src/api/ApiServerLocal.h new file mode 100644 index 000000000..3e8100453 --- /dev/null +++ b/libresapi/src/api/ApiServerLocal.h @@ -0,0 +1,58 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "ApiServer.h" + +namespace resource_api +{ +class ApiServer; + +class ApiServerLocal : public QThread +{ + Q_OBJECT + +public: + ApiServerLocal(ApiServer* server); + ~ApiServerLocal(); + +public slots: + void handleConnection(); + +private: + ApiServer* mApiServer; + QLocalServer mLocalServer; + + const static QString& serverName() + { + const static QString sockPath(RsAccounts::AccountDirectory() + .append("/libresapi.sock").c_str()); + return sockPath; + } +}; + +class ApiLocalConnectionHandler : public QThread +{ + Q_OBJECT + +public: + ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock); + ~ApiLocalConnectionHandler(); + enum State {WAITING_PATH, WAITING_DATA}; + +public slots: + void handleRequest(); + +private: + ApiServer* mApiServer; + QLocalSocket* mLocalSocket; + State mState; + std::string reqPath; +}; + +} // namespace resource_api diff --git a/libresapi/src/api/ApiTypes.h b/libresapi/src/api/ApiTypes.h index ab34b4507..f5ab6094b 100644 --- a/libresapi/src/api/ApiTypes.h +++ b/libresapi/src/api/ApiTypes.h @@ -193,6 +193,33 @@ public: // then each handler should pop the top element std::stack mPath; std::string mFullPath; + bool setPath(std::string reqPath) + { + std::string str; + for(std::string::reverse_iterator sit = reqPath.rbegin(); sit != reqPath.rend(); sit++) + { + if((*sit) != '/') + { + // add to front because we are traveling in reverse order + str = *sit + str; + } + else + { + if(str != "") + { + mPath.push(str); + str.clear(); + } + } + } + if(str != "") + { + mPath.push(str); + } + mFullPath = reqPath; + + return true; + } // parameters should be used to influence the result // for example include or exclude some information diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index f83996ee1..01dd68ffb 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -3,7 +3,8 @@ TEMPLATE = lib CONFIG += staticlib CONFIG += create_prl -CONFIG -= qt +CONFIG += qt +QT += network TARGET = resapi TARGET_PRL = libresapi DESTDIR = lib @@ -146,7 +147,8 @@ SOURCES += \ api/TmpBlobStore.cpp \ util/ContentTypes.cpp \ api/ApiPluginHandler.cpp \ - api/ChannelsHandler.cpp + api/ChannelsHandler.cpp \ + api/ApiServerLocal.cpp HEADERS += \ api/ApiServer.h \ @@ -172,4 +174,5 @@ HEADERS += \ api/TmpBlobStore.h \ util/ContentTypes.h \ api/ApiPluginHandler.h \ - api/ChannelsHandler.h + api/ChannelsHandler.h \ + api/ApiServerLocal.h diff --git a/retroshare-gui/src/gui/settings/WebuiPage.cpp b/retroshare-gui/src/gui/settings/WebuiPage.cpp index b05bf8698..9a9f98a8f 100644 --- a/retroshare-gui/src/gui/settings/WebuiPage.cpp +++ b/retroshare-gui/src/gui/settings/WebuiPage.cpp @@ -7,6 +7,7 @@ #include "api/ApiServer.h" #include "api/ApiServerMHD.h" +#include "api/ApiServerLocal.h" #include "api/RsControlModule.h" #include "api/GetPluginInterfaces.h" @@ -14,6 +15,7 @@ resource_api::ApiServer* WebuiPage::apiServer = 0; resource_api::ApiServerMHD* WebuiPage::apiServerMHD = 0; +resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0; resource_api::RsControlModule* WebuiPage::controlModule = 0; WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/) @@ -92,6 +94,9 @@ QString WebuiPage::helpText() const "", Settings->getWebinterfaceAllowAllIps()); apiServerMHD->start(); + + apiServerLocal = new resource_api::ApiServerLocal(apiServer); + return ok; } @@ -102,6 +107,8 @@ QString WebuiPage::helpText() const apiServerMHD->stop(); delete apiServerMHD; apiServerMHD = 0; + delete apiServerLocal; + apiServerLocal = 0; delete apiServer; apiServer = 0; delete controlModule; diff --git a/retroshare-gui/src/gui/settings/WebuiPage.h b/retroshare-gui/src/gui/settings/WebuiPage.h index 67e9b0e40..879d26ee8 100644 --- a/retroshare-gui/src/gui/settings/WebuiPage.h +++ b/retroshare-gui/src/gui/settings/WebuiPage.h @@ -6,6 +6,7 @@ namespace resource_api{ class ApiServer; class ApiServerMHD; + class ApiServerLocal; class RsControlModule; } @@ -48,5 +49,6 @@ private: static resource_api::ApiServer* apiServer; static resource_api::ApiServerMHD* apiServerMHD; + static resource_api::ApiServerLocal* apiServerLocal; static resource_api::RsControlModule* controlModule; };