http.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 <iostream>
  41. #include <sstream>
  42. #include "config.h"
  43. #include "connect.h"
  44. #include "http.h"
  45. #include <apti18n.h>
  46. /*}}}*/
  47. using namespace std;
  48. unsigned long long CircleBuf::BwReadLimit=0;
  49. unsigned long long CircleBuf::BwTickReadData=0;
  50. struct timeval CircleBuf::BwReadTick={0,0};
  51. const unsigned int CircleBuf::BW_HZ=10;
  52. // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. CircleBuf::CircleBuf(unsigned long long Size)
  56. : Size(Size), Hash(NULL), TotalWriten(0)
  57. {
  58. Buf = new unsigned char[Size];
  59. Reset();
  60. CircleBuf::BwReadLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
  61. }
  62. /*}}}*/
  63. // CircleBuf::Reset - Reset to the default state /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* */
  66. void CircleBuf::Reset()
  67. {
  68. InP = 0;
  69. OutP = 0;
  70. StrPos = 0;
  71. TotalWriten = 0;
  72. MaxGet = (unsigned long long)-1;
  73. OutQueue = string();
  74. if (Hash != NULL)
  75. {
  76. delete Hash;
  77. Hash = NULL;
  78. }
  79. }
  80. /*}}}*/
  81. // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
  82. // ---------------------------------------------------------------------
  83. /* This fills up the buffer with as much data as is in the FD, assuming it
  84. is non-blocking.. */
  85. bool CircleBuf::Read(int Fd)
  86. {
  87. while (1)
  88. {
  89. // Woops, buffer is full
  90. if (InP - OutP == Size)
  91. return true;
  92. // what's left to read in this tick
  93. unsigned long long const BwReadMax = CircleBuf::BwReadLimit/BW_HZ;
  94. if(CircleBuf::BwReadLimit) {
  95. struct timeval now;
  96. gettimeofday(&now,0);
  97. unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 +
  98. now.tv_usec-CircleBuf::BwReadTick.tv_usec;
  99. if(d > 1000000/BW_HZ) {
  100. CircleBuf::BwReadTick = now;
  101. CircleBuf::BwTickReadData = 0;
  102. }
  103. if(CircleBuf::BwTickReadData >= BwReadMax) {
  104. usleep(1000000/BW_HZ);
  105. return true;
  106. }
  107. }
  108. // Write the buffer segment
  109. ssize_t Res;
  110. if(CircleBuf::BwReadLimit) {
  111. Res = read(Fd,Buf + (InP%Size),
  112. BwReadMax > LeftRead() ? LeftRead() : BwReadMax);
  113. } else
  114. Res = read(Fd,Buf + (InP%Size),LeftRead());
  115. if(Res > 0 && BwReadLimit > 0)
  116. CircleBuf::BwTickReadData += Res;
  117. if (Res == 0)
  118. return false;
  119. if (Res < 0)
  120. {
  121. if (errno == EAGAIN)
  122. return true;
  123. return false;
  124. }
  125. if (InP == 0)
  126. gettimeofday(&Start,0);
  127. InP += Res;
  128. }
  129. }
  130. /*}}}*/
  131. // CircleBuf::Read - Put the string into the buffer /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* This will hold the string in and fill the buffer with it as it empties */
  134. bool CircleBuf::Read(string Data)
  135. {
  136. OutQueue += Data;
  137. FillOut();
  138. return true;
  139. }
  140. /*}}}*/
  141. // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* */
  144. void CircleBuf::FillOut()
  145. {
  146. if (OutQueue.empty() == true)
  147. return;
  148. while (1)
  149. {
  150. // Woops, buffer is full
  151. if (InP - OutP == Size)
  152. return;
  153. // Write the buffer segment
  154. unsigned long long Sz = LeftRead();
  155. if (OutQueue.length() - StrPos < Sz)
  156. Sz = OutQueue.length() - StrPos;
  157. memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz);
  158. // Advance
  159. StrPos += Sz;
  160. InP += Sz;
  161. if (OutQueue.length() == StrPos)
  162. {
  163. StrPos = 0;
  164. OutQueue = "";
  165. return;
  166. }
  167. }
  168. }
  169. /*}}}*/
  170. // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
  171. // ---------------------------------------------------------------------
  172. /* This empties the buffer into the FD. */
  173. bool CircleBuf::Write(int Fd)
  174. {
  175. while (1)
  176. {
  177. FillOut();
  178. // Woops, buffer is empty
  179. if (OutP == InP)
  180. return true;
  181. if (OutP == MaxGet)
  182. return true;
  183. // Write the buffer segment
  184. ssize_t Res;
  185. Res = write(Fd,Buf + (OutP%Size),LeftWrite());
  186. if (Res == 0)
  187. return false;
  188. if (Res < 0)
  189. {
  190. if (errno == EAGAIN)
  191. return true;
  192. return false;
  193. }
  194. TotalWriten += Res;
  195. if (Hash != NULL)
  196. Hash->Add(Buf + (OutP%Size),Res);
  197. OutP += Res;
  198. }
  199. }
  200. /*}}}*/
  201. // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
  202. // ---------------------------------------------------------------------
  203. /* This copies till the first empty line */
  204. bool CircleBuf::WriteTillEl(string &Data,bool Single)
  205. {
  206. // We cheat and assume it is unneeded to have more than one buffer load
  207. for (unsigned long long I = OutP; I < InP; I++)
  208. {
  209. if (Buf[I%Size] != '\n')
  210. continue;
  211. ++I;
  212. if (Single == false)
  213. {
  214. if (I < InP && Buf[I%Size] == '\r')
  215. ++I;
  216. if (I >= InP || Buf[I%Size] != '\n')
  217. continue;
  218. ++I;
  219. }
  220. Data = "";
  221. while (OutP < I)
  222. {
  223. unsigned long long Sz = LeftWrite();
  224. if (Sz == 0)
  225. return false;
  226. if (I - OutP < Sz)
  227. Sz = I - OutP;
  228. Data += string((char *)(Buf + (OutP%Size)),Sz);
  229. OutP += Sz;
  230. }
  231. return true;
  232. }
  233. return false;
  234. }
  235. /*}}}*/
  236. // CircleBuf::Stats - Print out stats information /*{{{*/
  237. // ---------------------------------------------------------------------
  238. /* */
  239. void CircleBuf::Stats()
  240. {
  241. if (InP == 0)
  242. return;
  243. struct timeval Stop;
  244. gettimeofday(&Stop,0);
  245. /* float Diff = Stop.tv_sec - Start.tv_sec +
  246. (float)(Stop.tv_usec - Start.tv_usec)/1000000;
  247. clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
  248. }
  249. /*}}}*/
  250. CircleBuf::~CircleBuf()
  251. {
  252. delete [] Buf;
  253. delete Hash;
  254. }
  255. // HttpServerState::HttpServerState - Constructor /*{{{*/
  256. HttpServerState::HttpServerState(URI Srv,HttpMethod *Owner) : ServerState(Srv, Owner), In(64*1024), Out(4*1024)
  257. {
  258. TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut);
  259. Reset();
  260. }
  261. /*}}}*/
  262. // HttpServerState::Open - Open a connection to the server /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* This opens a connection to the server. */
  265. bool HttpServerState::Open()
  266. {
  267. // Use the already open connection if possible.
  268. if (ServerFd != -1)
  269. return true;
  270. Close();
  271. In.Reset();
  272. Out.Reset();
  273. Persistent = true;
  274. // Determine the proxy setting
  275. AutoDetectProxy(ServerName);
  276. string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
  277. if (!SpecificProxy.empty())
  278. {
  279. if (SpecificProxy == "DIRECT")
  280. Proxy = "";
  281. else
  282. Proxy = SpecificProxy;
  283. }
  284. else
  285. {
  286. string DefProxy = _config->Find("Acquire::http::Proxy");
  287. if (!DefProxy.empty())
  288. {
  289. Proxy = DefProxy;
  290. }
  291. else
  292. {
  293. char* result = getenv("http_proxy");
  294. Proxy = result ? result : "";
  295. }
  296. }
  297. // Parse no_proxy, a , separated list of domains
  298. if (getenv("no_proxy") != 0)
  299. {
  300. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  301. Proxy = "";
  302. }
  303. // Determine what host and port to use based on the proxy settings
  304. int Port = 0;
  305. string Host;
  306. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  307. {
  308. if (ServerName.Port != 0)
  309. Port = ServerName.Port;
  310. Host = ServerName.Host;
  311. }
  312. else if (Proxy.Access != "http")
  313. return _error->Error("Unsupported proxy configured: %s", URI::SiteOnly(Proxy).c_str());
  314. else
  315. {
  316. if (Proxy.Port != 0)
  317. Port = Proxy.Port;
  318. Host = Proxy.Host;
  319. }
  320. // Connect to the remote server
  321. if (Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner) == false)
  322. return false;
  323. return true;
  324. }
  325. /*}}}*/
  326. // HttpServerState::Close - Close a connection to the server /*{{{*/
  327. // ---------------------------------------------------------------------
  328. /* */
  329. bool HttpServerState::Close()
  330. {
  331. close(ServerFd);
  332. ServerFd = -1;
  333. return true;
  334. }
  335. /*}}}*/
  336. // HttpServerState::RunData - Transfer the data from the socket /*{{{*/
  337. bool HttpServerState::RunData(FileFd * const File)
  338. {
  339. State = Data;
  340. // Chunked transfer encoding is fun..
  341. if (Encoding == Chunked)
  342. {
  343. while (1)
  344. {
  345. // Grab the block size
  346. bool Last = true;
  347. string Data;
  348. In.Limit(-1);
  349. do
  350. {
  351. if (In.WriteTillEl(Data,true) == true)
  352. break;
  353. }
  354. while ((Last = Go(false, File)) == true);
  355. if (Last == false)
  356. return false;
  357. // See if we are done
  358. unsigned long long Len = strtoull(Data.c_str(),0,16);
  359. if (Len == 0)
  360. {
  361. In.Limit(-1);
  362. // We have to remove the entity trailer
  363. Last = true;
  364. do
  365. {
  366. if (In.WriteTillEl(Data,true) == true && Data.length() <= 2)
  367. break;
  368. }
  369. while ((Last = Go(false, File)) == true);
  370. if (Last == false)
  371. return false;
  372. return !_error->PendingError();
  373. }
  374. // Transfer the block
  375. In.Limit(Len);
  376. while (Go(true, File) == true)
  377. if (In.IsLimit() == true)
  378. break;
  379. // Error
  380. if (In.IsLimit() == false)
  381. return false;
  382. // The server sends an extra new line before the next block specifier..
  383. In.Limit(-1);
  384. Last = true;
  385. do
  386. {
  387. if (In.WriteTillEl(Data,true) == true)
  388. break;
  389. }
  390. while ((Last = Go(false, File)) == true);
  391. if (Last == false)
  392. return false;
  393. }
  394. }
  395. else
  396. {
  397. /* Closes encoding is used when the server did not specify a size, the
  398. loss of the connection means we are done */
  399. if (JunkSize != 0)
  400. In.Limit(JunkSize);
  401. else if (DownloadSize != 0)
  402. In.Limit(DownloadSize);
  403. else if (Persistent == false)
  404. In.Limit(-1);
  405. // Just transfer the whole block.
  406. do
  407. {
  408. if (In.IsLimit() == false)
  409. continue;
  410. In.Limit(-1);
  411. return !_error->PendingError();
  412. }
  413. while (Go(true, File) == true);
  414. }
  415. return Owner->Flush() && !_error->PendingError();
  416. }
  417. /*}}}*/
  418. bool HttpServerState::RunDataToDevNull() /*{{{*/
  419. {
  420. FileFd DevNull("/dev/null", FileFd::WriteOnly);
  421. return RunData(&DevNull);
  422. }
  423. /*}}}*/
  424. bool HttpServerState::ReadHeaderLines(std::string &Data) /*{{{*/
  425. {
  426. return In.WriteTillEl(Data);
  427. }
  428. /*}}}*/
  429. bool HttpServerState::LoadNextResponse(bool const ToFile, FileFd * const File)/*{{{*/
  430. {
  431. return Go(ToFile, File);
  432. }
  433. /*}}}*/
  434. bool HttpServerState::WriteResponse(const std::string &Data) /*{{{*/
  435. {
  436. return Out.Read(Data);
  437. }
  438. /*}}}*/
  439. APT_PURE bool HttpServerState::IsOpen() /*{{{*/
  440. {
  441. return (ServerFd != -1);
  442. }
  443. /*}}}*/
  444. bool HttpServerState::InitHashes(HashStringList const &ExpectedHashes) /*{{{*/
  445. {
  446. delete In.Hash;
  447. In.Hash = new Hashes(ExpectedHashes);
  448. return true;
  449. }
  450. /*}}}*/
  451. APT_PURE Hashes * HttpServerState::GetHashes() /*{{{*/
  452. {
  453. return In.Hash;
  454. }
  455. /*}}}*/
  456. // HttpServerState::Die - The server has closed the connection. /*{{{*/
  457. bool HttpServerState::Die(FileFd * const File)
  458. {
  459. unsigned int LErrno = errno;
  460. // Dump the buffer to the file
  461. if (State == ServerState::Data)
  462. {
  463. if (File == nullptr)
  464. return true;
  465. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  466. // can't be set
  467. if (File->Name() != "/dev/null")
  468. SetNonBlock(File->Fd(),false);
  469. while (In.WriteSpace() == true)
  470. {
  471. if (In.Write(File->Fd()) == false)
  472. return _error->Errno("write",_("Error writing to the file"));
  473. // Done
  474. if (In.IsLimit() == true)
  475. return true;
  476. }
  477. }
  478. // See if this is because the server finished the data stream
  479. if (In.IsLimit() == false && State != HttpServerState::Header &&
  480. Persistent == true)
  481. {
  482. Close();
  483. if (LErrno == 0)
  484. return _error->Error(_("Error reading from server. Remote end closed connection"));
  485. errno = LErrno;
  486. return _error->Errno("read",_("Error reading from server"));
  487. }
  488. else
  489. {
  490. In.Limit(-1);
  491. // Nothing left in the buffer
  492. if (In.WriteSpace() == false)
  493. return false;
  494. // We may have got multiple responses back in one packet..
  495. Close();
  496. return true;
  497. }
  498. return false;
  499. }
  500. /*}}}*/
  501. // HttpServerState::Flush - Dump the buffer into the file /*{{{*/
  502. // ---------------------------------------------------------------------
  503. /* This takes the current input buffer from the Server FD and writes it
  504. into the file */
  505. bool HttpServerState::Flush(FileFd * const File)
  506. {
  507. if (File != NULL)
  508. {
  509. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  510. // can't be set
  511. if (File->Name() != "/dev/null")
  512. SetNonBlock(File->Fd(),false);
  513. if (In.WriteSpace() == false)
  514. return true;
  515. while (In.WriteSpace() == true)
  516. {
  517. if (In.Write(File->Fd()) == false)
  518. return _error->Errno("write",_("Error writing to file"));
  519. if (In.IsLimit() == true)
  520. return true;
  521. }
  522. if (In.IsLimit() == true || Persistent == false)
  523. return true;
  524. }
  525. return false;
  526. }
  527. /*}}}*/
  528. // HttpServerState::Go - Run a single loop /*{{{*/
  529. // ---------------------------------------------------------------------
  530. /* This runs the select loop over the server FDs, Output file FDs and
  531. stdin. */
  532. bool HttpServerState::Go(bool ToFile, FileFd * const File)
  533. {
  534. // Server has closed the connection
  535. if (ServerFd == -1 && (In.WriteSpace() == false ||
  536. ToFile == false))
  537. return false;
  538. fd_set rfds,wfds;
  539. FD_ZERO(&rfds);
  540. FD_ZERO(&wfds);
  541. /* Add the server. We only send more requests if the connection will
  542. be persisting */
  543. if (Out.WriteSpace() == true && ServerFd != -1
  544. && Persistent == true)
  545. FD_SET(ServerFd,&wfds);
  546. if (In.ReadSpace() == true && ServerFd != -1)
  547. FD_SET(ServerFd,&rfds);
  548. // Add the file
  549. int FileFD = -1;
  550. if (File != NULL)
  551. FileFD = File->Fd();
  552. if (In.WriteSpace() == true && ToFile == true && FileFD != -1)
  553. FD_SET(FileFD,&wfds);
  554. // Add stdin
  555. if (_config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  556. FD_SET(STDIN_FILENO,&rfds);
  557. // Figure out the max fd
  558. int MaxFd = FileFD;
  559. if (MaxFd < ServerFd)
  560. MaxFd = ServerFd;
  561. // Select
  562. struct timeval tv;
  563. tv.tv_sec = TimeOut;
  564. tv.tv_usec = 0;
  565. int Res = 0;
  566. if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0)
  567. {
  568. if (errno == EINTR)
  569. return true;
  570. return _error->Errno("select",_("Select failed"));
  571. }
  572. if (Res == 0)
  573. {
  574. _error->Error(_("Connection timed out"));
  575. return Die(File);
  576. }
  577. // Handle server IO
  578. if (ServerFd != -1 && FD_ISSET(ServerFd,&rfds))
  579. {
  580. errno = 0;
  581. if (In.Read(ServerFd) == false)
  582. return Die(File);
  583. }
  584. if (ServerFd != -1 && FD_ISSET(ServerFd,&wfds))
  585. {
  586. errno = 0;
  587. if (Out.Write(ServerFd) == false)
  588. return Die(File);
  589. }
  590. // Send data to the file
  591. if (FileFD != -1 && FD_ISSET(FileFD,&wfds))
  592. {
  593. if (In.Write(FileFD) == false)
  594. return _error->Errno("write",_("Error writing to output file"));
  595. }
  596. if (MaximumSize > 0 && File && File->Tell() > MaximumSize)
  597. {
  598. Owner->SetFailReason("MaximumSizeExceeded");
  599. return _error->Error("Writing more data than expected (%llu > %llu)",
  600. File->Tell(), MaximumSize);
  601. }
  602. // Handle commands from APT
  603. if (FD_ISSET(STDIN_FILENO,&rfds))
  604. {
  605. if (Owner->Run(true) != -1)
  606. exit(100);
  607. }
  608. return true;
  609. }
  610. /*}}}*/
  611. // HttpMethod::SendReq - Send the HTTP request /*{{{*/
  612. // ---------------------------------------------------------------------
  613. /* This places the http request in the outbound buffer */
  614. void HttpMethod::SendReq(FetchItem *Itm)
  615. {
  616. URI Uri = Itm->Uri;
  617. // The HTTP server expects a hostname with a trailing :port
  618. std::stringstream Req;
  619. string ProperHost;
  620. if (Uri.Host.find(':') != string::npos)
  621. ProperHost = '[' + Uri.Host + ']';
  622. else
  623. ProperHost = Uri.Host;
  624. /* RFC 2616 §5.1.2 requires absolute URIs for requests to proxies,
  625. but while its a must for all servers to accept absolute URIs,
  626. it is assumed clients will sent an absolute path for non-proxies */
  627. std::string requesturi;
  628. if (Server->Proxy.empty() == true || Server->Proxy.Host.empty())
  629. requesturi = Uri.Path;
  630. else
  631. requesturi = Itm->Uri;
  632. // The "+" is encoded as a workaround for a amazon S3 bug
  633. // see LP bugs #1003633 and #1086997.
  634. requesturi = QuoteString(requesturi, "+~ ");
  635. /* Build the request. No keep-alive is included as it is the default
  636. in 1.1, can cause problems with proxies, and we are an HTTP/1.1
  637. client anyway.
  638. C.f. https://tools.ietf.org/wg/httpbis/trac/ticket/158 */
  639. Req << "GET " << requesturi << " HTTP/1.1\r\n";
  640. if (Uri.Port != 0)
  641. Req << "Host: " << ProperHost << ":" << std::to_string(Uri.Port) << "\r\n";
  642. else
  643. Req << "Host: " << ProperHost << "\r\n";
  644. // generate a cache control header (if needed)
  645. if (_config->FindB("Acquire::http::No-Cache",false) == true)
  646. Req << "Cache-Control: no-cache\r\n"
  647. << "Pragma: no-cache\r\n";
  648. else if (Itm->IndexFile == true)
  649. Req << "Cache-Control: max-age=" << std::to_string(_config->FindI("Acquire::http::Max-Age",0)) << "\r\n";
  650. else if (_config->FindB("Acquire::http::No-Store",false) == true)
  651. Req << "Cache-Control: no-store\r\n";
  652. // If we ask for uncompressed files servers might respond with content-
  653. // negotiation which lets us end up with compressed files we do not support,
  654. // see 657029, 657560 and co, so if we have no extension on the request
  655. // ask for text only. As a sidenote: If there is nothing to negotate servers
  656. // seem to be nice and ignore it.
  657. if (_config->FindB("Acquire::http::SendAccept", true) == true)
  658. {
  659. size_t const filepos = Itm->Uri.find_last_of('/');
  660. string const file = Itm->Uri.substr(filepos + 1);
  661. if (flExtension(file) == file)
  662. Req << "Accept: text/*\r\n";
  663. }
  664. // Check for a partial file and send if-queries accordingly
  665. struct stat SBuf;
  666. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  667. Req << "Range: bytes=" << std::to_string(SBuf.st_size) << "-\r\n"
  668. << "If-Range: " << TimeRFC1123(SBuf.st_mtime, false) << "\r\n";
  669. else if (Itm->LastModified != 0)
  670. Req << "If-Modified-Since: " << TimeRFC1123(Itm->LastModified, false).c_str() << "\r\n";
  671. if (Server->Proxy.User.empty() == false || Server->Proxy.Password.empty() == false)
  672. Req << "Proxy-Authorization: Basic "
  673. << Base64Encode(Server->Proxy.User + ":" + Server->Proxy.Password) << "\r\n";
  674. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  675. if (Uri.User.empty() == false || Uri.Password.empty() == false)
  676. Req << "Authorization: Basic "
  677. << Base64Encode(Uri.User + ":" + Uri.Password) << "\r\n";
  678. Req << "User-Agent: " << _config->Find("Acquire::http::User-Agent",
  679. "Debian APT-HTTP/1.3 (" PACKAGE_VERSION ")") << "\r\n";
  680. Req << "\r\n";
  681. if (Debug == true)
  682. cerr << Req.str() << endl;
  683. Server->WriteResponse(Req.str());
  684. }
  685. /*}}}*/
  686. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  687. // ---------------------------------------------------------------------
  688. /* We stash the desired pipeline depth */
  689. bool HttpMethod::Configuration(string Message)
  690. {
  691. if (ServerMethod::Configuration(Message) == false)
  692. return false;
  693. AllowRedirect = _config->FindB("Acquire::http::AllowRedirect",true);
  694. PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
  695. PipelineDepth);
  696. Debug = _config->FindB("Debug::Acquire::http",false);
  697. return true;
  698. }
  699. /*}}}*/
  700. std::unique_ptr<ServerState> HttpMethod::CreateServerState(URI const &uri)/*{{{*/
  701. {
  702. return std::unique_ptr<ServerState>(new HttpServerState(uri, this));
  703. }
  704. /*}}}*/
  705. void HttpMethod::RotateDNS() /*{{{*/
  706. {
  707. ::RotateDNS();
  708. }
  709. /*}}}*/