Skip to content

Commit

Permalink
Handle empty responses from errors
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed May 5, 2024
1 parent 702c934 commit 19016cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/https.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,26 @@ EM_JS(void, em_https_request, (const char* type, const char* url, const char* bo
Module._free(response_buffer);
} else {
console.log('The request failed: ' + xhr.status + ' ' + xhr.statusText);
Module.ccall('em_https_request_callback_wrapper', 'void', ['number', 'number', 'number'], [callback, 0, 0]);
}
};
xhr.onerror = function() {
console.log('The request failed!');
Module.ccall('em_https_request_callback_wrapper', 'void', ['number', 'number', 'number'], [callback, 0, 0]);
};
xhr.send(body_arr);
});


extern "C" void em_https_request_callback_wrapper(void* callback, void* data, int size)
{
std::vector<uint8_t> result((uint8_t*)data, (uint8_t*)data + (size_t)size);
std::vector<uint8_t> result;

if (size != 0)
{
result = std::vector<uint8_t>((uint8_t*)data, (uint8_t*)data + (size_t)size);
}

std::function<void(const std::vector<uint8_t>&)>* fcallback =
(std::function<void(const std::vector<uint8_t>&)>*)callback;
(*fcallback)(result);
Expand Down
7 changes: 7 additions & 0 deletions src/retro_achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,13 @@ void ra_state_t::download(ra_game_state_ptr game_state, const std::string& url,
// The image is not already downloaded, let's download it
https_request(http_request_e::GET, url, {}, {},
[url, game_state, callback](const std::vector<uint8_t>& result) {
if (result.empty())
{
printf("[rcheevos]: empty response from: %s\n", url.c_str());
game_state->dec();
return;
}

rc_api_server_response_t response;
response.body = (const char*)result.data();
response.body_length = result.size();
Expand Down

0 comments on commit 19016cc

Please sign in to comment.