diff options
author | Alexander Tkachev | 2016-05-24 11:57:49 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 826a2a921cd0b0a72f71dd6f323097a2f449fab0 (patch) | |
tree | 52df58e3cad96ada3765c6516fef10affb7aa5d7 /backends | |
parent | 3582f6165ce829e4990c15bf77d1792ee20dca55 (diff) | |
download | scummvm-rg350-826a2a921cd0b0a72f71dd6f323097a2f449fab0.tar.gz scummvm-rg350-826a2a921cd0b0a72f71dd6f323097a2f449fab0.tar.bz2 scummvm-rg350-826a2a921cd0b0a72f71dd6f323097a2f449fab0.zip |
CLOUD: Add DownloadRequest stub
It reads the passed NetworkReadStream and prints its contents onto
console (for now). It would be writing contents into file.
To simplify work with raw NetworkReadStream there is a new CurlRequest.
It basically does nothing, but as ConnMan handles transfers only if
there is an active Request, you need some Request to get
NetworkReadStream working. Thus, there is a CurlRequest, which is active
until NetworkReadStream is completely read. CurlRequest also has useful
addHeader() and addPostField() methods in order to customize the request
easily. Use execute() method to get its NetworkReadStream.
DropboxStorage implements streamFile() and download() API methods. As
DownloadRequest is incomplete, it is not actually downloading a file,
though.
Diffstat (limited to 'backends')
-rw-r--r-- | backends/cloud/downloadrequest.cpp | 67 | ||||
-rw-r--r-- | backends/cloud/downloadrequest.h | 45 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp | 1 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.h | 2 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 21 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 9 | ||||
-rw-r--r-- | backends/cloud/storage.h | 10 | ||||
-rw-r--r-- | backends/module.mk | 2 | ||||
-rw-r--r-- | backends/networking/curl/connectionmanager.cpp | 2 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.cpp | 24 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.h | 15 | ||||
-rw-r--r-- | backends/networking/curl/curlrequest.cpp | 72 | ||||
-rw-r--r-- | backends/networking/curl/curlrequest.h | 58 | ||||
-rw-r--r-- | backends/networking/curl/networkreadstream.cpp | 3 |
14 files changed, 288 insertions, 43 deletions
diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp new file mode 100644 index 0000000000..ed84d4f0e5 --- /dev/null +++ b/backends/cloud/downloadrequest.cpp @@ -0,0 +1,67 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#include "backends/cloud/downloadrequest.h" +#include "common/debug.h" +#include "common/textconsole.h" + +namespace Cloud { + +DownloadRequest::DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream): + Request(0), _boolCallback(callback), _stream(stream) {} + +bool DownloadRequest::handle() { + if (!_stream) { + warning("DownloadRequest: no stream to read"); + return true; + } + + const int kBufSize = 16 * 1024; + char buf[kBufSize]; + uint32 readBytes = _stream->read(buf, kBufSize); + + //TODO: save into file + /* + if (readBytes != 0) + if (_outputStream.write(buf, readBytes) != readBytes) + warning("DropboxDownloadRequest: unable to write all received bytes into output stream"); + */ + + buf[readBytes] = 0; + debug("%s", buf); //TODO: remove + + if (_stream->eos()) { + if (_stream->httpResponseCode() != 200) { + warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); + //TODO: do something about it actually + } + + if (_boolCallback) (*_boolCallback)(_stream->httpResponseCode() == 200); + + //TODO: close file stream + return true; + } + + return false; +} + +} //end of namespace Cloud diff --git a/backends/cloud/downloadrequest.h b/backends/cloud/downloadrequest.h new file mode 100644 index 0000000000..a819910b1b --- /dev/null +++ b/backends/cloud/downloadrequest.h @@ -0,0 +1,45 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_CLOUD_DOWNLOADREQUEST_H +#define BACKENDS_CLOUD_DOWNLOADREQUEST_H + +#include "backends/networking/curl/request.h" +#include "backends/networking/curl/networkreadstream.h" +#include "backends/cloud/storage.h" + +namespace Cloud { + +class DownloadRequest: public Networking::Request { + Networking::NetworkReadStream *_stream; + Storage::BoolCallback _boolCallback; + +public: + DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream); + virtual ~DownloadRequest() {} + + virtual bool handle(); +}; + +} //end of namespace Cloud + +#endif diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index e28a445d63..5e5957b12c 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -24,6 +24,7 @@ #include "backends/cloud/iso8601.h" #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" +#include "common/json.h" namespace Cloud { namespace Dropbox { diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h index 03b4fc121a..0c10512782 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h @@ -24,8 +24,8 @@ #define BACKENDS_CLOUD_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H #include "backends/cloud/storage.h" -#include "common/callback.h" #include "backends/networking/curl/request.h" +#include "common/callback.h" namespace Cloud { namespace Dropbox { diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 6de9424efc..1b6dc1b92f 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -23,6 +23,7 @@ #include "backends/cloud/dropbox/dropboxstorage.h" #include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h" +#include "backends/cloud/downloadrequest.h" #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" #include "common/config-manager.h" @@ -86,10 +87,28 @@ void DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerC ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive)); } +Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { + Common::JSONObject jsonRequestParameters; + jsonRequestParameters.setVal("path", new Common::JSONValue(path)); + Common::JSONValue value(jsonRequestParameters); + + Networking::CurlRequest *request = new Networking::CurlRequest(0, "https://content.dropboxapi.com/2/files/download"); + request->addHeader("Authorization: Bearer " + _token); + request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value)); + request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded) + + return request->execute(); +} + +void DropboxStorage::download(Common::String path, BoolCallback callback) { + ConnMan.addRequest(new DownloadRequest(callback, streamFile(path))); +} + void DropboxStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation //"" is root in Dropbox, not "/" - listDirectory("", new Common::Callback<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true); + //listDirectory("", new Common::Callback<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true); + download("/notempty.txt", 0); } void DropboxStorage::info(StorageInfoCallback outerCallback) { diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index c1c2e03497..92c3746da5 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -67,10 +67,13 @@ public: virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false); /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) {} //TODO + virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) {} //TODO - /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, ReadStreamCallback callback) {} //TODO + /** Returns pointer to Networking::NetworkReadStream. */ + virtual Networking::NetworkReadStream *streamFile(Common::String path); + + /** Calls the callback when finished. */ + virtual void download(Common::String path, BoolCallback callback); /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) {} //TODO diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 1435be8329..92f3bb96b2 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -29,6 +29,7 @@ #include "common/callback.h" #include "backends/cloud/storagefile.h" #include "backends/cloud/storageinfo.h" +#include "backends/networking/curl/networkreadstream.h" namespace Cloud { @@ -62,10 +63,13 @@ public: virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) = 0; /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) = 0; + virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) = 0; - /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, ReadStreamCallback callback) = 0; + /** Returns pointer to Networking::NetworkReadStream. */ + virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; + + /** Calls the callback when finished. */ + virtual void download(Common::String path, BoolCallback callback) = 0; /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) = 0; diff --git a/backends/module.mk b/backends/module.mk index 7e83192ee0..eb5ce070b5 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -24,6 +24,7 @@ MODULE_OBJS += \ cloud/iso8601.o \ cloud/manager.o \ cloud/storagefile.o \ + cloud/downloadrequest.o \ cloud/dropbox/dropboxstorage.o \ cloud/dropbox/dropboxlistdirectoryrequest.o endif @@ -32,6 +33,7 @@ ifdef USE_LIBCURL MODULE_OBJS += \ networking/curl/connectionmanager.o \ networking/curl/networkreadstream.o \ + networking/curl/curlrequest.o \ networking/curl/curljsonrequest.o endif diff --git a/backends/networking/curl/connectionmanager.cpp b/backends/networking/curl/connectionmanager.cpp index 31e99f989c..97ae31a446 100644 --- a/backends/networking/curl/connectionmanager.cpp +++ b/backends/networking/curl/connectionmanager.cpp @@ -83,7 +83,7 @@ void ConnectionManager::handle() { void ConnectionManager::interateRequests() { //call handle() of all running requests (so they can do their work) - debug("handler's here"); + debug("handling %d request(s)", _requests.size()); for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) { if ((*i)->handle()) { delete (*i); diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index 805852ea0a..0366e3b403 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -23,7 +23,6 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/networking/curl/curljsonrequest.h" -#include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/networkreadstream.h" #include "common/debug.h" #include "common/json.h" @@ -31,13 +30,10 @@ namespace Networking { -CurlJsonRequest::CurlJsonRequest(Common::BaseCallback<> *cb, const char *url): Request(cb), _stream(0), _headersList(0), _contentsStream(DisposeAfterUse::YES) { - _url = url; -} +CurlJsonRequest::CurlJsonRequest(Common::BaseCallback<> *cb, const char *url): + CurlRequest(cb, url), _contentsStream(DisposeAfterUse::YES) {} -CurlJsonRequest::~CurlJsonRequest() { - if (_stream) delete _stream; -} +CurlJsonRequest::~CurlJsonRequest() {} char *CurlJsonRequest::getPreparedContents() { //write one more byte in the end @@ -70,7 +66,7 @@ bool CurlJsonRequest::handle() { if (_stream->eos()) { if (_stream->httpResponseCode() != 200) - warning("HTTP response code is not 200 OK (it's %d)", _stream->httpResponseCode()); + warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); if (_callback) { char *contents = getPreparedContents(); @@ -86,16 +82,4 @@ bool CurlJsonRequest::handle() { return false; } -void CurlJsonRequest::addHeader(Common::String header) { - _headersList = curl_slist_append(_headersList, header.c_str()); -} - -void CurlJsonRequest::addPostField(Common::String keyValuePair) { - if (_postFields == "") - _postFields = keyValuePair; - else - _postFields += "&" + keyValuePair; -} - - } //end of namespace Networking diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index 50fa8183dd..cfb82e97e3 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -23,21 +23,14 @@ #ifndef BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H #define BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H -#include "backends/networking/curl/request.h" +#include "backends/networking/curl/curlrequest.h" #include "common/memstream.h" -#include "common/json.h" - -struct curl_slist; namespace Networking { class NetworkReadStream; -class CurlJsonRequest : public Request { - const char *_url; - NetworkReadStream *_stream; - curl_slist *_headersList; - Common::String _postFields; +class CurlJsonRequest: public CurlRequest { Common::MemoryWriteStreamDynamic _contentsStream; /** Prepares raw bytes from _contentsStream to be parsed with Common::JSON::parse(). */ @@ -48,10 +41,6 @@ public: virtual ~CurlJsonRequest(); virtual bool handle(); - - void addHeader(Common::String header); - - void addPostField(Common::String header); }; } //end of namespace Networking diff --git a/backends/networking/curl/curlrequest.cpp b/backends/networking/curl/curlrequest.cpp new file mode 100644 index 0000000000..e13adaca59 --- /dev/null +++ b/backends/networking/curl/curlrequest.cpp @@ -0,0 +1,72 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#include "backends/networking/curl/curlrequest.h" +#include "backends/networking/curl/connectionmanager.h" +#include "backends/networking/curl/networkreadstream.h" +#include "common/textconsole.h" +#include <curl/curl.h> + +namespace Networking { + +CurlRequest::CurlRequest(Common::BaseCallback<> *cb, const char *url): + Request(cb), _url(url), _stream(0), _headersList(0) {} + +CurlRequest::~CurlRequest() { + if (_stream) delete _stream; +} + +bool CurlRequest::handle() { + if (!_stream) _stream = new NetworkReadStream(_url, _headersList, _postFields); + + if (_stream && _stream->eos()) { + if (_stream->httpResponseCode() != 200) + warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); + return true; + } + + return false; +} + +void CurlRequest::addHeader(Common::String header) { + _headersList = curl_slist_append(_headersList, header.c_str()); +} + +void CurlRequest::addPostField(Common::String keyValuePair) { + if (_postFields == "") + _postFields = keyValuePair; + else + _postFields += "&" + keyValuePair; +} + +NetworkReadStream *CurlRequest::execute() { + if (!_stream) { + _stream = new NetworkReadStream(_url, _headersList, _postFields); + ConnMan.addRequest(this); + } + + return _stream; +} + +} //end of namespace Networking diff --git a/backends/networking/curl/curlrequest.h b/backends/networking/curl/curlrequest.h new file mode 100644 index 0000000000..22f50be418 --- /dev/null +++ b/backends/networking/curl/curlrequest.h @@ -0,0 +1,58 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_NETWORKING_CURL_CURLREQUEST_H +#define BACKENDS_NETWORKING_CURL_CURLREQUEST_H + +#include "backends/networking/curl/request.h" +#include "common/str.h" + +struct curl_slist; + +namespace Networking { + +class NetworkReadStream; + +class CurlRequest: public Request { +protected: + const char *_url; + NetworkReadStream *_stream; + curl_slist *_headersList; + Common::String _postFields; + +public: + CurlRequest(Common::BaseCallback<> *cb, const char *url); + virtual ~CurlRequest(); + + virtual bool handle(); + + void addHeader(Common::String header); + + void addPostField(Common::String header); + + /** Start this Request with ConnMan. Returns its ReadStream. */ + NetworkReadStream *execute(); +}; + +} //end of namespace Networking + +#endif diff --git a/backends/networking/curl/networkreadstream.cpp b/backends/networking/curl/networkreadstream.cpp index 8fd39d6884..0cf7118a8a 100644 --- a/backends/networking/curl/networkreadstream.cpp +++ b/backends/networking/curl/networkreadstream.cpp @@ -52,7 +52,8 @@ NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, C } NetworkReadStream::~NetworkReadStream() { - curl_easy_cleanup(_easy); + if (_easy) + curl_easy_cleanup(_easy); } bool NetworkReadStream::eos() const { |