Skip to content

Commit e1c9cf0

Browse files
committed
Revert HTTP cache
Partial revert of ee8f491 We've had report of failing updates recently, and this is the most likely culprit
1 parent f1a7e02 commit e1c9cf0

14 files changed

+22
-320
lines changed

thcrap_test/src/repo_discovery.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FakeHttpHandle : public IHttpHandle
2727
files.clear();
2828
}
2929

30-
HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)>, DownloadCache*) override
30+
HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)>) override
3131
{
3232
auto it = files.find(url);
3333
if (it == files.end()) {

thcrap_update/src/download_cache.cpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

thcrap_update/src/download_cache.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

thcrap_update/src/downloader.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ std::list<DownloadUrl> Downloader::serversListToDownloadUrlList(const std::list<
2222
}
2323

2424
void Downloader::addFile(const std::list<std::string>& serversUrl, std::string filePath,
25-
File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback,
26-
DownloadCache *cache)
25+
File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback)
2726
{
2827
std::scoped_lock lock(this->mutex);
2928

@@ -32,7 +31,7 @@ void Downloader::addFile(const std::list<std::string>& serversUrl, std::string f
3231
current_++;
3332
return successCallback(url, data);
3433
};
35-
std::shared_ptr<File> file = std::make_unique<File>(std::move(urls), successLambda, failureCallback, progressCallback, cache);
34+
std::shared_ptr<File> file = std::make_unique<File>(std::move(urls), successLambda, failureCallback, progressCallback);
3635

3736
this->futuresList.push_back(this->pool.enqueue([file]() {
3837
file->download();
@@ -42,15 +41,14 @@ void Downloader::addFile(const std::list<std::string>& serversUrl, std::string f
4241
}
4342

4443
void Downloader::addFile(char** serversUrl, std::string filePath,
45-
File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback,
46-
DownloadCache *cache)
44+
File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback)
4745
{
4846
std::list<std::string> serversList;
4947

5048
for (size_t i = 0; serversUrl && serversUrl[i]; i++) {
5149
serversList.push_back(serversUrl[i]);
5250
}
53-
this->addFile(serversList, filePath, successCallback, failureCallback, progressCallback, cache);
51+
this->addFile(serversList, filePath, successCallback, failureCallback, progressCallback);
5452
}
5553

5654
size_t Downloader::current() const

thcrap_update/src/downloader.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <atomic>
77
#include <libs/include/ThreadPool.h>
88
#include "file.h"
9-
#include "download_cache.h"
109

1110
class Downloader
1211
{
@@ -25,13 +24,11 @@ class Downloader
2524
void addFile(const std::list<std::string>& servers, std::string filename,
2625
File::success_t successCallback = File::defaultSuccessFunction,
2726
File::failure_t failureCallback = File::defaultFailureFunction,
28-
File::progress_t progressCallback = File::defaultProgressFunction,
29-
DownloadCache *cache = nullptr);
27+
File::progress_t progressCallback = File::defaultProgressFunction);
3028
void addFile(char** servers, std::string filename,
3129
File::success_t successCallback = File::defaultSuccessFunction,
3230
File::failure_t failureCallback = File::defaultFailureFunction,
33-
File::progress_t progressCallback = File::defaultProgressFunction,
34-
DownloadCache *cache = nullptr);
31+
File::progress_t progressCallback = File::defaultProgressFunction);
3532
size_t current() const;
3633
size_t total() const;
3734
void wait();

thcrap_update/src/file.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,15 @@ using namespace std::string_literals;
1010
File::File(std::list<DownloadUrl>&& urls,
1111
success_t successCallback,
1212
failure_t failureCallback,
13-
progress_t progressCallback,
14-
DownloadCache *cache)
15-
: status(Status::Todo), urls(urls), cache(cache),
13+
progress_t progressCallback)
14+
: status(Status::Todo), urls(urls),
1615
userSuccessCallback(successCallback), userFailureCallback(failureCallback), userProgressCallback(progressCallback)
1716
{
1817
if (urls.empty()) {
1918
throw new std::invalid_argument("Input URL list must not be empty");
2019
}
2120
}
2221

23-
File::~File()
24-
{
25-
if (this->cache) {
26-
delete this->cache;
27-
}
28-
}
29-
3022
size_t File::writeCallback(std::vector<uint8_t>& buffer, const uint8_t *data, size_t size)
3123
{
3224
buffer.insert(buffer.end(), data, data + size);
@@ -67,8 +59,7 @@ void File::download(IHttpHandle& http, const DownloadUrl& url)
6759
},
6860
[this, &url](size_t dlnow, size_t dltotal) {
6961
return this->progressCallback(url, dlnow, dltotal);
70-
},
71-
this->cache
62+
}
7263
);
7364
if (!status) {
7465
if (status.get() == HttpStatus::ServerError || status.get() == HttpStatus::SystemError) {

thcrap_update/src/file.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <list>
66
#include <mutex>
77
#include <vector>
8-
#include "download_cache.h"
98
#include "download_url.h"
109
#include "http_interface.h"
1110

@@ -34,7 +33,6 @@ class File
3433
// different servers.
3534
// When starting a download, we remove the corresponding URL from the list.
3635
std::list<DownloadUrl> urls;
37-
DownloadCache *cache;
3836

3937
// User-provided callbacks
4038
success_t userSuccessCallback;
@@ -50,13 +48,11 @@ class File
5048
File(std::list<DownloadUrl>&& urls,
5149
success_t successCallback = defaultSuccessFunction,
5250
failure_t failureCallback = defaultFailureFunction,
53-
progress_t progressCallback = defaultProgressFunction,
54-
DownloadCache *cache = nullptr);
51+
progress_t progressCallback = defaultProgressFunction);
5552
File(const File&) = delete;
5653
File(File&&) = delete;
5754
File& operator=(const File&) = delete;
5855
File& operator=(File&&) = delete;
59-
~File();
6056

6157
// Start a download thread.
6258
// The status of the download will be returned in the callbacks given

thcrap_update/src/http_curl.cpp

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -70,59 +70,7 @@ static std::string encode_UTF8_url(std::string in)
7070
return out;
7171
}
7272

