diff options
| author | Alexander Tkachev | 2016-07-26 13:58:02 +0600 | 
|---|---|---|
| committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 | 
| commit | a2e019972779575bcc9038949bca73d78f7bf07b (patch) | |
| tree | 7345209ff1b3b7313811abb730a63968537ef1ce /backends | |
| parent | 166d1121e5973868b24aa3db6156028edb1d6a72 (diff) | |
| download | scummvm-rg350-a2e019972779575bcc9038949bca73d78f7bf07b.tar.gz scummvm-rg350-a2e019972779575bcc9038949bca73d78f7bf07b.tar.bz2 scummvm-rg350-a2e019972779575bcc9038949bca73d78f7bf07b.zip | |
CLOUD: Update BoxUploadRequest
More JSON checks there.
Diffstat (limited to 'backends')
| -rw-r--r-- | backends/cloud/box/boxuploadrequest.cpp | 93 | 
1 files changed, 53 insertions, 40 deletions
| diff --git a/backends/cloud/box/boxuploadrequest.cpp b/backends/cloud/box/boxuploadrequest.cpp index f68ba6a580..929c9ceb75 100644 --- a/backends/cloud/box/boxuploadrequest.cpp +++ b/backends/cloud/box/boxuploadrequest.cpp @@ -145,56 +145,69 @@ void BoxUploadRequest::uploadedCallback(Networking::JsonResponse response) {  	if (error.httpResponseCode != 200 && error.httpResponseCode != 201)  		warning("BoxUploadRequest: looks like an error (bad HTTP code)"); -	//TODO: add more JSON warnings there +	//check JSON and show warnings if it's malformed  	Common::JSONValue *json = response.value; -	if (json) { -		if (json->isObject()) { -			Common::JSONObject object = json->asObject(); -			if (object.contains("entries") && object.getVal("entries")->isArray()) { -				Common::JSONArray entries = object.getVal("entries")->asArray(); -				if (entries.size() > 0) { -					Common::JSONObject entry = entries[0]->asObject(); - -					//finished -					Common::String id = entry.getVal("id")->asString(); -					Common::String name = entry.getVal("name")->asString(); -					bool isDirectory = (entry.getVal("type")->asString() == "folder"); -					uint32 size = 0, timestamp = 0; -					if (entry.contains("size")) { -						if (entry.getVal("size")->isString()) -							size = entry.getVal("size")->asString().asUint64(); -						else if (entry.getVal("size")->isIntegerNumber()) -							size = entry.getVal("size")->asIntegerNumber(); -						else -							warning("strange type for field 'size'"); -					} -					if (entry.contains("modified_at") && entry.getVal("modified_at")->isString()) -						timestamp = ISO8601::convertToTimestamp(entry.getVal("modified_at")->asString()); - -					//as we list directory by id, we can't determine full path for the file, so we leave it empty -					finishUpload(StorageFile(id, _savePath, name, size, timestamp, isDirectory)); -					return; +	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; +	} + +	Common::JSONObject object = json->asObject(); +	if (Networking::CurlJsonRequest::jsonContainsArray(object, "entries", "BoxUploadRequest")) { +		Common::JSONArray entries = object.getVal("entries")->asArray(); +		if (entries.size() == 0) { +			warning("BoxUploadRequest: 'entries' found, but it's empty"); +		} else if (Networking::CurlJsonRequest::jsonIsObject(entries[0], "BoxUploadRequest")) { +			warning("BoxUploadRequest: 'entries' first item is not an object"); +		} else { +			Common::JSONObject item = entries[0]->asObject(); + +			if (Networking::CurlJsonRequest::jsonContainsString(item, "id", "BoxUploadRequest") && +				Networking::CurlJsonRequest::jsonContainsString(item, "name", "BoxUploadRequest") && +				Networking::CurlJsonRequest::jsonContainsString(item, "type", "BoxUploadRequest") && +				Networking::CurlJsonRequest::jsonContainsString(item, "modified_at", "BoxUploadRequest") && +				Networking::CurlJsonRequest::jsonContainsStringOrIntegerNumber(item, "size", "BoxUploadRequest")) { + +				//finished +				Common::String id = item.getVal("id")->asString(); +				Common::String name = item.getVal("name")->asString(); +				bool isDirectory = (item.getVal("type")->asString() == "folder"); +				uint32 size; +				if (item.getVal("size")->isString()) { +					size = item.getVal("size")->asString().asUint64(); +				} else { +					size = item.getVal("size")->asIntegerNumber();  				} -			} +				uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("modified_at")->asString()); -			//TODO: check errors -			/* -			if (object.contains("error")) { -				warning("Box returned error: %s", json->stringify(true).c_str()); +				finishUpload(StorageFile(id, _savePath, name, size, timestamp, isDirectory));  				delete json; -				error.response = json->stringify(true); -				finishError(error);  				return;  			} -			*/  		} +	} -		warning("BoxUploadRequest: no file info to return"); -		finishUpload(StorageFile(_savePath, 0, 0, false)); -	} else { -		warning("BoxUploadRequest: null, not json"); +	//TODO: check errors +	/* +	if (object.contains("error")) { +		warning("Box returned error: %s", json->stringify(true).c_str()); +		delete json; +		error.response = json->stringify(true);  		finishError(error); +		return;  	} +	*/ + +	warning("BoxUploadRequest: no file info to return"); +	finishUpload(StorageFile(_savePath, 0, 0, false));  	delete json;  } | 
