CLOUD: Update Dropbox Requests

Adding more JSON checks there.
This commit is contained in:
Alexander Tkachev 2016-07-26 14:10:54 +06:00
parent a2e0199727
commit 6be736b5ed
2 changed files with 65 additions and 14 deletions

View file

@ -79,19 +79,27 @@ void DropboxCreateDirectoryRequest::responseCallback(Networking::JsonResponse re
if (rq && rq->getNetworkReadStream()) if (rq && rq->getNetworkReadStream())
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode(); error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
if (!json) { if (json == nullptr) {
warning("DropboxCreateDirectoryRequest: NULL passed instead of JSON"); error.response = "Failed to parse JSON, null passed!";
finishError(error); finishError(error);
return; return;
} }
if (!json->isObject()) {
error.response = "Passed JSON is not an object!";
finishError(error);
delete json;
return;
}
Common::JSONObject info = json->asObject(); Common::JSONObject info = json->asObject();
if (info.contains("id")) { if (info.contains("id")) {
finishCreation(true); finishCreation(true);
} else { } else {
if (info.contains("error_summary") && info.getVal("error_summary")->isString()) { if (Networking::CurlJsonRequest::jsonContainsString(info, "error_summary", "DropboxCreateDirectoryRequest")) {
Common::String summary = info.getVal("error_summary")->asString(); Common::String summary = info.getVal("error_summary")->asString();
if (summary.contains("path") && summary.contains("conflict") && summary.contains("folder")) { if (summary.contains("path") && summary.contains("conflict") && summary.contains("folder")) {
// existing directory - not an error for CreateDirectoryRequest
finishCreation(false); finishCreation(false);
delete json; delete json;
return; return;

View file

@ -76,18 +76,34 @@ void DropboxInfoRequest::userResponseCallback(Networking::JsonResponse response)
if (rq && rq->getNetworkReadStream()) if (rq && rq->getNetworkReadStream())
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode(); error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
if (!json) { if (json == nullptr) {
warning("DropboxInfoRequest: NULL passed instead of JSON"); error.response = "Failed to parse JSON, null passed!";
finishError(error); finishError(error);
return; return;
} }
if (!json->isObject()) {
error.response = "Passed JSON is not an object!";
finishError(error);
delete json;
return;
}
//Dropbox documentation states there are no errors for this API method //Dropbox documentation states there are no errors for this API method
Common::JSONObject info = json->asObject(); Common::JSONObject info = json->asObject();
if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "name", "DropboxInfoRequest") &&
Networking::CurlJsonRequest::jsonIsObject(info.getVal("name"), "DropboxInfoRequest")) {
Common::JSONObject nameInfo = info.getVal("name")->asObject(); Common::JSONObject nameInfo = info.getVal("name")->asObject();
_uid = info.getVal("account_id")->asString(); if (Networking::CurlJsonRequest::jsonContainsString(nameInfo, "display_name", "DropboxInfoRequest")) {
_name = nameInfo.getVal("display_name")->asString(); _name = nameInfo.getVal("display_name")->asString();
}
}
if (Networking::CurlJsonRequest::jsonContainsString(info, "account_id", "DropboxInfoRequest")) {
_uid = info.getVal("account_id")->asString();
}
if (Networking::CurlJsonRequest::jsonContainsString(info, "email", "DropboxInfoRequest")) {
_email = info.getVal("email")->asString(); _email = info.getVal("email")->asString();
}
CloudMan.setStorageUsername(kStorageDropboxId, _email); CloudMan.setStorageUsername(kStorageDropboxId, _email);
delete json; delete json;
@ -114,17 +130,44 @@ void DropboxInfoRequest::quotaResponseCallback(Networking::JsonResponse response
if (rq && rq->getNetworkReadStream()) if (rq && rq->getNetworkReadStream())
error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode(); error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
if (!json) { if (json == nullptr) {
warning("DropboxInfoRequest: NULL passed instead of JSON"); error.response = "Failed to parse JSON, null passed!";
finishError(error); finishError(error);
return; return;
} }
if (!json->isObject()) {
error.response = "Passed JSON is not an object!";
finishError(error);
delete json;
return;
}
//Dropbox documentation states there are no errors for this API method //Dropbox documentation states there are no errors for this API method
Common::JSONObject info = json->asObject(); Common::JSONObject info = json->asObject();
if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(info, "used", "DropboxInfoRequest")) {
error.response = "Passed JSON misses 'used' attribute!";
finishError(error);
delete json;
return;
}
uint64 used = info.getVal("used")->asIntegerNumber(), allocated = 0;
if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "allocation", "DropboxInfoRequest") &&
Networking::CurlJsonRequest::jsonIsObject(info.getVal("allocation"), "DropboxInfoRequest")) {
Common::JSONObject allocation = info.getVal("allocation")->asObject(); Common::JSONObject allocation = info.getVal("allocation")->asObject();
uint64 used = info.getVal("used")->asIntegerNumber(); if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(allocation, "allocated", "DropboxInfoRequest")) {
uint64 allocated = allocation.getVal("allocated")->asIntegerNumber(); error.response = "Passed JSON misses 'allocation/allocated' attribute!";
finishError(error);
delete json;
return;
}
allocated = allocation.getVal("allocated")->asIntegerNumber();
}
finishInfo(StorageInfo(_uid, _name, _email, used, allocated)); finishInfo(StorageInfo(_uid, _name, _email, used, allocated));
delete json; delete json;
} }