diff options
author | Alexander Tkachev | 2016-07-09 15:49:18 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | e4bb7c4e750d53434140a8d1a401e7fa6ee221ab (patch) | |
tree | 4b6d555163353d71b51e64308e5a4dccc85f2216 /backends/networking/sdl_net/client.cpp | |
parent | a424ebbc28549c0e06b9a8ca4985bf799179c404 (diff) | |
download | scummvm-rg350-e4bb7c4e750d53434140a8d1a401e7fa6ee221ab.tar.gz scummvm-rg350-e4bb7c4e750d53434140a8d1a401e7fa6ee221ab.tar.bz2 scummvm-rg350-e4bb7c4e750d53434140a8d1a401e7fa6ee221ab.zip |
CLOUD: Add UploadFileClientHandler
Now Client reads the first headers block, then LocalWebserver decides
which Handler to use. In case of "/upload", UploadFileHandler is used.
But now it only knows the "path" parameter. If that's valid, actual
UploadFileClientHandler is created, which reads the contents of the
request and, when finds there an "upload_file" field, starts saving it
in the directory specified by "path".
With that we don't need temp files approach from Reader class.
Diffstat (limited to 'backends/networking/sdl_net/client.cpp')
-rw-r--r-- | backends/networking/sdl_net/client.cpp | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/backends/networking/sdl_net/client.cpp b/backends/networking/sdl_net/client.cpp index 13543a6c44..89b50faf86 100644 --- a/backends/networking/sdl_net/client.cpp +++ b/backends/networking/sdl_net/client.cpp @@ -26,12 +26,13 @@ #include "backends/networking/sdl_net/localwebserver.h" #include "common/textconsole.h" #include <SDL/SDL_net.h> +#include <common/memstream.h> namespace Networking { -Client::Client() : _state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr) {} +Client::Client() : _state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr), _previousHandler(nullptr), _stream(nullptr) {} -Client::Client(SDLNet_SocketSet set, TCPsocket socket) : _state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr) { +Client::Client(SDLNet_SocketSet set, TCPsocket socket) : _state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr), _previousHandler(nullptr), _stream(nullptr) { open(set, socket); } @@ -46,7 +47,11 @@ void Client::open(SDLNet_SocketSet set, TCPsocket socket) { _set = set; Reader cleanReader; _reader = cleanReader; + if (_handler) delete _handler; _handler = nullptr; + if (_previousHandler) delete _previousHandler; + _previousHandler = nullptr; + _stream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES); if (set) { int numused = SDLNet_TCP_AddSocket(set, socket); if (numused == -1) { @@ -55,9 +60,11 @@ void Client::open(SDLNet_SocketSet set, TCPsocket socket) { } } -void Client::readHeaders() { - if (!_socket) return; - if (!SDLNet_SocketReady(_socket)) return; +bool Client::readMoreIfNeeded() { + if (_stream == nullptr) return false; //nothing to read into + if (_stream->size() - _stream->pos() > 0) return true; //not needed, some data left in the stream + if (!_socket) return false; + if (!SDLNet_SocketReady(_socket)) return false; const uint32 BUFFER_SIZE = 16 * 1024; byte buffer[BUFFER_SIZE]; @@ -65,16 +72,48 @@ void Client::readHeaders() { if (bytes <= 0) { warning("Client::readHeaders recv fail"); close(); - return; + return false; + } + + if (_stream->write(buffer, bytes) != bytes) { + warning("failed to write() into MemoryReadWriteStream"); + close(); + return false; } - - _reader.setContent(buffer, bytes); - if (_reader.readRequest()) + + return true; +} + +void Client::readHeaders() { + if (!readMoreIfNeeded()) return; + _reader.setContent(_stream); + if (_reader.readFirstHeaders()) _state = (_reader.badRequest() ? BAD_REQUEST : READ_HEADERS); } +bool Client::readContent(Common::WriteStream *stream) { + if (!readMoreIfNeeded()) return false; + _reader.setContent(_stream); + return _reader.readFirstContent(stream); +} + +bool Client::readBlockHeaders(Common::WriteStream *stream) { + if (!readMoreIfNeeded()) return false; + _reader.setContent(_stream); + return _reader.readBlockHeaders(stream); +} + +bool Client::readBlockContent(Common::WriteStream *stream) { + if (!readMoreIfNeeded()) return false; + _reader.setContent(_stream); + return _reader.readBlockContent(stream); +} + void Client::setHandler(ClientHandler *handler) { - if (_handler) delete _handler; + if (_handler) { + if (_previousHandler) delete _previousHandler; + _previousHandler = _handler; //can't just delete it, as setHandler() could've been called by handler itself + } _state = BEING_HANDLED; _handler = handler; } @@ -100,6 +139,11 @@ void Client::close() { _socket = nullptr; } + if (_stream) { + delete _stream; + _stream = nullptr; + } + _state = INVALID; } |