http.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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 aquire 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 <utime.h>
  33. #include <unistd.h>
  34. #include <signal.h>
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #include <string.h>
  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. string HttpMethod::FailFile;
  50. int HttpMethod::FailFd = -1;
  51. time_t HttpMethod::FailTime = 0;
  52. unsigned long PipelineDepth = 10;
  53. unsigned long TimeOut = 120;
  54. bool AllowRedirect = false;
  55. bool Debug = false;
  56. URI Proxy;
  57. unsigned long long CircleBuf::BwReadLimit=0;
  58. unsigned long long CircleBuf::BwTickReadData=0;
  59. struct timeval CircleBuf::BwReadTick={0,0};
  60. const unsigned int CircleBuf::BW_HZ=10;
  61. // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. CircleBuf::CircleBuf(unsigned long long Size) : Size(Size), Hash(0)
  65. {
  66. Buf = new unsigned char[Size];
  67. Reset();
  68. CircleBuf::BwReadLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
  69. }
  70. /*}}}*/
  71. // CircleBuf::Reset - Reset to the default state /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* */
  74. void CircleBuf::Reset()
  75. {
  76. InP = 0;
  77. OutP = 0;
  78. StrPos = 0;
  79. MaxGet = (unsigned long long)-1;
  80. OutQueue = string();
  81. if (Hash != 0)
  82. {
  83. delete Hash;
  84. Hash = new Hashes;
  85. }
  86. };
  87. /*}}}*/
  88. // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* This fills up the buffer with as much data as is in the FD, assuming it
  91. is non-blocking.. */
  92. bool CircleBuf::Read(int Fd)
  93. {
  94. unsigned long long BwReadMax;
  95. while (1)
  96. {
  97. // Woops, buffer is full
  98. if (InP - OutP == Size)
  99. return true;
  100. // what's left to read in this tick
  101. BwReadMax = CircleBuf::BwReadLimit/BW_HZ;
  102. if(CircleBuf::BwReadLimit) {
  103. struct timeval now;
  104. gettimeofday(&now,0);
  105. unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 +
  106. now.tv_usec-CircleBuf::BwReadTick.tv_usec;
  107. if(d > 1000000/BW_HZ) {
  108. CircleBuf::BwReadTick = now;
  109. CircleBuf::BwTickReadData = 0;
  110. }
  111. if(CircleBuf::BwTickReadData >= BwReadMax) {
  112. usleep(1000000/BW_HZ);
  113. return true;
  114. }
  115. }
  116. // Write the buffer segment
  117. ssize_t Res;
  118. if(CircleBuf::BwReadLimit) {
  119. Res = read(Fd,Buf + (InP%Size),
  120. BwReadMax > LeftRead() ? LeftRead() : BwReadMax);
  121. } else
  122. Res = read(Fd,Buf + (InP%Size),LeftRead());
  123. if(Res > 0 && BwReadLimit > 0)
  124. CircleBuf::BwTickReadData += Res;
  125. if (Res == 0)
  126. return false;
  127. if (Res < 0)
  128. {
  129. if (errno == EAGAIN)
  130. return true;
  131. return false;
  132. }
  133. if (InP == 0)
  134. gettimeofday(&Start,0);
  135. InP += Res;
  136. }
  137. }
  138. /*}}}*/
  139. // CircleBuf::Read - Put the string into the buffer /*{{{*/
  140. // ---------------------------------------------------------------------
  141. /* This will hold the string in and fill the buffer with it as it empties */
  142. bool CircleBuf::Read(string Data)
  143. {
  144. OutQueue += Data;
  145. FillOut();
  146. return true;
  147. }
  148. /*}}}*/
  149. // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* */
  152. void CircleBuf::FillOut()
  153. {
  154. if (OutQueue.empty() == true)
  155. return;
  156. while (1)
  157. {
  158. // Woops, buffer is full
  159. if (InP - OutP == Size)
  160. return;
  161. // Write the buffer segment
  162. unsigned long long Sz = LeftRead();
  163. if (OutQueue.length() - StrPos < Sz)
  164. Sz = OutQueue.length() - StrPos;
  165. memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz);
  166. // Advance
  167. StrPos += Sz;
  168. InP += Sz;
  169. if (OutQueue.length() == StrPos)
  170. {
  171. StrPos = 0;
  172. OutQueue = "";
  173. return;
  174. }
  175. }
  176. }
  177. /*}}}*/
  178. // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* This empties the buffer into the FD. */
  181. bool CircleBuf::Write(int Fd)
  182. {
  183. while (1)
  184. {
  185. FillOut();
  186. // Woops, buffer is empty
  187. if (OutP == InP)
  188. return true;
  189. if (OutP == MaxGet)
  190. return true;
  191. // Write the buffer segment
  192. ssize_t Res;
  193. Res = write(Fd,Buf + (OutP%Size),LeftWrite());
  194. if (Res == 0)
  195. return false;
  196. if (Res < 0)
  197. {
  198. if (errno == EAGAIN)
  199. return true;
  200. return false;
  201. }
  202. if (Hash != 0)
  203. Hash->Add(Buf + (OutP%Size),Res);
  204. OutP += Res;
  205. }
  206. }
  207. /*}}}*/
  208. // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* This copies till the first empty line */
  211. bool CircleBuf::WriteTillEl(string &Data,bool Single)
  212. {
  213. // We cheat and assume it is unneeded to have more than one buffer load
  214. for (unsigned long long I = OutP; I < InP; I++)
  215. {
  216. if (Buf[I%Size] != '\n')
  217. continue;
  218. ++I;
  219. if (Single == false)
  220. {
  221. if (I < InP && Buf[I%Size] == '\r')
  222. ++I;
  223. if (I >= InP || Buf[I%Size] != '\n')
  224. continue;
  225. ++I;
  226. }
  227. Data = "";
  228. while (OutP < I)
  229. {
  230. unsigned long long Sz = LeftWrite();
  231. if (Sz == 0)
  232. return false;
  233. if (I - OutP < Sz)
  234. Sz = I - OutP;
  235. Data += string((char *)(Buf + (OutP%Size)),Sz);
  236. OutP += Sz;
  237. }
  238. return true;
  239. }
  240. return false;
  241. }
  242. /*}}}*/
  243. // CircleBuf::Stats - Print out stats information /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* */
  246. void CircleBuf::Stats()
  247. {
  248. if (InP == 0)
  249. return;
  250. struct timeval Stop;
  251. gettimeofday(&Stop,0);
  252. /* float Diff = Stop.tv_sec - Start.tv_sec +
  253. (float)(Stop.tv_usec - Start.tv_usec)/1000000;
  254. clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
  255. }
  256. /*}}}*/
  257. CircleBuf::~CircleBuf()
  258. {
  259. delete [] Buf;
  260. delete Hash;
  261. }
  262. // ServerState::ServerState - Constructor /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* */
  265. ServerState::ServerState(URI Srv,HttpMethod *Owner) : Owner(Owner),
  266. In(64*1024), Out(4*1024),
  267. ServerName(Srv)
  268. {
  269. Reset();
  270. }
  271. /*}}}*/
  272. // ServerState::Open - Open a connection to the server /*{{{*/
  273. // ---------------------------------------------------------------------
  274. /* This opens a connection to the server. */
  275. bool ServerState::Open()
  276. {
  277. // Use the already open connection if possible.
  278. if (ServerFd != -1)
  279. return true;
  280. Close();
  281. In.Reset();
  282. Out.Reset();
  283. Persistent = true;
  284. // Determine the proxy setting
  285. string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
  286. if (!SpecificProxy.empty())
  287. {
  288. if (SpecificProxy == "DIRECT")
  289. Proxy = "";
  290. else
  291. Proxy = SpecificProxy;
  292. }
  293. else
  294. {
  295. string DefProxy = _config->Find("Acquire::http::Proxy");
  296. if (!DefProxy.empty())
  297. {
  298. Proxy = DefProxy;
  299. }
  300. else
  301. {
  302. char* result = getenv("http_proxy");
  303. Proxy = result ? result : "";
  304. }
  305. }
  306. // Parse no_proxy, a , separated list of domains
  307. if (getenv("no_proxy") != 0)
  308. {
  309. if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
  310. Proxy = "";
  311. }
  312. // Determine what host and port to use based on the proxy settings
  313. int Port = 0;
  314. string Host;
  315. if (Proxy.empty() == true || Proxy.Host.empty() == true)
  316. {
  317. if (ServerName.Port != 0)
  318. Port = ServerName.Port;
  319. Host = ServerName.Host;
  320. }
  321. else
  322. {
  323. if (Proxy.Port != 0)
  324. Port = Proxy.Port;
  325. Host = Proxy.Host;
  326. }
  327. // Connect to the remote server
  328. if (Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner) == false)
  329. return false;
  330. return true;
  331. }
  332. /*}}}*/
  333. // ServerState::Close - Close a connection to the server /*{{{*/
  334. // ---------------------------------------------------------------------
  335. /* */
  336. bool ServerState::Close()
  337. {
  338. close(ServerFd);
  339. ServerFd = -1;
  340. return true;
  341. }
  342. /*}}}*/
  343. // ServerState::RunHeaders - Get the headers before the data /*{{{*/
  344. // ---------------------------------------------------------------------
  345. /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
  346. parse error occurred */
  347. ServerState::RunHeadersResult ServerState::RunHeaders()
  348. {
  349. State = Header;
  350. Owner->Status(_("Waiting for headers"));
  351. Major = 0;
  352. Minor = 0;
  353. Result = 0;
  354. Size = 0;
  355. StartPos = 0;
  356. Encoding = Closes;
  357. HaveContent = false;
  358. time(&Date);
  359. do
  360. {
  361. string Data;
  362. if (In.WriteTillEl(Data) == false)
  363. continue;
  364. if (Debug == true)
  365. clog << Data;
  366. for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
  367. {
  368. string::const_iterator J = I;
  369. for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J);
  370. if (HeaderLine(string(I,J)) == false)
  371. return RUN_HEADERS_PARSE_ERROR;
  372. I = J;
  373. }
  374. // 100 Continue is a Nop...
  375. if (Result == 100)
  376. continue;
  377. // Tidy up the connection persistance state.
  378. if (Encoding == Closes && HaveContent == true)
  379. Persistent = false;
  380. return RUN_HEADERS_OK;
  381. }
  382. while (Owner->Go(false,this) == true);
  383. return RUN_HEADERS_IO_ERROR;
  384. }
  385. /*}}}*/
  386. // ServerState::RunData - Transfer the data from the socket /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* */
  389. bool ServerState::RunData()
  390. {
  391. State = Data;
  392. // Chunked transfer encoding is fun..
  393. if (Encoding == Chunked)
  394. {
  395. while (1)
  396. {
  397. // Grab the block size
  398. bool Last = true;
  399. string Data;
  400. In.Limit(-1);
  401. do
  402. {
  403. if (In.WriteTillEl(Data,true) == true)
  404. break;
  405. }
  406. while ((Last = Owner->Go(false,this)) == true);
  407. if (Last == false)
  408. return false;
  409. // See if we are done
  410. unsigned long long Len = strtoull(Data.c_str(),0,16);
  411. if (Len == 0)
  412. {
  413. In.Limit(-1);
  414. // We have to remove the entity trailer
  415. Last = true;
  416. do
  417. {
  418. if (In.WriteTillEl(Data,true) == true && Data.length() <= 2)
  419. break;
  420. }
  421. while ((Last = Owner->Go(false,this)) == true);
  422. if (Last == false)
  423. return false;
  424. return !_error->PendingError();
  425. }
  426. // Transfer the block
  427. In.Limit(Len);
  428. while (Owner->Go(true,this) == true)
  429. if (In.IsLimit() == true)
  430. break;
  431. // Error
  432. if (In.IsLimit() == false)
  433. return false;
  434. // The server sends an extra new line before the next block specifier..
  435. In.Limit(-1);
  436. Last = true;
  437. do
  438. {
  439. if (In.WriteTillEl(Data,true) == true)
  440. break;
  441. }
  442. while ((Last = Owner->Go(false,this)) == true);
  443. if (Last == false)
  444. return false;
  445. }
  446. }
  447. else
  448. {
  449. /* Closes encoding is used when the server did not specify a size, the
  450. loss of the connection means we are done */
  451. if (Encoding == Closes)
  452. In.Limit(-1);
  453. else
  454. In.Limit(Size - StartPos);
  455. // Just transfer the whole block.
  456. do
  457. {
  458. if (In.IsLimit() == false)
  459. continue;
  460. In.Limit(-1);
  461. return !_error->PendingError();
  462. }
  463. while (Owner->Go(true,this) == true);
  464. }
  465. return Owner->Flush(this) && !_error->PendingError();
  466. }
  467. /*}}}*/
  468. // ServerState::HeaderLine - Process a header line /*{{{*/
  469. // ---------------------------------------------------------------------
  470. /* */
  471. bool ServerState::HeaderLine(string Line)
  472. {
  473. if (Line.empty() == true)
  474. return true;
  475. // The http server might be trying to do something evil.
  476. if (Line.length() >= MAXLEN)
  477. return _error->Error(_("Got a single header line over %u chars"),MAXLEN);
  478. string::size_type Pos = Line.find(' ');
  479. if (Pos == string::npos || Pos+1 > Line.length())
  480. {
  481. // Blah, some servers use "connection:closes", evil.
  482. Pos = Line.find(':');
  483. if (Pos == string::npos || Pos + 2 > Line.length())
  484. return _error->Error(_("Bad header line"));
  485. Pos++;
  486. }
  487. // Parse off any trailing spaces between the : and the next word.
  488. string::size_type Pos2 = Pos;
  489. while (Pos2 < Line.length() && isspace(Line[Pos2]) != 0)
  490. Pos2++;
  491. string Tag = string(Line,0,Pos);
  492. string Val = string(Line,Pos2);
  493. if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
  494. {
  495. // Evil servers return no version
  496. if (Line[4] == '/')
  497. {
  498. int const elements = sscanf(Line.c_str(),"HTTP/%u.%u %u%[^\n]",&Major,&Minor,&Result,Code);
  499. if (elements == 3)
  500. {
  501. Code[0] = '\0';
  502. if (Debug == true)
  503. clog << "HTTP server doesn't give Reason-Phrase for " << Result << std::endl;
  504. }
  505. else if (elements != 4)
  506. return _error->Error(_("The HTTP server sent an invalid reply header"));
  507. }
  508. else
  509. {
  510. Major = 0;
  511. Minor = 9;
  512. if (sscanf(Line.c_str(),"HTTP %u%[^\n]",&Result,Code) != 2)
  513. return _error->Error(_("The HTTP server sent an invalid reply header"));
  514. }
  515. /* Check the HTTP response header to get the default persistance
  516. state. */
  517. if (Major < 1)
  518. Persistent = false;
  519. else
  520. {
  521. if (Major == 1 && Minor <= 0)
  522. Persistent = false;
  523. else
  524. Persistent = true;
  525. }
  526. return true;
  527. }
  528. if (stringcasecmp(Tag,"Content-Length:") == 0)
  529. {
  530. if (Encoding == Closes)
  531. Encoding = Stream;
  532. HaveContent = true;
  533. // The length is already set from the Content-Range header
  534. if (StartPos != 0)
  535. return true;
  536. if (sscanf(Val.c_str(),"%llu",&Size) != 1)
  537. return _error->Error(_("The HTTP server sent an invalid Content-Length header"));
  538. return true;
  539. }
  540. if (stringcasecmp(Tag,"Content-Type:") == 0)
  541. {
  542. HaveContent = true;
  543. return true;
  544. }
  545. if (stringcasecmp(Tag,"Content-Range:") == 0)
  546. {
  547. HaveContent = true;
  548. if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&Size) != 2)
  549. return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
  550. if ((unsigned long long)StartPos > Size)
  551. return _error->Error(_("This HTTP server has broken range support"));
  552. return true;
  553. }
  554. if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
  555. {
  556. HaveContent = true;
  557. if (stringcasecmp(Val,"chunked") == 0)
  558. Encoding = Chunked;
  559. return true;
  560. }
  561. if (stringcasecmp(Tag,"Connection:") == 0)
  562. {
  563. if (stringcasecmp(Val,"close") == 0)
  564. Persistent = false;
  565. if (stringcasecmp(Val,"keep-alive") == 0)
  566. Persistent = true;
  567. return true;
  568. }
  569. if (stringcasecmp(Tag,"Last-Modified:") == 0)
  570. {
  571. if (RFC1123StrToTime(Val.c_str(), Date) == false)
  572. return _error->Error(_("Unknown date format"));
  573. return true;
  574. }
  575. if (stringcasecmp(Tag,"Location:") == 0)
  576. {
  577. Location = Val;
  578. return true;
  579. }
  580. return true;
  581. }
  582. /*}}}*/
  583. // HttpMethod::SendReq - Send the HTTP request /*{{{*/
  584. // ---------------------------------------------------------------------
  585. /* This places the http request in the outbound buffer */
  586. void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out)
  587. {
  588. URI Uri = Itm->Uri;
  589. // The HTTP server expects a hostname with a trailing :port
  590. char Buf[1000];
  591. string ProperHost = Uri.Host;
  592. if (Uri.Port != 0)
  593. {
  594. sprintf(Buf,":%u",Uri.Port);
  595. ProperHost += Buf;
  596. }
  597. // Just in case.
  598. if (Itm->Uri.length() >= sizeof(Buf))
  599. abort();
  600. /* Build the request. We include a keep-alive header only for non-proxy
  601. requests. This is to tweak old http/1.0 servers that do support keep-alive
  602. but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server
  603. will glitch HTTP/1.0 proxies because they do not filter it out and
  604. pass it on, HTTP/1.1 says the connection should default to keep alive
  605. and we expect the proxy to do this */
  606. if (Proxy.empty() == true || Proxy.Host.empty())
  607. sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n",
  608. QuoteString(Uri.Path,"~").c_str(),ProperHost.c_str());
  609. else
  610. {
  611. /* Generate a cache control header if necessary. We place a max
  612. cache age on index files, optionally set a no-cache directive
  613. and a no-store directive for archives. */
  614. sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\n",
  615. Itm->Uri.c_str(),ProperHost.c_str());
  616. }
  617. // generate a cache control header (if needed)
  618. if (_config->FindB("Acquire::http::No-Cache",false) == true)
  619. {
  620. strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
  621. }
  622. else
  623. {
  624. if (Itm->IndexFile == true)
  625. {
  626. sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n",
  627. _config->FindI("Acquire::http::Max-Age",0));
  628. }
  629. else
  630. {
  631. if (_config->FindB("Acquire::http::No-Store",false) == true)
  632. strcat(Buf,"Cache-Control: no-store\r\n");
  633. }
  634. }
  635. string Req = Buf;
  636. // Check for a partial file
  637. struct stat SBuf;
  638. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  639. {
  640. // In this case we send an if-range query with a range header
  641. sprintf(Buf,"Range: bytes=%lli-\r\nIf-Range: %s\r\n",(long long)SBuf.st_size - 1,
  642. TimeRFC1123(SBuf.st_mtime).c_str());
  643. Req += Buf;
  644. }
  645. else
  646. {
  647. if (Itm->LastModified != 0)
  648. {
  649. sprintf(Buf,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm->LastModified).c_str());
  650. Req += Buf;
  651. }
  652. }
  653. if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
  654. Req += string("Proxy-Authorization: Basic ") +
  655. Base64Encode(Proxy.User + ":" + Proxy.Password) + "\r\n";
  656. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  657. if (Uri.User.empty() == false || Uri.Password.empty() == false)
  658. {
  659. Req += string("Authorization: Basic ") +
  660. Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n";
  661. }
  662. Req += "User-Agent: " + _config->Find("Acquire::http::User-Agent",
  663. "Debian APT-HTTP/1.3 ("VERSION")") + "\r\n\r\n";
  664. if (Debug == true)
  665. cerr << Req << endl;
  666. Out.Read(Req);
  667. }
  668. /*}}}*/
  669. // HttpMethod::Go - Run a single loop /*{{{*/
  670. // ---------------------------------------------------------------------
  671. /* This runs the select loop over the server FDs, Output file FDs and
  672. stdin. */
  673. bool HttpMethod::Go(bool ToFile,ServerState *Srv)
  674. {
  675. // Server has closed the connection
  676. if (Srv->ServerFd == -1 && (Srv->In.WriteSpace() == false ||
  677. ToFile == false))
  678. return false;
  679. fd_set rfds,wfds;
  680. FD_ZERO(&rfds);
  681. FD_ZERO(&wfds);
  682. /* Add the server. We only send more requests if the connection will
  683. be persisting */
  684. if (Srv->Out.WriteSpace() == true && Srv->ServerFd != -1
  685. && Srv->Persistent == true)
  686. FD_SET(Srv->ServerFd,&wfds);
  687. if (Srv->In.ReadSpace() == true && Srv->ServerFd != -1)
  688. FD_SET(Srv->ServerFd,&rfds);
  689. // Add the file
  690. int FileFD = -1;
  691. if (File != 0)
  692. FileFD = File->Fd();
  693. if (Srv->In.WriteSpace() == true && ToFile == true && FileFD != -1)
  694. FD_SET(FileFD,&wfds);
  695. // Add stdin
  696. if (_config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  697. FD_SET(STDIN_FILENO,&rfds);
  698. // Figure out the max fd
  699. int MaxFd = FileFD;
  700. if (MaxFd < Srv->ServerFd)
  701. MaxFd = Srv->ServerFd;
  702. // Select
  703. struct timeval tv;
  704. tv.tv_sec = TimeOut;
  705. tv.tv_usec = 0;
  706. int Res = 0;
  707. if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0)
  708. {
  709. if (errno == EINTR)
  710. return true;
  711. return _error->Errno("select",_("Select failed"));
  712. }
  713. if (Res == 0)
  714. {
  715. _error->Error(_("Connection timed out"));
  716. return ServerDie(Srv);
  717. }
  718. // Handle server IO
  719. if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&rfds))
  720. {
  721. errno = 0;
  722. if (Srv->In.Read(Srv->ServerFd) == false)
  723. return ServerDie(Srv);
  724. }
  725. if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&wfds))
  726. {
  727. errno = 0;
  728. if (Srv->Out.Write(Srv->ServerFd) == false)
  729. return ServerDie(Srv);
  730. }
  731. // Send data to the file
  732. if (FileFD != -1 && FD_ISSET(FileFD,&wfds))
  733. {
  734. if (Srv->In.Write(FileFD) == false)
  735. return _error->Errno("write",_("Error writing to output file"));
  736. }
  737. // Handle commands from APT
  738. if (FD_ISSET(STDIN_FILENO,&rfds))
  739. {
  740. if (Run(true) != -1)
  741. exit(100);
  742. }
  743. return true;
  744. }
  745. /*}}}*/
  746. // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
  747. // ---------------------------------------------------------------------
  748. /* This takes the current input buffer from the Server FD and writes it
  749. into the file */
  750. bool HttpMethod::Flush(ServerState *Srv)
  751. {
  752. if (File != 0)
  753. {
  754. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  755. // can't be set
  756. if (File->Name() != "/dev/null")
  757. SetNonBlock(File->Fd(),false);
  758. if (Srv->In.WriteSpace() == false)
  759. return true;
  760. while (Srv->In.WriteSpace() == true)
  761. {
  762. if (Srv->In.Write(File->Fd()) == false)
  763. return _error->Errno("write",_("Error writing to file"));
  764. if (Srv->In.IsLimit() == true)
  765. return true;
  766. }
  767. if (Srv->In.IsLimit() == true || Srv->Encoding == ServerState::Closes)
  768. return true;
  769. }
  770. return false;
  771. }
  772. /*}}}*/
  773. // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
  774. // ---------------------------------------------------------------------
  775. /* */
  776. bool HttpMethod::ServerDie(ServerState *Srv)
  777. {
  778. unsigned int LErrno = errno;
  779. // Dump the buffer to the file
  780. if (Srv->State == ServerState::Data)
  781. {
  782. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  783. // can't be set
  784. if (File->Name() != "/dev/null")
  785. SetNonBlock(File->Fd(),false);
  786. while (Srv->In.WriteSpace() == true)
  787. {
  788. if (Srv->In.Write(File->Fd()) == false)
  789. return _error->Errno("write",_("Error writing to the file"));
  790. // Done
  791. if (Srv->In.IsLimit() == true)
  792. return true;
  793. }
  794. }
  795. // See if this is because the server finished the data stream
  796. if (Srv->In.IsLimit() == false && Srv->State != ServerState::Header &&
  797. Srv->Encoding != ServerState::Closes)
  798. {
  799. Srv->Close();
  800. if (LErrno == 0)
  801. return _error->Error(_("Error reading from server. Remote end closed connection"));
  802. errno = LErrno;
  803. return _error->Errno("read",_("Error reading from server"));
  804. }
  805. else
  806. {
  807. Srv->In.Limit(-1);
  808. // Nothing left in the buffer
  809. if (Srv->In.WriteSpace() == false)
  810. return false;
  811. // We may have got multiple responses back in one packet..
  812. Srv->Close();
  813. return true;
  814. }
  815. return false;
  816. }
  817. /*}}}*/
  818. // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
  819. // ---------------------------------------------------------------------
  820. /* We look at the header data we got back from the server and decide what
  821. to do. Returns DealWithHeadersResult (see http.h for details).
  822. */
  823. HttpMethod::DealWithHeadersResult
  824. HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
  825. {
  826. // Not Modified
  827. if (Srv->Result == 304)
  828. {
  829. unlink(Queue->DestFile.c_str());
  830. Res.IMSHit = true;
  831. Res.LastModified = Queue->LastModified;
  832. return IMS_HIT;
  833. }
  834. /* Redirect
  835. *
  836. * Note that it is only OK for us to treat all redirection the same
  837. * because we *always* use GET, not other HTTP methods. There are
  838. * three redirection codes for which it is not appropriate that we
  839. * redirect. Pass on those codes so the error handling kicks in.
  840. */
  841. if (AllowRedirect
  842. && (Srv->Result > 300 && Srv->Result < 400)
  843. && (Srv->Result != 300 // Multiple Choices
  844. && Srv->Result != 304 // Not Modified
  845. && Srv->Result != 306)) // (Not part of HTTP/1.1, reserved)
  846. {
  847. if (Srv->Location.empty() == true);
  848. else if (Srv->Location[0] == '/' && Queue->Uri.empty() == false)
  849. {
  850. URI Uri = Queue->Uri;
  851. if (Uri.Host.empty() == false)
  852. {
  853. if (Uri.Port != 0)
  854. strprintf(NextURI, "http://%s:%u", Uri.Host.c_str(), Uri.Port);
  855. else
  856. NextURI = "http://" + Uri.Host;
  857. }
  858. else
  859. NextURI.clear();
  860. NextURI.append(DeQuoteString(Srv->Location));
  861. return TRY_AGAIN_OR_REDIRECT;
  862. }
  863. else
  864. {
  865. NextURI = DeQuoteString(Srv->Location);
  866. return TRY_AGAIN_OR_REDIRECT;
  867. }
  868. /* else pass through for error message */
  869. }
  870. /* We have a reply we dont handle. This should indicate a perm server
  871. failure */
  872. if (Srv->Result < 200 || Srv->Result >= 300)
  873. {
  874. char err[255];
  875. snprintf(err,sizeof(err)-1,"HttpError%i",Srv->Result);
  876. SetFailReason(err);
  877. _error->Error("%u %s",Srv->Result,Srv->Code);
  878. if (Srv->HaveContent == true)
  879. return ERROR_WITH_CONTENT_PAGE;
  880. return ERROR_UNRECOVERABLE;
  881. }
  882. // This is some sort of 2xx 'data follows' reply
  883. Res.LastModified = Srv->Date;
  884. Res.Size = Srv->Size;
  885. // Open the file
  886. delete File;
  887. File = new FileFd(Queue->DestFile,FileFd::WriteAny);
  888. if (_error->PendingError() == true)
  889. return ERROR_NOT_FROM_SERVER;
  890. FailFile = Queue->DestFile;
  891. FailFile.c_str(); // Make sure we dont do a malloc in the signal handler
  892. FailFd = File->Fd();
  893. FailTime = Srv->Date;
  894. // Set the expected size
  895. if (Srv->StartPos >= 0)
  896. {
  897. Res.ResumePoint = Srv->StartPos;
  898. if (ftruncate(File->Fd(),Srv->StartPos) < 0)
  899. _error->Errno("ftruncate", _("Failed to truncate file"));
  900. }
  901. // Set the start point
  902. lseek(File->Fd(),0,SEEK_END);
  903. delete Srv->In.Hash;
  904. Srv->In.Hash = new Hashes;
  905. // Fill the Hash if the file is non-empty (resume)
  906. if (Srv->StartPos > 0)
  907. {
  908. lseek(File->Fd(),0,SEEK_SET);
  909. if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false)
  910. {
  911. _error->Errno("read",_("Problem hashing file"));
  912. return ERROR_NOT_FROM_SERVER;
  913. }
  914. lseek(File->Fd(),0,SEEK_END);
  915. }
  916. SetNonBlock(File->Fd(),true);
  917. return FILE_IS_OPEN;
  918. }
  919. /*}}}*/
  920. // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
  921. // ---------------------------------------------------------------------
  922. /* This closes and timestamps the open file. This is neccessary to get
  923. resume behavoir on user abort */
  924. void HttpMethod::SigTerm(int)
  925. {
  926. if (FailFd == -1)
  927. _exit(100);
  928. close(FailFd);
  929. // Timestamp
  930. struct utimbuf UBuf;
  931. UBuf.actime = FailTime;
  932. UBuf.modtime = FailTime;
  933. utime(FailFile.c_str(),&UBuf);
  934. _exit(100);
  935. }
  936. /*}}}*/
  937. // HttpMethod::Fetch - Fetch an item /*{{{*/
  938. // ---------------------------------------------------------------------
  939. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  940. depth. */
  941. bool HttpMethod::Fetch(FetchItem *)
  942. {
  943. if (Server == 0)
  944. return true;
  945. // Queue the requests
  946. int Depth = -1;
  947. for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
  948. I = I->Next, Depth++)
  949. {
  950. // If pipelining is disabled, we only queue 1 request
  951. if (Server->Pipeline == false && Depth >= 0)
  952. break;
  953. // Make sure we stick with the same server
  954. if (Server->Comp(I->Uri) == false)
  955. break;
  956. if (QueueBack == I)
  957. {
  958. QueueBack = I->Next;
  959. SendReq(I,Server->Out);
  960. continue;
  961. }
  962. }
  963. return true;
  964. };
  965. /*}}}*/
  966. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  967. // ---------------------------------------------------------------------
  968. /* We stash the desired pipeline depth */
  969. bool HttpMethod::Configuration(string Message)
  970. {
  971. if (pkgAcqMethod::Configuration(Message) == false)
  972. return false;
  973. AllowRedirect = _config->FindB("Acquire::http::AllowRedirect",true);
  974. TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut);
  975. PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
  976. PipelineDepth);
  977. Debug = _config->FindB("Debug::Acquire::http",false);
  978. AutoDetectProxyCmd = _config->Find("Acquire::http::ProxyAutoDetect");
  979. // Get the proxy to use
  980. AutoDetectProxy();
  981. return true;
  982. }
  983. /*}}}*/
  984. // HttpMethod::Loop - Main loop /*{{{*/
  985. // ---------------------------------------------------------------------
  986. /* */
  987. int HttpMethod::Loop()
  988. {
  989. typedef vector<string> StringVector;
  990. typedef vector<string>::iterator StringVectorIterator;
  991. map<string, StringVector> Redirected;
  992. signal(SIGTERM,SigTerm);
  993. signal(SIGINT,SigTerm);
  994. Server = 0;
  995. int FailCounter = 0;
  996. while (1)
  997. {
  998. // We have no commands, wait for some to arrive
  999. if (Queue == 0)
  1000. {
  1001. if (WaitFd(STDIN_FILENO) == false)
  1002. return 0;
  1003. }
  1004. /* Run messages, we can accept 0 (no message) if we didn't
  1005. do a WaitFd above.. Otherwise the FD is closed. */
  1006. int Result = Run(true);
  1007. if (Result != -1 && (Result != 0 || Queue == 0))
  1008. {
  1009. if(FailReason.empty() == false ||
  1010. _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  1011. return 100;
  1012. else
  1013. return 0;
  1014. }
  1015. if (Queue == 0)
  1016. continue;
  1017. // Connect to the server
  1018. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  1019. {
  1020. delete Server;
  1021. Server = new ServerState(Queue->Uri,this);
  1022. }
  1023. /* If the server has explicitly said this is the last connection
  1024. then we pre-emptively shut down the pipeline and tear down
  1025. the connection. This will speed up HTTP/1.0 servers a tad
  1026. since we don't have to wait for the close sequence to
  1027. complete */
  1028. if (Server->Persistent == false)
  1029. Server->Close();
  1030. // Reset the pipeline
  1031. if (Server->ServerFd == -1)
  1032. QueueBack = Queue;
  1033. // Connnect to the host
  1034. if (Server->Open() == false)
  1035. {
  1036. Fail(true);
  1037. delete Server;
  1038. Server = 0;
  1039. continue;
  1040. }
  1041. // Fill the pipeline.
  1042. Fetch(0);
  1043. // Fetch the next URL header data from the server.
  1044. switch (Server->RunHeaders())
  1045. {
  1046. case ServerState::RUN_HEADERS_OK:
  1047. break;
  1048. // The header data is bad
  1049. case ServerState::RUN_HEADERS_PARSE_ERROR:
  1050. {
  1051. _error->Error(_("Bad header data"));
  1052. Fail(true);
  1053. RotateDNS();
  1054. continue;
  1055. }
  1056. // The server closed a connection during the header get..
  1057. default:
  1058. case ServerState::RUN_HEADERS_IO_ERROR:
  1059. {
  1060. FailCounter++;
  1061. _error->Discard();
  1062. Server->Close();
  1063. Server->Pipeline = false;
  1064. if (FailCounter >= 2)
  1065. {
  1066. Fail(_("Connection failed"),true);
  1067. FailCounter = 0;
  1068. }
  1069. RotateDNS();
  1070. continue;
  1071. }
  1072. };
  1073. // Decide what to do.
  1074. FetchResult Res;
  1075. Res.Filename = Queue->DestFile;
  1076. switch (DealWithHeaders(Res,Server))
  1077. {
  1078. // Ok, the file is Open
  1079. case FILE_IS_OPEN:
  1080. {
  1081. URIStart(Res);
  1082. // Run the data
  1083. bool Result = Server->RunData();
  1084. /* If the server is sending back sizeless responses then fill in
  1085. the size now */
  1086. if (Res.Size == 0)
  1087. Res.Size = File->Size();
  1088. // Close the file, destroy the FD object and timestamp it
  1089. FailFd = -1;
  1090. delete File;
  1091. File = 0;
  1092. // Timestamp
  1093. struct utimbuf UBuf;
  1094. time(&UBuf.actime);
  1095. UBuf.actime = Server->Date;
  1096. UBuf.modtime = Server->Date;
  1097. utime(Queue->DestFile.c_str(),&UBuf);
  1098. // Send status to APT
  1099. if (Result == true)
  1100. {
  1101. Res.TakeHashes(*Server->In.Hash);
  1102. URIDone(Res);
  1103. }
  1104. else
  1105. {
  1106. if (Server->ServerFd == -1)
  1107. {
  1108. FailCounter++;
  1109. _error->Discard();
  1110. Server->Close();
  1111. if (FailCounter >= 2)
  1112. {
  1113. Fail(_("Connection failed"),true);
  1114. FailCounter = 0;
  1115. }
  1116. QueueBack = Queue;
  1117. }
  1118. else
  1119. Fail(true);
  1120. }
  1121. break;
  1122. }
  1123. // IMS hit
  1124. case IMS_HIT:
  1125. {
  1126. URIDone(Res);
  1127. break;
  1128. }
  1129. // Hard server error, not found or something
  1130. case ERROR_UNRECOVERABLE:
  1131. {
  1132. Fail();
  1133. break;
  1134. }
  1135. // Hard internal error, kill the connection and fail
  1136. case ERROR_NOT_FROM_SERVER:
  1137. {
  1138. delete File;
  1139. File = 0;
  1140. Fail();
  1141. RotateDNS();
  1142. Server->Close();
  1143. break;
  1144. }
  1145. // We need to flush the data, the header is like a 404 w/ error text
  1146. case ERROR_WITH_CONTENT_PAGE:
  1147. {
  1148. Fail();
  1149. // Send to content to dev/null
  1150. File = new FileFd("/dev/null",FileFd::WriteExists);
  1151. Server->RunData();
  1152. delete File;
  1153. File = 0;
  1154. break;
  1155. }
  1156. // Try again with a new URL
  1157. case TRY_AGAIN_OR_REDIRECT:
  1158. {
  1159. // Clear rest of response if there is content
  1160. if (Server->HaveContent)
  1161. {
  1162. File = new FileFd("/dev/null",FileFd::WriteExists);
  1163. Server->RunData();
  1164. delete File;
  1165. File = 0;
  1166. }
  1167. /* Detect redirect loops. No more redirects are allowed
  1168. after the same URI is seen twice in a queue item. */
  1169. StringVector &R = Redirected[Queue->DestFile];
  1170. bool StopRedirects = false;
  1171. if (R.size() == 0)
  1172. R.push_back(Queue->Uri);
  1173. else if (R[0] == "STOP" || R.size() > 10)
  1174. StopRedirects = true;
  1175. else
  1176. {
  1177. for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
  1178. if (Queue->Uri == *I)
  1179. {
  1180. R[0] = "STOP";
  1181. break;
  1182. }
  1183. R.push_back(Queue->Uri);
  1184. }
  1185. if (StopRedirects == false)
  1186. Redirect(NextURI);
  1187. else
  1188. Fail();
  1189. break;
  1190. }
  1191. default:
  1192. Fail(_("Internal error"));
  1193. break;
  1194. }
  1195. FailCounter = 0;
  1196. }
  1197. return 0;
  1198. }
  1199. /*}}}*/
  1200. // HttpMethod::AutoDetectProxy - auto detect proxy /*{{{*/
  1201. // ---------------------------------------------------------------------
  1202. /* */
  1203. bool HttpMethod::AutoDetectProxy()
  1204. {
  1205. if (AutoDetectProxyCmd.empty())
  1206. return true;
  1207. if (Debug)
  1208. clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << endl;
  1209. int Pipes[2] = {-1,-1};
  1210. if (pipe(Pipes) != 0)
  1211. return _error->Errno("pipe", "Failed to create Pipe");
  1212. pid_t Process = ExecFork();
  1213. if (Process == 0)
  1214. {
  1215. close(Pipes[0]);
  1216. dup2(Pipes[1],STDOUT_FILENO);
  1217. SetCloseExec(STDOUT_FILENO,false);
  1218. const char *Args[2];
  1219. Args[0] = AutoDetectProxyCmd.c_str();
  1220. Args[1] = 0;
  1221. execv(Args[0],(char **)Args);
  1222. cerr << "Failed to exec method " << Args[0] << endl;
  1223. _exit(100);
  1224. }
  1225. char buf[512];
  1226. int InFd = Pipes[0];
  1227. close(Pipes[1]);
  1228. int res = read(InFd, buf, sizeof(buf));
  1229. ExecWait(Process, "ProxyAutoDetect", true);
  1230. if (res < 0)
  1231. return _error->Errno("read", "Failed to read");
  1232. if (res == 0)
  1233. return _error->Warning("ProxyAutoDetect returned no data");
  1234. // add trailing \0
  1235. buf[res] = 0;
  1236. if (Debug)
  1237. clog << "auto detect command returned: '" << buf << "'" << endl;
  1238. if (strstr(buf, "http://") == buf)
  1239. _config->Set("Acquire::http::proxy", _strstrip(buf));
  1240. return true;
  1241. }
  1242. /*}}}*/