@@ -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}
0 commit comments