Prepare for merging

This commit is contained in:
Gioacchino Mazzurco 2019-11-27 18:44:10 +01:00
parent b1860d8682
commit 1d4ca64dee
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
27 changed files with 1442 additions and 992 deletions

View file

@ -91,7 +91,7 @@ void RsServer::rsGlobalShutDown()
mNetMgr->shutdown(); /* Handles UPnP */
#ifdef RS_JSONAPI
rsJsonAPI->stop();
rsJsonApi->fullstop();
#endif
rsAutoProxyMonitor::instance()->stopAllRSShutdown();

View file

@ -34,7 +34,6 @@
#include "retroshare/rsiface.h"
#include "retroshare/rstypes.h"
#include "util/rsthreads.h"
#include "jsonapi/jsonapi.h"
#include "chat/p3chatservice.h"
#include "gxstunnel/p3gxstunnel.h"
@ -141,7 +140,7 @@ public:
private:
std::string getSQLCipherVersion();
std::string getSQLCipherVersion(); // TODO: move to rsversion.h
// The real Server Parts.
@ -157,9 +156,6 @@ public:
RsPluginManager *mPluginsManager;
//sslroot *sslr;
JsonApiServer *mJsonAPIServer;
/* services */
p3heartbeat *mHeart;
p3discovery2 *mDisc;

View file

