]>
Commit | Line | Data |
---|---|---|
b9e9a44b | 1 | //-*- mode: cpp; mode: fold -*- |
d546f98d MV |
2 | // Description /*{{{*/ |
3 | // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $ | |
4 | /* ###################################################################### | |
5 | ||
1e3f4083 | 6 | HTTPS Acquire Method - This is the HTTPS acquire method for APT. |
d546f98d MV |
7 | |
8 | It uses libcurl | |
9 | ||
10 | ##################################################################### */ | |
11 | /*}}}*/ | |
12 | // Include Files /*{{{*/ | |
ea542140 DK |
13 | #include <config.h> |
14 | ||
d546f98d | 15 | #include <apt-pkg/fileutl.h> |
d546f98d MV |
16 | #include <apt-pkg/error.h> |
17 | #include <apt-pkg/hashes.h> | |
592b7800 | 18 | #include <apt-pkg/netrc.h> |
472ff00e | 19 | #include <apt-pkg/configuration.h> |
453b82a3 DK |
20 | #include <apt-pkg/macros.h> |
21 | #include <apt-pkg/strutl.h> | |
c6ee61ea | 22 | #include <apt-pkg/proxy.h> |
d546f98d MV |
23 | |
24 | #include <sys/stat.h> | |
25 | #include <sys/time.h> | |
d546f98d | 26 | #include <unistd.h> |
d546f98d | 27 | #include <stdio.h> |
d546f98d | 28 | #include <iostream> |
d546f98d | 29 | #include <sstream> |
453b82a3 DK |
30 | #include <ctype.h> |
31 | #include <stdlib.h> | |
d546f98d | 32 | |
d546f98d | 33 | #include "https.h" |
453b82a3 | 34 | |
ea542140 | 35 | #include <apti18n.h> |
d546f98d MV |
36 | /*}}}*/ |
37 | using namespace std; | |
38 | ||
27925d82 DK |
39 | struct APT_HIDDEN CURLUserPointer { |
40 | HttpsMethod * const https; | |
41 | HttpsMethod::FetchResult * const Res; | |
dcbb364f DK |
42 | HttpsMethod::FetchItem const * const Itm; |
43 | CURLUserPointer(HttpsMethod * const https, HttpsMethod::FetchResult * const Res, | |
44 | HttpsMethod::FetchItem const * const Itm) : https(https), Res(Res), Itm(Itm) {} | |
27925d82 DK |
45 | }; |
46 | ||
fd46d305 DK |
47 | size_t |
48 | HttpsMethod::parse_header(void *buffer, size_t size, size_t nmemb, void *userp) | |
49 | { | |
50 | size_t len = size * nmemb; | |
258b9e51 | 51 | CURLUserPointer *me = static_cast<CURLUserPointer *>(userp); |
fd46d305 DK |
52 | std::string line((char*) buffer, len); |
53 | for (--len; len > 0; --len) | |
74dedb4a | 54 | if (isspace_ascii(line[len]) == 0) |
fd46d305 DK |
55 | { |
56 | ++len; | |
57 | break; | |
58 | } | |
59 | line.erase(len); | |
60 | ||
61 | if (line.empty() == true) | |
62 | { | |
27925d82 | 63 | if (me->https->Server->Result != 416 && me->https->Server->StartPos != 0) |
fd46d305 | 64 | ; |
dcbb364f | 65 | else if (me->https->Server->Result == 416) |
fd46d305 | 66 | { |
dcbb364f DK |
67 | bool partialHit = false; |
68 | if (me->Itm->ExpectedHashes.usable() == true) | |
69 | { | |
70 | Hashes resultHashes(me->Itm->ExpectedHashes); | |
71 | FileFd file(me->Itm->DestFile, FileFd::ReadOnly); | |
4fc6b757 | 72 | me->https->Server->TotalFileSize = file.FileSize(); |
dcbb364f DK |
73 | me->https->Server->Date = file.ModificationTime(); |
74 | resultHashes.AddFD(file); | |
75 | HashStringList const hashList = resultHashes.GetHashStringList(); | |
76 | partialHit = (me->Itm->ExpectedHashes == hashList); | |
77 | } | |
4fc6b757 | 78 | else if (me->https->Server->Result == 416 && me->https->Server->TotalFileSize == me->https->File->FileSize()) |
dcbb364f DK |
79 | partialHit = true; |
80 | ||
81 | if (partialHit == true) | |
82 | { | |
83 | me->https->Server->Result = 200; | |
4fc6b757 | 84 | me->https->Server->StartPos = me->https->Server->TotalFileSize; |
dcbb364f DK |
85 | // the actual size is not important for https as curl will deal with it |
86 | // by itself and e.g. doesn't bother us with transport-encoding… | |
87 | me->https->Server->JunkSize = std::numeric_limits<unsigned long long>::max(); | |
88 | } | |
89 | else | |
90 | me->https->Server->StartPos = 0; | |
fd46d305 DK |
91 | } |
92 | else | |
27925d82 DK |
93 | me->https->Server->StartPos = 0; |
94 | ||
27925d82 | 95 | me->Res->LastModified = me->https->Server->Date; |
4fc6b757 | 96 | me->Res->Size = me->https->Server->TotalFileSize; |
27925d82 | 97 | me->Res->ResumePoint = me->https->Server->StartPos; |
fd46d305 | 98 | |
27925d82 | 99 | // we expect valid data, so tell our caller we get the file now |
34faa8f7 DK |
100 | if (me->https->Server->Result >= 200 && me->https->Server->Result < 300) |
101 | { | |
102 | if (me->https->Server->JunkSize == 0 && me->Res->Size != 0 && me->Res->Size > me->Res->ResumePoint) | |
103 | me->https->URIStart(*me->Res); | |
104 | if (me->https->Server->AddPartialFileToHashes(*(me->https->File)) == false) | |
105 | return 0; | |
106 | } | |
fd46d305 | 107 | } |
27925d82 | 108 | else if (me->https->Server->HeaderLine(line) == false) |
fd46d305 DK |
109 | return 0; |
110 | ||
111 | return size*nmemb; | |
112 | } | |
113 | ||
d546f98d MV |
114 | size_t |
115 | HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp) | |
116 | { | |
258b9e51 | 117 | HttpsMethod *me = static_cast<HttpsMethod *>(userp); |
ed793a19 DK |
118 | size_t buffer_size = size * nmemb; |
119 | // we don't need to count the junk here, just drop anything we get as | |
120 | // we don't always know how long it would be, e.g. in chunked encoding. | |
121 | if (me->Server->JunkSize != 0) | |
122 | return buffer_size; | |
d546f98d | 123 | |
ed793a19 | 124 | if(me->File->Write(buffer, buffer_size) != true) |
d61960d9 | 125 | return 0; |
d546f98d | 126 | |
d61960d9 | 127 | if(me->Queue->MaximumSize > 0) |
ee279506 | 128 | { |
d61960d9 DK |
129 | unsigned long long const TotalWritten = me->File->Tell(); |
130 | if (TotalWritten > me->Queue->MaximumSize) | |
131 | { | |
132 | me->SetFailReason("MaximumSizeExceeded"); | |
133 | _error->Error("Writing more data than expected (%llu > %llu)", | |
134 | TotalWritten, me->Queue->MaximumSize); | |
135 | return 0; | |
136 | } | |
ee279506 | 137 | } |
d546f98d | 138 | |
34faa8f7 DK |
139 | if (me->Server->GetHashes()->Add((unsigned char const * const)buffer, buffer_size) == false) |
140 | return 0; | |
141 | ||
ed793a19 | 142 | return buffer_size; |
d546f98d MV |
143 | } |
144 | ||
fd46d305 | 145 | // HttpsServerState::HttpsServerState - Constructor /*{{{*/ |
34faa8f7 | 146 | HttpsServerState::HttpsServerState(URI Srv,HttpsMethod * Owner) : ServerState(Srv, Owner), Hash(NULL) |
fd46d305 DK |
147 | { |
148 | TimeOut = _config->FindI("Acquire::https::Timeout",TimeOut); | |
149 | Reset(); | |
150 | } | |
151 | /*}}}*/ | |
34faa8f7 DK |
152 | bool HttpsServerState::InitHashes(HashStringList const &ExpectedHashes) /*{{{*/ |
153 | { | |
154 | delete Hash; | |
155 | Hash = new Hashes(ExpectedHashes); | |
156 | return true; | |
157 | } | |
158 | /*}}}*/ | |
159 | APT_PURE Hashes * HttpsServerState::GetHashes() /*{{{*/ | |
160 | { | |
161 | return Hash; | |
162 | } | |
163 | /*}}}*/ | |
fd46d305 | 164 | |
27925d82 | 165 | void HttpsMethod::SetupProxy() /*{{{*/ |
d546f98d MV |
166 | { |
167 | URI ServerName = Queue->Uri; | |
168 | ||
c6ee61ea MV |
169 | // Determine the proxy setting |
170 | AutoDetectProxy(ServerName); | |
171 | ||
5b63d2a9 MV |
172 | // Curl should never read proxy settings from the environment, as |
173 | // we determine which proxy to use. Do this for consistency among | |
174 | // methods and prevent an environment variable overriding a | |
175 | // no-proxy ("DIRECT") setting in apt.conf. | |
176 | curl_easy_setopt(curl, CURLOPT_PROXY, ""); | |
177 | ||
4407a02f MV |
178 | // Determine the proxy setting - try https first, fallback to http and use env at last |
179 | string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host, | |
180 | _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str()); | |
181 | ||
182 | if (UseProxy.empty() == true) | |
183 | UseProxy = _config->Find("Acquire::https::Proxy", _config->Find("Acquire::http::Proxy").c_str()); | |
184 | ||
185 | // User want to use NO proxy, so nothing to setup | |
186 | if (UseProxy == "DIRECT") | |
187 | return; | |
188 | ||
8707edd9 PC |
189 | // Parse no_proxy, a comma (,) separated list of domains we don't want to use |
190 | // a proxy for so we stop right here if it is in the list | |
191 | if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true) | |
192 | return; | |
193 | ||
194 | if (UseProxy.empty() == true) | |
d546f98d | 195 | { |
5b63d2a9 MV |
196 | const char* result = getenv("https_proxy"); |
197 | // FIXME: Fall back to http_proxy is to remain compatible with | |
198 | // existing setups and behaviour of apt.conf. This should be | |
199 | // deprecated in the future (including apt.conf). Most other | |
200 | // programs do not fall back to http proxy settings and neither | |
201 | // should Apt. | |
202 | if (result == NULL) | |
203 | result = getenv("http_proxy"); | |
4407a02f | 204 | UseProxy = result == NULL ? "" : result; |
d546f98d | 205 | } |
4407a02f | 206 | |
d546f98d | 207 | // Determine what host and port to use based on the proxy settings |
4407a02f | 208 | if (UseProxy.empty() == false) |
d546f98d | 209 | { |
4407a02f MV |
210 | Proxy = UseProxy; |
211 | if (Proxy.Port != 1) | |
d546f98d MV |
212 | curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port); |
213 | curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str()); | |
5b63d2a9 MV |
214 | if (Proxy.User.empty() == false || Proxy.Password.empty() == false) |
215 | { | |
216 | curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str()); | |
217 | curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str()); | |
218 | } | |
d546f98d | 219 | } |
b9e9a44b | 220 | } /*}}}*/ |
d546f98d MV |
221 | // HttpsMethod::Fetch - Fetch an item /*{{{*/ |
222 | // --------------------------------------------------------------------- | |
223 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
224 | depth. */ | |
225 | bool HttpsMethod::Fetch(FetchItem *Itm) | |
226 | { | |
d546f98d | 227 | struct stat SBuf; |
d61960d9 | 228 | struct curl_slist *headers=NULL; |
714ee06c | 229 | char curl_errorstr[CURL_ERROR_SIZE]; |
c769cd6f MV |
230 | URI Uri = Itm->Uri; |
231 | string remotehost = Uri.Host; | |
d546f98d MV |
232 | |
233 | // TODO: | |
d546f98d MV |
234 | // - http::Pipeline-Depth |
235 | // - error checking/reporting | |
236 | // - more debug options? (CURLOPT_DEBUGFUNCTION?) | |
237 | ||
5820530d | 238 | curl_easy_reset(curl); |
d546f98d MV |
239 | SetupProxy(); |
240 | ||
1de1f703 | 241 | maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc")); |
592b7800 | 242 | |
27925d82 | 243 | FetchResult Res; |
dcbb364f | 244 | CURLUserPointer userp(this, &Res, Itm); |
d546f98d | 245 | // callbacks |
01fc8930 | 246 | curl_easy_setopt(curl, CURLOPT_URL, static_cast<string>(Uri).c_str()); |
fd46d305 | 247 | curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, parse_header); |
27925d82 | 248 | curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &userp); |
d546f98d MV |
249 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); |
250 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); | |
dc95fee1 | 251 | // options |
27925d82 | 252 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, true); |
5820530d | 253 | curl_easy_setopt(curl, CURLOPT_FILETIME, true); |
889b0072 DK |
254 | // only allow curl to handle https, not the other stuff it supports |
255 | curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); | |
dc95fee1 | 256 | curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS); |
d546f98d | 257 | |
c769cd6f MV |
258 | // SSL parameters are set by default to the common (non mirror-specific) value |
259 | // if available (or a default one) and gets overload by mirror-specific ones. | |
260 | ||
261 | // File containing the list of trusted CA. | |
262 | string cainfo = _config->Find("Acquire::https::CaInfo",""); | |
263 | string knob = "Acquire::https::"+remotehost+"::CaInfo"; | |
264 | cainfo = _config->Find(knob.c_str(),cainfo.c_str()); | |
46e39c8e | 265 | if(cainfo.empty() == false) |
c769cd6f MV |
266 | curl_easy_setopt(curl, CURLOPT_CAINFO,cainfo.c_str()); |
267 | ||
268 | // Check server certificate against previous CA list ... | |
269 | bool peer_verify = _config->FindB("Acquire::https::Verify-Peer",true); | |
270 | knob = "Acquire::https::" + remotehost + "::Verify-Peer"; | |
271 | peer_verify = _config->FindB(knob.c_str(), peer_verify); | |
714ee06c MV |
272 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify); |
273 | ||
c769cd6f | 274 | // ... and hostname against cert CN or subjectAltName |
c769cd6f MV |
275 | bool verify = _config->FindB("Acquire::https::Verify-Host",true); |
276 | knob = "Acquire::https::"+remotehost+"::Verify-Host"; | |
277 | verify = _config->FindB(knob.c_str(),verify); | |
52b22cea DK |
278 | int const default_verify = (verify == true) ? 2 : 0; |
279 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, default_verify); | |
c769cd6f | 280 | |
46e39c8e MV |
281 | // Also enforce issuer of server certificate using its cert |
282 | string issuercert = _config->Find("Acquire::https::IssuerCert",""); | |
283 | knob = "Acquire::https::"+remotehost+"::IssuerCert"; | |
284 | issuercert = _config->Find(knob.c_str(),issuercert.c_str()); | |
285 | if(issuercert.empty() == false) | |
286 | curl_easy_setopt(curl, CURLOPT_ISSUERCERT,issuercert.c_str()); | |
287 | ||
c769cd6f | 288 | // For client authentication, certificate file ... |
714ee06c | 289 | string pem = _config->Find("Acquire::https::SslCert",""); |
c769cd6f MV |
290 | knob = "Acquire::https::"+remotehost+"::SslCert"; |
291 | pem = _config->Find(knob.c_str(),pem.c_str()); | |
46e39c8e | 292 | if(pem.empty() == false) |
714ee06c | 293 | curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str()); |
c769cd6f MV |
294 | |
295 | // ... and associated key. | |
296 | string key = _config->Find("Acquire::https::SslKey",""); | |
297 | knob = "Acquire::https::"+remotehost+"::SslKey"; | |
298 | key = _config->Find(knob.c_str(),key.c_str()); | |
46e39c8e | 299 | if(key.empty() == false) |
c769cd6f MV |
300 | curl_easy_setopt(curl, CURLOPT_SSLKEY, key.c_str()); |
301 | ||
302 | // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not | |
303 | // supported by GnuTLS). | |
304 | long final_version = CURL_SSLVERSION_DEFAULT; | |
305 | string sslversion = _config->Find("Acquire::https::SslForceVersion",""); | |
306 | knob = "Acquire::https::"+remotehost+"::SslForceVersion"; | |
307 | sslversion = _config->Find(knob.c_str(),sslversion.c_str()); | |
308 | if(sslversion == "TLSv1") | |
309 | final_version = CURL_SSLVERSION_TLSv1; | |
310 | else if(sslversion == "SSLv3") | |
311 | final_version = CURL_SSLVERSION_SSLv3; | |
312 | curl_easy_setopt(curl, CURLOPT_SSLVERSION, final_version); | |
d546f98d | 313 | |
46e39c8e MV |
314 | // CRL file |
315 | string crlfile = _config->Find("Acquire::https::CrlFile",""); | |
316 | knob = "Acquire::https::"+remotehost+"::CrlFile"; | |
317 | crlfile = _config->Find(knob.c_str(),crlfile.c_str()); | |
318 | if(crlfile.empty() == false) | |
319 | curl_easy_setopt(curl, CURLOPT_CRLFILE, crlfile.c_str()); | |
320 | ||
d546f98d | 321 | // cache-control |
b9e9a44b DK |
322 | if(_config->FindB("Acquire::https::No-Cache", |
323 | _config->FindB("Acquire::http::No-Cache",false)) == false) | |
d546f98d MV |
324 | { |
325 | // cache enabled | |
b9e9a44b DK |
326 | if (_config->FindB("Acquire::https::No-Store", |
327 | _config->FindB("Acquire::http::No-Store",false)) == true) | |
d546f98d | 328 | headers = curl_slist_append(headers,"Cache-Control: no-store"); |
8654fae9 | 329 | stringstream ss; |
b9e9a44b DK |
330 | ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age", |
331 | _config->FindI("Acquire::http::Max-Age",0))); | |
d546f98d MV |
332 | headers = curl_slist_append(headers, ss.str().c_str()); |
333 | } else { | |
334 | // cache disabled by user | |
335 | headers = curl_slist_append(headers, "Cache-Control: no-cache"); | |
336 | headers = curl_slist_append(headers, "Pragma: no-cache"); | |
337 | } | |
338 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
339 | ||
d546f98d | 340 | // speed limit |
46e39c8e | 341 | int const dlLimit = _config->FindI("Acquire::https::Dl-Limit", |
b9e9a44b | 342 | _config->FindI("Acquire::http::Dl-Limit",0))*1024; |
d546f98d MV |
343 | if (dlLimit > 0) |
344 | curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit); | |
345 | ||
346 | // set header | |
9f542bae DK |
347 | curl_easy_setopt(curl, CURLOPT_USERAGENT, |
348 | _config->Find("Acquire::https::User-Agent", | |
349 | _config->Find("Acquire::http::User-Agent", | |
335e2c82 | 350 | "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str()); |
d546f98d | 351 | |
cc615257 | 352 | // set timeout |
46e39c8e | 353 | int const timeout = _config->FindI("Acquire::https::Timeout", |
b9e9a44b | 354 | _config->FindI("Acquire::http::Timeout",120)); |
cc615257 | 355 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout); |
43cf55db | 356 | //set really low lowspeed timeout (see #497983) |
5085e660 | 357 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, DL_MIN_SPEED); |
43cf55db | 358 | curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout); |
cc615257 | 359 | |
668ce84d | 360 | // set redirect options and default to 10 redirects |
668ce84d MV |
361 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect); |
362 | curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10); | |
363 | ||
d546f98d | 364 | // debug |
905fba60 | 365 | if (Debug == true) |
d546f98d MV |
366 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
367 | ||
714ee06c | 368 | // error handling |
cc418115 | 369 | curl_errorstr[0] = '\0'; |
714ee06c MV |
370 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); |
371 | ||
6f4501f9 | 372 | // If we ask for uncompressed files servers might respond with content- |
1e3f4083 | 373 | // negotiation which lets us end up with compressed files we do not support, |
6f4501f9 DK |
374 | // see 657029, 657560 and co, so if we have no extension on the request |
375 | // ask for text only. As a sidenote: If there is nothing to negotate servers | |
376 | // seem to be nice and ignore it. | |
377 | if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true) | |
378 | { | |
379 | size_t const filepos = Itm->Uri.find_last_of('/'); | |
380 | string const file = Itm->Uri.substr(filepos + 1); | |
381 | if (flExtension(file) == file) | |
382 | headers = curl_slist_append(headers, "Accept: text/*"); | |
383 | } | |
384 | ||
d6039f9e | 385 | // if we have the file send an if-range query with a range header |
4c499611 MV |
386 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) |
387 | { | |
062074cb DK |
388 | std::string Buf; |
389 | strprintf(Buf, "Range: bytes=%lli-", (long long) SBuf.st_size); | |
390 | headers = curl_slist_append(headers, Buf.c_str()); | |
391 | strprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str()); | |
392 | headers = curl_slist_append(headers, Buf.c_str()); | |
8654fae9 | 393 | } |
d6039f9e MV |
394 | else if(Itm->LastModified > 0) |
395 | { | |
396 | curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
397 | curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified); | |
4c499611 | 398 | } |
d546f98d MV |
399 | |
400 | // go for it - if the file exists, append on it | |
401 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); | |
905fba60 | 402 | Server = CreateServerState(Itm->Uri); |
34faa8f7 DK |
403 | if (Server->InitHashes(Itm->ExpectedHashes) == false) |
404 | return false; | |
85050e76 | 405 | |
d546f98d MV |
406 | // keep apt updated |
407 | Res.Filename = Itm->DestFile; | |
408 | ||
409 | // get it! | |
410 | CURLcode success = curl_easy_perform(curl); | |
d546f98d | 411 | |
1dea08eb MV |
412 | // If the server returns 200 OK but the If-Modified-Since condition is not |
413 | // met, CURLINFO_CONDITION_UNMET will be set to 1 | |
414 | long curl_condition_unmet = 0; | |
415 | curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet); | |
416 | ||
db1f1469 | 417 | File->Close(); |
85050e76 | 418 | curl_slist_free_all(headers); |
db1f1469 | 419 | |
d546f98d | 420 | // cleanup |
bce8e59b | 421 | if (success != CURLE_OK) |
4c499611 | 422 | { |
bce8e59b DK |
423 | #pragma GCC diagnostic push |
424 | #pragma GCC diagnostic ignored "-Wswitch" | |
425 | switch (success) | |
426 | { | |
427 | case CURLE_COULDNT_RESOLVE_PROXY: | |
428 | case CURLE_COULDNT_RESOLVE_HOST: | |
429 | SetFailReason("ResolveFailure"); | |
430 | break; | |
431 | case CURLE_COULDNT_CONNECT: | |
432 | SetFailReason("ConnectionRefused"); | |
433 | break; | |
434 | case CURLE_OPERATION_TIMEDOUT: | |
435 | SetFailReason("Timeout"); | |
436 | break; | |
437 | } | |
438 | #pragma GCC diagnostic pop | |
439 | return _error->Error("%s", curl_errorstr); | |
85050e76 DK |
440 | } |
441 | ||
442 | // server says file not modified | |
fd46d305 | 443 | if (Server->Result == 304 || curl_condition_unmet == 1) |
85050e76 | 444 | { |
ce1f3a2c | 445 | RemoveFile("https", File->Name()); |
85050e76 DK |
446 | Res.IMSHit = true; |
447 | Res.LastModified = Itm->LastModified; | |
448 | Res.Size = 0; | |
449 | URIDone(Res); | |
d546f98d MV |
450 | return true; |
451 | } | |
fd46d305 | 452 | Res.IMSHit = false; |
d546f98d | 453 | |
fd46d305 DK |
454 | if (Server->Result != 200 && // OK |
455 | Server->Result != 206 && // Partial | |
456 | Server->Result != 416) // invalid Range | |
85050e76 DK |
457 | { |
458 | char err[255]; | |
fd46d305 | 459 | snprintf(err, sizeof(err) - 1, "HttpError%i", Server->Result); |
85050e76 | 460 | SetFailReason(err); |
8eafc759 | 461 | _error->Error("%i %s", Server->Result, Server->Code); |
85050e76 | 462 | // unlink, no need keep 401/404 page content in partial/ |
ce1f3a2c | 463 | RemoveFile("https", File->Name()); |
85050e76 DK |
464 | return false; |
465 | } | |
466 | ||
85050e76 | 467 | // invalid range-request |
fd46d305 | 468 | if (Server->Result == 416) |
85050e76 | 469 | { |
ce1f3a2c | 470 | RemoveFile("https", File->Name()); |
85050e76 DK |
471 | delete File; |
472 | Redirect(Itm->Uri); | |
473 | return true; | |
5820530d OS |
474 | } |
475 | ||
27925d82 DK |
476 | struct stat resultStat; |
477 | if (unlikely(stat(File->Name().c_str(), &resultStat) != 0)) | |
478 | { | |
479 | _error->Errno("stat", "Unable to access file %s", File->Name().c_str()); | |
480 | return false; | |
481 | } | |
482 | Res.Size = resultStat.st_size; | |
483 | ||
85050e76 DK |
484 | // Timestamp |
485 | curl_easy_getinfo(curl, CURLINFO_FILETIME, &Res.LastModified); | |
486 | if (Res.LastModified != -1) | |
487 | { | |
246bbb61 | 488 | struct timeval times[2]; |
9ce3cfc9 DK |
489 | times[0].tv_sec = Res.LastModified; |
490 | times[1].tv_sec = Res.LastModified; | |
246bbb61 DK |
491 | times[0].tv_usec = times[1].tv_usec = 0; |
492 | utimes(File->Name().c_str(), times); | |
85050e76 DK |
493 | } |
494 | else | |
495 | Res.LastModified = resultStat.st_mtime; | |
d546f98d MV |
496 | |
497 | // take hashes | |
34faa8f7 | 498 | Res.TakeHashes(*(Server->GetHashes())); |
85050e76 | 499 | |
d546f98d MV |
500 | // keep apt updated |
501 | URIDone(Res); | |
502 | ||
503 | // cleanup | |
d546f98d | 504 | delete File; |
d546f98d MV |
505 | |
506 | return true; | |
d3e8fbb3 | 507 | } |
905fba60 DK |
508 | /*}}}*/ |
509 | // HttpsMethod::Configuration - Handle a configuration message /*{{{*/ | |
510 | bool HttpsMethod::Configuration(string Message) | |
511 | { | |
512 | if (ServerMethod::Configuration(Message) == false) | |
513 | return false; | |
514 | ||
515 | AllowRedirect = _config->FindB("Acquire::https::AllowRedirect", | |
516 | _config->FindB("Acquire::http::AllowRedirect", true)); | |
517 | Debug = _config->FindB("Debug::Acquire::https",false); | |
518 | ||
519 | return true; | |
520 | } | |
521 | /*}}}*/ | |
830a1b8c | 522 | std::unique_ptr<ServerState> HttpsMethod::CreateServerState(URI const &uri)/*{{{*/ |
905fba60 | 523 | { |
830a1b8c | 524 | return std::unique_ptr<ServerState>(new HttpsServerState(uri, this)); |
905fba60 DK |
525 | } |
526 | /*}}}*/ | |
d546f98d MV |
527 | |
528 | int main() | |
529 | { | |
8b79c94a | 530 | return HttpsMethod().Run(); |
d546f98d MV |
531 | } |
532 |