1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H
24 #define BACKENDS_NETWORKING_CURL_CURLJSONREQUEST_H
25 
26 #include "backends/networking/curl/curlrequest.h"
27 #include "common/memstream.h"
28 #include "common/json.h"
29 
30 namespace Networking {
31 
32 typedef Response<Common::JSONValue *> JsonResponse;
33 typedef Common::BaseCallback<JsonResponse> *JsonCallback;
34 
35 #define CURL_JSON_REQUEST_BUFFER_SIZE 512 * 1024
36 
37 class CurlJsonRequest: public CurlRequest {
38 protected:
39 	JsonCallback _jsonCallback;
40 	Common::MemoryWriteStreamDynamic _contentsStream;
41 	byte *_buffer;
42 
43 	/** Prepares raw bytes from _contentsStream to be parsed with Common::JSON::parse(). */
44 	char *getPreparedContents();
45 
46 	/** Sets FINISHED state and passes the JSONValue * into user's callback in JsonResponse. */
47 	virtual void finishJson(Common::JSONValue *json);
48 
49 public:
50 	CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url);
51 	virtual ~CurlJsonRequest();
52 
53 	virtual void handle();
54 	virtual void restart();
55 
56 	static bool jsonIsObject(Common::JSONValue *item, const char *warningPrefix);
57 	static bool jsonContainsObject(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
58 	static bool jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
59 	static bool jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
60 	static bool jsonContainsArray(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
61 	static bool jsonContainsStringOrIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
62 	static bool jsonContainsAttribute(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false);
63 };
64 
65 } // End of namespace Networking
66 
67 #endif
68