https.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
  4. /* ######################################################################
  5. HTTPS Aquire Method - This is the HTTPS aquire method for APT.
  6. It uses libcurl
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <apt-pkg/fileutl.h>
  11. #include <apt-pkg/acquire-method.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/hashes.h>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <utime.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <iostream>
  23. #include <apti18n.h>
  24. #include <sstream>
  25. #include "config.h"
  26. #include "https.h"
  27. /*}}}*/
  28. using namespace std;
  29. size_t
  30. HttpsMethod::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
  31. {
  32. HttpsMethod *me = (HttpsMethod *)userp;
  33. if(me->File->Write(buffer, size*nmemb) != true)
  34. return false;
  35. return size*nmemb;
  36. }
  37. int
  38. HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow,
  39. double ultotal, double ulnow)
  40. {
  41. HttpsMethod *me = (HttpsMethod *)clientp;
  42. if(dltotal > 0 && me->Res.Size == 0) {
  43. me->Res.Size = dltotal;
  44. me->URIStart(me->Res);
  45. }
  46. return 0;
  47. }
  48. bool HttpsMethod::SetupProxy()
  49. {
  50. URI ServerName = Queue->Uri;
  51. // Determine the proxy setting
  52. if (getenv("http_proxy") == 0)
  53. {
  54. string DefProxy = _config->Find("Acquire::http::Proxy");
  55. string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
  56. if (SpecificProxy.empty() == false)
  57. {
  58. if (SpecificProxy == "DIRECT")
  59. Proxy = "";
  60. else
  61. Proxy = SpecificProxy;
  62. }
  63. else
  64. Proxy = DefProxy;
  65. }
  66. // Parse no_proxy, a , separated list of domains
  67. if (getenv("no_proxy") != 0)
  68. {
  69. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  70. Proxy = "";
  71. }
  72. // Determine what host and port to use based on the proxy settings
  73. int Port = 0;
  74. string Host;
  75. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  76. {
  77. }
  78. else
  79. {
  80. if (Proxy.Port != 0)
  81. curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
  82. curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
  83. }
  84. }
  85. // HttpsMethod::Fetch - Fetch an item /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  88. depth. */
  89. bool HttpsMethod::Fetch(FetchItem *Itm)
  90. {
  91. stringstream ss;
  92. struct stat SBuf;
  93. struct curl_slist *headers=NULL;
  94. // TODO:
  95. // - http::Timeout
  96. // - http::Pipeline-Depth
  97. // - error checking/reporting
  98. // - more debug options? (CURLOPT_DEBUGFUNCTION?)
  99. SetupProxy();
  100. // callbacks
  101. curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
  102. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  103. curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
  104. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
  105. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
  106. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
  107. curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
  108. // FIXME: https: offer various options of verification
  109. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  110. // cache-control
  111. if(_config->FindB("Acquire::http::No-Cache",false) == false)
  112. {
  113. // cache enabled
  114. if (_config->FindB("Acquire::http::No-Store",false) == true)
  115. headers = curl_slist_append(headers,"Cache-Control: no-store");
  116. ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::http::Max-Age",0));
  117. headers = curl_slist_append(headers, ss.str().c_str());
  118. } else {
  119. // cache disabled by user
  120. headers = curl_slist_append(headers, "Cache-Control: no-cache");
  121. headers = curl_slist_append(headers, "Pragma: no-cache");
  122. }
  123. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  124. // set time values
  125. curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
  126. curl_easy_setopt(curl, CURLOPT_TIMEVALUE, Itm->LastModified);
  127. // speed limit
  128. int dlLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
  129. if (dlLimit > 0)
  130. curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, dlLimit);
  131. // set header
  132. curl_easy_setopt(curl, CURLOPT_USERAGENT,"Debian APT-CURL/1.0 ("VERSION")");
  133. // debug
  134. if(_config->FindB("Debug::Acquire::http", false))
  135. curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
  136. // In this case we send an if-range query with a range header
  137. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  138. curl_easy_setopt(curl, CURLOPT_RESUME_FROM, (long)SBuf.st_size);
  139. // go for it - if the file exists, append on it
  140. File = new FileFd(Itm->DestFile, FileFd::WriteAny);
  141. File->Seek(File->Size());
  142. // keep apt updated
  143. Res.Filename = Itm->DestFile;
  144. // get it!
  145. CURLcode success = curl_easy_perform(curl);
  146. // cleanup
  147. if(success != 0) {
  148. Fail();
  149. return true;
  150. }
  151. if (Res.Size == 0)
  152. Res.Size = File->Size();
  153. // check the downloaded result
  154. struct stat Buf;
  155. if (stat(File->Name().c_str(),&Buf) == 0)
  156. {
  157. Res.Size = Buf.st_size;
  158. Res.Filename = File->Name();
  159. Res.LastModified = Buf.st_mtime;
  160. Res.IMSHit = false;
  161. if (Itm->LastModified == Buf.st_mtime && Itm->LastModified != 0)
  162. Res.IMSHit = true;
  163. }
  164. // take hashes
  165. Hashes Hash;
  166. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  167. Hash.AddFD(Fd.Fd(), Fd.Size());
  168. Res.TakeHashes(Hash);
  169. // keep apt updated
  170. URIDone(Res);
  171. // cleanup
  172. File->Close();
  173. Res.Size = 0;
  174. delete File;
  175. curl_slist_free_all(headers);
  176. return true;
  177. };
  178. int main()
  179. {
  180. setlocale(LC_ALL, "");
  181. HttpsMethod Mth;
  182. curl_global_init(CURL_GLOBAL_SSL) ;
  183. return Mth.Run();
  184. }