Merge pull request #436 from G10h4ck/libresapilocal

Libresapilocal
This commit is contained in:
Cyril Soler 2016-08-03 08:46:15 +02:00 committed by GitHub
commit 60cf2991d4
17 changed files with 623 additions and 384 deletions

View File

@ -0,0 +1,77 @@
#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)
{
sock->moveToThread(this);
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest()));
start();
}
void ApiLocalConnectionHandler::handleRequest()
{
char path[1024];
if(mLocalSocket->readLine(path, 1023) > 0)
{
reqPath = path;
char jsonData[20480];
if(mLocalSocket->read(jsonData, 20479) > 0)
{
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.c_str(), resultString.length());
mLocalSocket->write("\n\0");
quit();
}
else
{
mLocalSocket->write("\"{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for path.\\n\",\"returncode\":\"fail\"}\"\n\0");
quit();
}
}
else
{
mLocalSocket->write("{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for JSON data.\\n\",\"returncode\":\"fail\"}\"\n\0");
quit();
}
}
ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
{
mLocalSocket->flush();
mLocalSocket->close();
mLocalSocket->deleteLater();
}
} // namespace resource_api

View File

@ -0,0 +1,57 @@
#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();
public slots:
void handleRequest();
private:
ApiServer* mApiServer;
QLocalSocket* mLocalSocket;
std::string reqPath;
void _die();
};
} // namespace resource_api

View File

@ -559,7 +559,7 @@ int ApiServerMHD::accessHandlerCallback(MHD_Connection *connection,
return ((MHDHandlerBase*)(*con_cls))->handleRequest(connection, url, method, version, upload_data, upload_data_size);
}
// these characters are not allowe in the url, raise an error if they occur
// these characters are not allowed in the url, raise an error if they occur
// reason: don't want to serve files outside the current document root
const char *double_dots = "..";
if(strstr(url, double_dots))

View File

@ -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

View File

