CLOUD: Cleanup in UploadFileClientHandler
Adds Client::noMoreContent() and Reader::noMoreContent(), which return true when whole client's request was read.
This commit is contained in:
parent
abae5c4371
commit
7fcdcc10cb
6 changed files with 84 additions and 75 deletions
|
@ -164,6 +164,8 @@ Common::String Client::attachedFile(Common::String name) const { return _reader.
|
||||||
|
|
||||||
Common::String Client::anchor() const { return _reader.anchor(); }
|
Common::String Client::anchor() const { return _reader.anchor(); }
|
||||||
|
|
||||||
|
bool Client::noMoreContent() const { return _reader.noMoreContent(); }
|
||||||
|
|
||||||
bool Client::socketIsReady() { return SDLNet_SocketReady(_socket); }
|
bool Client::socketIsReady() { return SDLNet_SocketReady(_socket); }
|
||||||
|
|
||||||
int Client::recv(void *data, int maxlen) { return SDLNet_TCP_Recv(_socket, data, maxlen); }
|
int Client::recv(void *data, int maxlen) { return SDLNet_TCP_Recv(_socket, data, maxlen); }
|
||||||
|
|
|
@ -85,6 +85,7 @@ public:
|
||||||
Common::String queryParameter(Common::String name) const;
|
Common::String queryParameter(Common::String name) const;
|
||||||
Common::String attachedFile(Common::String name) const;
|
Common::String attachedFile(Common::String name) const;
|
||||||
Common::String anchor() const;
|
Common::String anchor() const;
|
||||||
|
bool noMoreContent() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return SDLNet_SocketReady(_socket).
|
* Return SDLNet_SocketReady(_socket).
|
||||||
|
|
|
@ -57,6 +57,7 @@ Reader::Reader(): _randomSource("Networking::Reader") {
|
||||||
_availableBytes = 0;
|
_availableBytes = 0;
|
||||||
_isFileField = false;
|
_isFileField = false;
|
||||||
_isBadRequest = false;
|
_isBadRequest = false;
|
||||||
|
_allContentRead = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -118,6 +119,7 @@ Reader &Reader::operator=(Reader &r) {
|
||||||
_currentTempFileName = r._currentTempFileName;
|
_currentTempFileName = r._currentTempFileName;
|
||||||
_isFileField = r._isFileField;
|
_isFileField = r._isFileField;
|
||||||
_isBadRequest = r._isBadRequest;
|
_isBadRequest = r._isBadRequest;
|
||||||
|
_allContentRead = r._allContentRead;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -216,19 +218,17 @@ bool Reader::readBlockContent(Common::WriteStream *stream) {
|
||||||
if (!readWholeContentIntoStream(stream))
|
if (!readWholeContentIntoStream(stream))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/*
|
|
||||||
if (_availableBytes >= 2) {
|
if (_availableBytes >= 2) {
|
||||||
Common::String bts;
|
Common::String bts;
|
||||||
bts += readOne();
|
bts += readOne();
|
||||||
bts += readOne();
|
bts += readOne();
|
||||||
if (bts == "--") _isOver = true;
|
if (bts == "--") _allContentRead = true;
|
||||||
else if (bts != "\r\n")
|
else if (bts != "\r\n")
|
||||||
warning("strange bytes: \"%s\"", bts.c_str());
|
warning("strange bytes: \"%s\"", bts.c_str());
|
||||||
} else {
|
} else {
|
||||||
warning("strange ending");
|
warning("strange ending");
|
||||||
_isOver = true;
|
_allContentRead = true;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -601,7 +601,9 @@ void Reader::setContent(Common::MemoryReadWriteStream *stream) {
|
||||||
_bytesLeft = stream->size() - stream->pos();
|
_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; }
|
Common::String Reader::method() const { return _method; }
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,7 @@ class Reader {
|
||||||
Common::String _currentFieldName, _currentFileName, _currentTempFileName;
|
Common::String _currentFieldName, _currentFileName, _currentTempFileName;
|
||||||
bool _isFileField;
|
bool _isFileField;
|
||||||
bool _isBadRequest;
|
bool _isBadRequest;
|
||||||
|
bool _allContentRead;
|
||||||
|
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
|
@ -101,7 +102,8 @@ public:
|
||||||
bool readBlockHeaders(Common::WriteStream *stream); //true when ended reading
|
bool readBlockHeaders(Common::WriteStream *stream); //true when ended reading
|
||||||
bool readBlockContent(Common::WriteStream *stream); //true when ended reading
|
bool readBlockContent(Common::WriteStream *stream); //true when ended reading
|
||||||
void setContent(Common::MemoryReadWriteStream *stream);
|
void setContent(Common::MemoryReadWriteStream *stream);
|
||||||
bool badRequest();
|
bool badRequest() const;
|
||||||
|
bool noMoreContent() const;
|
||||||
|
|
||||||
Common::String method() const;
|
Common::String method() const;
|
||||||
Common::String path() const;
|
Common::String path() const;
|
||||||
|
|
|
@ -21,14 +21,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "backends/networking/sdl_net/uploadfileclienthandler.h"
|
#include "backends/networking/sdl_net/uploadfileclienthandler.h"
|
||||||
#include "common/textconsole.h"
|
#include "backends/fs/fs-factory.h"
|
||||||
#include <common/memstream.h>
|
#include "backends/networking/sdl_net/handlerutils.h"
|
||||||
#include <common/translation.h>
|
#include "backends/networking/sdl_net/localwebserver.h"
|
||||||
#include <common/system.h>
|
#include "common/memstream.h"
|
||||||
#include <backends/fs/fs-factory.h>
|
#include "common/translation.h"
|
||||||
#include <common/debug.h>
|
|
||||||
#include "handlerutils.h"
|
|
||||||
#include "localwebserver.h"
|
|
||||||
|
|
||||||
namespace Networking {
|
namespace Networking {
|
||||||
|
|
||||||
|
@ -41,19 +38,50 @@ UploadFileClientHandler::~UploadFileClientHandler() {
|
||||||
delete _contentStream;
|
delete _contentStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
void UploadFileClientHandler::handle(Client *client) {
|
||||||
void readFromThatUntilLineEnd(const char *cstr, Common::String needle, Common::String &result) {
|
if (client == nullptr) {
|
||||||
const char *position = strstr(cstr, needle.c_str());
|
warning("UploadFileClientHandler::handle(): empty client pointer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (position) {
|
while (true) {
|
||||||
char c;
|
switch (_state) {
|
||||||
for (const char *i = position + needle.size(); c = *i, c != 0; ++i) {
|
case UFH_READING_CONTENT:
|
||||||
if (c == '\n' || c == '\r') break;
|
if (client->readContent(nullptr)) {
|
||||||
result += c;
|
_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) {
|
void readFromThatUntilDoubleQuote(const char *cstr, Common::String needle, Common::String &result) {
|
||||||
const char *position = strstr(cstr, needle.c_str());
|
const char *position = strstr(cstr, needle.c_str());
|
||||||
|
|
||||||
|
@ -80,6 +108,8 @@ Common::String readEverythingFromMemoryStream(Common::MemoryReadWriteStream *str
|
||||||
}
|
}
|
||||||
|
|
||||||
void UploadFileClientHandler::handleBlockHeaders(Client *client) {
|
void UploadFileClientHandler::handleBlockHeaders(Client *client) {
|
||||||
|
_state = UFH_READING_BLOCK_CONTENT;
|
||||||
|
|
||||||
// search for "upload_file" field
|
// search for "upload_file" field
|
||||||
Common::String headers = readEverythingFromMemoryStream(_headersStream);
|
Common::String headers = readEverythingFromMemoryStream(_headersStream);
|
||||||
Common::String fieldName = "";
|
Common::String fieldName = "";
|
||||||
|
@ -112,62 +142,33 @@ void UploadFileClientHandler::handleBlockHeaders(Client *client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UploadFileClientHandler::handle(Client *client) {
|
void UploadFileClientHandler::handleBlockContent(Client *client) {
|
||||||
if (client == nullptr) {
|
_state = UFH_READING_BLOCK_HEADERS;
|
||||||
warning("UploadFileClientHandler::handle(): empty client pointer");
|
|
||||||
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
// if no file field was found, but no more content avaiable - failure
|
||||||
switch (_state) {
|
if (client->noMoreContent()) {
|
||||||
case UFH_READING_CONTENT:
|
setErrorMessageHandler(*client, _("No file was passed!"));
|
||||||
if (client->readContent(nullptr)) {
|
return;
|
||||||
_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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UploadFileClientHandler::setErrorMessageHandler(Client &client, Common::String message) {
|
void UploadFileClientHandler::setErrorMessageHandler(Client &client, Common::String message) {
|
||||||
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
|
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
|
||||||
_state = UFH_ERROR;
|
_state = UFH_ERROR;
|
||||||
|
|
|
@ -32,7 +32,8 @@ enum UploadFileHandlerState {
|
||||||
UFH_READING_CONTENT,
|
UFH_READING_CONTENT,
|
||||||
UFH_READING_BLOCK_HEADERS,
|
UFH_READING_BLOCK_HEADERS,
|
||||||
UFH_READING_BLOCK_CONTENT,
|
UFH_READING_BLOCK_CONTENT,
|
||||||
UFH_ERROR
|
UFH_ERROR,
|
||||||
|
UFH_STOP
|
||||||
};
|
};
|
||||||
|
|
||||||
class UploadFileClientHandler: public ClientHandler {
|
class UploadFileClientHandler: public ClientHandler {
|
||||||
|
@ -42,7 +43,7 @@ class UploadFileClientHandler: public ClientHandler {
|
||||||
Common::String _parentDirectoryPath;
|
Common::String _parentDirectoryPath;
|
||||||
|
|
||||||
void handleBlockHeaders(Client *client);
|
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);
|
void setErrorMessageHandler(Client &client, Common::String message);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue