1 // -*- mode: cpp; mode: fold -*-
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 HTTPS Acquire Method - This is the HTTPS aquire method for APT.
10 ##################################################################### */
12 // Include Files /*{{{*/
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/acquire-method.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/hashes.h>
17 #include <apt-pkg/netrc.h>
38 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
40 HttpsMethod
*me
= (HttpsMethod
*)userp
;
42 if(me
->File
->Write(buffer
, size
*nmemb
) != true)
49 HttpsMethod::progress_callback(void *clientp
, double dltotal
, double dlnow
,
50 double ultotal
, double ulnow
)
52 HttpsMethod
*me
= (HttpsMethod
*)clientp
;
53 if(dltotal
> 0 && me
->Res
.Size
== 0) {
54 me
->Res
.Size
= (unsigned long)dltotal
;
55 me
->URIStart(me
->Res
);
60 void HttpsMethod::SetupProxy()
62 URI ServerName
= Queue
->Uri
;
64 // Determine the proxy setting
65 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
66 if (!SpecificProxy
.empty())
68 if (SpecificProxy
== "DIRECT")
71 Proxy
= SpecificProxy
;
75 string DefProxy
= _config
->Find("Acquire::http::Proxy");
76 if (!DefProxy
.empty())
82 char* result
= getenv("http_proxy");
83 Proxy
= result
? result
: "";
87 // Parse no_proxy, a , separated list of domains
88 if (getenv("no_proxy") != 0)
90 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
94 // Determine what host and port to use based on the proxy settings
96 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
102 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
103 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
108 // HttpsMethod::Fetch - Fetch an item /*{{{*/
109 // ---------------------------------------------------------------------
110 /* This adds an item to the pipeline. We keep the pipeline at a fixed
112 bool HttpsMethod::Fetch(FetchItem
*Itm
)
116 struct curl_slist
*headers
=NULL
;
117 char curl_errorstr
[CURL_ERROR_SIZE
];
118 long curl_responsecode
;
120 string remotehost
= Uri
.Host
;
123 // - http::Pipeline-Depth
124 // - error checking/reporting
125 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
127 curl_easy_reset(curl
);
130 maybe_add_auth (Uri
, _config
->FindFile("Dir::Etc::netrc"));
133 curl_easy_setopt(curl
, CURLOPT_URL
, static_cast<string
>(Uri
).c_str());
134 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
135 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
136 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, progress_callback
);
137 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, this);
138 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, false);
139 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, true);
140 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
142 // SSL parameters are set by default to the common (non mirror-specific) value
143 // if available (or a default one) and gets overload by mirror-specific ones.
145 // File containing the list of trusted CA.
146 string cainfo
= _config
->Find("Acquire::https::CaInfo","");
147 string knob
= "Acquire::https::"+remotehost
+"::CaInfo";
148 cainfo
= _config
->Find(knob
.c_str(),cainfo
.c_str());
150 curl_easy_setopt(curl
, CURLOPT_CAINFO
,cainfo
.c_str());
152 // Check server certificate against previous CA list ...
153 bool peer_verify
= _config
->FindB("Acquire::https::Verify-Peer",true);
154 knob
= "Acquire::https::" + remotehost
+ "::Verify-Peer";
155 peer_verify
= _config
->FindB(knob
.c_str(), peer_verify
);
156 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, peer_verify
);
158 // ... and hostname against cert CN or subjectAltName
159 int default_verify
= 2;
160 bool verify
= _config
->FindB("Acquire::https::Verify-Host",true);
161 knob
= "Acquire::https::"+remotehost
+"::Verify-Host";
162 verify
= _config
->FindB(knob
.c_str(),verify
);
165 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, verify
);
167 // For client authentication, certificate file ...
168 string pem
= _config
->Find("Acquire::https::SslCert","");
169 knob
= "Acquire::https::"+remotehost
+"::SslCert";
170 pem
= _config
->Find(knob
.c_str(),pem
.c_str());
172 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
174 // ... and associated key.
175 string key
= _config
->Find("Acquire::https::SslKey","");
176 knob
= "Acquire::https::"+remotehost
+"::SslKey";
177 key
= _config
->Find(knob
.c_str(),key
.c_str());
179 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
181 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
182 // supported by GnuTLS).
183 long final_version
= CURL_SSLVERSION_DEFAULT
;
184 string sslversion
= _config
->Find("Acquire::https::SslForceVersion","");
185 knob
= "Acquire::https::"+remotehost
+"::SslForceVersion";
186 sslversion
= _config
->Find(knob
.c_str(),sslversion
.c_str());
187 if(sslversion
== "TLSv1")
188 final_version
= CURL_SSLVERSION_TLSv1
;
189 else if(sslversion
== "SSLv3")
190 final_version
= CURL_SSLVERSION_SSLv3
;
191 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
194 if(_config
->FindB("Acquire::http::No-Cache",false) == false)
197 if (_config
->FindB("Acquire::http::No-Store",false) == true)
198 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
199 ioprintf(ss
, "Cache-Control: max-age=%u", _config
->FindI("Acquire::http::Max-Age",0));
200 headers
= curl_slist_append(headers
, ss
.str().c_str());
202 // cache disabled by user
203 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
204 headers
= curl_slist_append(headers
, "Pragma: no-cache");
206 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
209 int dlLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
211 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
214 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,"Debian APT-CURL/1.0 ("VERSION
")");
217 int timeout
= _config
->FindI("Acquire::http::Timeout",120);
218 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
219 //set really low lowspeed timeout (see #497983)
220 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_LIMIT
, DL_MIN_SPEED
);
221 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_TIME
, timeout
);
223 // set redirect options and default to 10 redirects
224 bool AllowRedirect
= _config
->FindI("Acquire::https::AllowRedirect", true);
225 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, AllowRedirect
);
226 curl_easy_setopt(curl
, CURLOPT_MAXREDIRS
, 10);
229 if(_config
->FindB("Debug::Acquire::https", false))
230 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
233 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
235 // if we have the file send an if-range query with a range header
236 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
239 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
240 (long)SBuf
.st_size
- 1,
241 TimeRFC1123(SBuf
.st_mtime
).c_str());
242 headers
= curl_slist_append(headers
, Buf
);
244 else if(Itm
->LastModified
> 0)
246 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
247 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
250 // go for it - if the file exists, append on it
251 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
252 if (File
->Size() > 0)
253 File
->Seek(File
->Size() - 1);
256 Res
.Filename
= Itm
->DestFile
;
259 CURLcode success
= curl_easy_perform(curl
);
260 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &curl_responsecode
);
263 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &curl_servdate
);
268 _error
->Error("%s", curl_errorstr
);
276 if (curl_servdate
!= -1) {
277 UBuf
.actime
= curl_servdate
;
278 UBuf
.modtime
= curl_servdate
;
279 utime(File
->Name().c_str(),&UBuf
);
282 // check the downloaded result
284 if (stat(File
->Name().c_str(),&Buf
) == 0)
286 Res
.Filename
= File
->Name();
287 Res
.LastModified
= Buf
.st_mtime
;
289 if (curl_responsecode
== 304)
291 unlink(File
->Name().c_str());
293 Res
.LastModified
= Itm
->LastModified
;
298 Res
.Size
= Buf
.st_size
;
303 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
304 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
305 Res
.TakeHashes(Hash
);
313 curl_slist_free_all(headers
);
320 setlocale(LC_ALL
, "");
323 curl_global_init(CURL_GLOBAL_SSL
) ;