* Fix compilation warnings:
[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
6 HTTPS Aquire 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 /*}}}*/
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];
d546f98d
MV
110
111 // TODO:
112 // - http::Timeout
113 // - http::Pipeline-Depth
114 // - error checking/reporting
115 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
116
117 SetupProxy();
118
119 // callbacks
120 curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
121 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
122 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
123 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
124 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
125 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
126 curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
127
128 // FIXME: https: offer various options of verification
714ee06c
MV
129 bool peer_verify = _config->FindB("Acquire::https::Verify-Peer", false);
130 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, peer_verify);
131
132 // sslcert file
133 string pem = _config->Find("Acquire::https::SslCert","");
134 if(pem != "")
135 curl_easy_setopt(curl, CURLOPT_SSLCERT, pem.c_str());
136
137 // CA-Dir
138 string certdir = _config->Find("Acquire::https::CaPath","");
139 if(certdir != "")
140 curl_easy_setopt(curl, CURLOPT_CAPATH, certdir.c_str());
141
142 // Server-verify
143 int verify = _config->FindI("Acquire::https::Verify-Host",2);
144 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify);
d546f98d
MV
145
146 // cache-control
147 if(_config->FindB("Acquire::http::No-Cache",false) == false)
148 {
149 // cache enabled
150 if (_config->FindB("Acquire::http::No-Store",false) == true)
151 headers = curl_slist_append(headers,"Cache-Control: no-store");
152 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
153 headers = curl_slist_append(headers, ss.str().c_str());
154 } else {
155 // cache disabled by user
156 headers = curl_slist_append(headers, "Cache-Control: no-cache");
157 headers = curl_slist_append(headers, "Pragma: no-cache");
158 }
159 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
160
161 // set time values
162 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
163 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
164
165 // speed limit
166 int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
167 if (dlLimit > 0)
168 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
169
170 // set header
171 curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
172
173 // debug
714ee06c 174 if(_config->FindB("Debug::Acquire::https", false))
d546f98d
MV
175 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
176
714ee06c
MV
177 // error handling
178 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
179
d546f98d
MV
180 // In this case we send an if-range query with a range header
181 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
182 curl_easy_setopt(curl, CURLOPT_RESUME_FROM, (long)SBuf.st_size);
183
184 // go for it - if the file exists, append on it
185 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
186 File->Seek(File->Size());
187
188 // keep apt updated
189 Res.Filename = Itm->DestFile;
190
191 // get it!
192 CURLcode success = curl_easy_perform(curl);
193
194
195 // cleanup
196 if(success != 0) {
714ee06c 197 _error->Error(curl_errorstr);
d546f98d
MV
198 Fail();
199 return true;
200 }
201
202 if (Res.Size == 0)
203 Res.Size = File->Size();
204
205 // check the downloaded result
206 struct stat Buf;
207 if (stat(File->Name().c_str(),&Buf) == 0)
208 {
209 Res.Size = Buf.st_size;
210 Res.Filename = File->Name();
211 Res.LastModified = Buf.st_mtime;
212 Res.IMSHit = false;
714ee06c
MV
213 if (Itm->LastModified != 0 && Buf.st_mtime >= Itm->LastModified)
214 {
d546f98d 215 Res.IMSHit = true;
714ee06c
MV
216 Res.LastModified = Itm->LastModified;
217 }
d546f98d
MV
218 }
219
220 // take hashes
221 Hashes Hash;
222 FileFd Fd(Res.Filename, FileFd::ReadOnly);
223 Hash.AddFD(Fd.Fd(), Fd.Size());
224 Res.TakeHashes(Hash);
225
226 // keep apt updated
227 URIDone(Res);
228
229 // cleanup
230 File->Close();
231 Res.Size = 0;
232 delete File;
233 curl_slist_free_all(headers);
234
235 return true;
236};
237
238int main()
239{
240 setlocale(LC_ALL, "");
241
242 HttpsMethod Mth;
243 curl_global_init(CURL_GLOBAL_SSL) ;
244
245 return Mth.Run();
246}
247
248