http.cc 22 KB

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