aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/curl/curljsonrequest.cpp
diff options
context:
space:
mode:
authorAlexander Tkachev2016-05-17 20:17:41 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commit5df8c5140292520bafe92efa94935a776d63d108 (patch)
tree7ea4faf4d7169411383eb5783dfbfd5fe2032dad /backends/networking/curl/curljsonrequest.cpp
parent03217cd5c3de3c17739a246f5967dfd4a14eb120 (diff)
downloadscummvm-rg350-5df8c5140292520bafe92efa94935a776d63d108.tar.gz
scummvm-rg350-5df8c5140292520bafe92efa94935a776d63d108.tar.bz2
scummvm-rg350-5df8c5140292520bafe92efa94935a776d63d108.zip
CLOUD: Fix CurlJsonRequest
It's using MemoryWriteStreamDynamic instead of String and it prepares raw byte contents of this stream for JSON::parse().
Diffstat (limited to 'backends/networking/curl/curljsonrequest.cpp')
-rw-r--r--backends/networking/curl/curljsonrequest.cpp36
1 files changed, 29 insertions, 7 deletions
diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp
index acd9675aed..f5a7a7c8d0 100644
--- a/backends/networking/curl/curljsonrequest.cpp
+++ b/backends/networking/curl/curljsonrequest.cpp
@@ -30,7 +30,7 @@
namespace Networking {
-CurlJsonRequest::CurlJsonRequest(Callback cb, const char *url) : Request(cb), _stream(0) {
+CurlJsonRequest::CurlJsonRequest(Callback cb, const char *url) : Request(cb), _stream(0), _contentsStream(DisposeAfterUse::YES) {
_url = url;
}
@@ -38,6 +38,24 @@ CurlJsonRequest::~CurlJsonRequest() {
if (_stream) delete _stream;
}
+char *CurlJsonRequest::getPreparedContents() {
+ //write one more byte in the end
+ byte zero[1] = { 0 };
+ _contentsStream.write(zero, 1);
+
+ //replace all "bad" bytes with '.' character
+ byte *result = _contentsStream.getData();
+ uint32 size = _contentsStream.size();
+ for (uint32 i = 0; i < size; ++i)
+ if (result[i] < 0x20 || result[i] > 0x7f)
+ result[i] = '.';
+
+ //make it zero-terminated string
+ result[size - 1] = '\0';
+
+ return (char *)result;
+}
+
bool CurlJsonRequest::handle(ConnectionManager &manager) {
if (!_stream) _stream = manager.makeRequest(_url);
@@ -45,13 +63,17 @@ bool CurlJsonRequest::handle(ConnectionManager &manager) {
const int kBufSize = 16*1024;
char buf[kBufSize+1];
uint32 readBytes = _stream->read(buf, kBufSize);
- if (readBytes != 0) _contents += Common::String(buf, readBytes);
- if (_stream->eos()) {
- //TODO: check that stream's CURL easy handle's HTTP response code is 200 OK
- debug("CurlJsonRequest: contents:\n%s", _contents.c_str());
+ if (readBytes != 0)
+ if (_contentsStream.write(buf, readBytes) != readBytes)
+ warning("MemoryWriteStreamDynamic was unable to write all the bytes");
+
+ if (_stream->eos()) {
+ if (_stream->httpResponseCode() != 200)
+ warning("HTTP response code is not 200 OK");
+
if (_callback) {
- Common::JSONValue *json = Common::JSON::parse(_contents.c_str()); //TODO: somehow fix JSON to work with UTF-8
- debug("CurlJsonRequest: JSONValue pointer = %p", json);
+ char *contents = getPreparedContents();
+ Common::JSONValue *json = Common::JSON::parse(contents);
_callback(json); //potential memory leak, free it in your callbacks!
}
return true;