server.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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) :
  213. DownloadSize(0), ServerName(Srv), TimeOut(120), Owner(Owner)
  214. {
  215. Reset();
  216. }
  217. /*}}}*/
  218. bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/
  219. {
  220. File.Truncate(StartPos);
  221. return GetHashes()->AddFD(File, StartPos);
  222. }
  223. /*}}}*/
  224. // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
  225. // ---------------------------------------------------------------------
  226. /* We look at the header data we got back from the server and decide what
  227. to do. Returns DealWithHeadersResult (see http.h for details).
  228. */
  229. ServerMethod::DealWithHeadersResult
  230. ServerMethod::DealWithHeaders(FetchResult &Res)
  231. {
  232. // Not Modified
  233. if (Server->Result == 304)
  234. {
  235. RemoveFile("server", Queue->DestFile);
  236. Res.IMSHit = true;
  237. Res.LastModified = Queue->LastModified;
  238. return IMS_HIT;
  239. }
  240. /* Redirect
  241. *
  242. * Note that it is only OK for us to treat all redirection the same
  243. * because we *always* use GET, not other HTTP methods. There are
  244. * three redirection codes for which it is not appropriate that we
  245. * redirect. Pass on those codes so the error handling kicks in.
  246. */
  247. if (AllowRedirect
  248. && (Server->Result > 300 && Server->Result < 400)
  249. && (Server->Result != 300 // Multiple Choices
  250. && Server->Result != 304 // Not Modified
  251. && Server->Result != 306)) // (Not part of HTTP/1.1, reserved)
  252. {
  253. if (Server->Location.empty() == true);
  254. else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
  255. {
  256. URI Uri = Queue->Uri;
  257. if (Uri.Host.empty() == false)
  258. NextURI = URI::SiteOnly(Uri);
  259. else
  260. NextURI.clear();
  261. NextURI.append(DeQuoteString(Server->Location));
  262. return TRY_AGAIN_OR_REDIRECT;
  263. }
  264. else
  265. {
  266. NextURI = DeQuoteString(Server->Location);
  267. URI tmpURI = NextURI;
  268. URI Uri = Queue->Uri;
  269. // same protocol redirects are okay
  270. if (tmpURI.Access == Uri.Access)
  271. return TRY_AGAIN_OR_REDIRECT;
  272. // as well as http to https
  273. else if (Uri.Access == "http" && tmpURI.Access == "https")
  274. return TRY_AGAIN_OR_REDIRECT;
  275. }
  276. /* else pass through for error message */
  277. }
  278. // retry after an invalid range response without partial data
  279. else if (Server->Result == 416)
  280. {
  281. struct stat SBuf;
  282. if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  283. {
  284. bool partialHit = false;
  285. if (Queue->ExpectedHashes.usable() == true)
  286. {
  287. Hashes resultHashes(Queue->ExpectedHashes);
  288. FileFd file(Queue->DestFile, FileFd::ReadOnly);
  289. Server->TotalFileSize = file.FileSize();
  290. Server->Date = file.ModificationTime();
  291. resultHashes.AddFD(file);
  292. HashStringList const hashList = resultHashes.GetHashStringList();
  293. partialHit = (Queue->ExpectedHashes == hashList);
  294. }
  295. else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
  296. partialHit = true;
  297. if (partialHit == true)
  298. {
  299. // the file is completely downloaded, but was not moved
  300. if (Server->HaveContent == true)
  301. {
  302. // Send to error page to dev/null
  303. FileFd DevNull("/dev/null",FileFd::WriteExists);
  304. Server->RunData(&DevNull);
  305. }
  306. Server->HaveContent = false;
  307. Server->StartPos = Server->TotalFileSize;
  308. Server->Result = 200;
  309. }
  310. else if (RemoveFile("server", Queue->DestFile))
  311. {
  312. NextURI = Queue->Uri;
  313. return TRY_AGAIN_OR_REDIRECT;
  314. }
  315. }
  316. }
  317. /* We have a reply we don't handle. This should indicate a perm server
  318. failure */
  319. if (Server->Result < 200 || Server->Result >= 300)
  320. {
  321. std::string err;
  322. strprintf(err, "HttpError%u", Server->Result);
  323. SetFailReason(err);
  324. _error->Error("%u %s", Server->Result, Server->Code);
  325. if (Server->HaveContent == true)
  326. return ERROR_WITH_CONTENT_PAGE;
  327. return ERROR_UNRECOVERABLE;
  328. }
  329. // This is some sort of 2xx 'data follows' reply
  330. Res.LastModified = Server->Date;
  331. Res.Size = Server->TotalFileSize;
  332. // Open the file
  333. delete File;
  334. File = new FileFd(Queue->DestFile,FileFd::WriteAny);
  335. if (_error->PendingError() == true)
  336. return ERROR_NOT_FROM_SERVER;
  337. FailFile = Queue->DestFile;
  338. FailFile.c_str(); // Make sure we don't do a malloc in the signal handler
  339. FailFd = File->Fd();
  340. FailTime = Server->Date;
  341. if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false)
  342. {
  343. _error->Errno("read",_("Problem hashing file"));
  344. return ERROR_NOT_FROM_SERVER;
  345. }
  346. if (Server->StartPos > 0)
  347. Res.ResumePoint = Server->StartPos;
  348. SetNonBlock(File->Fd(),true);
  349. return FILE_IS_OPEN;
  350. }
  351. /*}}}*/
  352. // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
  353. // ---------------------------------------------------------------------
  354. /* This closes and timestamps the open file. This is necessary to get
  355. resume behavoir on user abort */
  356. void ServerMethod::SigTerm(int)
  357. {
  358. if (FailFd == -1)
  359. _exit(100);
  360. struct timeval times[2];
  361. times[0].tv_sec = FailTime;
  362. times[1].tv_sec = FailTime;
  363. times[0].tv_usec = times[1].tv_usec = 0;
  364. utimes(FailFile.c_str(), times);
  365. close(FailFd);
  366. _exit(100);
  367. }
  368. /*}}}*/
  369. // ServerMethod::Fetch - Fetch an item /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  372. depth. */
  373. bool ServerMethod::Fetch(FetchItem *)
  374. {
  375. if (Server == 0)
  376. return true;
  377. // Queue the requests
  378. int Depth = -1;
  379. for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
  380. I = I->Next, Depth++)
  381. {
  382. if (Depth >= 0)
  383. {
  384. // If pipelining is disabled, we only queue 1 request
  385. if (Server->Pipeline == false)
  386. break;
  387. // if we have no hashes, do at most one such request
  388. // as we can't fixup pipeling misbehaviors otherwise
  389. else if (I->ExpectedHashes.usable() == false)
  390. break;
  391. }
  392. // Make sure we stick with the same server
  393. if (Server->Comp(I->Uri) == false)
  394. break;
  395. if (QueueBack == I)
  396. {
  397. QueueBack = I->Next;
  398. SendReq(I);
  399. continue;
  400. }
  401. }
  402. return true;
  403. }
  404. /*}}}*/
  405. // ServerMethod::Loop - Main loop /*{{{*/
  406. int ServerMethod::Loop()
  407. {
  408. typedef vector<string> StringVector;
  409. typedef vector<string>::iterator StringVectorIterator;
  410. map<string, StringVector> Redirected;
  411. signal(SIGTERM,SigTerm);
  412. signal(SIGINT,SigTerm);
  413. Server = 0;
  414. int FailCounter = 0;
  415. while (1)
  416. {
  417. // We have no commands, wait for some to arrive
  418. if (Queue == 0)
  419. {
  420. if (WaitFd(STDIN_FILENO) == false)
  421. return 0;
  422. }
  423. /* Run messages, we can accept 0 (no message) if we didn't
  424. do a WaitFd above.. Otherwise the FD is closed. */
  425. int Result = Run(true);
  426. if (Result != -1 && (Result != 0 || Queue == 0))
  427. {
  428. if(FailReason.empty() == false ||
  429. _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
  430. return 100;
  431. else
  432. return 0;
  433. }
  434. if (Queue == 0)
  435. continue;
  436. // Connect to the server
  437. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  438. Server = CreateServerState(Queue->Uri);
  439. /* If the server has explicitly said this is the last connection
  440. then we pre-emptively shut down the pipeline and tear down
  441. the connection. This will speed up HTTP/1.0 servers a tad
  442. since we don't have to wait for the close sequence to
  443. complete */
  444. if (Server->Persistent == false)
  445. Server->Close();
  446. // Reset the pipeline
  447. if (Server->IsOpen() == false)
  448. QueueBack = Queue;
  449. // Connnect to the host
  450. if (Server->Open() == false)
  451. {
  452. Fail(true);
  453. Server = nullptr;
  454. continue;
  455. }
  456. // Fill the pipeline.
  457. Fetch(0);
  458. // Fetch the next URL header data from the server.
  459. switch (Server->RunHeaders(File, Queue->Uri))
  460. {
  461. case ServerState::RUN_HEADERS_OK:
  462. break;
  463. // The header data is bad
  464. case ServerState::RUN_HEADERS_PARSE_ERROR:
  465. {
  466. _error->Error(_("Bad header data"));
  467. Fail(true);
  468. RotateDNS();
  469. continue;
  470. }
  471. // The server closed a connection during the header get..
  472. default:
  473. case ServerState::RUN_HEADERS_IO_ERROR:
  474. {
  475. FailCounter++;
  476. _error->Discard();
  477. Server->Close();
  478. Server->Pipeline = false;
  479. Server->PipelineAllowed = false;
  480. if (FailCounter >= 2)
  481. {
  482. Fail(_("Connection failed"),true);
  483. FailCounter = 0;
  484. }
  485. RotateDNS();
  486. continue;
  487. }
  488. };
  489. // Decide what to do.
  490. FetchResult Res;
  491. Res.Filename = Queue->DestFile;
  492. switch (DealWithHeaders(Res))
  493. {
  494. // Ok, the file is Open
  495. case FILE_IS_OPEN:
  496. {
  497. URIStart(Res);
  498. // Run the data
  499. bool Result = true;
  500. // ensure we don't fetch too much
  501. // we could do "Server->MaximumSize = Queue->MaximumSize" here
  502. // but that would break the clever pipeline messup detection
  503. // so instead we use the size of the biggest item in the queue
  504. Server->MaximumSize = FindMaximumObjectSizeInQueue();
  505. if (Server->HaveContent)
  506. Result = Server->RunData(File);
  507. /* If the server is sending back sizeless responses then fill in
  508. the size now */
  509. if (Res.Size == 0)
  510. Res.Size = File->Size();
  511. // Close the file, destroy the FD object and timestamp it
  512. FailFd = -1;
  513. delete File;
  514. File = 0;
  515. // Timestamp
  516. struct timeval times[2];
  517. times[0].tv_sec = times[1].tv_sec = Server->Date;
  518. times[0].tv_usec = times[1].tv_usec = 0;
  519. utimes(Queue->DestFile.c_str(), times);
  520. // Send status to APT
  521. if (Result == true)
  522. {
  523. Hashes * const resultHashes = Server->GetHashes();
  524. HashStringList const hashList = resultHashes->GetHashStringList();
  525. if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
  526. {
  527. // we did not get the expected hash… mhhh:
  528. // could it be that server/proxy messed up pipelining?
  529. FetchItem * BeforeI = Queue;
  530. for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
  531. {
  532. if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
  533. {
  534. // yes, he did! Disable pipelining and rewrite queue
  535. if (Server->Pipeline == true)
  536. {
  537. // FIXME: fake a warning message as we have no proper way of communicating here
  538. std::string out;
  539. strprintf(out, _("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::PipelineDepth");
  540. std::cerr << "W: " << out << std::endl;
  541. Server->Pipeline = false;
  542. Server->PipelineAllowed = false;
  543. // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
  544. }
  545. Rename(Res.Filename, I->DestFile);
  546. Res.Filename = I->DestFile;
  547. BeforeI->Next = I->Next;
  548. I->Next = Queue;
  549. Queue = I;
  550. break;
  551. }
  552. BeforeI = I;
  553. }
  554. }
  555. Res.TakeHashes(*resultHashes);
  556. URIDone(Res);
  557. }
  558. else
  559. {
  560. if (Server->IsOpen() == false)
  561. {
  562. FailCounter++;
  563. _error->Discard();
  564. Server->Close();
  565. if (FailCounter >= 2)
  566. {
  567. Fail(_("Connection failed"),true);
  568. FailCounter = 0;
  569. }
  570. QueueBack = Queue;
  571. }
  572. else
  573. {
  574. Server->Close();
  575. Fail(true);
  576. }
  577. }
  578. break;
  579. }
  580. // IMS hit
  581. case IMS_HIT:
  582. {
  583. URIDone(Res);
  584. break;
  585. }
  586. // Hard server error, not found or something
  587. case ERROR_UNRECOVERABLE:
  588. {
  589. Fail();
  590. break;
  591. }
  592. // Hard internal error, kill the connection and fail
  593. case ERROR_NOT_FROM_SERVER:
  594. {
  595. delete File;
  596. File = 0;
  597. Fail();
  598. RotateDNS();
  599. Server->Close();
  600. break;
  601. }
  602. // We need to flush the data, the header is like a 404 w/ error text
  603. case ERROR_WITH_CONTENT_PAGE:
  604. {
  605. Fail();
  606. // Send to content to dev/null
  607. File = new FileFd("/dev/null",FileFd::WriteExists);
  608. Server->RunData(File);
  609. delete File;
  610. File = 0;
  611. break;
  612. }
  613. // Try again with a new URL
  614. case TRY_AGAIN_OR_REDIRECT:
  615. {
  616. // Clear rest of response if there is content
  617. if (Server->HaveContent)
  618. {
  619. File = new FileFd("/dev/null",FileFd::WriteExists);
  620. Server->RunData(File);
  621. delete File;
  622. File = 0;
  623. }
  624. /* Detect redirect loops. No more redirects are allowed
  625. after the same URI is seen twice in a queue item. */
  626. StringVector &R = Redirected[Queue->DestFile];
  627. bool StopRedirects = false;
  628. if (R.empty() == true)
  629. R.push_back(Queue->Uri);
  630. else if (R[0] == "STOP" || R.size() > 10)
  631. StopRedirects = true;
  632. else
  633. {
  634. for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
  635. if (Queue->Uri == *I)
  636. {
  637. R[0] = "STOP";
  638. break;
  639. }
  640. R.push_back(Queue->Uri);
  641. }
  642. if (StopRedirects == false)
  643. Redirect(NextURI);
  644. else
  645. Fail();
  646. break;
  647. }
  648. default:
  649. Fail(_("Internal error"));
  650. break;
  651. }
  652. FailCounter = 0;
  653. }
  654. return 0;
  655. }
  656. /*}}}*/
  657. unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
  658. {
  659. unsigned long long MaxSizeInQueue = 0;
  660. for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
  661. MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
  662. return MaxSizeInQueue;
  663. }
  664. /*}}}*/
  665. ServerMethod::ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
  666. aptMethod(Binary, Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
  667. AllowRedirect(false), Debug(false)
  668. {
  669. }
  670. /*}}}*/