HTTP: Don't override status codes that are not OK

This commit is contained in:
Yonas Habteab 2020-12-04 18:25:48 +01:00
parent 6f33c2f90c
commit 066db5ef60
1 changed files with 22 additions and 3 deletions

View File

@ -100,12 +100,31 @@ bool ActionsHandler::HandleRequest(
}
int statusCode = 500;
std::set<int> okStatusCodes, nonOkStatusCodes;
for (const Dictionary::Ptr& res : results) {
if (res->Contains("code") && res->Get("code") >= 200 && res->Get("code") <= 299) {
statusCode = 200;
break;
if (!res->Contains("code")) {
continue;
}
auto code = res->Get("code");
if (code >= 200 && code <= 299) {
okStatusCodes.insert(code);
} else {
nonOkStatusCodes.insert(code);
}
}
size_t okSize = okStatusCodes.size();
size_t nonOkSize = nonOkStatusCodes.size();
if (okSize == 1u && nonOkSize == 0u) {
statusCode = *okStatusCodes.begin();
} else if (nonOkSize == 1u) {
statusCode = *nonOkStatusCodes.begin();
} else if (okSize >= 2u && nonOkSize == 0u) {
statusCode = 200;
}
response.result(statusCode);