You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

789 lines
22 KiB

  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/configuration.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <ctype.h>
  15. #include <signal.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <iostream>
  23. #include <limits>
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. #include "server.h"
  28. #include <apti18n.h>
  29. /*}}}*/
  30. using namespace std;
  31. string ServerMethod::FailFile;
  32. int ServerMethod::FailFd = -1;
  33. time_t ServerMethod::FailTime = 0;
  34. // ServerState::RunHeaders - Get the headers before the data /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
  37. parse error occurred */
  38. ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File,
  39. const std::string &Uri)
  40. {
  41. State = Header;
  42. Owner->Status(_("Waiting for headers"));
  43. Major = 0;
  44. Minor = 0;
  45. Result = 0;
  46. TotalFileSize = 0;
  47. JunkSize = 0;
  48. StartPos = 0;
  49. Encoding = Closes;
  50. HaveContent = false;
  51. time(&Date);
  52. do
  53. {
  54. string Data;
  55. if (ReadHeaderLines(Data) == false)
  56. continue;
  57. if (Owner->Debug == true)
  58. clog << "Answer for: " << Uri << endl << Data;
  59. for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
  60. {
  61. string::const_iterator J = I;
  62. for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J);
  63. if (HeaderLine(string(I,J)) == false)
  64. return RUN_HEADERS_PARSE_ERROR;
  65. I = J;
  66. }
  67. // 100 Continue is a Nop...
  68. if (Result == 100)
  69. continue;
  70. // Tidy up the connection persistence state.
  71. if (Encoding == Closes && HaveContent == true)
  72. Persistent = false;
  73. return RUN_HEADERS_OK;
  74. }
  75. while (LoadNextResponse(false, File) == true);
  76. return RUN_HEADERS_IO_ERROR;
  77. }
  78. /*}}}*/
  79. // ServerState::HeaderLine - Process a header line /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* */
  82. bool ServerState::HeaderLine(string Line)
  83. {
  84. if (Line.empty() == true)
  85. return true;
  86. string::size_type Pos = Line.find(' ');
  87. if (Pos == string::npos || Pos+1 > Line.length())
  88. {
  89. // Blah, some servers use "connection:closes", evil.
  90. Pos = Line.find(':');
  91. if (Pos == string::npos || Pos + 2 > Line.length())
  92. return _error->Error(_("Bad header line"));
  93. Pos++;
  94. }
  95. // Parse off any trailing spaces between the : and the next word.
  96. string::size_type Pos2 = Pos;
  97. while (Pos2 < Line.length() && isspace_ascii(Line[Pos2]) != 0)
  98. Pos2++;
  99. string Tag = string(Line,0,Pos);
  100. string Val = string(Line,Pos2);
  101. if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
  102. {
  103. // Evil servers return no version
  104. if (Line[4] == '/')
  105. {
  106. int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code);
  107. if (elements == 3)
  108. {
  109. Code[0] = '\0';
  110. if (Owner != NULL && Owner->Debug == true)
  111. clog << "HTTP server doesn't give Reason-Phrase for " << std::to_string(Result) << std::endl;
  112. }
  113. else if (elements != 4)
  114. return _error->Error(_("The HTTP server sent an invalid reply header"));
  115. }
  116. else
  117. {
  118. Major = 0;
  119. Minor = 9;
  120. if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2)
  121. return _error->Error(_("The HTTP server sent an invalid reply header"));
  122. }
  123. /* Check the HTTP response header to get the default persistence
  124. state. */
  125. if (Major < 1)
  126. Persistent = false;
  127. else
  128. {
  129. if (Major == 1 && Minor == 0)
  130. {
  131. Persistent = false;
  132. }
  133. else
  134. {
  135. Persistent = true;
  136. if (PipelineAllowed)
  137. Pipeline = true;
  138. }
  139. }
  140. return true;
  141. }
  142. if (stringcasecmp(Tag,"Content-Length:") == 0)
  143. {
  144. if (Encoding == Closes)
  145. Encoding = Stream;
  146. HaveContent = true;
  147. unsigned long long * DownloadSizePtr = &DownloadSize;
  148. if (Result == 416)
  149. DownloadSizePtr = &JunkSize;
  150. *DownloadSizePtr = strtoull(Val.c_str(), NULL, 10);
  151. if (*DownloadSizePtr >= std::numeric_limits<unsigned long long>::max())
  152. return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
  153. else if (*DownloadSizePtr == 0)
  154. HaveContent = false;
  155. // On partial content (206) the Content-Length less than the real
  156. // size, so do not set it here but leave that to the Content-Range
  157. // header instead
  158. if(Result != 206 && TotalFileSize == 0)
  159. TotalFileSize = DownloadSize;
  160. return true;
  161. }
  162. if (stringcasecmp(Tag,"Content-Type:") == 0)
  163. {
  164. HaveContent = true;
  165. return true;
  166. }
  167. if (stringcasecmp(Tag,"Content-Range:") == 0)
  168. {
  169. HaveContent = true;
  170. // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
  171. if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&TotalFileSize) == 1)
  172. ; // we got the expected filesize which is all we wanted
  173. else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2)
  174. return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
  175. if ((unsigned long long)StartPos > TotalFileSize)
  176. return _error->Error(_("This HTTP server has broken range support"));
  177. // figure out what we will download
  178. DownloadSize = TotalFileSize - StartPos;
  179. return true;
  180. }
  181. if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
  182. {
  183. HaveContent = true;
  184. if (stringcasecmp(Val,"chunked") == 0)
  185. Encoding = Chunked;
  186. return true;
  187. }
  188. if (stringcasecmp(Tag,"Connection:") == 0)
  189. {
  190. if (stringcasecmp(Val,"close") == 0)
  191. Persistent = false;
  192. if (stringcasecmp(Val,"keep-alive") == 0)
  193. Persistent = true;
  194. return true;
  195. }
  196. if (stringcasecmp(Tag,"Last-Modified:") == 0)
  197. {
  198. if (RFC1123StrToTime(Val.c_str(), Date) == false)
  199. return _error->Error(_("Unknown date format"));
  200. return true;
  201. }
  202. if (stringcasecmp(Tag,"Location:") == 0)
  203. {
  204. Location = Val;
  205. return true;
  206. }
  207. return true;
  208. }
  209. /*}}}*/
  210. // ServerState::ServerState - Constructor /*{{{*/
  211. ServerState::ServerState(URI Srv, ServerMethod *Owner) :
  212. DownloadSize(0), 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 == nullptr || QueueBack == nullptr)
  375. return true;
  376. // If pipelining is disabled, we only queue 1 request
  377. auto const AllowedDepth = Server->Pipeline ? PipelineDepth : 0;
  378. // how deep is our pipeline currently?
  379. decltype(PipelineDepth) CurrentDepth = 0;
  380. for (FetchItem const *I = Queue; I != QueueBack; I = I->Next)
  381. ++CurrentDepth;
  382. if (CurrentDepth > AllowedDepth)
  383. return true;
  384. do {
  385. // Make sure we stick with the same server
  386. if (Server->Comp(QueueBack->Uri) == false)
  387. break;
  388. bool const UsableHashes = QueueBack->ExpectedHashes.usable();
  389. // if we have no hashes, do at most one such request
  390. // as we can't fixup pipeling misbehaviors otherwise
  391. if (CurrentDepth != 0 && UsableHashes == false)
  392. break;
  393. if (UsableHashes && FileExists(QueueBack->DestFile))
  394. {
  395. FileFd partial(QueueBack->DestFile, FileFd::ReadOnly);
  396. Hashes wehave(QueueBack->ExpectedHashes);
  397. if (QueueBack->ExpectedHashes.FileSize() == partial.FileSize())
  398. {
  399. if (wehave.AddFD(partial) &&
  400. wehave.GetHashStringList() == QueueBack->ExpectedHashes)
  401. {
  402. FetchResult Res;
  403. Res.Filename = QueueBack->DestFile;
  404. Res.ResumePoint = QueueBack->ExpectedHashes.FileSize();
  405. URIStart(Res);
  406. // move item to the start of the queue as URIDone will
  407. // always dequeued the first item in the queue
  408. if (Queue != QueueBack)
  409. {
  410. FetchItem *Prev = Queue;
  411. for (; Prev->Next != QueueBack; Prev = Prev->Next)
  412. /* look for the previous queue item */;
  413. Prev->Next = QueueBack->Next;
  414. QueueBack->Next = Queue;
  415. Queue = QueueBack;
  416. QueueBack = Prev->Next;
  417. }
  418. Res.TakeHashes(wehave);
  419. URIDone(Res);
  420. continue;
  421. }
  422. else
  423. RemoveFile("Fetch-Partial", QueueBack->DestFile);
  424. }
  425. }
  426. auto const Tmp = QueueBack;
  427. QueueBack = QueueBack->Next;
  428. SendReq(Tmp);
  429. ++CurrentDepth;
  430. } while (CurrentDepth <= AllowedDepth && QueueBack != nullptr);
  431. return true;
  432. }
  433. /*}}}*/
  434. // ServerMethod::Loop - Main loop /*{{{*/
  435. int ServerMethod::Loop()
  436. {
  437. typedef vector<string> StringVector;
  438. typedef vector<string>::iterator StringVectorIterator;
  439. map<string, StringVector> Redirected;
  440. signal(SIGTERM,SigTerm);
  441. signal(SIGINT,SigTerm);
  442. Server = 0;
  443. int FailCounter = 0;
  444. while (1)
  445. {
  446. // We have no commands, wait for some to arrive
  447. if (Queue == 0)
  448. {
  449. if (WaitFd(STDIN_FILENO) == false)
  450. return 0;
  451. }
  452. /* Run messages, we can accept 0 (no message) if we didn't
  453. do a WaitFd above.. Otherwise the FD is closed. */
  454. int Result = Run(true);
  455. if (Result != -1 && (Result != 0 || Queue == 0))
  456. {
  457. if(FailReason.empty() == false ||
  458. _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  459. return 100;
  460. else
  461. return 0;
  462. }
  463. if (Queue == 0)
  464. continue;
  465. // Connect to the server
  466. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  467. Server = CreateServerState(Queue->Uri);
  468. /* If the server has explicitly said this is the last connection
  469. then we pre-emptively shut down the pipeline and tear down
  470. the connection. This will speed up HTTP/1.0 servers a tad
  471. since we don't have to wait for the close sequence to
  472. complete */
  473. if (Server->Persistent == false)
  474. Server->Close();
  475. // Reset the pipeline
  476. if (Server->IsOpen() == false)
  477. QueueBack = Queue;
  478. // Connnect to the host
  479. if (Server->Open() == false)
  480. {
  481. Fail(true);
  482. Server = nullptr;
  483. continue;
  484. }
  485. // Fill the pipeline.
  486. Fetch(0);
  487. // Fetch the next URL header data from the server.
  488. switch (Server->RunHeaders(File, Queue->Uri))
  489. {
  490. case ServerState::RUN_HEADERS_OK:
  491. break;
  492. // The header data is bad
  493. case ServerState::RUN_HEADERS_PARSE_ERROR:
  494. {
  495. _error->Error(_("Bad header data"));
  496. Fail(true);
  497. Server->Close();
  498. RotateDNS();
  499. continue;
  500. }
  501. // The server closed a connection during the header get..
  502. default:
  503. case ServerState::RUN_HEADERS_IO_ERROR:
  504. {
  505. FailCounter++;
  506. _error->Discard();
  507. Server->Close();
  508. Server->Pipeline = false;
  509. Server->PipelineAllowed = false;
  510. if (FailCounter >= 2)
  511. {
  512. Fail(_("Connection failed"),true);
  513. FailCounter = 0;
  514. }
  515. RotateDNS();
  516. continue;
  517. }
  518. };
  519. // Decide what to do.
  520. FetchResult Res;
  521. Res.Filename = Queue->DestFile;
  522. switch (DealWithHeaders(Res))
  523. {
  524. // Ok, the file is Open
  525. case FILE_IS_OPEN:
  526. {
  527. URIStart(Res);
  528. // Run the data
  529. bool Result = true;
  530. // ensure we don't fetch too much
  531. // we could do "Server->MaximumSize = Queue->MaximumSize" here
  532. // but that would break the clever pipeline messup detection
  533. // so instead we use the size of the biggest item in the queue
  534. Server->MaximumSize = FindMaximumObjectSizeInQueue();
  535. if (Server->HaveContent)
  536. Result = Server->RunData(File);
  537. /* If the server is sending back sizeless responses then fill in
  538. the size now */
  539. if (Res.Size == 0)
  540. Res.Size = File->Size();
  541. // Close the file, destroy the FD object and timestamp it
  542. FailFd = -1;
  543. delete File;
  544. File = 0;
  545. // Timestamp
  546. struct timeval times[2];
  547. times[0].tv_sec = times[1].tv_sec = Server->Date;
  548. times[0].tv_usec = times[1].tv_usec = 0;
  549. utimes(Queue->DestFile.c_str(), times);
  550. // Send status to APT
  551. if (Result == true)
  552. {
  553. Hashes * const resultHashes = Server->GetHashes();
  554. HashStringList const hashList = resultHashes->GetHashStringList();
  555. if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
  556. {
  557. // we did not get the expected hash… mhhh:
  558. // could it be that server/proxy messed up pipelining?
  559. FetchItem * BeforeI = Queue;
  560. for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
  561. {
  562. if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
  563. {
  564. // yes, he did! Disable pipelining and rewrite queue
  565. if (Server->Pipeline == true)
  566. {
  567. Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
  568. Server->Pipeline = false;
  569. Server->PipelineAllowed = false;
  570. // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
  571. }
  572. Rename(Res.Filename, I->DestFile);
  573. Res.Filename = I->DestFile;
  574. BeforeI->Next = I->Next;
  575. I->Next = Queue;
  576. Queue = I;
  577. break;
  578. }
  579. BeforeI = I;
  580. }
  581. }
  582. Res.TakeHashes(*resultHashes);
  583. URIDone(Res);
  584. }
  585. else
  586. {
  587. if (Server->IsOpen() == false)
  588. {
  589. FailCounter++;
  590. _error->Discard();
  591. Server->Close();
  592. if (FailCounter >= 2)
  593. {
  594. Fail(_("Connection failed"),true);
  595. FailCounter = 0;
  596. }
  597. QueueBack = Queue;
  598. }
  599. else
  600. {
  601. Server->Close();
  602. Fail(true);
  603. }
  604. }
  605. break;
  606. }
  607. // IMS hit
  608. case IMS_HIT:
  609. {
  610. URIDone(Res);
  611. break;
  612. }
  613. // Hard server error, not found or something
  614. case ERROR_UNRECOVERABLE:
  615. {
  616. Fail();
  617. break;
  618. }
  619. // Hard internal error, kill the connection and fail
  620. case ERROR_NOT_FROM_SERVER:
  621. {
  622. delete File;
  623. File = 0;
  624. Fail();
  625. RotateDNS();
  626. Server->Close();
  627. break;
  628. }
  629. // We need to flush the data, the header is like a 404 w/ error text
  630. case ERROR_WITH_CONTENT_PAGE:
  631. {
  632. Fail();
  633. // Send to content to dev/null
  634. File = new FileFd("/dev/null",FileFd::WriteExists);
  635. Server->RunData(File);
  636. delete File;
  637. File = 0;
  638. break;
  639. }
  640. // Try again with a new URL
  641. case TRY_AGAIN_OR_REDIRECT:
  642. {
  643. // Clear rest of response if there is content
  644. if (Server->HaveContent)
  645. {
  646. File = new FileFd("/dev/null",FileFd::WriteExists);
  647. Server->RunData(File);
  648. delete File;
  649. File = 0;
  650. }
  651. /* Detect redirect loops. No more redirects are allowed
  652. after the same URI is seen twice in a queue item. */
  653. StringVector &R = Redirected[Queue->DestFile];
  654. bool StopRedirects = false;
  655. if (R.empty() == true)
  656. R.push_back(Queue->Uri);
  657. else if (R[0] == "STOP" || R.size() > 10)
  658. StopRedirects = true;
  659. else
  660. {
  661. for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
  662. if (Queue->Uri == *I)
  663. {
  664. R[0] = "STOP";
  665. break;
  666. }
  667. R.push_back(Queue->Uri);
  668. }
  669. if (StopRedirects == false)
  670. Redirect(NextURI);
  671. else
  672. Fail();
  673. break;
  674. }
  675. default:
  676. Fail(_("Internal error"));
  677. break;
  678. }
  679. FailCounter = 0;
  680. }
  681. return 0;
  682. }
  683. /*}}}*/
  684. unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
  685. {
  686. unsigned long long MaxSizeInQueue = 0;
  687. for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
  688. MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
  689. return MaxSizeInQueue;
  690. }
  691. /*}}}*/
  692. ServerMethod::ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
  693. aptMethod(Binary, Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
  694. AllowRedirect(false), Debug(false)
  695. {
  696. }
  697. /*}}}*/