@ -8,10 +8,11 @@ TARGET = resapi
TARGET_PRL = libresapi
DESTDIR = lib
CONFIG += libmicrohttpd
INCLUDEPATH += ../../libretroshare/src
libresapihttpserver {
CONFIG += libmicrohttpd
unix {
webui_files.path = "$${DATA_DIR}/webui"
@ -106,7 +107,6 @@ win32{
system($$MAKE_SRC\\init.bat .)
}
libmicrohttpd{
linux {
CONFIG += link_pkgconfig
PKGCONFIG *= libmicrohttpd
@ -173,3 +173,10 @@ HEADERS += \
util/ContentTypes.h \
api/ApiPluginHandler.h \
api/ChannelsHandler.h
libresapilocalserver {
CONFIG *= qt
QT *= network
SOURCES *= api/ApiServerLocal.cpp
HEADERS *= api/ApiServerLocal.h
}

View File

@ -25,7 +25,7 @@
#include <stdio.h>
#ifndef WINDOWS_SYS
#if !defined(WINDOWS_SYS) && !defined(__ANDROID__)
#include <stdlib.h>
#include <execinfo.h>
@ -110,13 +110,13 @@ static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames
free(symbollist);
}
#else // WINDOWS_SYS
#else // !defined(WINDOWS_SYS) && !defined(__ANDROID__)
static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames = 63)
{
(void) max_frames;
fprintf(out, "TODO: 2016/01/01 print_stacktrace not implemented yet for WINDOWS_SYS\n");
fprintf(out, "TODO: 2016/01/01 print_stacktrace not implemented yet for WINDOWS_SYS and ANDROID\n");
}
#endif // WINDOWS_SYS
#endif // !defined(WINDOWS_SYS) && !defined(__ANDROID__)
#endif // _STACKTRACE_H_

View File

@ -29,7 +29,10 @@
#include <retroshare/rsdisc.h>
#include <retroshare/rspeers.h>
#include "settings/rsharesettings.h"
#ifdef ENABLE_WEBUI
#include <microhttpd.h>
#endif
#include <QClipboard>
#include <QSysInfo>
@ -781,11 +784,13 @@ void AboutDialog::on_copy_button_clicked()
RsControl::instance()->getLibraries(libraries);
verInfo+=addLibraries("libretroshare", libraries);
#ifdef ENABLE_WEBUI
/* Add version numbers of RetroShare */
// Add versions here. Find a better place.
libraries.clear();
libraries.push_back(RsLibraryInfo("Libmicrohttpd", MHD_get_version()));
verInfo+=addLibraries("RetroShare", libraries);
#endif // ENABLE_WEBUI
/* Add version numbers of plugins */
if (rsPlugins) {

View File

@ -24,7 +24,10 @@
#include <retroshare/rsiface.h>
#include <retroshare/rsplugin.h>
#ifdef ENABLE_WEBUI
#include <microhttpd.h>
#endif // ENABLE_WEBUI
#include <QFile>
#include <QTextStream>
@ -94,11 +97,13 @@ HelpDialog::HelpDialog(QWidget *parent) :
RsControl::instance()->getLibraries(libraries);
addLibraries(ui->libraryLayout, "libretroshare", libraries);
#ifdef ENABLE_WEBUI
/* Add version numbers of RetroShare */
// Add versions here. Find a better place.
libraries.clear();
libraries.push_back(RsLibraryInfo("Libmicrohttpd", MHD_get_version()));
addLibraries(ui->libraryLayout, "RetroShare", libraries);
#endif // ENABLE_WEBUI
/* Add version numbers of plugins */
if (rsPlugins) {

View File

@ -562,7 +562,9 @@ void MainWindow::createTrayIcon()
trayMenu->addSeparator();
trayMenu->addAction(QIcon(IMAGE_MESSENGER), tr("Open Messenger"), this, SLOT(showMessengerWindow()));
trayMenu->addAction(QIcon(IMAGE_MESSAGES), tr("Open Messages"), this, SLOT(showMess()));
#ifdef ENABLE_WEBUI
trayMenu->addAction(QIcon(":/images/emblem-web.png"), tr("Show web interface"), this, SLOT(showWebinterface()));
#endif // ENABLE_WEBUI
trayMenu->addAction(QIcon(IMAGE_BWGRAPH), tr("Bandwidth Graph"), _bandwidthGraph, SLOT(showWindow()));
trayMenu->addAction(QIcon(IMAGE_DHT), tr("Statistics"), this, SLOT(showStatisticsWindow()));
@ -1061,10 +1063,12 @@ void MainWindow::showStatisticsWindow()
StatisticsWindow::showYourself();
}
#ifdef ENABLE_WEBUI
void MainWindow::showWebinterface()
{
WebuiPage::showWebui();
}
#endif // ENABLE_WEBUI
/** Shows Application window */
#ifdef UNFINISHED

View File

@ -211,7 +211,9 @@ private slots:
void newRsCollection();
void showMessengerWindow();
void showStatisticsWindow();
#ifdef ENABLE_WEBUI
void showWebinterface();
#endif
//void servicePermission();
#ifdef UNFINISHED

View File

@ -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,10 @@
resource_api::ApiServer* WebuiPage::apiServer = 0;
resource_api::ApiServerMHD* WebuiPage::apiServerMHD = 0;
// TODO: LIBRESAPI_LOCAL_SERVER Put indipendent option for libresapilocalserver in appropriate place
#ifdef LIBRESAPI_LOCAL_SERVER
resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0;
#endif
resource_api::RsControlModule* WebuiPage::controlModule = 0;
WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
@ -92,6 +97,11 @@ QString WebuiPage::helpText() const
"",
Settings->getWebinterfaceAllowAllIps());
apiServerMHD->start();
// TODO: LIBRESAPI_LOCAL_SERVER Move in appropriate place
#ifdef LIBRESAPI_LOCAL_SERVER
apiServerLocal = new resource_api::ApiServerLocal(apiServer);
#endif
return ok;
}
@ -102,6 +112,11 @@ QString WebuiPage::helpText() const
apiServerMHD->stop();
delete apiServerMHD;
apiServerMHD = 0;
// TODO: LIBRESAPI_LOCAL_SERVER Move in appropriate place
#ifdef LIBRESAPI_LOCAL_SERVER
delete apiServerLocal;
apiServerLocal = 0;
#endif
delete apiServer;
apiServer = 0;
delete controlModule;

View File

@ -6,6 +6,7 @@
namespace resource_api{
class ApiServer;
class ApiServerMHD;
class ApiServerLocal;
class RsControlModule;
}
@ -48,5 +49,8 @@ private:
static resource_api::ApiServer* apiServer;
static resource_api::ApiServerMHD* apiServerMHD;
#ifdef LIBRESAPI_LOCAL_SERVER
static resource_api::ApiServerLocal* apiServerLocal;
#endif
static resource_api::RsControlModule* controlModule;
};

View File

