aboutsummaryrefslogtreecommitdiff
path: root/backends
diff options
context:
space:
mode:
authorAlexander Tkachev2016-06-08 16:46:18 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commitb29497effe60eaec891ca1b5ce78f8dae69fd599 (patch)
tree825e9c832c65b01a930313f623d868a17c223713 /backends
parente273e3d6e8dcca6f7e70d3e7c4e6dfc836832378 (diff)
downloadscummvm-rg350-b29497effe60eaec891ca1b5ce78f8dae69fd599.tar.gz
scummvm-rg350-b29497effe60eaec891ca1b5ce78f8dae69fd599.tar.bz2
scummvm-rg350-b29497effe60eaec891ca1b5ce78f8dae69fd599.zip
CLOUD: Add GoogleDriveUploadRequest
Includes NetworkReadStream PATCH method and Headers remembering feature.
Diffstat (limited to 'backends')
-rw-r--r--backends/cloud/googledrive/googledrivestorage.cpp4
-rw-r--r--backends/cloud/googledrive/googledriveuploadrequest.cpp341
-rw-r--r--backends/cloud/googledrive/googledriveuploadrequest.h70
-rw-r--r--backends/module.mk1
-rw-r--r--backends/networking/curl/curlrequest.cpp10
-rw-r--r--backends/networking/curl/curlrequest.h4
-rw-r--r--backends/networking/curl/networkreadstream.cpp35
-rw-r--r--backends/networking/curl/networkreadstream.h27
8 files changed, 481 insertions, 11 deletions
diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp
index d9db4f7dd8..bb762a4d90 100644
--- a/backends/cloud/googledrive/googledrivestorage.cpp
+++ b/backends/cloud/googledrive/googledrivestorage.cpp
@@ -37,6 +37,7 @@
#include "googledrivelistdirectoryrequest.h"
#include "googledrivestreamfilerequest.h"
#include "googledrivedownloadrequest.h"
+#include "googledriveuploadrequest.h"
namespace Cloud {
namespace GoogleDrive {
@@ -230,8 +231,7 @@ Networking::Request *GoogleDriveStorage::listDirectoryById(Common::String id, Li
}
Networking::Request *GoogleDriveStorage::upload(Common::String path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
- //return addRequest(new GoogleDriveUploadRequest(this, path, contents, callback, errorCallback));
- return nullptr; //TODO
+ return addRequest(new GoogleDriveUploadRequest(this, path, contents, callback, errorCallback));
}
Networking::Request *GoogleDriveStorage::streamFile(Common::String path, Networking::NetworkReadStreamCallback outerCallback, Networking::ErrorCallback errorCallback) {
diff --git a/backends/cloud/googledrive/googledriveuploadrequest.cpp b/backends/cloud/googledrive/googledriveuploadrequest.cpp
new file mode 100644
index 0000000000..7ab9f2e3c0
--- /dev/null
+++ b/backends/cloud/googledrive/googledriveuploadrequest.cpp
@@ -0,0 +1,341 @@
+/* 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/googledrive/googledriveuploadrequest.h"
+#include "backends/cloud/googledrive/googledrivestorage.h"
+#include "backends/cloud/iso8601.h"
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/connectionmanager.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/networkreadstream.h"
+#include "common/json.h"
+#include "common/debug.h"
+#include "googledrivetokenrefresher.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+
+GoogleDriveUploadRequest::GoogleDriveUploadRequest(GoogleDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb):
+ Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _contentsStream(contents), _uploadCallback(callback),
+ _workingRequest(nullptr), _ignoreCallback(false) {
+ start();
+}
+
+GoogleDriveUploadRequest::~GoogleDriveUploadRequest() {
+ _ignoreCallback = true;
+ if (_workingRequest) _workingRequest->finish();
+ delete _contentsStream;
+ delete _uploadCallback;
+}
+
+void GoogleDriveUploadRequest::start() {
+ _ignoreCallback = true;
+ if (_workingRequest) _workingRequest->finish();
+ if (!_contentsStream->seek(0)) {
+ warning("GoogleDriveUploadRequest: cannot restart because stream couldn't seek(0)");
+ finishError(Networking::ErrorResponse(this, false, true, "", -1));
+ }
+ _resolvedId = ""; //used to update file contents
+ _parentId = ""; //used to create file within parent directory
+ _serverReceivedBytes = 0;
+ _ignoreCallback = false;
+
+ resolveId();
+}
+
+void GoogleDriveUploadRequest::resolveId() {
+ //check whether such file already exists
+ Storage::UploadCallback innerCallback = new Common::Callback<GoogleDriveUploadRequest, Storage::UploadResponse>(this, &GoogleDriveUploadRequest::idResolvedCallback);
+ Networking::ErrorCallback innerErrorCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::idResolveFailedCallback);
+ _workingRequest = _storage->resolveFileId(_savePath, innerCallback, innerErrorCallback);
+}
+
+void GoogleDriveUploadRequest::idResolvedCallback(Storage::UploadResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+ _resolvedId = response.value.id();
+ startUpload();
+}
+
+void GoogleDriveUploadRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+
+ //not resolved => error or no such file
+ if (error.response.contains("no such file found in its parent directory")) {
+ //parent's id after the '\n'
+ Common::String parentId = error.response;
+ for (uint32 i = 0; i < parentId.size(); ++i)
+ if (parentId[i] == '\n') {
+ parentId.erase(0, i + 1);
+ break;
+ }
+
+ _parentId = parentId;
+ startUpload();
+ return;
+ }
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::startUpload() {
+ Common::String name = _savePath;
+ for (uint32 i = name.size(); i > 0; --i) {
+ if (name[i - 1] == '/' || name[i - 1] == '\\') {
+ name.erase(0, i);
+ break;
+ }
+ }
+
+ Common::String url = "https://www.googleapis.com/upload/drive/v3/files";
+ if (_resolvedId != "") url += "/" + _resolvedId;
+ url += "?uploadType=resumable&fields=id,mimeType,modifiedTime,name,size";
+ Networking::JsonCallback callback = new Common::Callback<GoogleDriveUploadRequest, Networking::JsonResponse>(this, &GoogleDriveUploadRequest::startUploadCallback);
+ Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::startUploadErrorCallback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + _storage->accessToken());
+ request->addHeader("Content-Type: application/json");
+ if (_resolvedId != "") request->usePatch();
+
+ Common::JSONObject jsonRequestParameters;
+ if (_resolvedId != "") jsonRequestParameters.setVal("id", new Common::JSONValue(_resolvedId));
+ else {
+ Common::JSONArray parentsArray;
+ parentsArray.push_back(new Common::JSONValue(_parentId));
+ jsonRequestParameters.setVal("parents", new Common::JSONValue(parentsArray));
+ }
+ jsonRequestParameters.setVal("name", new Common::JSONValue(name));
+
+ Common::JSONValue value(jsonRequestParameters);
+ request->addPostField(Common::JSON::stringify(&value));
+
+ _workingRequest = ConnMan.addRequest(request);
+}
+
+void GoogleDriveUploadRequest::startUploadCallback(Networking::JsonResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+
+ Networking::ErrorResponse error(this, false, true, "", -1);
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ Common::String headers = stream->responseHeaders();
+ if (code == 200) {
+ const char *cstr = headers.c_str();
+ const char *position = strstr(cstr, "Location: ");
+
+ if (position) {
+ Common::String result = "";
+ char c;
+ for (const char *i = position + 10; c = *i, c != 0; ++i) {
+ if (c == '\n' || c == '\r') break;
+ result += c;
+ }
+ _uploadUrl = result;
+ uploadNextPart();
+ return;
+ }
+ }
+
+ error.httpResponseCode = code;
+ }
+ }
+
+ Common::JSONValue *json = response.value;
+ delete json;
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::startUploadErrorCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::uploadNextPart() {
+ const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024;
+ Common::String url = _uploadUrl;
+
+ Networking::JsonCallback callback = new Common::Callback<GoogleDriveUploadRequest, Networking::JsonResponse>(this, &GoogleDriveUploadRequest::partUploadedCallback);
+ Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveUploadRequest, Networking::ErrorResponse>(this, &GoogleDriveUploadRequest::partUploadedErrorCallback);
+ Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
+ request->addHeader("Authorization: Bearer " + _storage->accessToken());
+ request->usePut();
+
+ uint32 oldPos = _contentsStream->pos();
+ if (oldPos != _serverReceivedBytes) {
+ if (!_contentsStream->seek(_serverReceivedBytes)) {
+ warning("GoogleDriveUploadRequest: cannot upload because stream couldn't seek(%u)", _serverReceivedBytes);
+ finishError(Networking::ErrorResponse(this, false, true, "", -1));
+ return;
+ }
+ oldPos = _serverReceivedBytes;
+ }
+
+ byte *buffer = new byte[UPLOAD_PER_ONE_REQUEST];
+ uint32 size = _contentsStream->read(buffer, UPLOAD_PER_ONE_REQUEST);
+ request->setBuffer(buffer, size);
+
+ //request->addHeader(Common::String::format("Content-Length: %u", size));
+ if (_uploadUrl != "")
+ request->addHeader(Common::String::format("Content-Range: bytes %u-%u/%u", oldPos, _contentsStream->pos()-1, _contentsStream->size())); ;
+
+ _workingRequest = ConnMan.addRequest(request);
+}
+
+namespace {
+uint64 atoull(Common::String s) {
+ uint64 result = 0;
+ for (uint32 i = 0; i < s.size(); ++i) {
+ if (s[i] < '0' || s[i] > '9') break;
+ result = result * 10L + (s[i] - '0');
+ }
+ return result;
+}
+}
+
+bool GoogleDriveUploadRequest::handleHttp308(const Networking::NetworkReadStream *stream) {
+ //308 Resume Incomplete, with Range: X-Y header
+ if (!stream) return false;
+ if (stream->httpResponseCode() != 308) return false; //seriously
+
+ Common::String headers = stream->responseHeaders();
+ const char *cstr = headers.c_str();
+ for (int rangeTry = 0; rangeTry < 2; ++rangeTry) {
+ const char *needle = (rangeTry==0 ? "Range: 0-" : "Range: bytes=0-");
+ uint32 needleLength = (rangeTry == 0 ? 9 : 15);
+
+ const char *position = strstr(cstr, needle); //if it lost the first part, I refuse to talk with it
+
+ if (position) {
+ Common::String result = "";
+ char c;
+ for (const char *i = position + needleLength; c = *i, c != 0; ++i) {
+ if (c == '\n' || c == '\r') break;
+ result += c;
+ }
+ _serverReceivedBytes = atoull(result) + 1;
+ uploadNextPart();
+ return true;
+ }
+ }
+
+ return false;
+}
+
+void GoogleDriveUploadRequest::partUploadedCallback(Networking::JsonResponse response) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+
+ Networking::ErrorResponse error(this, false, true, "", -1);
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ error.httpResponseCode = code;
+ if (code == 308 && handleHttp308(stream)) {
+ delete (Common::JSONValue *)response.value;
+ return;
+ }
+ }
+ }
+
+ Common::JSONValue *json = response.value;
+ if (json) {
+ debug("%s", json->stringify(true).c_str());
+ if (json->isObject()) {
+ Common::JSONObject object = json->asObject();
+
+ if (object.contains("error")) {
+ warning("GoogleDrive returned error: %s", json->stringify(true).c_str());
+ delete json;
+ error.response = json->stringify(true);
+ finishError(error);
+ return;
+ }
+
+ if (object.contains("id") && object.contains("name")) {
+ //finished
+ Common::String id = object.getVal("id")->asString();
+ Common::String name = object.getVal("name")->asString();
+ bool isDirectory = (object.getVal("mimeType")->asString() == "application/vnd.google-apps.folder");
+ uint32 size = 0, timestamp = 0;
+ if (object.contains("size") && object.getVal("size")->isString())
+ size = atoull(object.getVal("size")->asString());
+ if (object.contains("modifiedTime") && object.getVal("modifiedTime")->isString())
+ timestamp = ISO8601::convertToTimestamp(object.getVal("modifiedTime")->asString());
+
+ //as we list directory by id, we can't determine full path for the file, so we leave it empty
+ finishSuccess(StorageFile(id, _savePath, name, size, timestamp, isDirectory));
+ return;
+ }
+ }
+
+ if (_contentsStream->eos() || _contentsStream->pos() >= _contentsStream->size() - 1) {
+ warning("no file info to return");
+ finishSuccess(StorageFile(_savePath, 0, 0, false));
+ } else {
+ uploadNextPart();
+ }
+ } else {
+ warning("null, not json");
+ finishError(error);
+ }
+
+ delete json;
+}
+
+void GoogleDriveUploadRequest::partUploadedErrorCallback(Networking::ErrorResponse error) {
+ _workingRequest = nullptr;
+ if (_ignoreCallback) return;
+
+ Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)error.request;
+ if (rq) {
+ const Networking::NetworkReadStream *stream = rq->getNetworkReadStream();
+ if (stream) {
+ long code = stream->httpResponseCode();
+ if (code == 308 && handleHttp308(stream)) {
+ return;
+ }
+ }
+ }
+
+ finishError(error);
+}
+
+void GoogleDriveUploadRequest::handle() {}
+
+void GoogleDriveUploadRequest::restart() { start(); }
+
+void GoogleDriveUploadRequest::finishSuccess(StorageFile file) {
+ Request::finishSuccess();
+ if (_uploadCallback) (*_uploadCallback)(Storage::UploadResponse(this, file));
+}
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
diff --git a/backends/cloud/googledrive/googledriveuploadrequest.h b/backends/cloud/googledrive/googledriveuploadrequest.h
new file mode 100644
index 0000000000..e417403542
--- /dev/null
+++ b/backends/cloud/googledrive/googledriveuploadrequest.h
@@ -0,0 +1,70 @@
+/* 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_GOOGLEDRIVE_GOOGLEDRIVEUPLOADREQUEST_H
+#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVEUPLOADREQUEST_H
+
+#include "backends/cloud/storage.h"
+#include "backends/networking/curl/curljsonrequest.h"
+#include "backends/networking/curl/request.h"
+#include "common/callback.h"
+
+namespace Cloud {
+namespace GoogleDrive {
+class GoogleDriveStorage;
+
+class GoogleDriveUploadRequest: public Networking::Request {
+ GoogleDriveStorage *_storage;
+ Common::String _savePath;
+ Common::SeekableReadStream *_contentsStream;
+ Storage::UploadCallback _uploadCallback;
+ Request *_workingRequest;
+ bool _ignoreCallback;
+ Common::String _resolvedId, _parentId;
+ Common::String _uploadUrl;
+ uint64 _serverReceivedBytes;
+
+ void start();
+ void resolveId();
+ void idResolvedCallback(Storage::UploadResponse response);
+ void idResolveFailedCallback(Networking::ErrorResponse error);
+ void startUpload();
+ void startUploadCallback(Networking::JsonResponse response);
+ void startUploadErrorCallback(Networking::ErrorResponse error);
+ void uploadNextPart();
+ void partUploadedCallback(Networking::JsonResponse response);
+ void partUploadedErrorCallback(Networking::ErrorResponse error);
+ bool handleHttp308(const Networking::NetworkReadStream *stream);
+ void finishSuccess(StorageFile status);
+
+public:
+ GoogleDriveUploadRequest(GoogleDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb);
+ virtual ~GoogleDriveUploadRequest();
+
+ virtual void handle();
+ virtual void restart();
+};
+
+} // End of namespace GoogleDrive
+} // End of namespace Cloud
+
+#endif
diff --git a/backends/module.mk b/backends/module.mk
index 817c82970c..95334fef65 100644
--- a/backends/module.mk
+++ b/backends/module.mk
@@ -40,6 +40,7 @@ MODULE_OBJS += \
cloud/googledrive/googledrivestorage.o \
cloud/googledrive/googledrivestreamfilerequest.o \
cloud/googledrive/googledrivetokenrefresher.o \
+ cloud/googledrive/googledriveuploadrequest.o \
cloud/onedrive/onedrivestorage.o \
cloud/onedrive/onedrivecreatedirectoryrequest.o \
cloud/onedrive/onedrivetokenrefresher.o \
diff --git a/backends/networking/curl/curlrequest.cpp b/backends/networking/curl/curlrequest.cpp
index a3f997a4ff..6ef0e346af 100644
--- a/backends/networking/curl/curlrequest.cpp
+++ b/backends/networking/curl/curlrequest.cpp
@@ -31,7 +31,8 @@
namespace Networking {
CurlRequest::CurlRequest(DataCallback cb, ErrorCallback ecb, Common::String url):
- Request(cb, ecb), _url(url), _stream(nullptr), _headersList(nullptr), _bytesBuffer(nullptr), _bytesBufferSize(0), _uploading(false) {}
+ Request(cb, ecb), _url(url), _stream(nullptr), _headersList(nullptr), _bytesBuffer(nullptr),
+ _bytesBufferSize(0), _uploading(false), _usingPatch(false) {}
CurlRequest::~CurlRequest() {
delete _stream;
@@ -40,11 +41,10 @@ CurlRequest::~CurlRequest() {
NetworkReadStream *CurlRequest::makeStream() {
if (_bytesBuffer)
- return new NetworkReadStream(_url.c_str(), _headersList, _bytesBuffer, _bytesBufferSize, _uploading, true);
- return new NetworkReadStream(_url.c_str(), _headersList, _postFields, _uploading);
+ return new NetworkReadStream(_url.c_str(), _headersList, _bytesBuffer, _bytesBufferSize, _uploading, _usingPatch, true);
+ return new NetworkReadStream(_url.c_str(), _headersList, _postFields, _uploading, _usingPatch);
}
-
void CurlRequest::handle() {
if (!_stream) _stream = makeStream();
@@ -99,6 +99,8 @@ void CurlRequest::setBuffer(byte *buffer, uint32 size) {
void CurlRequest::usePut() { _uploading = true; }
+void CurlRequest::usePatch() { _usingPatch = true; }
+
NetworkReadStreamResponse CurlRequest::execute() {
if (!_stream) {
_stream = makeStream();
diff --git a/backends/networking/curl/curlrequest.h b/backends/networking/curl/curlrequest.h
index 5737078b2d..5c06b58107 100644
--- a/backends/networking/curl/curlrequest.h
+++ b/backends/networking/curl/curlrequest.h
@@ -45,6 +45,7 @@ protected:
byte *_bytesBuffer;
uint32 _bytesBufferSize;
bool _uploading; //using PUT method
+ bool _usingPatch; //using PATCH method
virtual NetworkReadStream *makeStream();
@@ -70,6 +71,9 @@ public:
/** Remembers to use PUT method when it would create NetworkReadStream. */
virtual void usePut();
+ /** Remembers to use PATCH method when it would create NetworkReadStream. */
+ virtual void usePatch();
+
/**
* Starts this Request with ConnMan.
* @return its NetworkReadStream in NetworkReadStreamResponse.
diff --git a/backends/networking/curl/networkreadstream.cpp b/backends/networking/curl/networkreadstream.cpp
index 283e5e667f..ccfb3d5a29 100644
--- a/backends/networking/curl/networkreadstream.cpp
+++ b/backends/networking/curl/networkreadstream.cpp
@@ -41,16 +41,24 @@ static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p) {
return 0;
}
-NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading):
- NetworkReadStream(url, headersList, (byte *)postFields.c_str(), postFields.size(), uploading, false) {}
+static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p) {
+ NetworkReadStream *stream = (NetworkReadStream *)p;
+ if (stream) return stream->addResponseHeaders(d, n*l);
+ return 0;
+}
-NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool uploading, bool post):
+NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading, bool usingPatch):
+ NetworkReadStream(url, headersList, (byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false) {}
+
+NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post):
_easy(0), _eos(false), _requestComplete(false), _sendingContentsBuffer(nullptr), _sendingContentsSize(0), _sendingContentsPos(0) {
_easy = curl_easy_init();
curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback);
curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this); //so callback can call us
curl_easy_setopt(_easy, CURLOPT_PRIVATE, this); //so ConnectionManager can call us when request is complete
curl_easy_setopt(_easy, CURLOPT_HEADER, 0L);
+ curl_easy_setopt(_easy, CURLOPT_HEADERDATA, this);
+ curl_easy_setopt(_easy, CURLOPT_HEADERFUNCTION, curlHeadersCallback);
curl_easy_setopt(_easy, CURLOPT_URL, url);
curl_easy_setopt(_easy, CURLOPT_VERBOSE, 0L);
curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, 1L); //probably it's OK to have it always on
@@ -61,6 +69,8 @@ NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, b
curl_easy_setopt(_easy, CURLOPT_READFUNCTION, curlReadDataCallback);
_sendingContentsBuffer = buffer;
_sendingContentsSize = bufferSize;
+ } else if (usingPatch) {
+ curl_easy_setopt(_easy, CURLOPT_CUSTOMREQUEST, "PATCH");
} else {
if (post || bufferSize != 0) {
curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, bufferSize);
@@ -101,6 +111,20 @@ long NetworkReadStream::httpResponseCode() const {
return responseCode;
}
+Common::String NetworkReadStream::currentLocation() const {
+ Common::String result = "";
+ if (_easy) {
+ char *pointer;
+ curl_easy_getinfo(_easy, CURLINFO_EFFECTIVE_URL, &pointer);
+ result = Common::String(pointer);
+ }
+ return result;
+}
+
+Common::String NetworkReadStream::responseHeaders() const {
+ return _responseHeaders;
+}
+
uint32 NetworkReadStream::fillWithSendingContents(char *bufferToFill, uint32 maxSize) {
uint32 size = _sendingContentsSize - _sendingContentsPos;
if (size > maxSize) size = maxSize;
@@ -111,4 +135,9 @@ uint32 NetworkReadStream::fillWithSendingContents(char *bufferToFill, uint32 max
return size;
}
+uint32 NetworkReadStream::addResponseHeaders(char *buffer, uint32 size) {
+ _responseHeaders += Common::String(buffer, size);
+ return size;
+}
+
} // End of namespace Cloud
diff --git a/backends/networking/curl/networkreadstream.h b/backends/networking/curl/networkreadstream.h
index d48d01b198..991fdb346d 100644
--- a/backends/networking/curl/networkreadstream.h
+++ b/backends/networking/curl/networkreadstream.h
@@ -38,10 +38,11 @@ class NetworkReadStream: public Common::MemoryReadWriteStream {
byte *_sendingContentsBuffer;
uint32 _sendingContentsSize;
uint32 _sendingContentsPos;
+ Common::String _responseHeaders;
public:
- NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading = false);
- NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool uploading = false, bool post = true);
+ NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields, bool uploading = false, bool usingPatch = false);
+ NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool uploading = false, bool usingPatch = false, bool post = true);
virtual ~NetworkReadStream();
/**
@@ -87,6 +88,21 @@ public:
long httpResponseCode() const;
/**
+ * Return current location URL from inner CURL handle.
+ * "" is returned to indicate there is no inner handle.
+ *
+ * @note This method should be called when eos() == true.
+ */
+ Common::String currentLocation() const;
+
+ /**
+ * Return response headers.
+ *
+ * @note This method should be called when eos() == true.
+ */
+ Common::String responseHeaders() const;
+
+ /**
* Fills the passed buffer with _sendingContentsBuffer contents.
* It works similarly to read(), expect it's not for reading
* Stream's contents, but for sending our own data to the server.
@@ -94,6 +110,13 @@ public:
* @returns how many bytes were actually read (filled in)
*/
uint32 fillWithSendingContents(char *bufferToFill, uint32 maxSize);
+
+ /**
+ * Remembers headers returned to CURL in server's response.
+ *
+ * @returns how many bytes were actually read
+ */
+ uint32 addResponseHeaders(char *buffer, uint32 size);
};
} // End of namespace Networking