http.cc 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
  4. /* ######################################################################
  5. HTTP Acquire Method - This is the HTTP acquire method for APT.
  6. It uses HTTP/1.1 and many of the fancy options there-in, such as
  7. pipelining, range, if-range and so on.
  8. It is based on a doubly buffered select loop. A groupe of requests are
  9. fed into a single output buffer that is constantly fed out the
  10. socket. This provides ideal pipelining as in many cases all of the
  11. requests will fit into a single packet. The input socket is buffered
  12. the same way and fed into the fd for the file (may be a pipe in future).
  13. This double buffering provides fairly substantial transfer rates,
  14. compared to wget the http method is about 4% faster. Most importantly,
  15. when HTTP is compared with FTP as a protocol the speed difference is
  16. huge. In tests over the internet from two sites to llug (via ATM) this
  17. program got 230k/s sustained http transfer rates. FTP on the other
  18. hand topped out at 170k/s. That combined with the time to setup the
  19. FTP connection makes HTTP a vastly superior protocol.
  20. ##################################################################### */
  21. /*}}}*/
  22. // Include Files /*{{{*/
  23. #include <config.h>
  24. #include <apt-pkg/fileutl.h>
  25. #include <apt-pkg/configuration.h>
  26. #include <apt-pkg/error.h>
  27. #include <apt-pkg/hashes.h>
  28. #include <apt-pkg/netrc.h>
  29. #include <apt-pkg/strutl.h>
  30. #include <apt-pkg/proxy.h>
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <sys/select.h>
  34. #include <cstring>
  35. #include <sys/stat.h>
  36. #include <sys/time.h>
  37. #include <unistd.h>
  38. #include <stdio.h>
  39. #include <errno.h>
  40. #include <arpa/inet.h>
  41. #include <iostream>
  42. #include <sstream>
  43. #include "config.h"
  44. #include "connect.h"
  45. #include "http.h"
  46. #include <apti18n.h>
  47. /*}}}*/
  48. using namespace std;
  49. unsigned long long CircleBuf::BwReadLimit=0;
  50. unsigned long long CircleBuf::BwTickReadData=0;
  51. struct timeval CircleBuf::BwReadTick={0,0};
  52. const unsigned int CircleBuf::BW_HZ=10;
  53. // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* */
  56. CircleBuf::CircleBuf(HttpMethod const * const Owner, unsigned long long Size)
  57. : Size(Size), Hash(NULL), TotalWriten(0)
  58. {
  59. Buf = new unsigned char[Size];
  60. Reset();
  61. CircleBuf::BwReadLimit = Owner->ConfigFindI("Dl-Limit", 0) * 1024;
  62. }
  63. /*}}}*/
  64. // CircleBuf::Reset - Reset to the default state /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* */
  67. void CircleBuf::Reset()
  68. {
  69. InP = 0;
  70. OutP = 0;
  71. StrPos = 0;
  72. TotalWriten = 0;
  73. MaxGet = (unsigned long long)-1;
  74. OutQueue = string();
  75. if (Hash != NULL)
  76. {
  77. delete Hash;
  78. Hash = NULL;
  79. }
  80. }
  81. /*}}}*/
  82. // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* This fills up the buffer with as much data as is in the FD, assuming it
  85. is non-blocking.. */
  86. bool CircleBuf::Read(int Fd)
  87. {
  88. while (1)
  89. {
  90. // Woops, buffer is full
  91. if (InP - OutP == Size)
  92. return true;
  93. // what's left to read in this tick
  94. unsigned long long const BwReadMax = CircleBuf::BwReadLimit/BW_HZ;
  95. if(CircleBuf::BwReadLimit) {
  96. struct timeval now;
  97. gettimeofday(&now,0);
  98. unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 +
  99. now.tv_usec-CircleBuf::BwReadTick.tv_usec;
  100. if(d > 1000000/BW_HZ) {
  101. CircleBuf::BwReadTick = now;
  102. CircleBuf::BwTickReadData = 0;
  103. }
  104. if(CircleBuf::BwTickReadData >= BwReadMax) {
  105. usleep(1000000/BW_HZ);
  106. return true;
  107. }
  108. }
  109. // Write the buffer segment
  110. ssize_t Res;
  111. if(CircleBuf::BwReadLimit) {
  112. Res = read(Fd,Buf + (InP%Size),
  113. BwReadMax > LeftRead() ? LeftRead() : BwReadMax);
  114. } else
  115. Res = read(Fd,Buf + (InP%Size),LeftRead());
  116. if(Res > 0 && BwReadLimit > 0)
  117. CircleBuf::BwTickReadData += Res;
  118. if (Res == 0)
  119. return false;
  120. if (Res < 0)
  121. {
  122. if (errno == EAGAIN)
  123. return true;
  124. return false;
  125. }
  126. if (InP == 0)
  127. gettimeofday(&Start,0);
  128. InP += Res;
  129. }
  130. }
  131. /*}}}*/
  132. // CircleBuf::Read - Put the string into the buffer /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* This will hold the string in and fill the buffer with it as it empties */
  135. bool CircleBuf::Read(string const &Data)
  136. {
  137. OutQueue.append(Data);
  138. FillOut();
  139. return true;
  140. }
  141. /*}}}*/
  142. // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* */
  145. void CircleBuf::FillOut()
  146. {
  147. if (OutQueue.empty() == true)
  148. return;
  149. while (1)
  150. {
  151. // Woops, buffer is full
  152. if (InP - OutP == Size)
  153. return;
  154. // Write the buffer segment
  155. unsigned long long Sz = LeftRead();
  156. if (OutQueue.length() - StrPos < Sz)
  157. Sz = OutQueue.length() - StrPos;
  158. memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz);
  159. // Advance
  160. StrPos += Sz;
  161. InP += Sz;
  162. if (OutQueue.length() == StrPos)
  163. {
  164. StrPos = 0;
  165. OutQueue = "";
  166. return;
  167. }
  168. }
  169. }
  170. /*}}}*/
  171. // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* This empties the buffer into the FD. */
  174. bool CircleBuf::Write(int Fd)
  175. {
  176. while (1)
  177. {
  178. FillOut();
  179. // Woops, buffer is empty
  180. if (OutP == InP)
  181. return true;
  182. if (OutP == MaxGet)
  183. return true;
  184. // Write the buffer segment
  185. ssize_t Res;
  186. Res = write(Fd,Buf + (OutP%Size),LeftWrite());
  187. if (Res == 0)
  188. return false;
  189. if (Res < 0)
  190. {
  191. if (errno == EAGAIN)
  192. return true;
  193. return false;
  194. }
  195. TotalWriten += Res;
  196. if (Hash != NULL)
  197. Hash->Add(Buf + (OutP%Size),Res);
  198. OutP += Res;
  199. }
  200. }
  201. /*}}}*/
  202. // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* This copies till the first empty line */
  205. bool CircleBuf::WriteTillEl(string &Data,bool Single)
  206. {
  207. // We cheat and assume it is unneeded to have more than one buffer load
  208. for (unsigned long long I = OutP; I < InP; I++)
  209. {
  210. if (Buf[I%Size] != '\n')
  211. continue;
  212. ++I;
  213. if (Single == false)
  214. {
  215. if (I < InP && Buf[I%Size] == '\r')
  216. ++I;
  217. if (I >= InP || Buf[I%Size] != '\n')
  218. continue;
  219. ++I;
  220. }
  221. Data = "";
  222. while (OutP < I)
  223. {
  224. unsigned long long Sz = LeftWrite();
  225. if (Sz == 0)
  226. return false;
  227. if (I - OutP < Sz)
  228. Sz = I - OutP;
  229. Data += string((char *)(Buf + (OutP%Size)),Sz);
  230. OutP += Sz;
  231. }
  232. return true;
  233. }
  234. return false;
  235. }
  236. /*}}}*/
  237. // CircleBuf::Stats - Print out stats information /*{{{*/
  238. // ---------------------------------------------------------------------
  239. /* */
  240. void CircleBuf::Stats()
  241. {
  242. if (InP == 0)
  243. return;
  244. struct timeval Stop;
  245. gettimeofday(&Stop,0);
  246. /* float Diff = Stop.tv_sec - Start.tv_sec +
  247. (float)(Stop.tv_usec - Start.tv_usec)/1000000;
  248. clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
  249. }
  250. /*}}}*/
  251. CircleBuf::~CircleBuf()
  252. {
  253. delete [] Buf;
  254. delete Hash;
  255. }
  256. // HttpServerState::HttpServerState - Constructor /*{{{*/
  257. HttpServerState::HttpServerState(URI Srv,HttpMethod *Owner) : ServerState(Srv, Owner), In(Owner, 64*1024), Out(Owner, 4*1024)
  258. {
  259. TimeOut = Owner->ConfigFindI("Timeout", TimeOut);
  260. Reset();
  261. }
  262. /*}}}*/
  263. // HttpServerState::Open - Open a connection to the server /*{{{*/
  264. // ---------------------------------------------------------------------
  265. /* This opens a connection to the server. */
  266. static bool TalkToSocksProxy(int const ServerFd, std::string const &Proxy,
  267. char const * const type, bool const ReadWrite, uint8_t * const ToFrom,
  268. unsigned int const Size, unsigned int const Timeout)
  269. {
  270. if (WaitFd(ServerFd, ReadWrite, Timeout) == false)
  271. return _error->Error("Waiting for the SOCKS proxy %s to %s timed out", URI::SiteOnly(Proxy).c_str(), type);
  272. if (ReadWrite == false)
  273. {
  274. if (FileFd::Read(ServerFd, ToFrom, Size) == false)
  275. return _error->Error("Reading the %s from SOCKS proxy %s failed", type, URI::SiteOnly(Proxy).c_str());
  276. }
  277. else
  278. {
  279. if (FileFd::Write(ServerFd, ToFrom, Size) == false)
  280. return _error->Error("Writing the %s to SOCKS proxy %s failed", type, URI::SiteOnly(Proxy).c_str());
  281. }
  282. return true;
  283. }
  284. bool HttpServerState::Open()
  285. {
  286. // Use the already open connection if possible.
  287. if (ServerFd != -1)
  288. return true;
  289. Close();
  290. In.Reset();
  291. Out.Reset();
  292. Persistent = true;
  293. // Determine the proxy setting
  294. AutoDetectProxy(ServerName);
  295. string SpecificProxy = Owner->ConfigFind("Proxy::" + ServerName.Host, "");
  296. if (!SpecificProxy.empty())
  297. {
  298. if (SpecificProxy == "DIRECT")
  299. Proxy = "";
  300. else
  301. Proxy = SpecificProxy;
  302. }
  303. else
  304. {
  305. string DefProxy = Owner->ConfigFind("Proxy", "");
  306. if (!DefProxy.empty())
  307. {
  308. Proxy = DefProxy;
  309. }
  310. else
  311. {
  312. char* result = getenv("http_proxy");
  313. Proxy = result ? result : "";
  314. }
  315. }
  316. // Parse no_proxy, a , separated list of domains
  317. if (getenv("no_proxy") != 0)
  318. {
  319. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  320. Proxy = "";
  321. }
  322. if (Proxy.empty() == false)
  323. Owner->AddProxyAuth(Proxy, ServerName);
  324. if (Proxy.Access == "socks5h")
  325. {
  326. if (Connect(Proxy.Host, Proxy.Port, "socks", 1080, ServerFd, TimeOut, Owner) == false)
  327. return false;
  328. /* We implement a very basic SOCKS5 client here complying mostly to RFC1928 expect
  329. * for not offering GSSAPI auth which is a must (we only do no or user/pass auth).
  330. * We also expect the SOCKS5 server to do hostname lookup (aka socks5h) */
  331. std::string const ProxyInfo = URI::SiteOnly(Proxy);
  332. Owner->Status(_("Connecting to %s (%s)"),"SOCKS5h proxy",ProxyInfo.c_str());
  333. auto const Timeout = Owner->ConfigFindI("TimeOut", 120);
  334. #define APT_WriteOrFail(TYPE, DATA, LENGTH) if (TalkToSocksProxy(ServerFd, ProxyInfo, TYPE, true, DATA, LENGTH, Timeout) == false) return false
  335. #define APT_ReadOrFail(TYPE, DATA, LENGTH) if (TalkToSocksProxy(ServerFd, ProxyInfo, TYPE, false, DATA, LENGTH, Timeout) == false) return false
  336. if (ServerName.Host.length() > 255)
  337. return _error->Error("Can't use SOCKS5h as hostname %s is too long!", ServerName.Host.c_str());
  338. if (Proxy.User.length() > 255 || Proxy.Password.length() > 255)
  339. return _error->Error("Can't use user&pass auth as they are too long (%lu and %lu) for the SOCKS5!", Proxy.User.length(), Proxy.Password.length());
  340. if (Proxy.User.empty())
  341. {
  342. uint8_t greeting[] = { 0x05, 0x01, 0x00 };
  343. APT_WriteOrFail("greet-1", greeting, sizeof(greeting));
  344. }
  345. else
  346. {
  347. uint8_t greeting[] = { 0x05, 0x02, 0x00, 0x02 };
  348. APT_WriteOrFail("greet-2", greeting, sizeof(greeting));
  349. }
  350. uint8_t greeting[2];
  351. APT_ReadOrFail("greet back", greeting, sizeof(greeting));
  352. if (greeting[0] != 0x05)
  353. return _error->Error("SOCKS proxy %s greets back with wrong version: %d", ProxyInfo.c_str(), greeting[0]);
  354. if (greeting[1] == 0x00)
  355. ; // no auth has no method-dependent sub-negotiations
  356. else if (greeting[1] == 0x02)
  357. {
  358. if (Proxy.User.empty())
  359. return _error->Error("SOCKS proxy %s negotiated user&pass auth, but we had not offered it!", ProxyInfo.c_str());
  360. // user&pass auth sub-negotiations are defined by RFC1929
  361. std::vector<uint8_t> auth = {{ 0x01, static_cast<uint8_t>(Proxy.User.length()) }};
  362. std::copy(Proxy.User.begin(), Proxy.User.end(), std::back_inserter(auth));
  363. auth.push_back(static_cast<uint8_t>(Proxy.Password.length()));
  364. std::copy(Proxy.Password.begin(), Proxy.Password.end(), std::back_inserter(auth));
  365. APT_WriteOrFail("user&pass auth", auth.data(), auth.size());
  366. uint8_t authstatus[2];
  367. APT_ReadOrFail("auth report", authstatus, sizeof(authstatus));
  368. if (authstatus[0] != 0x01)
  369. return _error->Error("SOCKS proxy %s auth status response with wrong version: %d", ProxyInfo.c_str(), authstatus[0]);
  370. if (authstatus[1] != 0x00)
  371. return _error->Error("SOCKS proxy %s reported authorization failure: username or password incorrect? (%d)", ProxyInfo.c_str(), authstatus[1]);
  372. }
  373. else
  374. return _error->Error("SOCKS proxy %s greets back having not found a common authorization method: %d", ProxyInfo.c_str(), greeting[1]);
  375. union { uint16_t * i; uint8_t * b; } portu;
  376. uint16_t port = htons(static_cast<uint16_t>(ServerName.Port == 0 ? 80 : ServerName.Port));
  377. portu.i = &port;
  378. std::vector<uint8_t> request = {{ 0x05, 0x01, 0x00, 0x03, static_cast<uint8_t>(ServerName.Host.length()) }};
  379. std::copy(ServerName.Host.begin(), ServerName.Host.end(), std::back_inserter(request));
  380. request.push_back(portu.b[0]);
  381. request.push_back(portu.b[1]);
  382. APT_WriteOrFail("request", request.data(), request.size());
  383. uint8_t response[4];
  384. APT_ReadOrFail("first part of response", response, sizeof(response));
  385. if (response[0] != 0x05)
  386. return _error->Error("SOCKS proxy %s response with wrong version: %d", ProxyInfo.c_str(), response[0]);
  387. if (response[2] != 0x00)
  388. return _error->Error("SOCKS proxy %s has unexpected non-zero reserved field value: %d", ProxyInfo.c_str(), response[2]);
  389. std::string bindaddr;
  390. if (response[3] == 0x01) // IPv4 address
  391. {
  392. uint8_t ip4port[6];
  393. APT_ReadOrFail("IPv4+Port of response", ip4port, sizeof(ip4port));
  394. portu.b[0] = ip4port[4];
  395. portu.b[1] = ip4port[5];
  396. port = ntohs(*portu.i);
  397. strprintf(bindaddr, "%d.%d.%d.%d:%d", ip4port[0], ip4port[1], ip4port[2], ip4port[3], port);
  398. }
  399. else if (response[3] == 0x03) // hostname
  400. {
  401. uint8_t namelength;
  402. APT_ReadOrFail("hostname length of response", &namelength, 1);
  403. uint8_t hostname[namelength + 2];
  404. APT_ReadOrFail("hostname of response", hostname, sizeof(hostname));
  405. portu.b[0] = hostname[namelength];
  406. portu.b[1] = hostname[namelength + 1];
  407. port = ntohs(*portu.i);
  408. hostname[namelength] = '\0';
  409. strprintf(bindaddr, "%s:%d", hostname, port);
  410. }
  411. else if (response[3] == 0x04) // IPv6 address
  412. {
  413. uint8_t ip6port[18];
  414. APT_ReadOrFail("IPv6+port of response", ip6port, sizeof(ip6port));
  415. portu.b[0] = ip6port[16];
  416. portu.b[1] = ip6port[17];
  417. port = ntohs(*portu.i);
  418. strprintf(bindaddr, "[%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X]:%d",
  419. ip6port[0], ip6port[1], ip6port[2], ip6port[3], ip6port[4], ip6port[5], ip6port[6], ip6port[7],
  420. ip6port[8], ip6port[9], ip6port[10], ip6port[11], ip6port[12], ip6port[13], ip6port[14], ip6port[15],
  421. port);
  422. }
  423. else
  424. return _error->Error("SOCKS proxy %s destination address is of unknown type: %d",
  425. ProxyInfo.c_str(), response[3]);
  426. if (response[1] != 0x00)
  427. {
  428. char const * errstr = nullptr;
  429. auto errcode = response[1];
  430. // Tor error reporting can be a bit arcane, lets try to detect & fix it up
  431. if (bindaddr == "0.0.0.0:0")
  432. {
  433. auto const lastdot = ServerName.Host.rfind('.');
  434. if (lastdot == std::string::npos || ServerName.Host.substr(lastdot) != ".onion")
  435. ;
  436. else if (errcode == 0x01)
  437. {
  438. auto const prevdot = ServerName.Host.rfind('.', lastdot - 1);
  439. if (lastdot == 16 && prevdot == std::string::npos)
  440. ; // valid .onion address
  441. else if (prevdot != std::string::npos && (lastdot - prevdot) == 17)
  442. ; // valid .onion address with subdomain(s)
  443. else
  444. {
  445. errstr = "Invalid hostname: onion service name must be 16 characters long";
  446. Owner->SetFailReason("SOCKS");
  447. }
  448. }
  449. // in all likelihood the service is either down or the address has
  450. // a typo and so "Host unreachable" is the better understood error
  451. // compared to the technically correct "TLL expired".
  452. else if (errcode == 0x06)
  453. errcode = 0x04;
  454. }
  455. if (errstr == nullptr)
  456. {
  457. switch (errcode)
  458. {
  459. case 0x01: errstr = "general SOCKS server failure"; Owner->SetFailReason("SOCKS"); break;
  460. case 0x02: errstr = "connection not allowed by ruleset"; Owner->SetFailReason("SOCKS"); break;
  461. case 0x03: errstr = "Network unreachable"; Owner->SetFailReason("ConnectionTimedOut"); break;
  462. case 0x04: errstr = "Host unreachable"; Owner->SetFailReason("ConnectionTimedOut"); break;
  463. case 0x05: errstr = "Connection refused"; Owner->SetFailReason("ConnectionRefused"); break;
  464. case 0x06: errstr = "TTL expired"; Owner->SetFailReason("Timeout"); break;
  465. case 0x07: errstr = "Command not supported"; Owner->SetFailReason("SOCKS"); break;
  466. case 0x08: errstr = "Address type not supported"; Owner->SetFailReason("SOCKS"); break;
  467. default: errstr = "Unknown error"; Owner->SetFailReason("SOCKS"); break;
  468. }
  469. }
  470. return _error->Error("SOCKS proxy %s could not connect to %s (%s) due to: %s (%d)",
  471. ProxyInfo.c_str(), ServerName.Host.c_str(), bindaddr.c_str(), errstr, response[1]);
  472. }
  473. else if (Owner->DebugEnabled())
  474. ioprintf(std::clog, "http: SOCKS proxy %s connection established to %s (%s)\n",
  475. ProxyInfo.c_str(), ServerName.Host.c_str(), bindaddr.c_str());
  476. if (WaitFd(ServerFd, true, Timeout) == false)
  477. return _error->Error("SOCKS proxy %s reported connection to %s (%s), but timed out",
  478. ProxyInfo.c_str(), ServerName.Host.c_str(), bindaddr.c_str());
  479. #undef APT_ReadOrFail
  480. #undef APT_WriteOrFail
  481. }
  482. else
  483. {
  484. // Determine what host and port to use based on the proxy settings
  485. int Port = 0;
  486. string Host;
  487. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  488. {
  489. if (ServerName.Port != 0)
  490. Port = ServerName.Port;
  491. Host = ServerName.Host;
  492. }
  493. else if (Proxy.Access != "http")
  494. return _error->Error("Unsupported proxy configured: %s", URI::SiteOnly(Proxy).c_str());
  495. else
  496. {
  497. if (Proxy.Port != 0)
  498. Port = Proxy.Port;
  499. Host = Proxy.Host;
  500. }
  501. return Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner);
  502. }
  503. return true;
  504. }
  505. /*}}}*/
  506. // HttpServerState::Close - Close a connection to the server /*{{{*/
  507. // ---------------------------------------------------------------------
  508. /* */
  509. bool HttpServerState::Close()
  510. {
  511. close(ServerFd);
  512. ServerFd = -1;
  513. return true;
  514. }
  515. /*}}}*/
  516. // HttpServerState::RunData - Transfer the data from the socket /*{{{*/
  517. bool HttpServerState::RunData(FileFd * const File)
  518. {
  519. State = Data;
  520. // Chunked transfer encoding is fun..
  521. if (Encoding == Chunked)
  522. {
  523. while (1)
  524. {
  525. // Grab the block size
  526. bool Last = true;
  527. string Data;
  528. In.Limit(-1);
  529. do
  530. {
  531. if (In.WriteTillEl(Data,true) == true)
  532. break;
  533. }
  534. while ((Last = Go(false, File)) == true);
  535. if (Last == false)
  536. return false;
  537. // See if we are done
  538. unsigned long long Len = strtoull(Data.c_str(),0,16);
  539. if (Len == 0)
  540. {
  541. In.Limit(-1);
  542. // We have to remove the entity trailer
  543. Last = true;
  544. do
  545. {
  546. if (In.WriteTillEl(Data,true) == true && Data.length() <= 2)
  547. break;
  548. }
  549. while ((Last = Go(false, File)) == true);
  550. if (Last == false)
  551. return false;
  552. return !_error->PendingError();
  553. }
  554. // Transfer the block
  555. In.Limit(Len);
  556. while (Go(true, File) == true)
  557. if (In.IsLimit() == true)
  558. break;
  559. // Error
  560. if (In.IsLimit() == false)
  561. return false;
  562. // The server sends an extra new line before the next block specifier..
  563. In.Limit(-1);
  564. Last = true;
  565. do
  566. {
  567. if (In.WriteTillEl(Data,true) == true)
  568. break;
  569. }
  570. while ((Last = Go(false, File)) == true);
  571. if (Last == false)
  572. return false;
  573. }
  574. }
  575. else
  576. {
  577. /* Closes encoding is used when the server did not specify a size, the
  578. loss of the connection means we are done */
  579. if (JunkSize != 0)
  580. In.Limit(JunkSize);
  581. else if (DownloadSize != 0)
  582. In.Limit(DownloadSize);
  583. else if (Persistent == false)
  584. In.Limit(-1);
  585. // Just transfer the whole block.
  586. do
  587. {
  588. if (In.IsLimit() == false)
  589. continue;
  590. In.Limit(-1);
  591. return !_error->PendingError();
  592. }
  593. while (Go(true, File) == true);
  594. }
  595. return Owner->Flush() && !_error->PendingError();
  596. }
  597. /*}}}*/
  598. bool HttpServerState::RunDataToDevNull() /*{{{*/
  599. {
  600. // no need to clean up if we discard the connection anyhow
  601. if (Persistent == false)
  602. return true;
  603. FileFd DevNull("/dev/null", FileFd::WriteOnly);
  604. return RunData(&DevNull);
  605. }
  606. /*}}}*/
  607. bool HttpServerState::ReadHeaderLines(std::string &Data) /*{{{*/
  608. {
  609. return In.WriteTillEl(Data);
  610. }
  611. /*}}}*/
  612. bool HttpServerState::LoadNextResponse(bool const ToFile, FileFd * const File)/*{{{*/
  613. {
  614. return Go(ToFile, File);
  615. }
  616. /*}}}*/
  617. bool HttpServerState::WriteResponse(const std::string &Data) /*{{{*/
  618. {
  619. return Out.Read(Data);
  620. }
  621. /*}}}*/
  622. APT_PURE bool HttpServerState::IsOpen() /*{{{*/
  623. {
  624. return (ServerFd != -1);
  625. }
  626. /*}}}*/
  627. bool HttpServerState::InitHashes(HashStringList const &ExpectedHashes) /*{{{*/
  628. {
  629. delete In.Hash;
  630. In.Hash = new Hashes(ExpectedHashes);
  631. return true;
  632. }
  633. /*}}}*/
  634. void HttpServerState::Reset(bool const Everything) /*{{{*/
  635. {
  636. ServerState::Reset(Everything);
  637. if (Everything)
  638. ServerFd = -1;
  639. }
  640. /*}}}*/
  641. APT_PURE Hashes * HttpServerState::GetHashes() /*{{{*/
  642. {
  643. return In.Hash;
  644. }
  645. /*}}}*/
  646. // HttpServerState::Die - The server has closed the connection. /*{{{*/
  647. bool HttpServerState::Die(FileFd * const File)
  648. {
  649. unsigned int LErrno = errno;
  650. // Dump the buffer to the file
  651. if (State == ServerState::Data)
  652. {
  653. if (File == nullptr)
  654. return true;
  655. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  656. // can't be set
  657. if (File->Name() != "/dev/null")
  658. SetNonBlock(File->Fd(),false);
  659. while (In.WriteSpace() == true)
  660. {
  661. if (In.Write(File->Fd()) == false)
  662. return _error->Errno("write",_("Error writing to the file"));
  663. // Done
  664. if (In.IsLimit() == true)
  665. return true;
  666. }
  667. }
  668. // See if this is because the server finished the data stream
  669. if (In.IsLimit() == false && State != HttpServerState::Header &&
  670. Persistent == true)
  671. {
  672. Close();
  673. if (LErrno == 0)
  674. return _error->Error(_("Error reading from server. Remote end closed connection"));
  675. errno = LErrno;
  676. return _error->Errno("read",_("Error reading from server"));
  677. }
  678. else
  679. {
  680. In.Limit(-1);
  681. // Nothing left in the buffer
  682. if (In.WriteSpace() == false)
  683. return false;
  684. // We may have got multiple responses back in one packet..
  685. Close();
  686. return true;
  687. }
  688. return false;
  689. }
  690. /*}}}*/
  691. // HttpServerState::Flush - Dump the buffer into the file /*{{{*/
  692. // ---------------------------------------------------------------------
  693. /* This takes the current input buffer from the Server FD and writes it
  694. into the file */
  695. bool HttpServerState::Flush(FileFd * const File)
  696. {
  697. if (File != NULL)
  698. {
  699. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  700. // can't be set
  701. if (File->Name() != "/dev/null")
  702. SetNonBlock(File->Fd(),false);
  703. if (In.WriteSpace() == false)
  704. return true;
  705. while (In.WriteSpace() == true)
  706. {
  707. if (In.Write(File->Fd()) == false)
  708. return _error->Errno("write",_("Error writing to file"));
  709. if (In.IsLimit() == true)
  710. return true;
  711. }
  712. if (In.IsLimit() == true || Persistent == false)
  713. return true;
  714. }
  715. return false;
  716. }
  717. /*}}}*/
  718. // HttpServerState::Go - Run a single loop /*{{{*/
  719. // ---------------------------------------------------------------------
  720. /* This runs the select loop over the server FDs, Output file FDs and
  721. stdin. */
  722. bool HttpServerState::Go(bool ToFile, FileFd * const File)
  723. {
  724. // Server has closed the connection
  725. if (ServerFd == -1 && (In.WriteSpace() == false ||
  726. ToFile == false))
  727. return false;
  728. fd_set rfds,wfds;
  729. FD_ZERO(&rfds);
  730. FD_ZERO(&wfds);
  731. /* Add the server. We only send more requests if the connection will
  732. be persisting */
  733. if (Out.WriteSpace() == true && ServerFd != -1
  734. && Persistent == true)
  735. FD_SET(ServerFd,&wfds);
  736. if (In.ReadSpace() == true && ServerFd != -1)
  737. FD_SET(ServerFd,&rfds);
  738. // Add the file
  739. int FileFD = -1;
  740. if (File != NULL)
  741. FileFD = File->Fd();
  742. if (In.WriteSpace() == true && ToFile == true && FileFD != -1)
  743. FD_SET(FileFD,&wfds);
  744. // Add stdin
  745. if (Owner->ConfigFindB("DependOnSTDIN", true) == true)
  746. FD_SET(STDIN_FILENO,&rfds);
  747. // Figure out the max fd
  748. int MaxFd = FileFD;
  749. if (MaxFd < ServerFd)
  750. MaxFd = ServerFd;
  751. // Select
  752. struct timeval tv;
  753. tv.tv_sec = TimeOut;
  754. tv.tv_usec = 0;
  755. int Res = 0;
  756. if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0)
  757. {
  758. if (errno == EINTR)
  759. return true;
  760. return _error->Errno("select",_("Select failed"));
  761. }
  762. if (Res == 0)
  763. {
  764. _error->Error(_("Connection timed out"));
  765. return Die(File);
  766. }
  767. // Handle server IO
  768. if (ServerFd != -1 && FD_ISSET(ServerFd,&rfds))
  769. {
  770. errno = 0;
  771. if (In.Read(ServerFd) == false)
  772. return Die(File);
  773. }
  774. if (ServerFd != -1 && FD_ISSET(ServerFd,&wfds))
  775. {
  776. errno = 0;
  777. if (Out.Write(ServerFd) == false)
  778. return Die(File);
  779. }
  780. // Send data to the file
  781. if (FileFD != -1 && FD_ISSET(FileFD,&wfds))
  782. {
  783. if (In.Write(FileFD) == false)
  784. return _error->Errno("write",_("Error writing to output file"));
  785. }
  786. if (MaximumSize > 0 && File && File->Tell() > MaximumSize)
  787. {
  788. Owner->SetFailReason("MaximumSizeExceeded");
  789. return _error->Error("Writing more data than expected (%llu > %llu)",
  790. File->Tell(), MaximumSize);
  791. }
  792. // Handle commands from APT
  793. if (FD_ISSET(STDIN_FILENO,&rfds))
  794. {
  795. if (Owner->Run(true) != -1)
  796. exit(100);
  797. }
  798. return true;
  799. }
  800. /*}}}*/
  801. // HttpMethod::SendReq - Send the HTTP request /*{{{*/
  802. // ---------------------------------------------------------------------
  803. /* This places the http request in the outbound buffer */
  804. void HttpMethod::SendReq(FetchItem *Itm)
  805. {
  806. URI Uri = Itm->Uri;
  807. {
  808. auto const plus = Binary.find('+');
  809. if (plus != std::string::npos)
  810. Uri.Access = Binary.substr(plus + 1);
  811. }
  812. // The HTTP server expects a hostname with a trailing :port
  813. std::stringstream Req;
  814. string ProperHost;
  815. if (Uri.Host.find(':') != string::npos)
  816. ProperHost = '[' + Uri.Host + ']';
  817. else
  818. ProperHost = Uri.Host;
  819. /* RFC 2616 §5.1.2 requires absolute URIs for requests to proxies,
  820. but while its a must for all servers to accept absolute URIs,
  821. it is assumed clients will sent an absolute path for non-proxies */
  822. std::string requesturi;
  823. if (Server->Proxy.Access != "http" || Server->Proxy.empty() == true || Server->Proxy.Host.empty())
  824. requesturi = Uri.Path;
  825. else
  826. requesturi = Uri;
  827. // The "+" is encoded as a workaround for a amazon S3 bug
  828. // see LP bugs #1003633 and #1086997.
  829. requesturi = QuoteString(requesturi, "+~ ");
  830. /* Build the request. No keep-alive is included as it is the default
  831. in 1.1, can cause problems with proxies, and we are an HTTP/1.1
  832. client anyway.
  833. C.f. https://tools.ietf.org/wg/httpbis/trac/ticket/158 */
  834. Req << "GET " << requesturi << " HTTP/1.1\r\n";
  835. if (Uri.Port != 0)
  836. Req << "Host: " << ProperHost << ":" << std::to_string(Uri.Port) << "\r\n";
  837. else
  838. Req << "Host: " << ProperHost << "\r\n";
  839. // generate a cache control header (if needed)
  840. if (ConfigFindB("No-Cache",false) == true)
  841. Req << "Cache-Control: no-cache\r\n"
  842. << "Pragma: no-cache\r\n";
  843. else if (Itm->IndexFile == true)
  844. Req << "Cache-Control: max-age=" << std::to_string(ConfigFindI("Max-Age", 0)) << "\r\n";
  845. else if (ConfigFindB("No-Store", false) == true)
  846. Req << "Cache-Control: no-store\r\n";
  847. // If we ask for uncompressed files servers might respond with content-
  848. // negotiation which lets us end up with compressed files we do not support,
  849. // see 657029, 657560 and co, so if we have no extension on the request
  850. // ask for text only. As a sidenote: If there is nothing to negotate servers
  851. // seem to be nice and ignore it.
  852. if (ConfigFindB("SendAccept", true) == true)
  853. {
  854. size_t const filepos = Itm->Uri.find_last_of('/');
  855. string const file = Itm->Uri.substr(filepos + 1);
  856. if (flExtension(file) == file)
  857. Req << "Accept: text/*\r\n";
  858. }
  859. // Check for a partial file and send if-queries accordingly
  860. struct stat SBuf;
  861. if (Server->RangesAllowed && stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  862. Req << "Range: bytes=" << std::to_string(SBuf.st_size) << "-\r\n"
  863. << "If-Range: " << TimeRFC1123(SBuf.st_mtime, false) << "\r\n";
  864. else if (Itm->LastModified != 0)
  865. Req << "If-Modified-Since: " << TimeRFC1123(Itm->LastModified, false).c_str() << "\r\n";
  866. if (Server->Proxy.Access == "http" &&
  867. (Server->Proxy.User.empty() == false || Server->Proxy.Password.empty() == false))
  868. Req << "Proxy-Authorization: Basic "
  869. << Base64Encode(Server->Proxy.User + ":" + Server->Proxy.Password) << "\r\n";
  870. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  871. if (Uri.User.empty() == false || Uri.Password.empty() == false)
  872. Req << "Authorization: Basic "
  873. << Base64Encode(Uri.User + ":" + Uri.Password) << "\r\n";
  874. Req << "User-Agent: " << ConfigFind("User-Agent",
  875. "Debian APT-HTTP/1.3 (" PACKAGE_VERSION ")") << "\r\n";
  876. Req << "\r\n";
  877. if (Debug == true)
  878. cerr << Req.str() << endl;
  879. Server->WriteResponse(Req.str());
  880. }
  881. /*}}}*/
  882. std::unique_ptr<ServerState> HttpMethod::CreateServerState(URI const &uri)/*{{{*/
  883. {
  884. return std::unique_ptr<ServerState>(new HttpServerState(uri, this));
  885. }
  886. /*}}}*/
  887. void HttpMethod::RotateDNS() /*{{{*/
  888. {
  889. ::RotateDNS();
  890. }
  891. /*}}}*/
  892. ServerMethod::DealWithHeadersResult HttpMethod::DealWithHeaders(FetchResult &Res)/*{{{*/
  893. {
  894. auto ret = ServerMethod::DealWithHeaders(Res);
  895. if (ret != ServerMethod::FILE_IS_OPEN)
  896. return ret;
  897. // Open the file
  898. delete File;
  899. File = new FileFd(Queue->DestFile,FileFd::WriteAny);
  900. if (_error->PendingError() == true)
  901. return ERROR_NOT_FROM_SERVER;
  902. FailFile = Queue->DestFile;
  903. FailFile.c_str(); // Make sure we don't do a malloc in the signal handler
  904. FailFd = File->Fd();
  905. FailTime = Server->Date;
  906. if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false)
  907. {
  908. _error->Errno("read",_("Problem hashing file"));
  909. return ERROR_NOT_FROM_SERVER;
  910. }
  911. if (Server->StartPos > 0)
  912. Res.ResumePoint = Server->StartPos;
  913. SetNonBlock(File->Fd(),true);
  914. return FILE_IS_OPEN;
  915. }
  916. /*}}}*/
  917. HttpMethod::HttpMethod(std::string &&pProg) : ServerMethod(pProg.c_str(), "1.2", Pipeline | SendConfig)/*{{{*/
  918. {
  919. auto addName = std::inserter(methodNames, methodNames.begin());
  920. if (Binary != "http")
  921. addName = "http";
  922. auto const plus = Binary.find('+');
  923. if (plus != std::string::npos)
  924. addName = Binary.substr(0, plus);
  925. File = 0;
  926. Server = 0;
  927. }
  928. /*}}}*/