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>
37 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
39 HttpsMethod
*me
= (HttpsMethod
*)userp
;
41 if(me
->File
->Write(buffer
, size
*nmemb
) != true)
48 HttpsMethod::progress_callback(void *clientp
, double dltotal
, double dlnow
,
49 double ultotal
, double ulnow
)
51 HttpsMethod
*me
= (HttpsMethod
*)clientp
;
52 if(dltotal
> 0 && me
->Res
.Size
== 0) {
53 me
->Res
.Size
= (unsigned long)dltotal
;
54 me
->URIStart(me
->Res
);
59 void HttpsMethod::SetupProxy()
61 URI ServerName
= Queue
->Uri
;
63 // Determine the proxy setting
64 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
65 if (!SpecificProxy
.empty())
67 if (SpecificProxy
== "DIRECT")
70 Proxy
= SpecificProxy
;
74 string DefProxy
= _config
->Find("Acquire::http::Proxy");
75 if (!DefProxy
.empty())
81 char* result
= getenv("http_proxy");
82 Proxy
= result
? result
: "";
86 // Parse no_proxy, a , separated list of domains
87 if (getenv("no_proxy") != 0)
89 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
93 // Determine what host and port to use based on the proxy settings
95 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
101 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
102 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
107 // HttpsMethod::Fetch - Fetch an item /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This adds an item to the pipeline. We keep the pipeline at a fixed
111 bool HttpsMethod::Fetch(FetchItem
*Itm
)
115 struct curl_slist
*headers
=NULL
;
116 char curl_errorstr
[CURL_ERROR_SIZE
];
117 long curl_responsecode
;
119 string remotehost
= Uri
.Host
;
122 // - http::Pipeline-Depth
123 // - error checking/reporting
124 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
126 curl_easy_reset(curl
);
130 curl_easy_setopt(curl
, CURLOPT_URL
, Itm
->Uri
.c_str());
131 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, write_data
);
132 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, this);
133 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, progress_callback
);
134 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, this);
135 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, false);
136 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, true);
137 curl_easy_setopt(curl
, CURLOPT_FILETIME
, true);
139 // SSL parameters are set by default to the common (non mirror-specific) value
140 // if available (or a default one) and gets overload by mirror-specific ones.
142 // File containing the list of trusted CA.
143 string cainfo
= _config
->Find("Acquire::https::CaInfo","");
144 string knob
= "Acquire::https::"+remotehost
+"::CaInfo";
145 cainfo
= _config
->Find(knob
.c_str(),cainfo
.c_str());
147 curl_easy_setopt(curl
, CURLOPT_CAINFO
,cainfo
.c_str());
149 // Check server certificate against previous CA list ...
150 bool peer_verify
= _config
->FindB("Acquire::https::Verify-Peer",true);
151 knob
= "Acquire::https::" + remotehost
+ "::Verify-Peer";
152 peer_verify
= _config
->FindB(knob
.c_str(), peer_verify
);
153 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, peer_verify
);
155 // ... and hostname against cert CN or subjectAltName
156 int default_verify
= 2;
157 bool verify
= _config
->FindB("Acquire::https::Verify-Host",true);
158 knob
= "Acquire::https::"+remotehost
+"::Verify-Host";
159 verify
= _config
->FindB(knob
.c_str(),verify
);
162 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYHOST
, verify
);
164 // For client authentication, certificate file ...
165 string pem
= _config
->Find("Acquire::https::SslCert","");
166 knob
= "Acquire::https::"+remotehost
+"::SslCert";
167 pem
= _config
->Find(knob
.c_str(),pem
.c_str());
169 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, pem
.c_str());
171 // ... and associated key.
172 string key
= _config
->Find("Acquire::https::SslKey","");
173 knob
= "Acquire::https::"+remotehost
+"::SslKey";
174 key
= _config
->Find(knob
.c_str(),key
.c_str());
176 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, key
.c_str());
178 // Allow forcing SSL version to SSLv3 or TLSv1 (SSLv2 is not
179 // supported by GnuTLS).
180 long final_version
= CURL_SSLVERSION_DEFAULT
;
181 string sslversion
= _config
->Find("Acquire::https::SslForceVersion","");
182 knob
= "Acquire::https::"+remotehost
+"::SslForceVersion";
183 sslversion
= _config
->Find(knob
.c_str(),sslversion
.c_str());
184 if(sslversion
== "TLSv1")
185 final_version
= CURL_SSLVERSION_TLSv1
;
186 else if(sslversion
== "SSLv3")
187 final_version
= CURL_SSLVERSION_SSLv3
;
188 curl_easy_setopt(curl
, CURLOPT_SSLVERSION
, final_version
);
191 if(_config
->FindB("Acquire::http::No-Cache",false) == false)
194 if (_config
->FindB("Acquire::http::No-Store",false) == true)
195 headers
= curl_slist_append(headers
,"Cache-Control: no-store");
196 ioprintf(ss
, "Cache-Control: max-age=%u", _config
->FindI("Acquire::http::Max-Age",0));
197 headers
= curl_slist_append(headers
, ss
.str().c_str());
199 // cache disabled by user
200 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
201 headers
= curl_slist_append(headers
, "Pragma: no-cache");
203 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
206 int dlLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
208 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
211 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,"Debian APT-CURL/1.0 ("VERSION
")");
214 int timeout
= _config
->FindI("Acquire::http::Timeout",120);
215 curl_easy_setopt(curl
, CURLOPT_TIMEOUT
, timeout
);
216 curl_easy_setopt(curl
, CURLOPT_CONNECTTIMEOUT
, timeout
);
219 if(_config
->FindB("Debug::Acquire::https", false))
220 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
223 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
225 // if we have the file send an if-range query with a range header
226 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
229 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
230 (long)SBuf
.st_size
- 1,
231 TimeRFC1123(SBuf
.st_mtime
).c_str());
232 headers
= curl_slist_append(headers
, Buf
);
234 else if(Itm
->LastModified
> 0)
236 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
237 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
240 // go for it - if the file exists, append on it
241 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
242 if (File
->Size() > 0)
243 File
->Seek(File
->Size() - 1);
246 Res
.Filename
= Itm
->DestFile
;
249 CURLcode success
= curl_easy_perform(curl
);
250 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &curl_responsecode
);
253 curl_easy_getinfo(curl
, CURLINFO_FILETIME
, &curl_servdate
);
258 unlink(File
->Name().c_str());
259 _error
->Error("%s", curl_errorstr
);
267 if (curl_servdate
!= -1) {
268 UBuf
.actime
= curl_servdate
;
269 UBuf
.modtime
= curl_servdate
;
270 utime(File
->Name().c_str(),&UBuf
);
273 // check the downloaded result
275 if (stat(File
->Name().c_str(),&Buf
) == 0)
277 Res
.Filename
= File
->Name();
278 Res
.LastModified
= Buf
.st_mtime
;
280 if (curl_responsecode
== 304)
282 unlink(File
->Name().c_str());
284 Res
.LastModified
= Itm
->LastModified
;
289 Res
.Size
= Buf
.st_size
;
294 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
295 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
296 Res
.TakeHashes(Hash
);
304 curl_slist_free_all(headers
);
311 setlocale(LC_ALL
, "");
314 curl_global_init(CURL_GLOBAL_SSL
) ;