73-
static curl_slist *setup_headers(DownloadCache *cache)
74-
{
75-
if (!cache) {
76-
return nullptr;
77-
}
78-
curl_slist *headers = nullptr;
79-
80-
std::optional<std::string> etag = cache->getEtag();
81-
if (etag) {
82-
BUILD_VLA_STR(char, header, "If-None-Match: ", etag->c_str());
83-
headers = curl_slist_append(headers, header);
84-
VLA_FREE(header);
85-
}
86-
87-
std::optional<std::string> lastModifiedDate = cache->getLastModifiedDate();
88-
if (lastModifiedDate) {
89-
BUILD_VLA_STR(char, header, "If-Modified-Since: ", lastModifiedDate->c_str());
90-
headers = curl_slist_append(headers, header);
91-
VLA_FREE(header);
92-
}
93-
94-
return headers;
95-
}
96-
97-
void update_cache(CURL *curl, DownloadCache *cache)
98-
{
99-
if (!cache) {
100-
return ;
101-
}
102-
103-
// The CURL doc says that the header struct is clobbered between
104-
// subsequent calls, the strings inside it might also be clobbered
105-
// (I don't think they are, but the doc doesn't guarantee it).
106-
// Better play it safe and make a copy of the 1st header.
107-
std::string etag;
108-
const char *lastModifiedDate = nullptr;
109-
110-
curl_header *header;
111-
if (curl_easy_header(curl, "ETag", 0, CURLH_HEADER, -1, &header) == CURLHE_OK) {
112-
etag = header->value;
113-
}
114-
115-
if (curl_easy_header(curl, "Last-Modified", 0, CURLH_HEADER, -1, &header) == CURLHE_OK) {
116-
lastModifiedDate = header->value;
117-
if (lastModifiedDate && !lastModifiedDate[0]) {
118-
lastModifiedDate = nullptr;
119-
}
120-
}
121-
122-
cache->setCacheData(!etag.empty() ? etag.c_str() : nullptr, lastModifiedDate);
123-
}
124-
125-
HttpStatus CurlHandle::download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback, DownloadCache *cache)
73+
HttpStatus CurlHandle::download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback)
12674
{
12775
curl_easy_setopt(this->curl, CURLOPT_CAINFO, "bin/cacert.pem");
12876
curl_easy_setopt(this->curl, CURLOPT_FOLLOWLOCATION, 1);
@@ -142,9 +90,6 @@ HttpStatus CurlHandle::download(const std::string& url, std::function<size_t(con
14290
curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, errbuf);
14391
errbuf[0] = 0;
14492

145-
curl_slist *headers = setup_headers(cache);
146-
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
147-
14893
std::string url_encoded = encode_UTF8_url(url);
14994
curl_easy_setopt(this->curl, CURLOPT_URL, url_encoded.c_str());
15095

@@ -172,28 +117,12 @@ HttpStatus CurlHandle::download(const std::string& url, std::function<size_t(con
172117
}
173118
}
174119
curl_easy_setopt(this->curl, CURLOPT_ERRORBUFFER, nullptr);
175-
if (headers) {
176-
curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, nullptr);
177-
curl_slist_free_all(headers);
178-
}
179120

180121
long response_code = 0;
181122
curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &response_code);
182-
if (response_code == 200) {
183-
update_cache(this->curl, cache);
184-
return HttpStatus::makeOk();
185-
}
186-
else if (response_code == 304) {
187-
if (!cache) {
188-
throw std::logic_error("Got 304 for a non-cached object");
189-
}
190-
std::vector<uint8_t> data = cache->getCachedFile();
191-
progressCallback(data.size(), data.size());
192-
writeCallback(data.data(), data.size());
193-
update_cache(this->curl, cache);
194-
return HttpStatus::makeOk();
195-
}
196-
else {
123+
if (response_code != 200) {
197124
return HttpStatus::makeNetworkError(response_code);
198125
}
126+
127+
return HttpStatus::makeOk();
199128
}

thcrap_update/src/http_curl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <string>
55
#include <curl/curl.h>
66
#include "http_interface.h"
7-
#include "download_cache.h"
87

98
class CurlHandle : public IHttpHandle
109
{
@@ -21,5 +20,5 @@ class CurlHandle : public IHttpHandle
2120
CurlHandle& operator=(CurlHandle& other) = delete;
2221
~CurlHandle();
2322

24-
HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback, DownloadCache *cache) override;
23+
HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback) override;
2524
};

thcrap_update/src/http_interface.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#pragma once
22

3-
#include <string>
43
#include "http_status.h"
5-
#include "download_cache.h"
64

75
class IHttpHandle
86
{
97
public:
108
virtual ~IHttpHandle() {}
11-
virtual HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback, DownloadCache *cache) = 0;
9+
virtual HttpStatus download(const std::string& url, std::function<size_t(const uint8_t*, size_t)> writeCallback, std::function<bool(size_t, size_t)> progressCallback) = 0;
1210
};

0 commit comments

Comments
 (0)