]> git.saurik.com Git - apt.git/blob - methods/https.cc
* Reset curl options and timestamp between downloaded files. Thanks to
[apt.git] / methods / https.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
5
6 HTTPS Acquire Method - This is the HTTPS aquire method for APT.
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 /*}}}*/
34 using namespace std;
35
36 size_t
37 HttpsMethod::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
47 int
48 HttpsMethod::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) {
53 me->Res.Size = (unsigned long)dltotal;
54 me->URIStart(me->Res);
55 }
56 return 0;
57 }
58
59 void HttpsMethod::SetupProxy()
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
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. */
104 bool HttpsMethod::Fetch(FetchItem *Itm)
105 {
106 stringstream ss;
107 struct stat SBuf;
108 struct curl_slist *headers=NULL;
109 char curl_errorstr[CURL_ERROR_SIZE];
110 long curl_responsecode;
111
112 // TODO:
113 // - http::Timeout
114 // - http::Pipeline-Depth
115 // - error checking/reporting
116 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
117
118 curl_easy_reset(curl);
119 SetupProxy();
120
121 // callbacks
122 curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
123 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
124 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
125 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
126 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
127 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
128 curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
129 curl_easy_setopt(curl, CURLOPT_FILETIME, true);
130
131 // FIXME: https: offer various options of verification
132 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer", false);
133 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
134
135 // sslcert file
136 string pem = _config->Find("Acquire::https::SslCert","");
137 if(pem != "")
138 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
139
140 // CA-Dir
141 string certdir = _config->Find("Acquire::https::CaPath","");
142 if(certdir != "")
143 curl_easy_setopt(curl, CURLOPT_CAPATH, certdir.c_str());
144
145 // Server-verify
146 int verify = _config->FindI("Acquire::https::Verify-Host",2);
147 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
148
149 // cache-control
150 if(_config->FindB("Acquire::http::No-Cache",false) == false)
151 {
152 // cache enabled
153 if (_config->FindB("Acquire::http::No-Store",false) == true)
154 headers = curl_slist_append(headers,"Cache-Control: no-store");
155 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
156 headers = curl_slist_append(headers, ss.str().c_str());
157 } else {
158 // cache disabled by user
159 headers = curl_slist_append(headers, "Cache-Control: no-cache");
160 headers = curl_slist_append(headers, "Pragma: no-cache");
161 }
162 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
163
164 // set time values
165 if(Itm->LastModified > 0)
166 {
167 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
168 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
169 }
170
171 // speed limit
172 int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
173 if (dlLimit > 0)
174 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
175
176 // set header
177 curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
178
179 // debug
180 if(_config->FindB("Debug::Acquire::https", false))
181 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
182
183 // error handling
184 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
185
186 // In this case we send an if-range query with a range header
187 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
188 {
189 char Buf[1000];
190 sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
191 (long)SBuf.st_size - 1,
192 TimeRFC1123(SBuf.st_mtime).c_str());
193 headers = curl_slist_append(headers, Buf);
194 }
195
196 // go for it - if the file exists, append on it
197 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
198 File->Seek(File->Size());
199
200 // keep apt updated
201 Res.Filename = Itm->DestFile;
202
203 // get it!
204 CURLcode success = curl_easy_perform(curl);
205 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode);
206
207 long curl_servdate;
208 curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
209
210 // cleanup
211 if(success != 0)
212 {
213 unlink(File->Name().c_str());
214 _error->Error(curl_errorstr);
215 Fail();
216 return true;
217 }
218 File->Close();
219
220 if (Res.Size == 0)
221 Res.Size = File->Size();
222
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
231 // check the downloaded result
232 struct stat Buf;
233 if (stat(File->Name().c_str(),&Buf) == 0)
234 {
235 Res.Size = Buf.st_size;
236 Res.Filename = File->Name();
237 Res.LastModified = Buf.st_mtime;
238 Res.IMSHit = false;
239 if (curl_responsecode == 304)
240 {
241 Res.IMSHit = true;
242 Res.LastModified = Itm->LastModified;
243 }
244 }
245
246 // take hashes
247 Hashes Hash;
248 FileFd Fd(Res.Filename, FileFd::ReadOnly);
249 Hash.AddFD(Fd.Fd(), Fd.Size());
250 Res.TakeHashes(Hash);
251
252 // keep apt updated
253 URIDone(Res);
254
255 // cleanup
256 Res.Size = 0;
257 delete File;
258 curl_slist_free_all(headers);
259
260 return true;
261 };
262
263 int main()
264 {
265 setlocale(LC_ALL, "");
266
267 HttpsMethod Mth;
268 curl_global_init(CURL_GLOBAL_SSL) ;
269
270 return Mth.Run();
271 }
272
273