diff options
author | Alexander Tkachev | 2016-07-09 16:10:58 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 7fcdcc10cb8f6dad370e942ef917ff00e888e2ec (patch) | |
tree | 257b8461c2576bca902a6514b39a1990ac1e395e /backends | |
parent | abae5c437142b53d9099653fe5f14b3ac1967b18 (diff) | |
download | scummvm-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')
-rw-r--r-- | backends/networking/sdl_net/client.cpp | 2 | ||||
-rw-r--r-- | backends/networking/sdl_net/client.h | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/reader.cpp | 12 | ||||
-rw-r--r-- | backends/networking/sdl_net/reader.h | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/uploadfileclienthandler.cpp | 135 | ||||
-rw-r--r-- | backends/networking/sdl_net/uploadfileclienthandler.h | 5 |
6 files changed, 84 insertions, 75 deletions
diff --git a/backends/networking/sdl_net/client.cpp b/backends/networking/sdl_net/client.cpp index 89b50faf86..ef46e81815 100644 --- a/backends/networking/sdl_net/client.cpp +++ b/backends/networking/sdl_net/client.cpp @@ -164,6 +164,8 @@ Common::String Client::attachedFile(Common::String name) const { return _reader. Common::String Client::anchor() const { return _reader.anchor(); } +bool Client::noMoreContent() const { return _reader.noMoreContent(); } + bool Client::socketIsReady() { return SDLNet_SocketReady(_socket); } int Client::recv(void *data, int maxlen) { return SDLNet_TCP_Recv(_socket, data, maxlen); } diff --git a/backends/networking/sdl_net/client.h b/backends/networking/sdl_net/client.h index 6a0dea32fe..a60545e2c6 100644 --- a/backends/networking/sdl_net/client.h +++ b/backends/networking/sdl_net/client.h @@ -85,6 +85,7 @@ public: Common::String queryParameter(Common::String name) const; Common::String attachedFile(Common::String name) const; Common::String anchor() const; + bool noMoreContent() const; /** * Return SDLNet_SocketReady(_socket). diff --git a/backends/networking/sdl_net/reader.cpp b/backends/networking/sdl_net/reader.cpp index d61eff2e68..143ab93816 100644 --- a/backends/networking/sdl_net/reader.cpp +++ b/backends/networking/sdl_net/reader.cpp @@ -57,6 +57,7 @@ Reader::Reader(): _randomSource("Networking::Reader") { _availableBytes = 0; _isFileField = false; _isBadRequest = false; + _allContentRead = false; } namespace { @@ -118,6 +119,7 @@ Reader &Reader::operator=(Reader &r) { _currentTempFileName = r._currentTempFileName; _isFileField = r._isFileField; _isBadRequest = r._isBadRequest; + _allContentRead = r._allContentRead; return *this; } @@ -216,19 +218,17 @@ bool Reader::readBlockContent(Common::WriteStream *stream) { if (!readWholeContentIntoStream(stream)) return false; - /* if (_availableBytes >= 2) { Common::String bts; bts += readOne(); bts += readOne(); - if (bts == "--") _isOver = true; + if (bts == "--") _allContentRead = true; else if (bts != "\r\n") warning("strange bytes: \"%s\"", bts.c_str()); } else { warning("strange ending"); - _isOver = true; + _allContentRead = true; } - */ return true; } @@ -601,7 +601,9 @@ void Reader::setContent(Common::MemoryReadWriteStream *stream) { _bytesLeft = stream->size() - stream->pos(); } -bool Reader::badRequest() { return _isBadRequest; } +bool Reader::badRequest() const { return _isBadRequest; } + +bool Reader::noMoreContent() const { return _allContentRead; } Common::String Reader::method() const { return _method; } diff --git a/backends/networking/sdl_net/reader.h b/backends/networking/sdl_net/reader.h index 5f951a39b1..0ed0d8c91e 100644 --- a/backends/networking/sdl_net/reader.h +++ b/backends/networking/sdl_net/reader.h @@ -66,6 +66,7 @@ class Reader { Common::String _currentFieldName, _currentFileName, _currentTempFileName; bool _isFileField; bool _isBadRequest; + bool _allContentRead; void cleanup(); @@ -101,7 +102,8 @@ public: bool readBlockHeaders(Common::WriteStream *stream); //true when ended reading bool readBlockContent(Common::WriteStream *stream); //true when ended reading void setContent(Common::MemoryReadWriteStream *stream); - bool badRequest(); + bool badRequest() const; + bool noMoreContent() const; Common::String method() const; Common::String path() const; 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; diff --git a/backends/networking/sdl_net/uploadfileclienthandler.h b/backends/networking/sdl_net/uploadfileclienthandler.h index 4f2ce621fb..de6941bf4f 100644 --- a/backends/networking/sdl_net/uploadfileclienthandler.h +++ b/backends/networking/sdl_net/uploadfileclienthandler.h @@ -32,7 +32,8 @@ enum UploadFileHandlerState { UFH_READING_CONTENT, UFH_READING_BLOCK_HEADERS, UFH_READING_BLOCK_CONTENT, - UFH_ERROR + UFH_ERROR, + UFH_STOP }; class UploadFileClientHandler: public ClientHandler { @@ -42,7 +43,7 @@ class UploadFileClientHandler: public ClientHandler { Common::String _parentDirectoryPath; void handleBlockHeaders(Client *client); - bool validFilename(Client &client, Common::String filename, Common::String &errorMessage); + void handleBlockContent(Client *client); void setErrorMessageHandler(Client &client, Common::String message); public: |