hyperion.ng/libsrc/webserver/StaticFileServing.cpp
LordGrey 7389068a66
LED Device Features, Fixes and Refactoring (Resubmit PR855) (#875)
* Refactor LedDevices - Initial version
* Small renamings
* Add WLED as own device
* Lpd8806 Remove open() method
* remove dependency on Qt 5.10
* Lpd8806 Remove open() method
* Update WS281x
* Update WS2812SPI
* Add writeBlack for WLED powerOff
* WLED remove extra bracket
* Allow different Nanoleaf panel numbering sequence (Feature req.#827)
* build(deps): bump websocket-extensions from 0.1.3 to 0.1.4 in /docs (#826)
* Bumps [websocket-extensions](https://github.com/faye/websocket-extensions-node) from 0.1.3 to 0.1.4.
  - [Release notes](https://github.com/faye/websocket-extensions-node/releases)
  - [Changelog](https://github.com/faye/websocket-extensions-node/blob/master/CHANGELOG.md)
  - [Commits](https://github.com/faye/websocket-extensions-node/compare/0.1.3...0.1.4)
* Fix typos
* Nanoleaf clean-up
* Yeelight support, generalize wizard elements
* Update Yeelight to handle quota in music mode
* Yeelight extend rage for extraTimeDarkness for testing
* Clean-up - Add commentary, Remove development debug statements
* Fix brightnessSwitchOffOnMinimum typo and default value
* Yeelight support restoreOriginalState, additional Fixes
* WLED - Remove UDP-Port, as it is not configurable
* Fix merging issue
* Remove QHostAddress::operator=(const QString&)' is deprecated
* Windows compile errors and (Qt 5.15 deprecation) warnings
* Fix order includes
* LedDeviceFile Support Qt5.7 and greater
* Windows compatibility and other Fixes
* Fix Qt Version compatability
* Rs232 - Resolve portname from unix /dev/ style, fix DMX sub-type support
* Disable WLED Wizard Button (until Wizard is available)
* Yeelight updates
* Add wrong log-type as per #505
* Fixes and Clean-up after clang-tidy report
* Fix udpe131 not enabled for generated CID
* Change timer into dynamic for Qt Thread-Affinity
* Hue clean-up and diyHue workaround
* Updates after review feedback by m-seker
* Add "chrono" includes
2020-07-12 20:27:56 +02:00

157 lines
3.9 KiB
C++

#include "StaticFileServing.h"
#include <utils/QStringUtils.h>
#include <QStringBuilder>
#include <QUrlQuery>
#include <QList>
#include <QPair>
#include <QFile>
#include <QFileInfo>
#include <QResource>
#include <exception>
StaticFileServing::StaticFileServing (QObject * parent)
: QObject (parent)
, _baseUrl ()
, _cgi(this)
, _log(Logger::getInstance("WEBSERVER"))
{
Q_INIT_RESOURCE(WebConfig);
_mimeDb = new QMimeDatabase;
}
StaticFileServing::~StaticFileServing ()
{
delete _mimeDb;
}
void StaticFileServing::setBaseUrl(const QString& url)
{
_baseUrl = 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);
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" );
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 ();
}
}
void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpReply * reply)
{
QString command = request->getCommand ();
if (command == QStringLiteral ("GET"))
{
QString path = request->getUrl ().path ();
QStringList uri_parts = QStringUtils::split(path,'/', QStringUtils::SplitBehavior::SkipEmptyParts);
// special uri handling for server commands
if ( ! uri_parts.empty() )
{
if(uri_parts.at(0) == "cgi")
{
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;
}
else if(uri_parts.at(0) == "description.xml" && !_ssdpDescription.isNull())
{
reply->addHeader ("Content-Type", "text/xml");
reply->appendRawData (_ssdpDescription);
return;
}
}
QFileInfo info(_baseUrl % "/" % path);
if ( path == "/" || path.isEmpty() )
{
path = "index.html";
}
else if (info.isDir() && path.endsWith("/") )
{
path += "index.html";
}
else if (info.isDir() && ! path.endsWith("/") )
{
path += "/index.html";
}
// get static files
QFile file(_baseUrl % "/" % path);
if (file.exists())
{
QMimeType mime = _mimeDb->mimeTypeForFile (file.fileName ());
if (file.open (QFile::ReadOnly)) {
QByteArray data = file.readAll ();
reply->addHeader ("Content-Type", mime.name ().toLocal8Bit ());
reply->addHeader(QtHttpHeader::AccessControlAllow, "*" );
reply->appendRawData (data);
file.close ();
}
else
{
printErrorToReply (reply, QtHttpReply::Forbidden ,"Requested file: " % path);
}
}
else
{
printErrorToReply (reply, QtHttpReply::NotFound, "Requested file: " % path);
}
}
else
{
printErrorToReply (reply, QtHttpReply::MethodNotAllowed,"Unhandled HTTP/1.1 method " % command);
}
}