http.cc 37 KB

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