aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/sdl_net/uploadfileclienthandler.cpp
diff options
context:
space:
mode:
authorAlexander Tkachev2016-07-09 16:10:58 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commit7fcdcc10cb8f6dad370e942ef917ff00e888e2ec (patch)
tree257b8461c2576bca902a6514b39a1990ac1e395e /backends/networking/sdl_net/uploadfileclienthandler.cpp
parentabae5c437142b53d9099653fe5f14b3ac1967b18 (diff)
downloadscummvm-rg350-7fcdcc10cb8f6dad370e942ef917ff00e888e2ec.tar.gz
scummvm-rg350-7fcdcc10cb8f6dad370e942ef917ff00e888e2ec.tar.bz2
scummvm-rg350-7fcdcc10cb8f6dad370e942ef917ff00e888e2ec.zip
CLOUD: Cleanup in UploadFileClientHandler
Adds Client::noMoreContent() and Reader::noMoreContent(), which return true when whole client's request was read.
Diffstat (limited to 'backends/networking/sdl_net/uploadfileclienthandler.cpp')
-rw-r--r--backends/networking/sdl_net/uploadfileclienthandler.cpp135
1 files changed, 68 insertions, 67 deletions
diff --git a/backends/networking/sdl_net/uploadfileclienthandler.cpp b/backends/networking/sdl_net/uploadfileclienthandler.cpp
index ae795da25c..733a87b102 100644
--- a/backends/networking/sdl_net/uploadfileclienthandler.cpp
+++ b/backends/networking/sdl_net/uploadfileclienthandler.cpp
@@ -21,14 +21,11 @@
*/
#include "backends/networking/sdl_net/uploadfileclienthandler.h"
-#include "common/textconsole.h"
-#include <common/memstream.h>
-#include <common/translation.h>
-#include <common/system.h>
-#include <backends/fs/fs-factory.h>
-#include <common/debug.h>
-#include "handlerutils.h"
-#include "localwebserver.h"
+#include "backends/fs/fs-factory.h"
+#include "backends/networking/sdl_net/handlerutils.h"
+#include "backends/networking/sdl_net/localwebserver.h"
+#include "common/memstream.h"
+#include "common/translation.h"
namespace Networking {
@@ -41,19 +38,50 @@ UploadFileClientHandler::~UploadFileClientHandler() {
delete _contentStream;
}
-namespace {
-void readFromThatUntilLineEnd(const char *cstr, Common::String needle, Common::String &result) {
- const char *position = strstr(cstr, needle.c_str());
+void UploadFileClientHandler::handle(Client *client) {
+ if (client == nullptr) {
+ warning("UploadFileClientHandler::handle(): empty client pointer");
+ return;
+ }
- if (position) {
- char c;
- for (const char *i = position + needle.size(); c = *i, c != 0; ++i) {
- if (c == '\n' || c == '\r') break;
- result += c;
+ while (true) {
+ switch (_state) {
+ case UFH_READING_CONTENT:
+ if (client->readContent(nullptr)) {
+ _state = UFH_READING_BLOCK_HEADERS;
+ continue;
+ }
+ break;
+
+ case UFH_READING_BLOCK_HEADERS:
+ if (_headersStream == nullptr)
+ _headersStream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
+
+ if (client->readBlockHeaders(_headersStream)) {
+ handleBlockHeaders(client);
+ continue;
+ }
+ break;
+
+ case UFH_READING_BLOCK_CONTENT:
+ // _contentStream is created by handleBlockHeaders() if needed
+
+ if (client->readBlockContent(_contentStream)) {
+ handleBlockContent(client);
+ continue;
+ }
+ break;
+
+ case UFH_ERROR:
+ case UFH_STOP:
+ return;
}
+
+ break;
}
}
+namespace {
void readFromThatUntilDoubleQuote(const char *cstr, Common::String needle, Common::String &result) {
const char *position = strstr(cstr, needle.c_str());
@@ -80,6 +108,8 @@ Common::String readEverythingFromMemoryStream(Common::MemoryReadWriteStream *str
}
void UploadFileClientHandler::handleBlockHeaders(Client *client) {
+ _state = UFH_READING_BLOCK_CONTENT;
+
// search for "upload_file" field
Common::String headers = readEverythingFromMemoryStream(_headersStream);
Common::String fieldName = "";
@@ -112,62 +142,33 @@ void UploadFileClientHandler::handleBlockHeaders(Client *client) {
}
}
-void UploadFileClientHandler::handle(Client *client) {
- if (client == nullptr) {
- warning("UploadFileClientHandler::handle(): empty client pointer");
+void UploadFileClientHandler::handleBlockContent(Client *client) {
+ _state = UFH_READING_BLOCK_HEADERS;
+
+ // if previous block headers were file-related and created a stream
+ if (_contentStream) {
+ _contentStream->flush();
+ // success - redirect back to directory listing
+ HandlerUtils::setMessageHandler(*client,
+ Common::String::format(
+ "%s<br/><a href=\"files?path=%s\">%s</a>",
+ _("Uploaded successfully!"),
+ client->queryParameter("path").c_str(),
+ _("Back to parent directory")
+ ),
+ "/files?path=" + LocalWebserver::urlEncodeQueryParameterValue(client->queryParameter("path"))
+ );
+ _state = UFH_STOP;
return;
}
-
- while (true) {
- switch (_state) {
- case UFH_READING_CONTENT:
- if (client->readContent(nullptr)) {
- _state = UFH_READING_BLOCK_HEADERS;
- continue;
- }
- break;
-
- case UFH_READING_BLOCK_HEADERS:
- if (_headersStream == nullptr)
- _headersStream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
-
- if (client->readBlockHeaders(_headersStream)) {
- handleBlockHeaders(client);
- _state = UFH_READING_BLOCK_CONTENT;
- continue;
- }
- break;
-
- case UFH_READING_BLOCK_CONTENT:
- if (client->readBlockContent(_contentStream)) {
- if (_contentStream) {
- _contentStream->flush();
- // success - redirect back to directory listing
- HandlerUtils::setMessageHandler(*client,
- Common::String::format(
- "%s<br/><a href=\"files?path=%s\">%s</a>",
- _("Uploaded successfully!"),
- client->queryParameter("path").c_str(),
- _("Back to parent directory")
- ),
- "/files?path=" + LocalWebserver::urlEncodeQueryParameterValue(client->queryParameter("path"))
- );
- return;
- }
- _state = UFH_READING_BLOCK_HEADERS;
- continue;
- }
- break;
-
- case UFH_ERROR:
- return;
- }
-
- break;
+
+ // if no file field was found, but no more content avaiable - failure
+ if (client->noMoreContent()) {
+ setErrorMessageHandler(*client, _("No file was passed!"));
+ return;
}
}
-
void UploadFileClientHandler::setErrorMessageHandler(Client &client, Common::String message) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
_state = UFH_ERROR;