aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/curl
diff options
context:
space:
mode:
authorAlexander Tkachev2016-05-24 11:57:49 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commit826a2a921cd0b0a72f71dd6f323097a2f449fab0 (patch)
tree52df58e3cad96ada3765c6516fef10affb7aa5d7 /backends/networking/curl
parent3582f6165ce829e4990c15bf77d1792ee20dca55 (diff)
downloadscummvm-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/networking/curl')
-rw-r--r--backends/networking/curl/connectionmanager.cpp2
-rw-r--r--backends/networking/curl/curljsonrequest.cpp24
-rw-r--r--backends/networking/curl/curljsonrequest.h15
-rw-r--r--backends/networking/curl/curlrequest.cpp72
-rw-r--r--backends/networking/curl/curlrequest.h58
-rw-r--r--backends/networking/curl/networkreadstream.cpp3
6 files changed, 139 insertions, 35 deletions
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 {