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() { /*{{{*/
61 URI ServerName
= Queue
->Uri
;
63 // Determine the proxy setting - try https first, fallback to http and use env at last
64 string UseProxy
= _config
->Find("Acquire::https::Proxy::" + ServerName
.Host
,
65 _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
));
67 if (UseProxy
.empty() == true)
68 UseProxy
= _config
->Find("Acquire::https::Proxy", _config
->Find("Acquire::http::Proxy"));
70 // User want to use NO proxy, so nothing to setup
71 if (UseProxy
== "DIRECT")
74 if (UseProxy
.empty() == false) {
75 // Parse no_proxy, a comma (,) separated list of domains we don't want to use
76 // a proxy for so we stop right here if it is in the list
77 if (getenv("no_proxy") != 0 && CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
80 const char* result
= getenv("http_proxy");
81 UseProxy
= result
== NULL
? "" : result
;
84 // Determine what host and port to use based on the proxy settings
85 if (UseProxy
.empty() == false) {
88 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
89 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
92 // HttpsMethod::Fetch - Fetch an item /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This adds an item to the pipeline. We keep the pipeline at a fixed
96 bool HttpsMethod::Fetch(FetchItem
*Itm
)
100 struct curl_slist
*headers
=NULL
;
101 char curl_errorstr
[CURL_ERROR_SIZE
];
102 long curl_responsecode
;
104 string remotehost
= Uri
.Host
;
107 // - http::Pipeline-Depth
108 // - error checking/reporting
109 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
111 curl_easy_reset(curl
);
114 maybe_add_auth (Uri
, _config
->FindFile("Dir::Etc::netrc"));
117 curl_easy_setopt(curl
, CURLOPT_URL
, static_cast<string
>(Uri
).c_str());
118 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
119 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
120 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, progress_callback
);
121 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, this);
122 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, false);
123 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, true);
124 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
126 // SSL parameters are set by default to the common (non mirror-specific) value
127 // if available (or a default one) and gets overload by mirror-specific ones.
129 // File containing the list of trusted CA.
130 string cainfo
= _config
->Find("Acquire::https::CaInfo","");
131 string knob
= "Acquire::https::"+remotehost
+"::CaInfo";
132 cainfo
= _config
->Find(knob
.c_str(),cainfo
.c_str());
134 curl_easy_setopt(curl
, CURLOPT_CAINFO
,cainfo
.c_str());
136 // Check server certificate against previous CA list ...
137 bool peer_verify
= _config
->FindB("Acquire::https::Verify-Peer",true);
138 knob
= "Acquire::https::" + remotehost
+ "::Verify-Peer";
139 peer_verify
= _config
->FindB(knob
.c_str(), peer_verify
);
140 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, peer_verify
);
142 // ... and hostname against cert CN or subjectAltName
143 int default_verify
= 2;
144 bool verify
= _config
->FindB("Acquire::https::Verify-Host",true);
145 knob
= "Acquire::https::"+remotehost
+"::Verify-Host";
146 verify
= _config
->FindB(knob
.c_str(),verify
);
149 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, verify
);
151 // For client authentication, certificate file ...
152 string pem
= _config
->Find("Acquire::https::SslCert","");
153 knob
= "Acquire::https::"+remotehost
+"::SslCert";
154 pem
= _config
->Find(knob
.c_str(),pem
.c_str());
156 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
158 // ... and associated key.
159 string key
= _config
->Find("Acquire::https::SslKey","");
160 knob
= "Acquire::https::"+remotehost
+"::SslKey";
161 key
= _config
->Find(knob
.c_str(),key
.c_str());
163 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
165 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
166 // supported by GnuTLS).
167 long final_version
= CURL_SSLVERSION_DEFAULT
;
168 string sslversion
= _config
->Find("Acquire::https::SslForceVersion","");
169 knob
= "Acquire::https::"+remotehost
+"::SslForceVersion";
170 sslversion
= _config
->Find(knob
.c_str(),sslversion
.c_str());
171 if(sslversion
== "TLSv1")
172 final_version
= CURL_SSLVERSION_TLSv1
;
173 else if(sslversion
== "SSLv3")
174 final_version
= CURL_SSLVERSION_SSLv3
;
175 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
178 if(_config
->FindB("Acquire::https::No-Cache",
179 _config
->FindB("Acquire::http::No-Cache",false)) == false)
182 if (_config
->FindB("Acquire::https::No-Store",
183 _config
->FindB("Acquire::http::No-Store",false)) == true)
184 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
185 ioprintf(ss
, "Cache-Control: max-age=%u", _config
->FindI("Acquire::https::Max-Age",
186 _config
->FindI("Acquire::http::Max-Age",0)));
187 headers
= curl_slist_append(headers
, ss
.str().c_str());
189 // cache disabled by user
190 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
191 headers
= curl_slist_append(headers
, "Pragma: no-cache");
193 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
196 int dlLimit
= _config
->FindI("Acquire::https::Dl-Limit",
197 _config
->FindI("Acquire::http::Dl-Limit",0))*1024;
199 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
202 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,
203 _config
->Find("Acquire::https::User-Agent",
204 _config
->Find("Acquire::http::User-Agent",
205 "Debian APT-CURL/1.0 ("VERSION
")")).c_str());
208 int timeout
= _config
->FindI("Acquire::https::Timeout",
209 _config
->FindI("Acquire::http::Timeout",120));
210 curl_easy_setopt(curl
, CURLOPT_TIMEOUT
, timeout
);
211 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
212 //set really low lowspeed timeout (see #497983)
213 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_LIMIT
, DL_MIN_SPEED
);
214 curl_easy_setopt(curl
, CURLOPT_LOW_SPEED_TIME
, timeout
);
216 // set redirect options and default to 10 redirects
217 bool AllowRedirect
= _config
->FindB("Acquire::https::AllowRedirect",
218 _config
->FindB("Acquire::http::AllowRedirect",true));
219 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, AllowRedirect
);
220 curl_easy_setopt(curl
, CURLOPT_MAXREDIRS
, 10);
223 if(_config
->FindB("Debug::Acquire::https", false))
224 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
227 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
229 // if we have the file send an if-range query with a range header
230 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
233 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
234 (long)SBuf
.st_size
- 1,
235 TimeRFC1123(SBuf
.st_mtime
).c_str());
236 headers
= curl_slist_append(headers
, Buf
);
238 else if(Itm
->LastModified
> 0)
240 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
241 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
244 // go for it - if the file exists, append on it
245 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
246 if (File
->Size() > 0)
247 File
->Seek(File
->Size() - 1);
250 Res
.Filename
= Itm
->DestFile
;
253 CURLcode success
= curl_easy_perform(curl
);
254 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &curl_responsecode
);
257 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &curl_servdate
);
262 _error
->Error("%s", curl_errorstr
);
270 if (curl_servdate
!= -1) {
271 UBuf
.actime
= curl_servdate
;
272 UBuf
.modtime
= curl_servdate
;
273 utime(File
->Name().c_str(),&UBuf
);
276 // check the downloaded result
278 if (stat(File
->Name().c_str(),&Buf
) == 0)
280 Res
.Filename
= File
->Name();
281 Res
.LastModified
= Buf
.st_mtime
;
283 if (curl_responsecode
== 304)
285 unlink(File
->Name().c_str());
287 Res
.LastModified
= Itm
->LastModified
;
292 Res
.Size
= Buf
.st_size
;
297 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
298 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
299 Res
.TakeHashes(Hash
);
307 curl_slist_free_all(headers
);
314 setlocale(LC_ALL
, "");
317 curl_global_init(CURL_GLOBAL_SSL
) ;