From fa910b8e62f27e54312dff6d4445baabd34d805d Mon Sep 17 00:00:00 2001 From: Roland Stigge Date: Wed, 10 Jan 2018 22:42:44 +0100 Subject: Add read only mode --- src/webbox.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 14 deletions(-) (limited to 'src/webbox.cpp') diff --git a/src/webbox.cpp b/src/webbox.cpp index c3f03e2..d0cfebc 100644 --- a/src/webbox.cpp +++ b/src/webbox.cpp @@ -72,22 +72,31 @@ struct CommandParameters { QHash paramHash; // derived from urlQuery int count; // request count for this instance + + // Webbox parameters, constant and set globally in Web server config + QString webboxPath; + QString webboxName; + bool webboxReadOnly; }; class Command { public: // call interface void execute(CommandParameters& p) { + // check if this webbox is writable and enforce this + if (p.webboxReadOnly && m_isWriteCommand) { + printHttpError(400, QString("Webbox is Read-Only"), p); + return; + } + + // check for correct method GET/POST QString requestMethod(FCGX_GetParam("REQUEST_METHOD", p.request.envp)); if (requestMethod != m_requestMethod) { printHttpError(403, QString("Bad request method"), p); return; } - // process environment - QString webboxPath(getenv("WEBBOX_PATH")); - - // FastCGI request environment + // Set parameters from FastCGI request environment m_pathInfo = FCGX_GetParam("PATH_INFO", p.request.envp); if (m_pathInfo == "") { m_pathInfo = "/"; @@ -97,7 +106,7 @@ class Command { return; } - m_path = webboxPath + m_pathInfo; + m_path = p.webboxPath + m_pathInfo; this->start(p); } @@ -118,6 +127,7 @@ class Command { // Implementation class constants QString m_commandName; QString m_requestMethod; + bool m_isWriteCommand; // if true, command must be prevented if p.webboxReadOnly // calculated during start of execute() QString m_pathInfo; // path inside webbox, derived from request @@ -169,6 +179,7 @@ class DiagCommand: public GetCommand { public: DiagCommand() { m_commandName = "diag"; + m_isWriteCommand = false; } protected: @@ -193,7 +204,7 @@ class DiagCommand: public GetCommand { tmp++; } - FCGX_PutS(QString("
WEBBOX_PATH=%1
\r\n").arg(getenv("WEBBOX_PATH")).toUtf8().data(), p.request.out); + FCGX_PutS(QString("
WEBBOX_PATH=%1
\r\n").arg(p.webboxPath).toUtf8().data(), p.request.out); FCGX_PutS(QString("
URL Query=%1
\r\n").arg(p.urlQuery.toString()).toUtf8().data(), p.request.out); @@ -206,6 +217,7 @@ class ListCommand: public GetCommand { public: ListCommand() { m_commandName = "list"; + m_isWriteCommand = false; } protected: @@ -233,16 +245,32 @@ class ListCommand: public GetCommand { } }; -class TitleCommand: public GetCommand { +// Retrieve from Server: +// Title +// ReadOnly flag +class ServerInfoCommand: public GetCommand { public: - TitleCommand() { - m_commandName = "title"; + ServerInfoCommand() { + m_commandName = "server-info"; + m_isWriteCommand = false; } protected: virtual void start(CommandParameters& p) { - FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out); - FCGX_PutS(getenv("WEBBOX_NAME"), p.request.out); + FCGX_PutS("Content-Type: text/xml\r\n\r\n", p.request.out); + + QByteArray xmlData; + QXmlStreamWriter xmlWriter(&xmlData); + xmlWriter.writeStartDocument(); + xmlWriter.writeStartElement("serverinfo"); + + xmlWriter.writeTextElement("title", p.webboxName); + + xmlWriter.writeTextElement("readonly", p.webboxReadOnly ? "1" : "0"); + + xmlWriter.writeEndElement(); // serverinfo + xmlWriter.writeEndDocument(); + FCGX_PutS(xmlData.data(), p.request.out); } }; @@ -250,12 +278,13 @@ class VersionCommand: public GetCommand { public: VersionCommand() { m_commandName = "version"; + m_isWriteCommand = false; } protected: virtual void start(CommandParameters& p) { FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out); - FCGX_PutS(QString("webbox %1
(C) 2017 Reichwein.IT\r\n").arg(PROGRAMVERSION).toUtf8().data(), p.request.out); + FCGX_PutS(QString("webbox %1
(C) 2018 Reichwein.IT\r\n").arg(PROGRAMVERSION).toUtf8().data(), p.request.out); } }; @@ -263,6 +292,7 @@ class NewDirCommand: public PostCommand { public: NewDirCommand() { m_commandName = "newdir"; + m_isWriteCommand = true; } protected: @@ -293,6 +323,7 @@ class InfoCommand: public PostCommand { public: InfoCommand() { m_commandName = "info"; + m_isWriteCommand = false; } protected: @@ -413,6 +444,7 @@ class DeleteCommand: public PostCommand { public: DeleteCommand() { m_commandName = "delete"; + m_isWriteCommand = true; } protected: @@ -464,6 +496,7 @@ class MoveCommand: public PostCommand { public: MoveCommand() { m_commandName = "move"; + m_isWriteCommand = true; } protected: @@ -518,6 +551,7 @@ class UploadCommand: public PostCommand { public: UploadCommand() { m_commandName = "upload"; + m_isWriteCommand = true; } protected: @@ -590,6 +624,7 @@ class DownloadCommand: public GetCommand { public: DownloadCommand() { m_commandName = ""; // default command w/o explict "command=" query argument + m_isWriteCommand = false; } protected: @@ -642,6 +677,12 @@ int main(int argc, char* argv[]) { commandParameters.count = 0; + // values constant to this instance: + commandParameters.webboxPath = getenv("WEBBOX_PATH"); + commandParameters.webboxName = getenv("WEBBOX_NAME"); + char* WEBBOX_READONLY = getenv("WEBBOX_READONLY"); + commandParameters.webboxReadOnly = ((WEBBOX_READONLY != NULL) && !strcmp(WEBBOX_READONLY, "On")); + Commands commands; DiagCommand diagCommand; @@ -650,8 +691,8 @@ int main(int argc, char* argv[]) { ListCommand listCommand; commands.registerCommand(listCommand); - TitleCommand titleCommand; - commands.registerCommand(titleCommand); + ServerInfoCommand serverInfoCommand; + commands.registerCommand(serverInfoCommand); VersionCommand versionCommand; commands.registerCommand(versionCommand); -- cgit v1.2.3