Fix: race-conditions in GUI updates when downloading HTTP files (#11639)

This commit is contained in:
Patric Stout
2024-01-02 22:05:25 +01:00
committed by GitHub
parent 344bdafb53
commit aef49e9933
8 changed files with 224 additions and 39 deletions

View File

@@ -602,19 +602,21 @@ void ClientNetworkContentSocketHandler::OnFailure()
}
}
void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
void ClientNetworkContentSocketHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t length)
{
assert(data == nullptr || length != 0);
assert(data.get() == nullptr || length != 0);
/* Ignore any latent data coming from a connection we closed. */
if (this->http_response_index == -2) return;
if (this->http_response_index == -2) {
return;
}
this->lastActivity = std::chrono::steady_clock::now();
if (this->http_response_index == -1) {
if (data != nullptr) {
/* Append the rest of the response. */
this->http_response.insert(this->http_response.end(), data, data + length);
this->http_response.insert(this->http_response.end(), data.get(), data.get() + length);
return;
} else {
/* Make sure the response is properly terminated. */
@@ -627,13 +629,14 @@ void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t l
if (data != nullptr) {
/* We have data, so write it to the file. */
if (fwrite(data, 1, length, this->curFile) != length) {
if (fwrite(data.get(), 1, length, this->curFile) != length) {
/* Writing failed somehow, let try via the old method. */
this->OnFailure();
} else {
/* Just received the data. */
this->OnDownloadProgress(this->curInfo, (int)length);
}
/* Nothing more to do now. */
return;
}