2016-06-12 22:27:24 +02:00
|
|
|
|
|
|
|
#include "StaticFileServing.h"
|
|
|
|
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QList>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QFile>
|
2016-08-16 17:12:47 +02:00
|
|
|
#include <QFileInfo>
|
2016-09-14 13:51:28 +02:00
|
|
|
#include <QResource>
|
2016-09-26 17:50:31 +02:00
|
|
|
#include <QHostInfo>
|
|
|
|
#include <bonjour/bonjourserviceregister.h>
|
|
|
|
#include <bonjour/bonjourrecord.h>
|
2017-07-05 16:54:41 +02:00
|
|
|
#include <HyperionConfig.h>
|
2017-01-29 21:20:12 +01:00
|
|
|
#include <exception>
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2016-06-14 20:14:06 +02:00
|
|
|
StaticFileServing::StaticFileServing (Hyperion *hyperion, QString baseUrl, quint16 port, QObject * parent)
|
2016-09-14 13:51:28 +02:00
|
|
|
: QObject (parent)
|
|
|
|
, _hyperion(hyperion)
|
|
|
|
, _baseUrl (baseUrl)
|
|
|
|
, _cgi(hyperion, baseUrl, this)
|
|
|
|
, _log(Logger::getInstance("WEBSERVER"))
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
2016-09-14 13:51:28 +02:00
|
|
|
Q_INIT_RESOURCE(WebConfig);
|
|
|
|
|
2016-06-14 20:14:06 +02:00
|
|
|
_mimeDb = new QMimeDatabase;
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2016-06-13 22:54:08 +02:00
|
|
|
_server = new QtHttpServer (this);
|
2016-07-16 22:51:31 +02:00
|
|
|
_server->setServerName (QStringLiteral ("Hyperion WebConfig"));
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2016-06-13 22:54:08 +02:00
|
|
|
connect (_server, &QtHttpServer::started, this, &StaticFileServing::onServerStarted);
|
|
|
|
connect (_server, &QtHttpServer::stopped, this, &StaticFileServing::onServerStopped);
|
|
|
|
connect (_server, &QtHttpServer::error, this, &StaticFileServing::onServerError);
|
|
|
|
connect (_server, &QtHttpServer::requestNeedsReply, this, &StaticFileServing::onRequestNeedsReply);
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2016-06-13 22:54:08 +02:00
|
|
|
_server->start (port);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StaticFileServing::~StaticFileServing ()
|
|
|
|
{
|
2016-06-13 22:54:08 +02:00
|
|
|
_server->stop ();
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StaticFileServing::onServerStarted (quint16 port)
|
|
|
|
{
|
2017-03-18 00:05:04 +01:00
|
|
|
Info(_log, "started on port %d name '%s'", port ,_server->getServerName().toStdString().c_str());
|
2017-03-22 23:08:01 +01:00
|
|
|
const QJsonObject & generalConfig = _hyperion->getQJsonConfig()["general"].toObject();
|
|
|
|
const QString mDNSDescr = generalConfig["name"].toString("") + "@" + QHostInfo::localHostName() + ":" + QString::number(port);
|
2016-09-26 17:50:31 +02:00
|
|
|
|
2017-07-05 16:54:41 +02:00
|
|
|
// txt record for zeroconf
|
|
|
|
QString id = _hyperion->id;
|
|
|
|
std::string version = HYPERION_VERSION;
|
|
|
|
std::vector<std::pair<std::string, std::string> > txtRecord = {{"id",id.toStdString()},{"version",version}};
|
|
|
|
|
2016-09-26 17:50:31 +02:00
|
|
|
BonjourServiceRegister *bonjourRegister_http = new BonjourServiceRegister();
|
|
|
|
bonjourRegister_http->registerService(
|
2017-03-04 22:17:42 +01:00
|
|
|
BonjourRecord(mDNSDescr, "_hyperiond-http._tcp", QString()),
|
2017-07-05 16:54:41 +02:00
|
|
|
port,
|
|
|
|
txtRecord
|
2016-09-26 17:50:31 +02:00
|
|
|
);
|
|
|
|
Debug(_log, "Web Config mDNS responder started");
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StaticFileServing::onServerStopped () {
|
2016-07-12 13:47:30 +02:00
|
|
|
Info(_log, "stopped %s", _server->getServerName().toStdString().c_str());
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StaticFileServing::onServerError (QString msg)
|
|
|
|
{
|
2016-07-12 13:47:30 +02:00
|
|
|
Error(_log, "%s", msg.toStdString().c_str());
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:20:12 +01:00
|
|
|
void StaticFileServing::printErrorToReply (QtHttpReply * reply, QtHttpReply::StatusCode code, QString errorMessage)
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
reply->setStatusCode(code);
|
|
|
|
reply->addHeader ("Content-Type", QByteArrayLiteral ("text/html"));
|
|
|
|
QFile errorPageHeader(_baseUrl % "/errorpages/header.html" );
|
|
|
|
QFile errorPageFooter(_baseUrl % "/errorpages/footer.html" );
|
|
|
|
QFile errorPage (_baseUrl % "/errorpages/" % QString::number((int)code) % ".html" );
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2017-01-29 21:20:12 +01:00
|
|
|
if (errorPageHeader.open (QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
QByteArray data = errorPageHeader.readAll();
|
|
|
|
reply->appendRawData (data);
|
|
|
|
errorPageHeader.close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorPage.open (QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
QByteArray data = errorPage.readAll();
|
|
|
|
data = data.replace("{MESSAGE}", errorMessage.toLocal8Bit() );
|
|
|
|
reply->appendRawData (data);
|
|
|
|
errorPage.close ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reply->appendRawData (QString(QString::number(code) + " - " +errorMessage).toLocal8Bit());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorPageFooter.open (QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
QByteArray data = errorPageFooter.readAll ();
|
|
|
|
reply->appendRawData (data);
|
|
|
|
errorPageFooter.close ();
|
|
|
|
}
|
2017-01-23 23:21:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:27:24 +02:00
|
|
|
void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpReply * reply)
|
|
|
|
{
|
|
|
|
QString command = request->getCommand ();
|
2017-11-20 00:06:45 +01:00
|
|
|
if (command == QStringLiteral ("GET"))
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
|
|
|
QString path = request->getUrl ().path ();
|
2016-06-14 20:14:06 +02:00
|
|
|
QStringList uri_parts = path.split('/', QString::SkipEmptyParts);
|
2016-06-13 22:54:08 +02:00
|
|
|
|
|
|
|
// special uri handling for server commands
|
2017-11-20 00:06:45 +01:00
|
|
|
if ( ! uri_parts.empty() && uri_parts.at(0) == "cgi" )
|
2016-06-13 22:54:08 +02:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
uri_parts.removeAt(0);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_cgi.exec(uri_parts, request, reply);
|
|
|
|
}
|
|
|
|
catch(int err)
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
Error(_log,"Exception while executing cgi %s : %d", path.toStdString().c_str(), err);
|
|
|
|
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
2017-11-20 00:06:45 +01:00
|
|
|
catch(std::exception &e)
|
2017-01-29 21:20:12 +01:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
Error(_log,"Exception while executing cgi %s : %s", path.toStdString().c_str(), e.what());
|
|
|
|
printErrorToReply (reply, QtHttpReply::InternalError, "script failed (" % path % ")");
|
2017-01-29 21:20:12 +01:00
|
|
|
}
|
2017-11-20 00:06:45 +01:00
|
|
|
return;
|
2016-06-13 22:54:08 +02:00
|
|
|
}
|
2016-09-14 13:51:28 +02:00
|
|
|
Q_INIT_RESOURCE(WebConfig);
|
2016-08-16 17:12:47 +02:00
|
|
|
|
|
|
|
QFileInfo info(_baseUrl % "/" % path);
|
2017-01-23 23:21:04 +01:00
|
|
|
if ( path == "/" || path.isEmpty() )
|
2016-08-16 17:12:47 +02:00
|
|
|
{
|
2016-06-12 22:27:24 +02:00
|
|
|
path = "index.html";
|
2016-08-16 17:12:47 +02:00
|
|
|
}
|
|
|
|
else if (info.isDir() && path.endsWith("/") )
|
|
|
|
{
|
|
|
|
path += "index.html";
|
|
|
|
}
|
|
|
|
else if (info.isDir() && ! path.endsWith("/") )
|
|
|
|
{
|
|
|
|
path += "/index.html";
|
|
|
|
}
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2016-08-16 17:12:47 +02:00
|
|
|
// get static files
|
|
|
|
QFile file(_baseUrl % "/" % path);
|
|
|
|
if (file.exists())
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
2016-06-14 20:14:06 +02:00
|
|
|
QMimeType mime = _mimeDb->mimeTypeForFile (file.fileName ());
|
2016-06-12 22:27:24 +02:00
|
|
|
if (file.open (QFile::ReadOnly)) {
|
|
|
|
QByteArray data = file.readAll ();
|
|
|
|
reply->addHeader ("Content-Type", mime.name ().toLocal8Bit ());
|
|
|
|
reply->appendRawData (data);
|
|
|
|
file.close ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
printErrorToReply (reply, QtHttpReply::Forbidden ,"Requested file: " % path);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
printErrorToReply (reply, QtHttpReply::NotFound, "Requested file: " % path);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 21:20:12 +01:00
|
|
|
printErrorToReply (reply, QtHttpReply::MethodNotAllowed,"Unhandled HTTP/1.1 method " % command);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
}
|