mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-13 08:29:32 -05:00
Basic support to access libresapi via unix socket / windows named pipes
This commit is contained in:
parent
c59686ff3c
commit
47414acb62
74
libresapi/src/api/ApiServerLocal.cpp
Normal file
74
libresapi/src/api/ApiServerLocal.cpp
Normal file
@ -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
|
58
libresapi/src/api/ApiServerLocal.h
Normal file
58
libresapi/src/api/ApiServerLocal.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <QLocalServer>
|
||||
#include <QString>
|
||||
#include <QThread>
|
||||
#include <QLocalSocket>
|
||||
#include <retroshare/rsinit.h>
|
||||
#include <string>
|
||||
|
||||
#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
|
@ -193,6 +193,33 @@ public:
|
||||
// then each handler should pop the top element
|
||||
std::stack<std::string> 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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user