fix: handle webseed servers that do not serve partial content (#4601)

Fixes #4595.
This commit is contained in:
Charles Kerr 2023-01-16 15:08:25 -06:00 committed by GitHub
parent bd461e1081
commit acd5d28fb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 1 deletions

View File

@ -431,6 +431,31 @@ public:
auto* task = static_cast<Task*>(vtask);
TR_ASSERT(std::this_thread::get_id() == task->impl.curl_thread->get_id());
if (auto const range = task->range(); range)
{
// https://curl.se/libcurl/c/CURLINFO_RESPONSE_CODE.html
// "The stored value will be zero if no server response code has been received"
static auto constexpr NoResponseCode = 0L;
static auto constexpr PartialContentResponseCode = 206L;
// Test for webservers that don't support partial-content, see GH #4595
auto code = long{};
(void)curl_easy_getinfo(task->easy(), CURLINFO_RESPONSE_CODE, &code);
if (code != NoResponseCode && code != PartialContentResponseCode)
{
tr_logAddWarn(fmt::format(
_("Couldn't fetch '{url}': expected HTTP response code {expected_code}, got {actual_code}"),
fmt::arg("url", task->url()),
fmt::arg("expected_code", PartialContentResponseCode),
fmt::arg("actual_code", code)));
// Tell curl to error out. Returning anything that's not
// `bytes_used` signals an error and causes the transfer
// to be aborted w/CURLE_WRITE_ERROR.
return bytes_used + 1;
}
}
if (auto const& tag = task->speedLimitTag(); tag)
{
// If this is more bandwidth than is allocated for this tag,
@ -530,7 +555,7 @@ public:
(void)curl_easy_setopt(e, CURLOPT_URL, task.url().c_str());
(void)curl_easy_setopt(e, CURLOPT_VERBOSE, curl_verbose ? 1L : 0L);
(void)curl_easy_setopt(e, CURLOPT_WRITEDATA, &task);
(void)curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, onDataReceived);
(void)curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, &tr_web::Impl::onDataReceived);
(void)curl_easy_setopt(e, CURLOPT_MAXREDIRS, MaxRedirects);
if (auto const addrstr = task.publicAddress(); addrstr)