]> git.saurik.com Git - apt.git/blob - methods/https.cc
06b7dff48040587211cb2873a271296544ed9cf4
[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 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 /*}}}*/
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 = dltotal;
54 me->URIStart(me->Res);
55 }
56 return 0;
57 }
58
59 bool 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 int Port = 0;
88 string Host;
89 if (Proxy.empty() == true || Proxy.Host.empty() == true)
90 {
91 }
92 else
93 {
94 if (Proxy.Port != 0)
95 curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
96 curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
97 }
98 }
99
100
101 // HttpsMethod::Fetch - Fetch an item /*{{{*/
102 // ---------------------------------------------------------------------
103 /* This adds an item to the pipeline. We keep the pipeline at a fixed
104 depth. */
105 bool HttpsMethod::Fetch(FetchItem *Itm)
106 {
107 stringstream ss;
108 struct stat SBuf;
109 struct curl_slist *headers=NULL;
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
129 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
130
131 // cache-control
132 if(_config->FindB("Acquire::http::No-Cache",false) == false)
133 {
134 // cache enabled
135 if (_config->FindB("Acquire::http::No-Store",false) == true)
136 headers = curl_slist_append(headers,"Cache-Control: no-store");
137 ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
138 headers = curl_slist_append(headers, ss.str().c_str());
139 } else {
140 // cache disabled by user
141 headers = curl_slist_append(headers, "Cache-Control: no-cache");
142 headers = curl_slist_append(headers, "Pragma: no-cache");
143 }
144 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
145
146 // set time values
147 curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
148 curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
149
150 // speed limit
151 int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
152 if (dlLimit > 0)
153 curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
154
155 // set header
156 curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
157
158 // debug
159 if(_config->FindB("Debug::Acquire::http", false))
160 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
161
162 // In this case we send an if-range query with a range header
163 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
164 curl_easy_setopt(curl, CURLOPT_RESUME_FROM, (long)SBuf.st_size);
165
166 // go for it - if the file exists, append on it
167 File = new FileFd(Itm->DestFile, FileFd::WriteAny);
168 File->Seek(File->Size());
169
170 // keep apt updated
171 Res.Filename = Itm->DestFile;
172
173 // get it!
174 CURLcode success = curl_easy_perform(curl);
175
176
177 // cleanup
178 if(success != 0) {
179 Fail();
180 return true;
181 }
182
183 if (Res.Size == 0)
184 Res.Size = File->Size();
185
186 // check the downloaded result
187 struct stat Buf;
188 if (stat(File->Name().c_str(),&Buf) == 0)
189 {
190 Res.Size = Buf.st_size;
191 Res.Filename = File->Name();
192 Res.LastModified = Buf.st_mtime;
193 Res.IMSHit = false;
194 if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
195 Res.IMSHit = true;
196 }
197
198 // take hashes
199 Hashes Hash;
200 FileFd Fd(Res.Filename, FileFd::ReadOnly);
201 Hash.AddFD(Fd.Fd(), Fd.Size());
202 Res.TakeHashes(Hash);
203
204 // keep apt updated
205 URIDone(Res);
206
207 // cleanup
208 File->Close();
209 Res.Size = 0;
210 delete File;
211 curl_slist_free_all(headers);
212
213 return true;
214 };
215
216 int main()
217 {
218 setlocale(LC_ALL, "");
219
220 HttpsMethod Mth;
221 curl_global_init(CURL_GLOBAL_SSL) ;
222
223 return Mth.Run();
224 }
225
226