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-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-09-14 13:51:28 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2016-07-12 13:47:30 +02:00
|
|
|
Info(_log, "started on port %d name \"%s\"", port ,_server->getServerName().toStdString().c_str());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static inline void printErrorToReply (QtHttpReply * reply, QString errorMessage)
|
|
|
|
{
|
|
|
|
reply->addHeader ("Content-Type", QByteArrayLiteral ("text/plain"));
|
|
|
|
reply->appendRawData (errorMessage.toLocal8Bit ());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpReply * reply)
|
|
|
|
{
|
|
|
|
QString command = request->getCommand ();
|
|
|
|
if (command == QStringLiteral ("GET"))
|
|
|
|
{
|
|
|
|
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
|
2016-06-14 20:14:06 +02:00
|
|
|
if ( ! uri_parts.empty() && uri_parts.at(0) == "cgi" )
|
2016-06-13 22:54:08 +02:00
|
|
|
{
|
2016-06-14 20:14:06 +02:00
|
|
|
uri_parts.removeAt(0);
|
|
|
|
try
|
|
|
|
{
|
2016-06-19 00:56:47 +02:00
|
|
|
_cgi.exec(uri_parts, request, reply);
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2016-09-03 15:54:33 +02:00
|
|
|
printErrorToReply (reply, "script failed (" % path % ")");
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
2016-06-13 22:54:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-14 13:51:28 +02:00
|
|
|
Q_INIT_RESOURCE(WebConfig);
|
2016-08-16 17:12:47 +02:00
|
|
|
|
|
|
|
QFileInfo info(_baseUrl % "/" % path);
|
|
|
|
if ( path == "/" || path.isEmpty() || ! info.exists() )
|
|
|
|
{
|
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
|
|
|
|
{
|
2016-06-14 20:14:06 +02:00
|
|
|
printErrorToReply (reply, "Requested file " % path % " couldn't be open for reading !");
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printErrorToReply (reply, "Requested file " % path % " couldn't be found !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printErrorToReply (reply, "Unhandled HTTP/1.1 method " % command % " on static file server !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|