remove protobuf (part 2)

This commit is contained in:
Paulchen-Panther
2018-12-30 22:07:53 +01:00
parent 559311e18c
commit 38950edf35
54 changed files with 1441 additions and 377 deletions

View File

@@ -49,7 +49,7 @@ public:
quint16 getServerPort (void) const;
QString getErrorString (void) const;
// const bool isListening(void) { return m_sockServer->isListening(); };
const bool isListening(void) { return m_sockServer->isListening(); };
public slots:
void start (quint16 port = 0);

View File

@@ -32,6 +32,14 @@ void StaticFileServing::setBaseUrl(const QString& url)
_cgi.setBaseUrl(url);
}
void StaticFileServing::setSSDPDescription(const QString& desc)
{
if(desc.isEmpty())
_ssdpDescription.clear();
else
_ssdpDescription = desc.toLocal8Bit();
}
void StaticFileServing::printErrorToReply (QtHttpReply * reply, QtHttpReply::StatusCode code, QString errorMessage)
{
reply->setStatusCode(code);
@@ -76,24 +84,33 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
// special uri handling for server commands
if ( ! uri_parts.empty() && uri_parts.at(0) == "cgi" )
if ( ! uri_parts.empty() )
{
uri_parts.removeAt(0);
try
if(uri_parts.at(0) == "cgi")
{
_cgi.exec(uri_parts, request, reply);
uri_parts.removeAt(0);
try
{
_cgi.exec(uri_parts, request, reply);
}
catch(int err)
{
Error(_log,"Exception while executing cgi %s : %d", path.toStdString().c_str(), err);
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
catch(std::exception &e)
{
Error(_log,"Exception while executing cgi %s : %s", path.toStdString().c_str(), e.what());
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
return;
}
catch(int err)
else if(uri_parts.at(0) == "description.xml" && !_ssdpDescription.isNull())
{
Error(_log,"Exception while executing cgi %s : %d", path.toStdString().c_str(), err);
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
reply->addHeader ("Content-Type", "text/xml");
reply->appendRawData (_ssdpDescription);
return;
}
catch(std::exception &e)
{
Error(_log,"Exception while executing cgi %s : %s", path.toStdString().c_str(), e.what());
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
}
return;
}
QFileInfo info(_baseUrl % "/" % path);

View File

@@ -1,16 +1,14 @@
#ifndef STATICFILESERVING_H
#define STATICFILESERVING_H
// locales includes
#include "CgiHandler.h"
// qt includes
#include <QMimeDatabase>
//#include "QtHttpServer.h"
#include "QtHttpRequest.h"
#include "QtHttpReply.h"
#include "QtHttpHeader.h"
#include "CgiHandler.h"
//utils includes
#include <utils/Logger.h>
class StaticFileServing : public QObject {
@@ -20,7 +18,15 @@ public:
explicit StaticFileServing (QObject * parent = nullptr);
virtual ~StaticFileServing (void);
///
/// @brief Overwrite current base url
///
void setBaseUrl(const QString& url);
///
/// @brief Set a new SSDP description, if empty the description will be unset and clients will get a NotFound
/// @param The description
///
void setSSDPDescription(const QString& desc);
public slots:
void onRequestNeedsReply (QtHttpRequest * request, QtHttpReply * reply);
@@ -30,6 +36,7 @@ private:
QMimeDatabase * _mimeDb;
CgiHandler _cgi;
Logger * _log;
QByteArray _ssdpDescription;
void printErrorToReply (QtHttpReply * reply, QtHttpReply::StatusCode code, QString errorMessage);

View File

@@ -1,23 +1,34 @@
#include "webserver/WebServer.h"
#include "StaticFileServing.h"
// qt includes
#include "QtHttpServer.h"
#include <QFileInfo>
#include <QJsonObject>
// bonjour includes
// bonjour
#include <bonjour/bonjourserviceregister.h>
#include <bonjour/bonjourrecord.h>
// utils includes
// netUtil
#include <utils/NetUtils.h>
WebServer::WebServer(const QJsonDocument& config, QObject * parent)
: QObject(parent)
, _config(config)
, _log(Logger::getInstance("WEBSERVER"))
, _server(new QtHttpServer (this))
, _server()
{
}
WebServer::~WebServer()
{
stop();
}
void WebServer::initServer()
{
_server = new QtHttpServer (this);
_server->setServerName (QStringLiteral ("Hyperion Webserver"));
connect (_server, &QtHttpServer::started, this, &WebServer::onServerStarted);
@@ -28,18 +39,14 @@ WebServer::WebServer(const QJsonDocument& config, QObject * parent)
_staticFileServing = new StaticFileServing (this);
connect(_server, &QtHttpServer::requestNeedsReply, _staticFileServing, &StaticFileServing::onRequestNeedsReply);
Debug(_log, "Instance created");
// init
handleSettingsUpdate(settings::WEBSERVER, config);
}
WebServer::~WebServer()
{
stop();
handleSettingsUpdate(settings::WEBSERVER, _config);
}
void WebServer::onServerStarted (quint16 port)
{
_inited= true;
Info(_log, "Started on port %d name '%s'", port ,_server->getServerName().toStdString().c_str());
if(_serviceRegister == nullptr)
@@ -53,10 +60,12 @@ void WebServer::onServerStarted (quint16 port)
_serviceRegister = new BonjourServiceRegister(this);
_serviceRegister->registerService("_hyperiond-http._tcp", port);
}
emit stateChange(true);
}
void WebServer::onServerStopped () {
Info(_log, "Stopped %s", _server->getServerName().toStdString().c_str());
emit stateChange(false);
}
void WebServer::onServerError (QString msg)
@@ -72,6 +81,7 @@ void WebServer::handleSettingsUpdate(const settings::type& type, const QJsonDocu
_baseUrl = obj["document_root"].toString(WEBSERVER_DEFAULT_PATH);
if ( (_baseUrl != ":/webconfig") && !_baseUrl.trimmed().isEmpty())
{
QFileInfo info(_baseUrl);
@@ -94,8 +104,11 @@ void WebServer::handleSettingsUpdate(const settings::type& type, const QJsonDocu
}
// eval if the port is available, will be incremented if not
NetUtils::portAvailable(_port, _log);
if(!_server->isListening())
NetUtils::portAvailable(_port, _log);
start();
emit portChanged(_port);
}
}
@@ -108,3 +121,8 @@ void WebServer::stop()
{
_server->stop();
}
void WebServer::setSSDPDescription(const QString & desc)
{
_staticFileServing->setSSDPDescription(desc);
}