]>
Commit | Line | Data |
---|---|---|
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 | /*}}}*/ | |
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; | |
714ee06c | 110 | char curl_errorstr[CURL_ERROR_SIZE]; |
4c499611 | 111 | long curl_responsecode; |
d546f98d MV |
112 | |
113 | // TODO: | |
114 | // - http::Timeout | |
115 | // - http::Pipeline-Depth | |
116 | // - error checking/reporting | |
117 | // - more debug options? (CURLOPT_DEBUGFUNCTION?) | |
118 | ||
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 | ||
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 | ||
163 | // set time values | |
4c499611 MV |
164 | if(Itm->LastModified > 0) |
165 | { | |
166 | curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
167 | curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified); | |
168 | } | |
d546f98d MV |
169 | |
170 | // speed limit | |
171 | int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024; | |
172 | if (dlLimit > 0) | |
173 | curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit); | |
174 | ||
175 | // set header | |
176 | curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")"); | |
177 | ||
178 | // debug | |
714ee06c | 179 | if(_config->FindB("Debug::Acquire::https", false)) |
d546f98d MV |
180 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
181 | ||
714ee06c MV |
182 | // error handling |
183 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); | |
184 | ||
d546f98d | 185 | // In this case we send an if-range query with a range header |
4c499611 MV |
186 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) |
187 | { | |
188 | char Buf[1000]; | |
189 | sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n", | |
190 | (long)SBuf.st_size - 1, | |
191 | TimeRFC1123(SBuf.st_mtime).c_str()); | |
192 | headers = curl_slist_append(headers, Buf); | |
193 | } | |
d546f98d MV |
194 | |
195 | // go for it - if the file exists, append on it | |
196 | File = new FileFd(Itm->DestFile, FileFd::WriteAny); | |
197 | File->Seek(File->Size()); | |
198 | ||
199 | // keep apt updated | |
200 | Res.Filename = Itm->DestFile; | |
201 | ||
202 | // get it! | |
203 | CURLcode success = curl_easy_perform(curl); | |
4c499611 | 204 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curl_responsecode); |
d546f98d MV |
205 | |
206 | // cleanup | |
4c499611 MV |
207 | if(success != 0) |
208 | { | |
209 | unlink(File->Name().c_str()); | |
714ee06c | 210 | _error->Error(curl_errorstr); |
d546f98d MV |
211 | Fail(); |
212 | return true; | |
213 | } | |
4c499611 | 214 | File->Close(); |
d546f98d MV |
215 | |
216 | if (Res.Size == 0) | |
217 | Res.Size = File->Size(); | |
218 | ||
219 | // check the downloaded result | |
220 | struct stat Buf; | |
221 | if (stat(File->Name().c_str(),&Buf) == 0) | |
222 | { | |
223 | Res.Size = Buf.st_size; | |
224 | Res.Filename = File->Name(); | |
225 | Res.LastModified = Buf.st_mtime; | |
226 | Res.IMSHit = false; | |
4c499611 | 227 | if (curl_responsecode == 304) |
714ee06c | 228 | { |
d546f98d | 229 | Res.IMSHit = true; |
714ee06c MV |
230 | Res.LastModified = Itm->LastModified; |
231 | } | |
d546f98d MV |
232 | } |
233 | ||
234 | // take hashes | |
235 | Hashes Hash; | |
236 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
237 | Hash.AddFD(Fd.Fd(), Fd.Size()); | |
238 | Res.TakeHashes(Hash); | |
239 | ||
240 | // keep apt updated | |
241 | URIDone(Res); | |
242 | ||
243 | // cleanup | |
d546f98d MV |
244 | Res.Size = 0; |
245 | delete File; | |
246 | curl_slist_free_all(headers); | |
247 | ||
248 | return true; | |
249 | }; | |
250 | ||
251 | int main() | |
252 | { | |
253 | setlocale(LC_ALL, ""); | |
254 | ||
255 | HttpsMethod Mth; | |
256 | curl_global_init(CURL_GLOBAL_SSL) ; | |
257 | ||
258 | return Mth.Run(); | |
259 | } | |
260 | ||
261 |