@ -6,16 +6,15 @@
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* it under the terms of the GNU Lesser General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
@ -28,26 +27,29 @@
#include <memory>
#include <chrono>
#include <cstdlib>
#include "util/rsthreads.h"
#include "util/rsdebug.h"
#include "retroshare/rswebui.h"
#include "rsserver/rsaccounts.h"
#include "retroshare/rsjsonapi.h"
enum {
TEXT_HTML = 0,
TEXT_CSS = 1,
TEXT_SVG = 2,
TEXT_TTF = 3,
TEXT_WOFF = 4,
APPLICATION_OCTET_STREAM = 5
};
#define DEBUG_RS_WEBUI 1
RsWebUI *rsWebUI = new p3WebUI;
RsWebUi *rsWebUI = new p3WebUI;
static const std::string mime_types[6] = {
enum MimeTypeIndex
{
TEXT_HTML,
TEXT_CSS,
TEXT_SVG,
TEXT_TTF,
TEXT_WOFF,
APPLICATION_OCTET_STREAM,
};
static const constexpr char* const mime_types[] =
{
"text/html",
"text/css",
"image/svg+xml",
@ -56,59 +58,59 @@ static const std::string mime_types[6] = {
"application/octet-stream",
};
const std::string RsWebUI::DEFAULT_BASE_DIRECTORY = RsAccountsDetail::PathDataDirectory(false); //"/usr/share/retroshare/webui/";
static std::string _base_directory = RsWebUI::DEFAULT_BASE_DIRECTORY;
const std::string RsWebUi::DEFAULT_BASE_DIRECTORY =
RsAccountsDetail::PathDataDirectory(false) + "/webui/";
static std::string _base_directory = RsWebUi::DEFAULT_BASE_DIRECTORY;
template<int MIME_TYPE_INDEX> class handler
template<MimeTypeIndex MIME_TYPE_INDEX> class handler
{
public:
static void get_handler( const std::shared_ptr< restbed::Session > session )
public:
static void get_handler( const std::shared_ptr< restbed::Session > session )
{
const auto request = session->get_request( );
const std::string filename = request->get_path_parameter( "filename" );
std::string directory = request->get_path_parameter( "dir" );
if(!directory.empty()) directory += "/";
std::string resource_filename = _base_directory + "/" + directory + filename;
RsDbg() << "Reading file: \"" << resource_filename << "\"" << std::endl;
std::ifstream stream( resource_filename, std::ifstream::in );
if(stream.is_open())
{
const auto request = session->get_request( );
const std::string filename = request->get_path_parameter( "filename" );
std::string directory = request->get_path_parameter( "dir" );
const std::string body = std::string(
std::istreambuf_iterator<char>(stream),
std::istreambuf_iterator<char>() );
if(!directory.empty())
directory += "/";
RsDbg() << __PRETTY_FUNCTION__
<< " body length=" << body.length() << std::endl;
std::string resource_filename = _base_directory + "/" + directory + filename;
RsDbg() << "Reading file: \"" << resource_filename << "\"" << std::endl;
std::ifstream stream( resource_filename, std::ifstream::in );
if ( stream.is_open( ) )
const std::multimap<std::string, std::string> headers
{
const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) );
{ "Content-Type", mime_types[MIME_TYPE_INDEX] },
{ "Content-Length", std::to_string(body.length()) }
};
RsDbg() << " body length=" << body.length() << std::endl;
const std::multimap< std::string, std::string > headers
{
{ "Content-Type", mime_types[MIME_TYPE_INDEX] },
{ "Content-Length", std::to_string( body.length( ) ) }
};
session->close( restbed::OK, body, headers );
}
else
{
RsErr() << "Could not open file " << resource_filename << std::endl;
session->close( restbed::NOT_FOUND );
}
session->close(restbed::OK, body, headers);
}
else
{
RsErr() << __PRETTY_FUNCTION__ << "Could not open file: "
<< resource_filename << std::endl;
session->close(restbed::NOT_FOUND);
}
}
};
static void service_ready_handler( restbed::Service& )
std::vector< std::shared_ptr<restbed::Resource> > p3WebUI::getResources() const
{
fprintf( stderr, "Hey! The service is up and running." );
}
static std::vector< std::shared_ptr<restbed::Resource> > rtab;
std::vector<std::shared_ptr<restbed::Resource> > p3WebUI::getResources() const
{
static std::vector<std::shared_ptr<restbed::Resource> > rtab;
if(rtab.empty())
if(rtab.empty())
{
auto resource1 = std::make_shared< restbed::Resource >( );
auto resource1 = std::make_shared< restbed::Resource >();
resource1->set_paths( {
"/{filename: index.html}",
"/{filename: app.js}",
@ -167,37 +169,29 @@ void p3WebUI::setHtmlFilesDirectory(const std::string& html_dir)
_base_directory = html_dir;
}
int p3WebUI::status() const
{
if(rsJsonAPI->isRunning() && rsJsonAPI->hasResourceProvider(this))
return WEBUI_STATUS_RUNNING;
else
return WEBUI_STATUS_NOT_RUNNING;
}
bool p3WebUI::isRunning() const
{ return rsJsonApi->isRunning() && rsJsonApi->hasResourceProvider(*this); }
void p3WebUI::setUserPassword(const std::string& passwd)
{
#ifdef RS_JSONAPI
std::cerr << "Updating webui token with new passwd \"" << passwd << "\"" << std::endl;
RsDbg() << __PRETTY_FUNCTION__ << " Updating webui token with new passwd \""
<< passwd << "\"" << std::endl;
if(!rsJsonAPI->authorizeUser("webui",passwd))
if(!rsJsonApi->authorizeUser("webui",passwd))
std::cerr << "(EE) Cannot register webui token. Some error occurred when calling authorizeUser()" << std::endl;
#else
std::cerr << "(EE) JsonAPI is not available in this buildof Retroshare! Cannot register a user password for the WebUI" << std::endl;
#endif
}
bool p3WebUI::restart()
{
rsJsonAPI->registerResourceProvider(this);
rsJsonAPI->restart();
rsJsonApi->registerResourceProvider(*this);
rsJsonApi->restart();
return true;
}
bool p3WebUI::stop()
{
rsJsonAPI->unregisterResourceProvider(this);
rsJsonAPI->restart();
rsJsonApi->unregisterResourceProvider(*this);
rsJsonApi->restart();
return true;
}

View file

@ -6,16 +6,15 @@
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* it under the terms of the GNU Lesser General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
@ -26,11 +25,10 @@
#include "retroshare/rswebui.h"
#include "jsonapi/jsonapi.h"
class p3WebUI: public RsWebUI, public JsonApiResourceProvider
class p3WebUI: public RsWebUi, public JsonApiResourceProvider
{
public:
p3WebUI(){}
virtual ~p3WebUI() = default;
~p3WebUI() override = default;
// implements RsWebUI
@ -39,11 +37,9 @@ public:
virtual bool restart() override ;
virtual bool stop() override ;
virtual int status() const override;
bool isRunning() const override;
// implements JsonApiResourceProvider
virtual std::string getName() const override { return "Web Interface" ;}
virtual std::vector<std::shared_ptr<restbed::Resource> > getResources() const override;
};

View file

@ -393,20 +393,16 @@ int RsInit::InitRetroShare(const RsConfigOptions& conf)
return error_code ;
#ifdef RS_JSONAPI
// We create the JsonApiServer this early, because it is needed *before* login
// We create the JsonApiServer this early, because it is needed *before* login
RsInfo() << __PRETTY_FUNCTION__
<< "Allocating jsonAPI server (not launched yet)" << std::endl;
JsonApiServer* jas = new JsonApiServer();
jas->setListeningPort(conf.jsonApiPort);
jas->setBindingAddress(conf.jsonApiBindAddress);
RsInfo() << "Allocating jsonAPI server (not launched yet) " << std::endl;
JsonApiServer *jas = new JsonApiServer();
jas->setListeningPort(conf.jsonApiPort);
jas->setBindAddress(conf.jsonApiBindAddress);
if(conf.jsonApiPort != 0) jas->restart();
if(conf.jsonApiPort != 0)
{
RsInfo() << "Launching jsonAPI server on port " << conf.jsonApiPort << std::endl;
jas->restart();
}
rsJsonAPI = jas;
rsJsonApi = jas;
#endif
#ifdef RS_AUTOLOGIN
@ -1224,10 +1220,8 @@ int RsServer::StartupRetroShare()
mPluginsManager->loadPlugins(programatically_inserted_plugins) ;
#ifdef RS_JSONAPI
// add jsonapi server to config manager so that it can save/load its tokens
if(rsJsonAPI)
rsJsonAPI->connectToConfigManager(mConfigMgr);
// add jsonapi server to config manager so that it can save/load its tokens
if(rsJsonApi) rsJsonApi->connectToConfigManager(*mConfigMgr);
#endif
/**** Reputation system ****/