diff options
author | Eugene Sandulenko | 2019-10-23 23:09:50 +0200 |
---|---|---|
committer | Eugene Sandulenko | 2019-10-24 00:15:33 +0200 |
commit | 0ad210a5a477c4d4241e03a54c7007ac669a3a3c (patch) | |
tree | c12a06988bee9e70ab496053facd21507e5182e0 /backends/networking/curl | |
parent | f11c81c5f04b4a6f98fa5e9d53bbd3765755478b (diff) | |
download | scummvm-rg350-0ad210a5a477c4d4241e03a54c7007ac669a3a3c.tar.gz scummvm-rg350-0ad210a5a477c4d4241e03a54c7007ac669a3a3c.tar.bz2 scummvm-rg350-0ad210a5a477c4d4241e03a54c7007ac669a3a3c.zip |
NETWORKING: Added simple request handler
Diffstat (limited to 'backends/networking/curl')
-rw-r--r-- | backends/networking/curl/postrequest.cpp | 110 | ||||
-rw-r--r-- | backends/networking/curl/postrequest.h | 56 |
2 files changed, 166 insertions, 0 deletions
diff --git a/backends/networking/curl/postrequest.cpp b/backends/networking/curl/postrequest.cpp new file mode 100644 index 0000000000..0becc8eef9 --- /dev/null +++ b/backends/networking/curl/postrequest.cpp @@ -0,0 +1,110 @@ +/* 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/networking/curl/postrequest.h" +#include "backends/networking/curl/connectionmanager.h" +#include "backends/networking/curl/curljsonrequest.h" +#include "backends/networking/curl/networkreadstream.h" +#include "common/json.h" + +namespace Networking { + +PostRequest::PostRequest(Common::String url, byte *postData, int postLen, Networking::JsonCallback cb, Networking::ErrorCallback ecb): + Networking::Request(nullptr, ecb), _url(url), _jsonCallback(cb), + _workingRequest(nullptr), _ignoreCallback(false), _postData(postData), _postLen(postLen) { + start(); +} + +PostRequest::~PostRequest() { + _ignoreCallback = true; + if (_workingRequest) + _workingRequest->finish(); + delete _jsonCallback; +} + +void PostRequest::start() { + _ignoreCallback = true; + if (_workingRequest) + _workingRequest->finish(); + _ignoreCallback = false; + + 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); + + _workingRequest = ConnMan.addRequest(request); +} + +void PostRequest::responseCallback(Networking::JsonResponse response) { + Common::JSONValue *json = response.value; + _workingRequest = nullptr; + if (_ignoreCallback) { + delete json; + return; + } + if (response.request) _date = response.request->date(); + + Networking::ErrorResponse error(this, "PostRequest::responseCallback: unknown error"); + Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request; + if (rq && rq->getNetworkReadStream()) + error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode(); + + if (json == nullptr) { + error.response = "Failed to parse JSON, null passed!"; + finishError(error); + return; + } + + if (!json->isObject()) { + error.response = "Passed JSON is not an object!"; + finishError(error); + delete json; + return; + } + + finishSuccess(); + + if (_jsonCallback) + (*_jsonCallback)(Networking::JsonResponse(this, json)); + + delete json; +} + +void PostRequest::errorCallback(Networking::ErrorResponse error) { + _workingRequest = nullptr; + if (_ignoreCallback) + return; + if (error.request) + _date = error.request->date(); + finishError(error); +} + +void PostRequest::handle() {} + +void PostRequest::restart() { start(); } + +Common::String PostRequest::date() const { return _date; } + +} // End of namespace Networking diff --git a/backends/networking/curl/postrequest.h b/backends/networking/curl/postrequest.h new file mode 100644 index 0000000000..775af31a3f --- /dev/null +++ b/backends/networking/curl/postrequest.h @@ -0,0 +1,56 @@ +/* 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_POSTREQUEST_H +#define BACKENDS_NETWORKING_CURL_POSTREQUEST_H + +#include "backends/networking/curl/request.h" +#include "backends/networking/curl/curljsonrequest.h" + +namespace Networking { + +class PostRequest: public Networking::Request { + Common::String _url; + Networking::JsonCallback _jsonCallback; + Request *_workingRequest; + bool _ignoreCallback; + Common::String _date; + + byte *_postData; + int _postLen; + + void start(); + void responseCallback(Networking::JsonResponse response); + void errorCallback(Networking::ErrorResponse error); + +public: + PostRequest(Common::String url, byte *postData, int postLen, Networking::JsonCallback cb, Networking::ErrorCallback ecb); + virtual ~PostRequest(); + + virtual void handle(); + virtual void restart(); + virtual Common::String date() const; +}; + +} // End of namespace Networking + +#endif |