http.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. // If we ask for uncompressed files servers might respond with content-
  636. // negotation which lets us end up with compressed files we do not support,
  637. // see 657029, 657560 and co, so if we have no extension on the request
  638. // ask for text only. As a sidenote: If there is nothing to negotate servers
  639. // seem to be nice and ignore it.
  640. if (_config->FindB("Acquire::http::SendAccept", true) == true)
  641. {
  642. size_t const filepos = Itm->Uri.find_last_of('/');
  643. string const file = Itm->Uri.substr(filepos + 1);
  644. if (flExtension(file) == file)
  645. strcat(Buf,"Accept: text/*\r\n");
  646. }
  647. string Req = Buf;
  648. // Check for a partial file
  649. struct stat SBuf;
  650. if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  651. {
  652. // In this case we send an if-range query with a range header
  653. sprintf(Buf,"Range: bytes=%lli-\r\nIf-Range: %s\r\n",(long long)SBuf.st_size - 1,
  654. TimeRFC1123(SBuf.st_mtime).c_str());
  655. Req += Buf;
  656. }
  657. else
  658. {
  659. if (Itm->LastModified != 0)
  660. {
  661. sprintf(Buf,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm->LastModified).c_str());
  662. Req += Buf;
  663. }
  664. }
  665. if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
  666. Req += string("Proxy-Authorization: Basic ") +
  667. Base64Encode(Proxy.User + ":" + Proxy.Password) + "\r\n";
  668. maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc"));
  669. if (Uri.User.empty() == false || Uri.Password.empty() == false)
  670. {
  671. Req += string("Authorization: Basic ") +
  672. Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n";
  673. }
  674. Req += "User-Agent: " + _config->Find("Acquire::http::User-Agent",
  675. "Debian APT-HTTP/1.3 ("VERSION")") + "\r\n\r\n";
  676. if (Debug == true)
  677. cerr << Req << endl;
  678. Out.Read(Req);
  679. }
  680. /*}}}*/
  681. // HttpMethod::Go - Run a single loop /*{{{*/
  682. // ---------------------------------------------------------------------
  683. /* This runs the select loop over the server FDs, Output file FDs and
  684. stdin. */
  685. bool HttpMethod::Go(bool ToFile,ServerState *Srv)
  686. {
  687. // Server has closed the connection
  688. if (Srv->ServerFd == -1 && (Srv->In.WriteSpace() == false ||
  689. ToFile == false))
  690. return false;
  691. fd_set rfds,wfds;
  692. FD_ZERO(&rfds);
  693. FD_ZERO(&wfds);
  694. /* Add the server. We only send more requests if the connection will
  695. be persisting */
  696. if (Srv->Out.WriteSpace() == true && Srv->ServerFd != -1
  697. && Srv->Persistent == true)
  698. FD_SET(Srv->ServerFd,&wfds);
  699. if (Srv->In.ReadSpace() == true && Srv->ServerFd != -1)
  700. FD_SET(Srv->ServerFd,&rfds);
  701. // Add the file
  702. int FileFD = -1;
  703. if (File != 0)
  704. FileFD = File->Fd();
  705. if (Srv->In.WriteSpace() == true && ToFile == true && FileFD != -1)
  706. FD_SET(FileFD,&wfds);
  707. // Add stdin
  708. if (_config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  709. FD_SET(STDIN_FILENO,&rfds);
  710. // Figure out the max fd
  711. int MaxFd = FileFD;
  712. if (MaxFd < Srv->ServerFd)
  713. MaxFd = Srv->ServerFd;
  714. // Select
  715. struct timeval tv;
  716. tv.tv_sec = TimeOut;
  717. tv.tv_usec = 0;
  718. int Res = 0;
  719. if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0)
  720. {
  721. if (errno == EINTR)
  722. return true;
  723. return _error->Errno("select",_("Select failed"));
  724. }
  725. if (Res == 0)
  726. {
  727. _error->Error(_("Connection timed out"));
  728. return ServerDie(Srv);
  729. }
  730. // Handle server IO
  731. if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&rfds))
  732. {
  733. errno = 0;
  734. if (Srv->In.Read(Srv->ServerFd) == false)
  735. return ServerDie(Srv);
  736. }
  737. if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&wfds))
  738. {
  739. errno = 0;
  740. if (Srv->Out.Write(Srv->ServerFd) == false)
  741. return ServerDie(Srv);
  742. }
  743. // Send data to the file
  744. if (FileFD != -1 && FD_ISSET(FileFD,&wfds))
  745. {
  746. if (Srv->In.Write(FileFD) == false)
  747. return _error->Errno("write",_("Error writing to output file"));
  748. }
  749. // Handle commands from APT
  750. if (FD_ISSET(STDIN_FILENO,&rfds))
  751. {
  752. if (Run(true) != -1)
  753. exit(100);
  754. }
  755. return true;
  756. }
  757. /*}}}*/
  758. // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
  759. // ---------------------------------------------------------------------
  760. /* This takes the current input buffer from the Server FD and writes it
  761. into the file */
  762. bool HttpMethod::Flush(ServerState *Srv)
  763. {
  764. if (File != 0)
  765. {
  766. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  767. // can't be set
  768. if (File->Name() != "/dev/null")
  769. SetNonBlock(File->Fd(),false);
  770. if (Srv->In.WriteSpace() == false)
  771. return true;
  772. while (Srv->In.WriteSpace() == true)
  773. {
  774. if (Srv->In.Write(File->Fd()) == false)
  775. return _error->Errno("write",_("Error writing to file"));
  776. if (Srv->In.IsLimit() == true)
  777. return true;
  778. }
  779. if (Srv->In.IsLimit() == true || Srv->Encoding == ServerState::Closes)
  780. return true;
  781. }
  782. return false;
  783. }
  784. /*}}}*/
  785. // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
  786. // ---------------------------------------------------------------------
  787. /* */
  788. bool HttpMethod::ServerDie(ServerState *Srv)
  789. {
  790. unsigned int LErrno = errno;
  791. // Dump the buffer to the file
  792. if (Srv->State == ServerState::Data)
  793. {
  794. // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
  795. // can't be set
  796. if (File->Name() != "/dev/null")
  797. SetNonBlock(File->Fd(),false);
  798. while (Srv->In.WriteSpace() == true)
  799. {
  800. if (Srv->In.Write(File->Fd()) == false)
  801. return _error->Errno("write",_("Error writing to the file"));
  802. // Done
  803. if (Srv->In.IsLimit() == true)
  804. return true;
  805. }
  806. }
  807. // See if this is because the server finished the data stream
  808. if (Srv->In.IsLimit() == false && Srv->State != ServerState::Header &&
  809. Srv->Encoding != ServerState::Closes)
  810. {
  811. Srv->Close();
  812. if (LErrno == 0)
  813. return _error->Error(_("Error reading from server. Remote end closed connection"));
  814. errno = LErrno;
  815. return _error->Errno("read",_("Error reading from server"));
  816. }
  817. else
  818. {
  819. Srv->In.Limit(-1);
  820. // Nothing left in the buffer
  821. if (Srv->In.WriteSpace() == false)
  822. return false;
  823. // We may have got multiple responses back in one packet..
  824. Srv->Close();
  825. return true;
  826. }
  827. return false;
  828. }
  829. /*}}}*/
  830. // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
  831. // ---------------------------------------------------------------------
  832. /* We look at the header data we got back from the server and decide what
  833. to do. Returns DealWithHeadersResult (see http.h for details).
  834. */
  835. HttpMethod::DealWithHeadersResult
  836. HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
  837. {
  838. // Not Modified
  839. if (Srv->Result == 304)
  840. {
  841. unlink(Queue->DestFile.c_str());
  842. Res.IMSHit = true;
  843. Res.LastModified = Queue->LastModified;
  844. return IMS_HIT;
  845. }
  846. /* Redirect
  847. *
  848. * Note that it is only OK for us to treat all redirection the same
  849. * because we *always* use GET, not other HTTP methods. There are
  850. * three redirection codes for which it is not appropriate that we
  851. * redirect. Pass on those codes so the error handling kicks in.
  852. */
  853. if (AllowRedirect
  854. && (Srv->Result > 300 && Srv->Result < 400)
  855. && (Srv->Result != 300 // Multiple Choices
  856. && Srv->Result != 304 // Not Modified
  857. && Srv->Result != 306)) // (Not part of HTTP/1.1, reserved)
  858. {
  859. if (Srv->Location.empty() == true);
  860. else if (Srv->Location[0] == '/' && Queue->Uri.empty() == false)
  861. {
  862. URI Uri = Queue->Uri;
  863. if (Uri.Host.empty() == false)
  864. {
  865. if (Uri.Port != 0)
  866. strprintf(NextURI, "http://%s:%u", Uri.Host.c_str(), Uri.Port);
  867. else
  868. NextURI = "http://" + Uri.Host;
  869. }
  870. else
  871. NextURI.clear();
  872. NextURI.append(DeQuoteString(Srv->Location));
  873. return TRY_AGAIN_OR_REDIRECT;
  874. }
  875. else
  876. {
  877. NextURI = DeQuoteString(Srv->Location);
  878. return TRY_AGAIN_OR_REDIRECT;
  879. }
  880. /* else pass through for error message */
  881. }
  882. /* We have a reply we dont handle. This should indicate a perm server
  883. failure */
  884. if (Srv->Result < 200 || Srv->Result >= 300)
  885. {
  886. char err[255];
  887. snprintf(err,sizeof(err)-1,"HttpError%i",Srv->Result);
  888. SetFailReason(err);
  889. _error->Error("%u %s",Srv->Result,Srv->Code);
  890. if (Srv->HaveContent == true)
  891. return ERROR_WITH_CONTENT_PAGE;
  892. return ERROR_UNRECOVERABLE;
  893. }
  894. // This is some sort of 2xx 'data follows' reply
  895. Res.LastModified = Srv->Date;
  896. Res.Size = Srv->Size;
  897. // Open the file
  898. delete File;
  899. File = new FileFd(Queue->DestFile,FileFd::WriteAny);
  900. if (_error->PendingError() == true)
  901. return ERROR_NOT_FROM_SERVER;
  902. FailFile = Queue->DestFile;
  903. FailFile.c_str(); // Make sure we dont do a malloc in the signal handler
  904. FailFd = File->Fd();
  905. FailTime = Srv->Date;
  906. delete Srv->In.Hash;
  907. Srv->In.Hash = new Hashes;
  908. // Set the expected size and read file for the hashes
  909. if (Srv->StartPos >= 0)
  910. {
  911. Res.ResumePoint = Srv->StartPos;
  912. File->Truncate(Srv->StartPos);
  913. if (Srv->In.Hash->AddFD(*File,Srv->StartPos) == false)
  914. {
  915. _error->Errno("read",_("Problem hashing file"));
  916. return ERROR_NOT_FROM_SERVER;
  917. }
  918. }
  919. SetNonBlock(File->Fd(),true);
  920. return FILE_IS_OPEN;
  921. }
  922. /*}}}*/
  923. // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
  924. // ---------------------------------------------------------------------
  925. /* This closes and timestamps the open file. This is neccessary to get
  926. resume behavoir on user abort */
  927. void HttpMethod::SigTerm(int)
  928. {
  929. if (FailFd == -1)
  930. _exit(100);
  931. close(FailFd);
  932. // Timestamp
  933. struct utimbuf UBuf;
  934. UBuf.actime = FailTime;
  935. UBuf.modtime = FailTime;
  936. utime(FailFile.c_str(),&UBuf);
  937. _exit(100);
  938. }
  939. /*}}}*/
  940. // HttpMethod::Fetch - Fetch an item /*{{{*/
  941. // ---------------------------------------------------------------------
  942. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  943. depth. */
  944. bool HttpMethod::Fetch(FetchItem *)
  945. {
  946. if (Server == 0)
  947. return true;
  948. // Queue the requests
  949. int Depth = -1;
  950. for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
  951. I = I->Next, Depth++)
  952. {
  953. // If pipelining is disabled, we only queue 1 request
  954. if (Server->Pipeline == false && Depth >= 0)
  955. break;
  956. // Make sure we stick with the same server
  957. if (Server->Comp(I->Uri) == false)
  958. break;
  959. if (QueueBack == I)
  960. {
  961. QueueBack = I->Next;
  962. SendReq(I,Server->Out);
  963. continue;
  964. }
  965. }
  966. return true;
  967. };
  968. /*}}}*/
  969. // HttpMethod::Configuration - Handle a configuration message /*{{{*/
  970. // ---------------------------------------------------------------------
  971. /* We stash the desired pipeline depth */
  972. bool HttpMethod::Configuration(string Message)
  973. {
  974. if (pkgAcqMethod::Configuration(Message) == false)
  975. return false;
  976. AllowRedirect = _config->FindB("Acquire::http::AllowRedirect",true);
  977. TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut);
  978. PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
  979. PipelineDepth);
  980. Debug = _config->FindB("Debug::Acquire::http",false);
  981. AutoDetectProxyCmd = _config->Find("Acquire::http::ProxyAutoDetect");
  982. // Get the proxy to use
  983. AutoDetectProxy();
  984. return true;
  985. }
  986. /*}}}*/
  987. // HttpMethod::Loop - Main loop /*{{{*/
  988. // ---------------------------------------------------------------------
  989. /* */
  990. int HttpMethod::Loop()
  991. {
  992. typedef vector<string> StringVector;
  993. typedef vector<string>::iterator StringVectorIterator;
  994. map<string, StringVector> Redirected;
  995. signal(SIGTERM,SigTerm);
  996. signal(SIGINT,SigTerm);
  997. Server = 0;
  998. int FailCounter = 0;
  999. while (1)
  1000. {
  1001. // We have no commands, wait for some to arrive
  1002. if (Queue == 0)
  1003. {
  1004. if (WaitFd(STDIN_FILENO) == false)
  1005. return 0;
  1006. }
  1007. /* Run messages, we can accept 0 (no message) if we didn't
  1008. do a WaitFd above.. Otherwise the FD is closed. */
  1009. int Result = Run(true);
  1010. if (Result != -1 && (Result != 0 || Queue == 0))
  1011. {
  1012. if(FailReason.empty() == false ||
  1013. _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  1014. return 100;
  1015. else
  1016. return 0;
  1017. }
  1018. if (Queue == 0)
  1019. continue;
  1020. // Connect to the server
  1021. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  1022. {
  1023. delete Server;
  1024. Server = new ServerState(Queue->Uri,this);
  1025. }
  1026. /* If the server has explicitly said this is the last connection
  1027. then we pre-emptively shut down the pipeline and tear down
  1028. the connection. This will speed up HTTP/1.0 servers a tad
  1029. since we don't have to wait for the close sequence to
  1030. complete */
  1031. if (Server->Persistent == false)
  1032. Server->Close();
  1033. // Reset the pipeline
  1034. if (Server->ServerFd == -1)
  1035. QueueBack = Queue;
  1036. // Connnect to the host
  1037. if (Server->Open() == false)
  1038. {
  1039. Fail(true);
  1040. delete Server;
  1041. Server = 0;
  1042. continue;
  1043. }
  1044. // Fill the pipeline.
  1045. Fetch(0);
  1046. // Fetch the next URL header data from the server.
  1047. switch (Server->RunHeaders())
  1048. {
  1049. case ServerState::RUN_HEADERS_OK:
  1050. break;
  1051. // The header data is bad
  1052. case ServerState::RUN_HEADERS_PARSE_ERROR:
  1053. {
  1054. _error->Error(_("Bad header data"));
  1055. Fail(true);
  1056. RotateDNS();
  1057. continue;
  1058. }
  1059. // The server closed a connection during the header get..
  1060. default:
  1061. case ServerState::RUN_HEADERS_IO_ERROR:
  1062. {
  1063. FailCounter++;
  1064. _error->Discard();
  1065. Server->Close();
  1066. Server->Pipeline = false;
  1067. if (FailCounter >= 2)
  1068. {
  1069. Fail(_("Connection failed"),true);
  1070. FailCounter = 0;
  1071. }
  1072. RotateDNS();
  1073. continue;
  1074. }
  1075. };
  1076. // Decide what to do.
  1077. FetchResult Res;
  1078. Res.Filename = Queue->DestFile;
  1079. switch (DealWithHeaders(Res,Server))
  1080. {
  1081. // Ok, the file is Open
  1082. case FILE_IS_OPEN:
  1083. {
  1084. URIStart(Res);
  1085. // Run the data
  1086. bool Result = Server->RunData();
  1087. /* If the server is sending back sizeless responses then fill in
  1088. the size now */
  1089. if (Res.Size == 0)
  1090. Res.Size = File->Size();
  1091. // Close the file, destroy the FD object and timestamp it
  1092. FailFd = -1;
  1093. delete File;
  1094. File = 0;
  1095. // Timestamp
  1096. struct utimbuf UBuf;
  1097. time(&UBuf.actime);
  1098. UBuf.actime = Server->Date;
  1099. UBuf.modtime = Server->Date;
  1100. utime(Queue->DestFile.c_str(),&UBuf);
  1101. // Send status to APT
  1102. if (Result == true)
  1103. {
  1104. Res.TakeHashes(*Server->In.Hash);
  1105. URIDone(Res);
  1106. }
  1107. else
  1108. {
  1109. if (Server->ServerFd == -1)
  1110. {
  1111. FailCounter++;
  1112. _error->Discard();
  1113. Server->Close();
  1114. if (FailCounter >= 2)
  1115. {
  1116. Fail(_("Connection failed"),true);
  1117. FailCounter = 0;
  1118. }
  1119. QueueBack = Queue;
  1120. }
  1121. else
  1122. Fail(true);
  1123. }
  1124. break;
  1125. }
  1126. // IMS hit
  1127. case IMS_HIT:
  1128. {
  1129. URIDone(Res);
  1130. break;
  1131. }
  1132. // Hard server error, not found or something
  1133. case ERROR_UNRECOVERABLE:
  1134. {
  1135. Fail();
  1136. break;
  1137. }
  1138. // Hard internal error, kill the connection and fail
  1139. case ERROR_NOT_FROM_SERVER:
  1140. {
  1141. delete File;
  1142. File = 0;
  1143. Fail();
  1144. RotateDNS();
  1145. Server->Close();
  1146. break;
  1147. }
  1148. // We need to flush the data, the header is like a 404 w/ error text
  1149. case ERROR_WITH_CONTENT_PAGE:
  1150. {
  1151. Fail();
  1152. // Send to content to dev/null
  1153. File = new FileFd("/dev/null",FileFd::WriteExists);
  1154. Server->RunData();
  1155. delete File;
  1156. File = 0;
  1157. break;
  1158. }
  1159. // Try again with a new URL
  1160. case TRY_AGAIN_OR_REDIRECT:
  1161. {
  1162. // Clear rest of response if there is content
  1163. if (Server->HaveContent)
  1164. {
  1165. File = new FileFd("/dev/null",FileFd::WriteExists);
  1166. Server->RunData();
  1167. delete File;
  1168. File = 0;
  1169. }
  1170. /* Detect redirect loops. No more redirects are allowed
  1171. after the same URI is seen twice in a queue item. */
  1172. StringVector &R = Redirected[Queue->DestFile];
  1173. bool StopRedirects = false;
  1174. if (R.size() == 0)
  1175. R.push_back(Queue->Uri);
  1176. else if (R[0] == "STOP" || R.size() > 10)
  1177. StopRedirects = true;
  1178. else
  1179. {
  1180. for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
  1181. if (Queue->Uri == *I)
  1182. {
  1183. R[0] = "STOP";
  1184. break;
  1185. }
  1186. R.push_back(Queue->Uri);
  1187. }
  1188. if (StopRedirects == false)
  1189. Redirect(NextURI);
  1190. else
  1191. Fail();
  1192. break;
  1193. }
  1194. default:
  1195. Fail(_("Internal error"));
  1196. break;
  1197. }
  1198. FailCounter = 0;
  1199. }
  1200. return 0;
  1201. }
  1202. /*}}}*/
  1203. // HttpMethod::AutoDetectProxy - auto detect proxy /*{{{*/
  1204. // ---------------------------------------------------------------------
  1205. /* */
  1206. bool HttpMethod::AutoDetectProxy()
  1207. {
  1208. if (AutoDetectProxyCmd.empty())
  1209. return true;
  1210. if (Debug)
  1211. clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << endl;
  1212. int Pipes[2] = {-1,-1};
  1213. if (pipe(Pipes) != 0)
  1214. return _error->Errno("pipe", "Failed to create Pipe");
  1215. pid_t Process = ExecFork();
  1216. if (Process == 0)
  1217. {
  1218. close(Pipes[0]);
  1219. dup2(Pipes[1],STDOUT_FILENO);
  1220. SetCloseExec(STDOUT_FILENO,false);
  1221. const char *Args[2];
  1222. Args[0] = AutoDetectProxyCmd.c_str();
  1223. Args[1] = 0;
  1224. execv(Args[0],(char **)Args);
  1225. cerr << "Failed to exec method " << Args[0] << endl;
  1226. _exit(100);
  1227. }
  1228. char buf[512];
  1229. int InFd = Pipes[0];
  1230. close(Pipes[1]);
  1231. int res = read(InFd, buf, sizeof(buf));
  1232. ExecWait(Process, "ProxyAutoDetect", true);
  1233. if (res < 0)
  1234. return _error->Errno("read", "Failed to read");
  1235. if (res == 0)
  1236. return _error->Warning("ProxyAutoDetect returned no data");
  1237. // add trailing \0
  1238. buf[res] = 0;
  1239. if (Debug)
  1240. clog << "auto detect command returned: '" << buf << "'" << endl;
  1241. if (strstr(buf, "http://") == buf)
  1242. _config->Set("Acquire::http::proxy", _strstrip(buf));
  1243. return true;
  1244. }
  1245. /*}}}*/