1 // -*- mode: cpp; mode: fold -*-
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 HTTPS Aquire Method - This is the HTTPS aquire method for APT.
10 ##################################################################### */
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>
37 HttpsMethod::write_data(void *buffer
, size_t size
, size_t nmemb
, void *userp
)
39 HttpsMethod
*me
= (HttpsMethod
*)userp
;
41 if(me
->File
->Write(buffer
, size
*nmemb
) != true)
48 HttpsMethod::progress_callback(void *clientp
, double dltotal
, double dlnow
,
49 double ultotal
, double ulnow
)
51 HttpsMethod
*me
= (HttpsMethod
*)clientp
;
52 if(dltotal
> 0 && me
->Res
.Size
== 0) {
53 me
->Res
.Size
= dltotal
;
54 me
->URIStart(me
->Res
);
59 bool HttpsMethod::SetupProxy()
61 URI ServerName
= Queue
->Uri
;
63 // Determine the proxy setting
64 if (getenv("http_proxy") == 0)
66 string DefProxy
= _config
->Find("Acquire::http::Proxy");
67 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
68 if (SpecificProxy
.empty() == false)
70 if (SpecificProxy
== "DIRECT")
73 Proxy
= SpecificProxy
;
79 // Parse no_proxy, a , separated list of domains
80 if (getenv("no_proxy") != 0)
82 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
86 // Determine what host and port to use based on the proxy settings
89 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
95 curl_easy_setopt(curl
, CURLOPT_PROXYPORT
, Proxy
.Port
);
96 curl_easy_setopt(curl
, CURLOPT_PROXY
, Proxy
.Host
.c_str());
101 // HttpsMethod::Fetch - Fetch an item /*{{{*/
102 // ---------------------------------------------------------------------
103 /* This adds an item to the pipeline. We keep the pipeline at a fixed
105 bool HttpsMethod::Fetch(FetchItem
*Itm
)
109 struct curl_slist
*headers
=NULL
;
113 // - http::Pipeline-Depth
114 // - error checking/reporting
115 // - more debug options? (CURLOPT_DEBUGFUNCTION?)
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);
128 // FIXME: https: offer various options of verification
129 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, false);
132 if(_config
->FindB("Acquire::http::No-Cache",false) == false)
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());
140 // cache disabled by user
141 headers
= curl_slist_append(headers
, "Cache-Control: no-cache");
142 headers
= curl_slist_append(headers
, "Pragma: no-cache");
144 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, headers
);
147 curl_easy_setopt(curl
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_IFMODSINCE
);
148 curl_easy_setopt(curl
, CURLOPT_TIMEVALUE
, Itm
->LastModified
);
151 int dlLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
153 curl_easy_setopt(curl
, CURLOPT_MAX_RECV_SPEED_LARGE
, dlLimit
);
156 curl_easy_setopt(curl
, CURLOPT_USERAGENT
,"Debian APT-CURL/1.0 ("VERSION
")");
159 if(_config
->FindB("Debug::Acquire::http", false))
160 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, true);
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
);
166 // go for it - if the file exists, append on it
167 File
= new FileFd(Itm
->DestFile
, FileFd::WriteAny
);
168 File
->Seek(File
->Size());
171 Res
.Filename
= Itm
->DestFile
;
174 CURLcode success
= curl_easy_perform(curl
);
184 Res
.Size
= File
->Size();
186 // check the downloaded result
188 if (stat(File
->Name().c_str(),&Buf
) == 0)
190 Res
.Size
= Buf
.st_size
;
191 Res
.Filename
= File
->Name();
192 Res
.LastModified
= Buf
.st_mtime
;
194 if (Itm
->LastModified
== Buf
.st_mtime
&& Itm
->LastModified
!= 0)
200 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
201 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
202 Res
.TakeHashes(Hash
);
211 curl_slist_free_all(headers
);
218 setlocale(LC_ALL
, "");
221 curl_global_init(CURL_GLOBAL_SSL
) ;