From d917592099381402c2681b291d379bda78a1c3f7 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Mon, 30 May 2016 02:23:29 +0600 Subject: CLOUD: Add DropboxUploadRequest --- backends/networking/curl/curljsonrequest.cpp | 2 +- backends/networking/curl/curlrequest.cpp | 35 +++++++++++++++++++++----- backends/networking/curl/curlrequest.h | 7 ++++++ backends/networking/curl/networkreadstream.cpp | 14 ++++++----- backends/networking/curl/networkreadstream.h | 1 + 5 files changed, 46 insertions(+), 13 deletions(-) (limited to 'backends/networking') diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index d114d69098..fee0932129 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -55,7 +55,7 @@ char *CurlJsonRequest::getPreparedContents() { } void CurlJsonRequest::handle() { - if (!_stream) _stream = new NetworkReadStream(_url.c_str(), _headersList, _postFields); + if (!_stream) _stream = makeStream(); if (_stream) { const int kBufSize = 16*1024; diff --git a/backends/networking/curl/curlrequest.cpp b/backends/networking/curl/curlrequest.cpp index a745741bc8..64f6c26fb9 100644 --- a/backends/networking/curl/curlrequest.cpp +++ b/backends/networking/curl/curlrequest.cpp @@ -31,12 +31,22 @@ namespace Networking { CurlRequest::CurlRequest(DataCallback cb, Common::String url): - Request(cb), _url(url), _stream(0), _headersList(0) {} + Request(cb), _url(url), _stream(nullptr), _headersList(nullptr), _bytesBuffer(nullptr), _bytesBufferSize(0) {} + +CurlRequest::~CurlRequest() { + delete _stream; + delete _bytesBuffer; +} + +NetworkReadStream *CurlRequest::makeStream() { + if (_bytesBuffer) + return new NetworkReadStream(_url.c_str(), _headersList, _bytesBuffer, _bytesBufferSize, true); + return new NetworkReadStream(_url.c_str(), _headersList, _postFields); +} -CurlRequest::~CurlRequest() { delete _stream; } void CurlRequest::handle() { - if (!_stream) _stream = new NetworkReadStream(_url.c_str(), _headersList, _postFields); + if (!_stream) _stream = makeStream(); if (_stream && _stream->eos()) { if (_stream->httpResponseCode() != 200) @@ -47,13 +57,13 @@ void CurlRequest::handle() { void CurlRequest::restart() { if (_stream) delete _stream; - _stream = 0; + _stream = nullptr; //with no stream available next handle() will create another one } void CurlRequest::setHeaders(Common::Array &headers) { curl_slist_free_all(_headersList); - _headersList = 0; + _headersList = nullptr; for (uint32 i = 0; i < headers.size(); ++i) addHeader(headers[i]); } @@ -63,15 +73,28 @@ void CurlRequest::addHeader(Common::String header) { } void CurlRequest::addPostField(Common::String keyValuePair) { + if (_bytesBuffer) + warning("CurlRequest: added POST fields would be ignored, because there is buffer present"); + if (_postFields == "") _postFields = keyValuePair; else _postFields += "&" + keyValuePair; } +void CurlRequest::setBuffer(byte *buffer, uint32 size) { + if (_postFields != "") + warning("CurlRequest: added POST fields would be ignored, because buffer added"); + + if (_bytesBuffer) delete _bytesBuffer; + + _bytesBuffer = buffer; + _bytesBufferSize = size; +} + NetworkReadStreamResponse CurlRequest::execute() { if (!_stream) { - _stream = new NetworkReadStream(_url.c_str(), _headersList, _postFields); + _stream = makeStream(); ConnMan.addRequest(this); } diff --git a/backends/networking/curl/curlrequest.h b/backends/networking/curl/curlrequest.h index 8623a487d3..461f153b9d 100644 --- a/backends/networking/curl/curlrequest.h +++ b/backends/networking/curl/curlrequest.h @@ -42,6 +42,10 @@ protected: NetworkReadStream *_stream; curl_slist *_headersList; Common::String _postFields; + byte *_bytesBuffer; + uint32 _bytesBufferSize; + + virtual NetworkReadStream *makeStream(); public: CurlRequest(DataCallback cb, Common::String url); @@ -59,6 +63,9 @@ public: /** Adds a post field (key=value pair). */ virtual void addPostField(Common::String field); + /** Sets bytes buffer. */ + virtual void setBuffer(byte *buffer, uint32 size); + /** * 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 997f0b2fc3..f96a069e16 100644 --- a/backends/networking/curl/networkreadstream.cpp +++ b/backends/networking/curl/networkreadstream.cpp @@ -36,8 +36,10 @@ static size_t curlDataCallback(char *d, size_t n, size_t l, void *p) { } NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields): - _easy(0), _eos(false), _requestComplete(false) -{ + NetworkReadStream(url, headersList, (byte *)postFields.c_str(), postFields.size(), false) {} + +NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool post) : + _easy(0), _eos(false), _requestComplete(false) { _easy = curl_easy_init(); curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback); curl_easy_setopt(_easy, CURLOPT_WRITEDATA, this); //so callback can call us @@ -46,10 +48,10 @@ NetworkReadStream::NetworkReadStream(const char *url, curl_slist *headersList, C 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 - curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList); - if (postFields.size() != 0) { - curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, postFields.size()); - curl_easy_setopt(_easy, CURLOPT_COPYPOSTFIELDS, postFields.c_str()); + curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList); + if (post || bufferSize != 0) { + curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, bufferSize); + curl_easy_setopt(_easy, CURLOPT_COPYPOSTFIELDS, buffer); } ConnMan.registerEasyHandle(_easy); } diff --git a/backends/networking/curl/networkreadstream.h b/backends/networking/curl/networkreadstream.h index 33edade8d7..f1f41264aa 100644 --- a/backends/networking/curl/networkreadstream.h +++ b/backends/networking/curl/networkreadstream.h @@ -38,6 +38,7 @@ class NetworkReadStream: public Common::MemoryReadWriteStream { public: NetworkReadStream(const char *url, curl_slist *headersList, Common::String postFields); + NetworkReadStream(const char *url, curl_slist *headersList, byte *buffer, uint32 bufferSize, bool post = true); virtual ~NetworkReadStream(); /** -- cgit v1.2.3