merged from debian
[ntk/apt.git] / methods / https.cc
CommitLineData
d546f98d
MV
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4/* ######################################################################
5
ae58a985 6 HTTPS Acquire Method - This is the HTTPS aquire method for APT.
d546f98d
MV
7
8 It uses libcurl
9
10 ##################################################################### */
11 /*}}}*/
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
18#include <sys/stat.h>
19#include <sys/time.h>
20#include <utime.h>
21#include <unistd.h>
22#include <signal.h>
23#include <stdio.h>
24#include <errno.h>
25#include <string.h>
26#include <iostream>
27#include <apti18n.h>
28#include <sstream>
29
30#include "config.h"
31#include "https.h"
32
33 /*}}}*/
34using namespace std;
35
36size_t
37HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
38{
39 HttpsMethod *me = (HttpsMethod *)userp;
40
41 if(me->File->Write(buffer, size*nmemb) != true)
42 return false;
43
44 return size*nmemb;
45}
46
47int
48HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow,
49 double ultotal, double ulnow)
50{
51 HttpsMethod *me = (HttpsMethod *)clientp;
52 if(dltotal > 0 && me->Res.Size == 0) {
21fd1746 53 me->Res.Size = (unsigned long)dltotal;
d546f98d
MV
54 me->URIStart(me->Res);
55 }
56 return 0;
57}
58
21fd1746 59void HttpsMethod::SetupProxy()
d546f98d
MV
60{
61 URI ServerName = Queue->Uri;
62
63 // Determine the proxy setting
64 if (getenv("http_proxy") == 0)
65 {
66 string DefProxy = _config->Find("Acquire::http::Proxy");
67 string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
68 if (SpecificProxy.empty() == false)
69 {
70 if (SpecificProxy == "DIRECT")
71 Proxy = "";
72 else
73 Proxy = SpecificProxy;
74 }
75 else
76 Proxy = DefProxy;
77 }
78
79 // Parse no_proxy, a , separated list of domains
80 if (getenv("no_proxy") != 0)
81 {
82 if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
83 Proxy = "";
84 }
85
86 // Determine what host and port to use based on the proxy settings
d546f98d
MV
87 string Host;
88 if (Proxy.empty() == true || Proxy.Host.empty() == true)
89 {
90 }
91 else
92 {
93 if (Proxy.Port != 0)
94 curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
95 curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
96 }
97}
98
99
100// HttpsMethod::Fetch - Fetch an item /*{{{*/
101// ---------------------------------------------------------------------
102/* This adds an item to the pipeline. We keep the pipeline at a fixed
103 depth. */
104bool HttpsMethod::Fetch(FetchItem *Itm)
105{
106 stringstream ss;
107 struct stat SBuf;
108 struct curl_slist *headers=NULL;
714ee06c 109 char curl_errorstr[CURL_ERROR_SIZE];
4c499611 110 long curl_responsecode;
d546f98d
MV
111
112 // TODO:
d546f98d
MV
113 // - http::Pipeline-Depth
114 // - error checking/reporting
115 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
116
5820530d 117 curl_easy_reset(curl);
d546f98d
MV
118 SetupProxy();
119
120 // callbacks
121 curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
122 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
123 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
124 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
125 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
126 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
127 curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
5820530d 128 curl_easy_setopt(curl, CURLOPT_FILETIME, true);
d546f98d
MV
129
130 // FIXME: https: offer various options of verification
714ee06c
MV
131 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer", false);
132 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
133
134 // sslcert file
135 string pem = _config->Find("Acquire::https::SslCert","");
136 if(pem != "")
137 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
138
139 // CA-Dir
140 string certdir = _config->Find("Acquire::https::CaPath","");
141 if(certdir != "")
142 curl_easy_setopt(curl, CURLOPT_CAPATH, certdir.c_str());
143
144 // Server-verify
145 int verify = _config->FindI("Acquire::https::Verify-Host",2);
146 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
d546f98d
MV
147
148 // cache-control
149 if(_config->FindB("Acquire::http::No-Cache",false) == false)
150 {
151 // cache enabled
152 if (_config->FindB("Acquire::http::No-Store",false) == true)
153 headers = curl_slist_append(headers,"Cache-Control: no-store");
154 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
155 headers = curl_slist_append(headers, ss.str().c_str());
156 } else {
157 // cache disabled by user
158 headers = curl_slist_append(headers, "Cache-Control: no-cache");
159 headers = curl_slist_append(headers, "Pragma: no-cache");
160 }
161 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
162
d546f98d
MV
163 // speed limit
164 int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
165 if (dlLimit > 0)
166 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
167
168 // set header
169 curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
170
cc615257
OS
171 // set timeout
172 int timeout = _config->FindI("Acquire::http::Timeout",120);
173 curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
174 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
175
d546f98d 176 // debug
714ee06c 177 if(_config->FindB("Debug::Acquire::https", false))
d546f98d
MV
178 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
179
714ee06c
MV
180 // error handling
181 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
182
d6039f9e 183 // if we have the file send an if-range query with a range header
4c499611
MV
184 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
185 {
186 char Buf[1000];
187 sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
188 (long)SBuf.st_size - 1,
189 TimeRFC1123(SBuf.st_mtime).c_str());
190 headers = curl_slist_append(headers, Buf);
d6039f9e
MV
191 }
192 else if(Itm->LastModified > 0)
193 {
194 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
195 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
4c499611 196 }
d546f98d
MV
197
198 // go for it - if the file exists, append on it
199 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
d6039f9e
MV
200 if (File->Size() > 0)
201 File->Seek(File->Size() - 1);
d546f98d
MV
202
203 // keep apt updated
204 Res.Filename = Itm->DestFile;
205
206 // get it!
207 CURLcode success = curl_easy_perform(curl);
4c499611 208 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
d546f98d 209
5820530d
OS
210 long curl_servdate;
211 curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
212
d546f98d 213 // cleanup
4c499611
MV
214 if(success != 0)
215 {
216 unlink(File->Name().c_str());
714ee06c 217 _error->Error(curl_errorstr);
d546f98d
MV
218 Fail();
219 return true;
220 }
4c499611 221 File->Close();
d546f98d 222
5820530d
OS
223 // Timestamp
224 struct utimbuf UBuf;
225 if (curl_servdate != -1) {
226 UBuf.actime = curl_servdate;
227 UBuf.modtime = curl_servdate;
228 utime(File->Name().c_str(),&UBuf);
229 }
230
d546f98d
MV
231 // check the downloaded result
232 struct stat Buf;
233 if (stat(File->Name().c_str(),&Buf) == 0)
234 {
d546f98d
MV
235 Res.Filename = File->Name();
236 Res.LastModified = Buf.st_mtime;
237 Res.IMSHit = false;
4c499611 238 if (curl_responsecode == 304)
714ee06c 239 {
d6039f9e 240 unlink(File->Name().c_str());
d546f98d 241 Res.IMSHit = true;
714ee06c 242 Res.LastModified = Itm->LastModified;
d6039f9e
MV
243 Res.Size = 0;
244 URIDone(Res);
245 return true;
714ee06c 246 }
d6039f9e 247 Res.Size = Buf.st_size;
d546f98d
MV
248 }
249
250 // take hashes
251 Hashes Hash;
252 FileFd Fd(Res.Filename, FileFd::ReadOnly);
253 Hash.AddFD(Fd.Fd(), Fd.Size());
254 Res.TakeHashes(Hash);
255
256 // keep apt updated
257 URIDone(Res);
258
259 // cleanup
d546f98d
MV
260 Res.Size = 0;
261 delete File;
262 curl_slist_free_all(headers);
263
264 return true;
265};
266
267int main()
268{
269 setlocale(LC_ALL, "");
270
271 HttpsMethod Mth;
272 curl_global_init(CURL_GLOBAL_SSL) ;
273
274 return Mth.Run();
275}
276
277