@ -152,8 +152,9 @@ RSettingsWin::initStackedWidget()
addPage(new AppearancePage());
addPage(new SoundPage() );
addPage(new ServicePermissionsPage() );
#ifdef ENABLE_WEBUI
addPage(new WebuiPage() );
#endif // ENABLE_WEBUI
// add widgets from plugins
for(int i=0;i<rsPlugins->nbPlugins();++i)

View File

@ -397,13 +397,17 @@ feenableexcept(FE_INVALID | FE_DIVBYZERO);
notify->enable() ; // enable notification system after GUI creation, to avoid data races in Qt.
#ifdef ENABLE_WEBUI
WebuiPage::checkStartWebui();
#endif // ENABLE_WEBUI
/* dive into the endless loop */
int ti = rshare.exec();
delete w ;
#ifdef ENABLE_WEBUI
WebuiPage::checkShutdownWebui();
#endif // ENABLE_WEBUI
/* cleanup */
ChatDialog::cleanupChat();

View File

@ -551,7 +551,6 @@ HEADERS += rshare.h \
gui/connect/ConnectProgressDialog.h \
gui/groups/CreateGroup.h \
gui/GetStartedDialog.h \
gui/settings/WebuiPage.h \
gui/statistics/BWGraph.h \
util/RsSyntaxHighlighter.h \
util/imageutil.h
@ -671,7 +670,7 @@ FORMS += gui/StartDialog.ui \
gui/statistics/BwCtrlWindow.ui \
gui/statistics/RttStatistics.ui \
gui/GetStartedDialog.ui \
gui/settings/WebuiPage.ui
# gui/ForumsDialog.ui \
# gui/forums/CreateForum.ui \
@ -854,7 +853,6 @@ SOURCES += main.cpp \
gui/settings/ServicePermissionsPage.cpp \
gui/settings/AddFileAssociationDialog.cpp \
gui/settings/GroupFrameSettingsWidget.cpp \
gui/settings/WebuiPage.cpp \
gui/statusbar/peerstatus.cpp \
gui/statusbar/natstatus.cpp \
gui/statusbar/dhtstatus.cpp \
@ -1390,3 +1388,9 @@ gxsgui {
}
libresapihttpserver {
HEADERS *= gui/settings/WebuiPage.h
SOURCES *= gui/settings/WebuiPage.cpp
FORMS *= gui/settings/WebuiPage.ui
}

View File

@ -5,8 +5,6 @@ TARGET = RetroShare06-nogui
CONFIG += bitdht
#CONFIG += introserver
#CONFIG += sshserver
# webinterface, requires libmicrohttpd
CONFIG += webui
CONFIG -= qt xml gui
CONFIG += link_prl
@ -184,7 +182,7 @@ introserver {
DEFINES *= RS_INTRO_SERVER
}
webui {
libresapihttpserver {
DEFINES *= ENABLE_WEBUI
PRE_TARGETDEPS *= ../../libresapi/src/lib/libresapi.a
LIBS += ../../libresapi/src/lib/libresapi.a

View File

@ -1,3 +1,10 @@
# To {dis,en}able libresapi via local socket (unix domain socket or windows named pipes)
# {,un}comment the following line
#CONFIG *= libresapilocalserver
# To {dis,en}able libresapi via HTTP (libmicrohttpd) {,un}comment the following line
CONFIG *= libresapihttpserver
# Gxs is always enabled now.
DEFINES *= RS_ENABLE_GXS
@ -10,6 +17,25 @@ unix {
isEmpty(PLUGIN_DIR) { PLUGIN_DIR = "$${LIB_DIR}/retroshare/extensions6" }
}
android-g++ {
DEFINES *= NO_SQLCIPHER
DEFINES *= "fopen64=fopen"
DEFINES *= "fseeko64=fseeko"
DEFINES *= "ftello64=ftello"
INCLUDEPATH *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/include/
LIBS *= -L$$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/
LIBS *= -lssl -lcrypto -lsqlite3 -lupnp -lixml
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libcrypto.so
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libssl.so
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libbz2.so
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libsqlite3.so
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libupnp.so
ANDROID_EXTRA_LIBS *= $$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/libixml.so
message(NDK_TOOLCHAIN_PATH: $$NDK_TOOLCHAIN_PATH)
message(LIBS: $$LIBS)
message(ANDROID_EXTRA_LIBS: $$ANDROID_EXTRA_LIBS)
}
win32 {
message(***retroshare.pri:Win32)
exists($$PWD/../libs) {
@ -62,3 +88,6 @@ unfinished {
}
wikipoos:DEFINES *= RS_USE_WIKI
libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER
libresapihttpserver::DEFINES *= ENABLE_WEBUI