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>
|
2017-01-29 21:20:12 +01:00
|
|
|
#include <exception>
|
2016-06-12 22:27:24 +02:00
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
StaticFileServing::StaticFileServing (QObject * parent)
|
2016-09-14 13:51:28 +02:00
|
|
|
: QObject (parent)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _baseUrl ()
|
2018-12-28 18:12:45 +01:00
|
|
|
, _cgi(this)
|
2016-09-14 13:51:28 +02:00
|
|
|
, _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
|
|
|
}
|
|
|
|
|
|
|
|
StaticFileServing::~StaticFileServing ()
|
|
|
|
{
|
2019-06-05 18:19:08 +02:00
|
|
|
delete _mimeDb;
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void StaticFileServing::setBaseUrl(const QString& url)
|
2016-06-12 22:27:24 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_baseUrl = url;
|
|
|
|
_cgi.setBaseUrl(url);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:07:53 +01:00
|
|
|
void StaticFileServing::setSSDPDescription(const QString& desc)
|
|
|
|
{
|
|
|
|
if(desc.isEmpty())
|
|
|
|
_ssdpDescription.clear();
|
|
|
|
else
|
|
|
|
_ssdpDescription = desc.toLocal8Bit();
|
|
|
|
}
|
|
|
|
|
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
|
2018-12-30 22:07:53 +01:00
|
|
|
if ( ! uri_parts.empty() )
|
2016-06-13 22:54:08 +02:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
if(uri_parts.at(0) == "cgi")
|
2016-06-14 20:14:06 +02:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
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;
|
2016-06-14 20:14:06 +02:00
|
|
|
}
|
2018-12-30 22:07:53 +01:00
|
|
|
else if(uri_parts.at(0) == "description.xml" && !_ssdpDescription.isNull())
|
2017-01-29 21:20:12 +01:00
|
|
|
{
|
2018-12-30 22:07:53 +01:00
|
|
|
reply->addHeader ("Content-Type", "text/xml");
|
|
|
|
reply->appendRawData (_ssdpDescription);
|
|
|
|
return;
|
2017-01-29 21:20:12 +01:00
|
|
|
}
|
2016-06-13 22:54:08 +02:00
|
|
|
}
|
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 ());
|
2019-07-18 22:32:06 +02:00
|
|
|
reply->addHeader(QtHttpHeader::AccessControlAllow, "*" );
|
2016-06-12 22:27:24 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|