Change HTTPCallback::OnReceiveData to use UniqueBuffer

See: https://github.com/OpenTTD/OpenTTD/issues/11636
This commit is contained in:
Jonathan G Rennison
2023-12-30 19:51:58 +00:00
parent f457f306ce
commit f8085683fb
8 changed files with 23 additions and 37 deletions

View File

@@ -608,23 +608,19 @@ void ClientNetworkContentSocketHandler::OnFailure()
}
}
void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
void ClientNetworkContentSocketHandler::OnReceiveData(UniqueBuffer<char> data)
{
assert(data == nullptr || length != 0);
assert(data.get() == nullptr || data.size() != 0);
/* Ignore any latent data coming from a connection we closed. */
if (this->http_response_index == -2) {
if (data != nullptr) {
free(data);
}
return;
}
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);
free(data);
this->http_response.insert(this->http_response.end(), data.get(), data.get() + data.size());
return;
} else {
/* Make sure the response is properly terminated. */
@@ -637,16 +633,15 @@ 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, data.size(), this->curFile) != data.size()) {
/* Writing failed somehow, let try via the old method. */
this->OnFailure();
} else {
/* Just received the data. */
this->OnDownloadProgress(this->curInfo, (int)length);
this->OnDownloadProgress(this->curInfo, (int)data.size());
}
/* Nothing more to do now. */
free(data);
return;
}