diff options
author | Eugene Sandulenko | 2019-10-24 17:30:25 +0200 |
---|---|---|
committer | Eugene Sandulenko | 2019-10-24 17:41:28 +0200 |
commit | bbaebe594ea7a3b6afac4e40fbb367ed7b679e94 (patch) | |
tree | 6428c008230a69d7a1e89c1845eb205aba54bf04 /backends/networking/curl | |
parent | 901118c5cd5b3fe2f0a80d1785e51a0f035663ab (diff) | |
download | scummvm-rg350-bbaebe594ea7a3b6afac4e40fbb367ed7b679e94.tar.gz scummvm-rg350-bbaebe594ea7a3b6afac4e40fbb367ed7b679e94.tar.bz2 scummvm-rg350-bbaebe594ea7a3b6afac4e40fbb367ed7b679e94.zip |
NETWORKING: Reworked PostRequest to a more universal API
Diffstat (limited to 'backends/networking/curl')
-rw-r--r-- | backends/networking/curl/postrequest.cpp | 36 | ||||
-rw-r--r-- | backends/networking/curl/postrequest.h | 12 |
2 files changed, 41 insertions, 7 deletions
diff --git a/backends/networking/curl/postrequest.cpp b/backends/networking/curl/postrequest.cpp index 1dec36b670..5e0bc1aa88 100644 --- a/backends/networking/curl/postrequest.cpp +++ b/backends/networking/curl/postrequest.cpp @@ -28,10 +28,11 @@ namespace Networking { -PostRequest::PostRequest(Common::String url, byte *postData, int postLen, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb): +PostRequest::PostRequest(Common::String url, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _url(url), _jsonCallback(cb), - _workingRequest(nullptr), _ignoreCallback(false), _postData(postData), _postLen(postLen) { - start(); + _workingRequest(nullptr), _ignoreCallback(false), _postData(nullptr), _postLen(0), _jsonData(nullptr) { + + _contentType = "application/octet-stream"; } PostRequest::~PostRequest() { @@ -41,6 +42,19 @@ PostRequest::~PostRequest() { delete _jsonCallback; } +void PostRequest::setPostData(byte *postData, int postLen) { + _postData = postData; + _postLen = postLen; + + _contentType = "application/octet-stream"; +} + +void PostRequest::setJSONData(Common::JSONValue *jsonData) { + _jsonData = jsonData; + + _contentType = "application/json"; +} + void PostRequest::start() { _ignoreCallback = true; if (_workingRequest) @@ -50,9 +64,21 @@ void PostRequest::start() { Networking::JsonCallback innerCallback = new Common::Callback<PostRequest, Networking::JsonResponse>(this, &PostRequest::responseCallback); Networking::ErrorCallback errorResponseCallback = new Common::Callback<PostRequest, Networking::ErrorResponse>(this, &PostRequest::errorCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorResponseCallback, _url); - request->addHeader("Content-Type: application/json"); - request->setBuffer(_postData, _postLen); + if (_postData && _jsonData) { + warning("Error, both data and JSON present while calling %s", _url.c_str()); + + _jsonData = nullptr; + } + + request->addHeader(Common::String::format("Content-Type: %s", _contentType.c_str())); + + if (_postData) + request->setBuffer(_postData, _postLen); + + + if (_jsonData) + request->addPostField(Common::JSON::stringify(_jsonData)); _workingRequest = ConnMan.addRequest(request); } diff --git a/backends/networking/curl/postrequest.h b/backends/networking/curl/postrequest.h index 568979473c..8992d3a1a3 100644 --- a/backends/networking/curl/postrequest.h +++ b/backends/networking/curl/postrequest.h @@ -37,15 +37,23 @@ class PostRequest: public Networking::Request { byte *_postData; int _postLen; + Common::JSONValue *_jsonData; + + Common::String _contentType; - void start(); void responseCallback(Networking::JsonResponse response); void errorCallback(Networking::ErrorResponse error); public: - PostRequest(Common::String url, byte *postData, int postLen, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb); + PostRequest(Common::String url, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb); virtual ~PostRequest(); + void start(); + + void setPostData(byte *postData, int postLen); + void setJSONData(Common::JSONValue *jsonData); + void setContentType(Common::String type) { _contentType = type; } + virtual void handle(); virtual void restart(); virtual Common::String date() const; |