diff options
author | Alexander Tkachev | 2016-06-08 16:46:18 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | b29497effe60eaec891ca1b5ce78f8dae69fd599 (patch) | |
tree | 825e9c832c65b01a930313f623d868a17c223713 /backends/networking/curl | |
parent | e273e3d6e8dcca6f7e70d3e7c4e6dfc836832378 (diff) | |
download | scummvm-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/networking/curl')
-rw-r--r-- | backends/networking/curl/curlrequest.cpp | 10 | ||||
-rw-r--r-- | backends/networking/curl/curlrequest.h | 4 | ||||
-rw-r--r-- | backends/networking/curl/networkreadstream.cpp | 35 | ||||
-rw-r--r-- | backends/networking/curl/networkreadstream.h | 27 |
4 files changed, 67 insertions, 9 deletions
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 |