server.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. HTTP and HTTPS share a lot of common code and these classes are
  5. exactly the dumping ground for this common code
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/acquire-method.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <ctype.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <sys/stat.h>
  20. #include <sys/time.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <iostream>
  24. #include <limits>
  25. #include <map>
  26. #include <string>
  27. #include <vector>
  28. #include "server.h"
  29. #include <apti18n.h>
  30. /*}}}*/
  31. using namespace std;
  32. string ServerMethod::FailFile;
  33. int ServerMethod::FailFd = -1;
  34. time_t ServerMethod::FailTime = 0;
  35. // ServerState::RunHeaders - Get the headers before the data /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
  38. parse error occurred */
  39. ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File,
  40. const std::string &Uri)
  41. {
  42. State = Header;
  43. Owner->Status(_("Waiting for headers"));
  44. Major = 0;
  45. Minor = 0;
  46. Result = 0;
  47. TotalFileSize = 0;
  48. JunkSize = 0;
  49. StartPos = 0;
  50. Encoding = Closes;
  51. HaveContent = false;
  52. time(&Date);
  53. do
  54. {
  55. string Data;
  56. if (ReadHeaderLines(Data) == false)
  57. continue;
  58. if (Owner->Debug == true)
  59. clog << "Answer for: " << Uri << endl << Data;
  60. for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
  61. {
  62. string::const_iterator J = I;
  63. for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J);
  64. if (HeaderLine(string(I,J)) == false)
  65. return RUN_HEADERS_PARSE_ERROR;
  66. I = J;
  67. }
  68. // 100 Continue is a Nop...
  69. if (Result == 100)
  70. continue;
  71. // Tidy up the connection persistence state.
  72. if (Encoding == Closes && HaveContent == true)
  73. Persistent = false;
  74. return RUN_HEADERS_OK;
  75. }
  76. while (LoadNextResponse(false, File) == true);
  77. return RUN_HEADERS_IO_ERROR;
  78. }
  79. /*}}}*/
  80. // ServerState::HeaderLine - Process a header line /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* */
  83. bool ServerState::HeaderLine(string Line)
  84. {
  85. if (Line.empty() == true)
  86. return true;
  87. string::size_type Pos = Line.find(' ');
  88. if (Pos == string::npos || Pos+1 > Line.length())
  89. {
  90. // Blah, some servers use "connection:closes", evil.
  91. Pos = Line.find(':');
  92. if (Pos == string::npos || Pos + 2 > Line.length())
  93. return _error->Error(_("Bad header line"));
  94. Pos++;
  95. }
  96. // Parse off any trailing spaces between the : and the next word.
  97. string::size_type Pos2 = Pos;
  98. while (Pos2 < Line.length() && isspace_ascii(Line[Pos2]) != 0)
  99. Pos2++;
  100. string Tag = string(Line,0,Pos);
  101. string Val = string(Line,Pos2);
  102. if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
  103. {
  104. // Evil servers return no version
  105. if (Line[4] == '/')
  106. {
  107. int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code);
  108. if (elements == 3)
  109. {
  110. Code[0] = '\0';
  111. if (Owner != NULL && Owner->Debug == true)
  112. clog << "HTTP server doesn't give Reason-Phrase for " << Result << std::endl;
  113. }
  114. else if (elements != 4)
  115. return _error->Error(_("The HTTP server sent an invalid reply header"));
  116. }
  117. else
  118. {
  119. Major = 0;
  120. Minor = 9;
  121. if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2)
  122. return _error->Error(_("The HTTP server sent an invalid reply header"));
  123. }
  124. /* Check the HTTP response header to get the default persistence
  125. state. */
  126. if (Major < 1)
  127. Persistent = false;
  128. else
  129. {
  130. if (Major == 1 && Minor == 0)
  131. {
  132. Persistent = false;
  133. }
  134. else
  135. {
  136. Persistent = true;
  137. if (PipelineAllowed)
  138. Pipeline = true;
  139. }
  140. }
  141. return true;
  142. }
  143. if (stringcasecmp(Tag,"Content-Length:") == 0)
  144. {
  145. if (Encoding == Closes)
  146. Encoding = Stream;
  147. HaveContent = true;
  148. unsigned long long * DownloadSizePtr = &DownloadSize;
  149. if (Result == 416)
  150. DownloadSizePtr = &JunkSize;
  151. *DownloadSizePtr = strtoull(Val.c_str(), NULL, 10);
  152. if (*DownloadSizePtr >= std::numeric_limits<unsigned long long>::max())
  153. return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
  154. else if (*DownloadSizePtr == 0)
  155. HaveContent = false;
  156. // On partial content (206) the Content-Length less than the real
  157. // size, so do not set it here but leave that to the Content-Range
  158. // header instead
  159. if(Result != 206 && TotalFileSize == 0)
  160. TotalFileSize = DownloadSize;
  161. return true;
  162. }
  163. if (stringcasecmp(Tag,"Content-Type:") == 0)
  164. {
  165. HaveContent = true;
  166. return true;
  167. }
  168. if (stringcasecmp(Tag,"Content-Range:") == 0)
  169. {
  170. HaveContent = true;
  171. // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
  172. if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&TotalFileSize) == 1)
  173. ; // we got the expected filesize which is all we wanted
  174. else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2)
  175. return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
  176. if ((unsigned long long)StartPos > TotalFileSize)
  177. return _error->Error(_("This HTTP server has broken range support"));
  178. // figure out what we will download
  179. DownloadSize = TotalFileSize - StartPos;
  180. return true;
  181. }
  182. if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
  183. {
  184. HaveContent = true;
  185. if (stringcasecmp(Val,"chunked") == 0)
  186. Encoding = Chunked;
  187. return true;
  188. }
  189. if (stringcasecmp(Tag,"Connection:") == 0)
  190. {
  191. if (stringcasecmp(Val,"close") == 0)
  192. Persistent = false;
  193. if (stringcasecmp(Val,"keep-alive") == 0)
  194. Persistent = true;
  195. return true;
  196. }
  197. if (stringcasecmp(Tag,"Last-Modified:") == 0)
  198. {
  199. if (RFC1123StrToTime(Val.c_str(), Date) == false)
  200. return _error->Error(_("Unknown date format"));
  201. return true;
  202. }
  203. if (stringcasecmp(Tag,"Location:") == 0)
  204. {
  205. Location = Val;
  206. return true;
  207. }
  208. return true;
  209. }
  210. /*}}}*/
  211. // ServerState::ServerState - Constructor /*{{{*/
  212. ServerState::ServerState(URI Srv, ServerMethod *Owner) : ServerName(Srv), TimeOut(120), Owner(Owner)
  213. {
  214. Reset();
  215. }
  216. /*}}}*/
  217. bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/
  218. {
  219. File.Truncate(StartPos);
  220. return GetHashes()->AddFD(File, StartPos);
  221. }
  222. /*}}}*/
  223. // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* We look at the header data we got back from the server and decide what
  226. to do. Returns DealWithHeadersResult (see http.h for details).
  227. */
  228. ServerMethod::DealWithHeadersResult
  229. ServerMethod::DealWithHeaders(FetchResult &Res)
  230. {
  231. // Not Modified
  232. if (Server->Result == 304)
  233. {
  234. RemoveFile("server", Queue->DestFile);
  235. Res.IMSHit = true;
  236. Res.LastModified = Queue->LastModified;
  237. return IMS_HIT;
  238. }
  239. /* Redirect
  240. *
  241. * Note that it is only OK for us to treat all redirection the same
  242. * because we *always* use GET, not other HTTP methods. There are
  243. * three redirection codes for which it is not appropriate that we
  244. * redirect. Pass on those codes so the error handling kicks in.
  245. */
  246. if (AllowRedirect
  247. && (Server->Result > 300 && Server->Result < 400)
  248. && (Server->Result != 300 // Multiple Choices
  249. && Server->Result != 304 // Not Modified
  250. && Server->Result != 306)) // (Not part of HTTP/1.1, reserved)
  251. {
  252. if (Server->Location.empty() == true);
  253. else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
  254. {
  255. URI Uri = Queue->Uri;
  256. if (Uri.Host.empty() == false)
  257. NextURI = URI::SiteOnly(Uri);
  258. else
  259. NextURI.clear();
  260. NextURI.append(DeQuoteString(Server->Location));
  261. return TRY_AGAIN_OR_REDIRECT;
  262. }
  263. else
  264. {
  265. NextURI = DeQuoteString(Server->Location);
  266. URI tmpURI = NextURI;
  267. URI Uri = Queue->Uri;
  268. // same protocol redirects are okay
  269. if (tmpURI.Access == Uri.Access)
  270. return TRY_AGAIN_OR_REDIRECT;
  271. // as well as http to https
  272. else if (Uri.Access == "http" && tmpURI.Access == "https")
  273. return TRY_AGAIN_OR_REDIRECT;
  274. }
  275. /* else pass through for error message */
  276. }
  277. // retry after an invalid range response without partial data
  278. else if (Server->Result == 416)
  279. {
  280. struct stat SBuf;
  281. if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  282. {
  283. bool partialHit = false;
  284. if (Queue->ExpectedHashes.usable() == true)
  285. {
  286. Hashes resultHashes(Queue->ExpectedHashes);
  287. FileFd file(Queue->DestFile, FileFd::ReadOnly);
  288. Server->TotalFileSize = file.FileSize();
  289. Server->Date = file.ModificationTime();
  290. resultHashes.AddFD(file);
  291. HashStringList const hashList = resultHashes.GetHashStringList();
  292. partialHit = (Queue->ExpectedHashes == hashList);
  293. }
  294. else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
  295. partialHit = true;
  296. if (partialHit == true)
  297. {
  298. // the file is completely downloaded, but was not moved
  299. if (Server->HaveContent == true)
  300. {
  301. // Send to error page to dev/null
  302. FileFd DevNull("/dev/null",FileFd::WriteExists);
  303. Server->RunData(&DevNull);
  304. }
  305. Server->HaveContent = false;
  306. Server->StartPos = Server->TotalFileSize;
  307. Server->Result = 200;
  308. }
  309. else if (RemoveFile("server", Queue->DestFile))
  310. {
  311. NextURI = Queue->Uri;
  312. return TRY_AGAIN_OR_REDIRECT;
  313. }
  314. }
  315. }
  316. /* We have a reply we don't handle. This should indicate a perm server
  317. failure */
  318. if (Server->Result < 200 || Server->Result >= 300)
  319. {
  320. std::string err;
  321. strprintf(err, "HttpError%u", Server->Result);
  322. SetFailReason(err);
  323. _error->Error("%u %s", Server->Result, Server->Code);
  324. if (Server->HaveContent == true)
  325. return ERROR_WITH_CONTENT_PAGE;
  326. return ERROR_UNRECOVERABLE;
  327. }
  328. // This is some sort of 2xx 'data follows' reply
  329. Res.LastModified = Server->Date;
  330. Res.Size = Server->TotalFileSize;
  331. // Open the file
  332. delete File;
  333. File = new FileFd(Queue->DestFile,FileFd::WriteAny);
  334. if (_error->PendingError() == true)
  335. return ERROR_NOT_FROM_SERVER;
  336. FailFile = Queue->DestFile;
  337. FailFile.c_str(); // Make sure we don't do a malloc in the signal handler
  338. FailFd = File->Fd();
  339. FailTime = Server->Date;
  340. if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false)
  341. {
  342. _error->Errno("read",_("Problem hashing file"));
  343. return ERROR_NOT_FROM_SERVER;
  344. }
  345. if (Server->StartPos > 0)
  346. Res.ResumePoint = Server->StartPos;
  347. SetNonBlock(File->Fd(),true);
  348. return FILE_IS_OPEN;
  349. }
  350. /*}}}*/
  351. // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
  352. // ---------------------------------------------------------------------
  353. /* This closes and timestamps the open file. This is necessary to get
  354. resume behavoir on user abort */
  355. void ServerMethod::SigTerm(int)
  356. {
  357. if (FailFd == -1)
  358. _exit(100);
  359. struct timeval times[2];
  360. times[0].tv_sec = FailTime;
  361. times[1].tv_sec = FailTime;
  362. times[0].tv_usec = times[1].tv_usec = 0;
  363. utimes(FailFile.c_str(), times);
  364. close(FailFd);
  365. _exit(100);
  366. }
  367. /*}}}*/
  368. // ServerMethod::Fetch - Fetch an item /*{{{*/
  369. // ---------------------------------------------------------------------
  370. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  371. depth. */
  372. bool ServerMethod::Fetch(FetchItem *)
  373. {
  374. if (Server == 0)
  375. return true;
  376. // Queue the requests
  377. int Depth = -1;
  378. for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
  379. I = I->Next, Depth++)
  380. {
  381. if (Depth >= 0)
  382. {
  383. // If pipelining is disabled, we only queue 1 request
  384. if (Server->Pipeline == false)
  385. break;
  386. // if we have no hashes, do at most one such request
  387. // as we can't fixup pipeling misbehaviors otherwise
  388. else if (I->ExpectedHashes.usable() == false)
  389. break;
  390. }
  391. // Make sure we stick with the same server
  392. if (Server->Comp(I->Uri) == false)
  393. break;
  394. if (QueueBack == I)
  395. {
  396. QueueBack = I->Next;
  397. SendReq(I);
  398. continue;
  399. }
  400. }
  401. return true;
  402. }
  403. /*}}}*/
  404. // ServerMethod::Loop - Main loop /*{{{*/
  405. int ServerMethod::Loop()
  406. {
  407. typedef vector<string> StringVector;
  408. typedef vector<string>::iterator StringVectorIterator;
  409. map<string, StringVector> Redirected;
  410. signal(SIGTERM,SigTerm);
  411. signal(SIGINT,SigTerm);
  412. Server = 0;
  413. int FailCounter = 0;
  414. while (1)
  415. {
  416. // We have no commands, wait for some to arrive
  417. if (Queue == 0)
  418. {
  419. if (WaitFd(STDIN_FILENO) == false)
  420. return 0;
  421. }
  422. /* Run messages, we can accept 0 (no message) if we didn't
  423. do a WaitFd above.. Otherwise the FD is closed. */
  424. int Result = Run(true);
  425. if (Result != -1 && (Result != 0 || Queue == 0))
  426. {
  427. if(FailReason.empty() == false ||
  428. _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  429. return 100;
  430. else
  431. return 0;
  432. }
  433. if (Queue == 0)
  434. continue;
  435. // Connect to the server
  436. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  437. Server = CreateServerState(Queue->Uri);
  438. /* If the server has explicitly said this is the last connection
  439. then we pre-emptively shut down the pipeline and tear down
  440. the connection. This will speed up HTTP/1.0 servers a tad
  441. since we don't have to wait for the close sequence to
  442. complete */
  443. if (Server->Persistent == false)
  444. Server->Close();
  445. // Reset the pipeline
  446. if (Server->IsOpen() == false)
  447. QueueBack = Queue;
  448. // Connnect to the host
  449. if (Server->Open() == false)
  450. {
  451. Fail(true);
  452. Server = nullptr;
  453. continue;
  454. }
  455. // Fill the pipeline.
  456. Fetch(0);
  457. // Fetch the next URL header data from the server.
  458. switch (Server->RunHeaders(File, Queue->Uri))
  459. {
  460. case ServerState::RUN_HEADERS_OK:
  461. break;
  462. // The header data is bad
  463. case ServerState::RUN_HEADERS_PARSE_ERROR:
  464. {
  465. _error->Error(_("Bad header data"));
  466. Fail(true);
  467. RotateDNS();
  468. continue;
  469. }
  470. // The server closed a connection during the header get..
  471. default:
  472. case ServerState::RUN_HEADERS_IO_ERROR:
  473. {
  474. FailCounter++;
  475. _error->Discard();
  476. Server->Close();
  477. Server->Pipeline = false;
  478. Server->PipelineAllowed = false;
  479. if (FailCounter >= 2)
  480. {
  481. Fail(_("Connection failed"),true);
  482. FailCounter = 0;
  483. }
  484. RotateDNS();
  485. continue;
  486. }
  487. };
  488. // Decide what to do.
  489. FetchResult Res;
  490. Res.Filename = Queue->DestFile;
  491. switch (DealWithHeaders(Res))
  492. {
  493. // Ok, the file is Open
  494. case FILE_IS_OPEN:
  495. {
  496. URIStart(Res);
  497. // Run the data
  498. bool Result = true;
  499. // ensure we don't fetch too much
  500. // we could do "Server->MaximumSize = Queue->MaximumSize" here
  501. // but that would break the clever pipeline messup detection
  502. // so instead we use the size of the biggest item in the queue
  503. Server->MaximumSize = FindMaximumObjectSizeInQueue();
  504. if (Server->HaveContent)
  505. Result = Server->RunData(File);
  506. /* If the server is sending back sizeless responses then fill in
  507. the size now */
  508. if (Res.Size == 0)
  509. Res.Size = File->Size();
  510. // Close the file, destroy the FD object and timestamp it
  511. FailFd = -1;
  512. delete File;
  513. File = 0;
  514. // Timestamp
  515. struct timeval times[2];
  516. times[0].tv_sec = times[1].tv_sec = Server->Date;
  517. times[0].tv_usec = times[1].tv_usec = 0;
  518. utimes(Queue->DestFile.c_str(), times);
  519. // Send status to APT
  520. if (Result == true)
  521. {
  522. Hashes * const resultHashes = Server->GetHashes();
  523. HashStringList const hashList = resultHashes->GetHashStringList();
  524. if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
  525. {
  526. // we did not get the expected hash… mhhh:
  527. // could it be that server/proxy messed up pipelining?
  528. FetchItem * BeforeI = Queue;
  529. for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
  530. {
  531. if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
  532. {
  533. // yes, he did! Disable pipelining and rewrite queue
  534. if (Server->Pipeline == true)
  535. {
  536. // FIXME: fake a warning message as we have no proper way of communicating here
  537. std::string out;
  538. strprintf(out, _("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::PipelineDepth");
  539. std::cerr << "W: " << out << std::endl;
  540. Server->Pipeline = false;
  541. Server->PipelineAllowed = false;
  542. // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
  543. }
  544. Rename(Res.Filename, I->DestFile);
  545. Res.Filename = I->DestFile;
  546. BeforeI->Next = I->Next;
  547. I->Next = Queue;
  548. Queue = I;
  549. break;
  550. }
  551. BeforeI = I;
  552. }
  553. }
  554. Res.TakeHashes(*resultHashes);
  555. URIDone(Res);
  556. }
  557. else
  558. {
  559. if (Server->IsOpen() == false)
  560. {
  561. FailCounter++;
  562. _error->Discard();
  563. Server->Close();
  564. if (FailCounter >= 2)
  565. {
  566. Fail(_("Connection failed"),true);
  567. FailCounter = 0;
  568. }
  569. QueueBack = Queue;
  570. }
  571. else
  572. {
  573. Server->Close();
  574. Fail(true);
  575. }
  576. }
  577. break;
  578. }
  579. // IMS hit
  580. case IMS_HIT:
  581. {
  582. URIDone(Res);
  583. break;
  584. }
  585. // Hard server error, not found or something
  586. case ERROR_UNRECOVERABLE:
  587. {
  588. Fail();
  589. break;
  590. }
  591. // Hard internal error, kill the connection and fail
  592. case ERROR_NOT_FROM_SERVER:
  593. {
  594. delete File;
  595. File = 0;
  596. Fail();
  597. RotateDNS();
  598. Server->Close();
  599. break;
  600. }
  601. // We need to flush the data, the header is like a 404 w/ error text
  602. case ERROR_WITH_CONTENT_PAGE:
  603. {
  604. Fail();
  605. // Send to content to dev/null
  606. File = new FileFd("/dev/null",FileFd::WriteExists);
  607. Server->RunData(File);
  608. delete File;
  609. File = 0;
  610. break;
  611. }
  612. // Try again with a new URL
  613. case TRY_AGAIN_OR_REDIRECT:
  614. {
  615. // Clear rest of response if there is content
  616. if (Server->HaveContent)
  617. {
  618. File = new FileFd("/dev/null",FileFd::WriteExists);
  619. Server->RunData(File);
  620. delete File;
  621. File = 0;
  622. }
  623. /* Detect redirect loops. No more redirects are allowed
  624. after the same URI is seen twice in a queue item. */
  625. StringVector &R = Redirected[Queue->DestFile];
  626. bool StopRedirects = false;
  627. if (R.empty() == true)
  628. R.push_back(Queue->Uri);
  629. else if (R[0] == "STOP" || R.size() > 10)
  630. StopRedirects = true;
  631. else
  632. {
  633. for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
  634. if (Queue->Uri == *I)
  635. {
  636. R[0] = "STOP";
  637. break;
  638. }
  639. R.push_back(Queue->Uri);
  640. }
  641. if (StopRedirects == false)
  642. Redirect(NextURI);
  643. else
  644. Fail();
  645. break;
  646. }
  647. default:
  648. Fail(_("Internal error"));
  649. break;
  650. }
  651. FailCounter = 0;
  652. }
  653. return 0;
  654. }
  655. /*}}}*/
  656. unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
  657. {
  658. unsigned long long MaxSizeInQueue = 0;
  659. for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
  660. MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
  661. return MaxSizeInQueue;
  662. }
  663. /*}}}*/
  664. ServerMethod::ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
  665. aptMethod(Binary, Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
  666. AllowRedirect(false), Debug(false)
  667. {
  668. }
  669. /*}}}*/