server.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. if (Line.size() > 4 && stringcasecmp(Line.data(), Line.data()+4, "HTTP") == 0)
  87. {
  88. // Evil servers return no version
  89. if (Line[4] == '/')
  90. {
  91. int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code);
  92. if (elements == 3)
  93. {
  94. Code[0] = '\0';
  95. if (Owner != NULL && Owner->Debug == true)
  96. clog << "HTTP server doesn't give Reason-Phrase for " << std::to_string(Result) << std::endl;
  97. }
  98. else if (elements != 4)
  99. return _error->Error(_("The HTTP server sent an invalid reply header"));
  100. }
  101. else
  102. {
  103. Major = 0;
  104. Minor = 9;
  105. if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2)
  106. return _error->Error(_("The HTTP server sent an invalid reply header"));
  107. }
  108. /* Check the HTTP response header to get the default persistence
  109. state. */
  110. if (Major < 1)
  111. Persistent = false;
  112. else
  113. {
  114. if (Major == 1 && Minor == 0)
  115. {
  116. Persistent = false;
  117. }
  118. else
  119. {
  120. Persistent = true;
  121. if (PipelineAllowed)
  122. Pipeline = true;
  123. }
  124. }
  125. return true;
  126. }
  127. // Blah, some servers use "connection:closes", evil.
  128. // and some even send empty header fields…
  129. string::size_type Pos = Line.find(':');
  130. if (Pos == string::npos)
  131. return _error->Error(_("Bad header line"));
  132. ++Pos;
  133. // Parse off any trailing spaces between the : and the next word.
  134. string::size_type Pos2 = Pos;
  135. while (Pos2 < Line.length() && isspace_ascii(Line[Pos2]) != 0)
  136. Pos2++;
  137. string const Tag(Line,0,Pos);
  138. string const Val(Line,Pos2);
  139. if (stringcasecmp(Tag,"Content-Length:") == 0)
  140. {
  141. if (Encoding == Closes)
  142. Encoding = Stream;
  143. HaveContent = true;
  144. unsigned long long * DownloadSizePtr = &DownloadSize;
  145. if (Result == 416 || (Result >= 300 && Result < 400))
  146. DownloadSizePtr = &JunkSize;
  147. *DownloadSizePtr = strtoull(Val.c_str(), NULL, 10);
  148. if (*DownloadSizePtr >= std::numeric_limits<unsigned long long>::max())
  149. return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
  150. else if (*DownloadSizePtr == 0)
  151. HaveContent = false;
  152. // On partial content (206) the Content-Length less than the real
  153. // size, so do not set it here but leave that to the Content-Range
  154. // header instead
  155. if(Result != 206 && TotalFileSize == 0)
  156. TotalFileSize = DownloadSize;
  157. return true;
  158. }
  159. if (stringcasecmp(Tag,"Content-Type:") == 0)
  160. {
  161. HaveContent = true;
  162. return true;
  163. }
  164. if (stringcasecmp(Tag,"Content-Range:") == 0)
  165. {
  166. HaveContent = true;
  167. // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
  168. if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&TotalFileSize) == 1)
  169. ; // we got the expected filesize which is all we wanted
  170. else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2)
  171. return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
  172. if ((unsigned long long)StartPos > TotalFileSize)
  173. return _error->Error(_("This HTTP server has broken range support"));
  174. // figure out what we will download
  175. DownloadSize = TotalFileSize - StartPos;
  176. return true;
  177. }
  178. if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
  179. {
  180. HaveContent = true;
  181. if (stringcasecmp(Val,"chunked") == 0)
  182. Encoding = Chunked;
  183. return true;
  184. }
  185. if (stringcasecmp(Tag,"Connection:") == 0)
  186. {
  187. if (stringcasecmp(Val,"close") == 0)
  188. Persistent = false;
  189. if (stringcasecmp(Val,"keep-alive") == 0)
  190. Persistent = true;
  191. return true;
  192. }
  193. if (stringcasecmp(Tag,"Last-Modified:") == 0)
  194. {
  195. if (RFC1123StrToTime(Val.c_str(), Date) == false)
  196. return _error->Error(_("Unknown date format"));
  197. return true;
  198. }
  199. if (stringcasecmp(Tag,"Location:") == 0)
  200. {
  201. Location = Val;
  202. return true;
  203. }
  204. return true;
  205. }
  206. /*}}}*/
  207. // ServerState::ServerState - Constructor /*{{{*/
  208. ServerState::ServerState(URI Srv, ServerMethod *Owner) :
  209. DownloadSize(0), ServerName(Srv), TimeOut(120), Owner(Owner)
  210. {
  211. Reset();
  212. }
  213. /*}}}*/
  214. bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/
  215. {
  216. File.Truncate(StartPos);
  217. return GetHashes()->AddFD(File, StartPos);
  218. }
  219. /*}}}*/
  220. // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
  221. // ---------------------------------------------------------------------
  222. /* We look at the header data we got back from the server and decide what
  223. to do. Returns DealWithHeadersResult (see http.h for details).
  224. */
  225. ServerMethod::DealWithHeadersResult
  226. ServerMethod::DealWithHeaders(FetchResult &Res)
  227. {
  228. // Not Modified
  229. if (Server->Result == 304)
  230. {
  231. RemoveFile("server", Queue->DestFile);
  232. Res.IMSHit = true;
  233. Res.LastModified = Queue->LastModified;
  234. Res.Size = 0;
  235. return IMS_HIT;
  236. }
  237. /* Redirect
  238. *
  239. * Note that it is only OK for us to treat all redirection the same
  240. * because we *always* use GET, not other HTTP methods. There are
  241. * three redirection codes for which it is not appropriate that we
  242. * redirect. Pass on those codes so the error handling kicks in.
  243. */
  244. if (AllowRedirect
  245. && (Server->Result > 300 && Server->Result < 400)
  246. && (Server->Result != 300 // Multiple Choices
  247. && Server->Result != 304 // Not Modified
  248. && Server->Result != 306)) // (Not part of HTTP/1.1, reserved)
  249. {
  250. if (Server->Location.empty() == true)
  251. ;
  252. else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
  253. {
  254. URI Uri = Queue->Uri;
  255. if (Uri.Host.empty() == false)
  256. NextURI = URI::SiteOnly(Uri);
  257. else
  258. NextURI.clear();
  259. NextURI.append(DeQuoteString(Server->Location));
  260. if (Queue->Uri == NextURI)
  261. {
  262. SetFailReason("RedirectionLoop");
  263. _error->Error("Redirection loop encountered");
  264. if (Server->HaveContent == true)
  265. return ERROR_WITH_CONTENT_PAGE;
  266. return ERROR_UNRECOVERABLE;
  267. }
  268. return TRY_AGAIN_OR_REDIRECT;
  269. }
  270. else
  271. {
  272. NextURI = DeQuoteString(Server->Location);
  273. URI tmpURI = NextURI;
  274. if (tmpURI.Access.find('+') != std::string::npos)
  275. {
  276. _error->Error("Server tried to trick us into using a specific implementation: %s", tmpURI.Access.c_str());
  277. if (Server->HaveContent == true)
  278. return ERROR_WITH_CONTENT_PAGE;
  279. return ERROR_UNRECOVERABLE;
  280. }
  281. URI Uri = Queue->Uri;
  282. if (Binary.find('+') != std::string::npos)
  283. {
  284. auto base = Binary.substr(0, Binary.find('+'));
  285. if (base != tmpURI.Access)
  286. {
  287. tmpURI.Access = base + '+' + tmpURI.Access;
  288. if (tmpURI.Access == Binary)
  289. {
  290. std::string tmpAccess = Uri.Access;
  291. std::swap(tmpURI.Access, Uri.Access);
  292. NextURI = tmpURI;
  293. std::swap(tmpURI.Access, Uri.Access);
  294. }
  295. else
  296. NextURI = tmpURI;
  297. }
  298. }
  299. if (Queue->Uri == NextURI)
  300. {
  301. SetFailReason("RedirectionLoop");
  302. _error->Error("Redirection loop encountered");
  303. if (Server->HaveContent == true)
  304. return ERROR_WITH_CONTENT_PAGE;
  305. return ERROR_UNRECOVERABLE;
  306. }
  307. Uri.Access = Binary;
  308. // same protocol redirects are okay
  309. if (tmpURI.Access == Uri.Access)
  310. return TRY_AGAIN_OR_REDIRECT;
  311. // as well as http to https
  312. else if ((Uri.Access == "http" || Uri.Access == "https+http") && tmpURI.Access == "https")
  313. return TRY_AGAIN_OR_REDIRECT;
  314. else
  315. {
  316. auto const tmpplus = tmpURI.Access.find('+');
  317. if (tmpplus != std::string::npos && tmpURI.Access.substr(tmpplus + 1) == "https")
  318. {
  319. auto const uriplus = Uri.Access.find('+');
  320. if (uriplus == std::string::npos)
  321. {
  322. if (Uri.Access == tmpURI.Access.substr(0, tmpplus)) // foo -> foo+https
  323. return TRY_AGAIN_OR_REDIRECT;
  324. }
  325. else if (Uri.Access.substr(uriplus + 1) == "http" &&
  326. Uri.Access.substr(0, uriplus) == tmpURI.Access.substr(0, tmpplus)) // foo+http -> foo+https
  327. return TRY_AGAIN_OR_REDIRECT;
  328. }
  329. }
  330. _error->Error("Redirection from %s to '%s' is forbidden", Uri.Access.c_str(), NextURI.c_str());
  331. }
  332. /* else pass through for error message */
  333. }
  334. // retry after an invalid range response without partial data
  335. else if (Server->Result == 416)
  336. {
  337. struct stat SBuf;
  338. if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  339. {
  340. bool partialHit = false;
  341. if (Queue->ExpectedHashes.usable() == true)
  342. {
  343. Hashes resultHashes(Queue->ExpectedHashes);
  344. FileFd file(Queue->DestFile, FileFd::ReadOnly);
  345. Server->TotalFileSize = file.FileSize();
  346. Server->Date = file.ModificationTime();
  347. resultHashes.AddFD(file);
  348. HashStringList const hashList = resultHashes.GetHashStringList();
  349. partialHit = (Queue->ExpectedHashes == hashList);
  350. }
  351. else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
  352. partialHit = true;
  353. if (partialHit == true)
  354. {
  355. // the file is completely downloaded, but was not moved
  356. if (Server->HaveContent == true)
  357. {
  358. // nuke the sent error page
  359. Server->RunDataToDevNull();
  360. Server->HaveContent = false;
  361. }
  362. Server->StartPos = Server->TotalFileSize;
  363. Server->Result = 200;
  364. }
  365. else if (RemoveFile("server", Queue->DestFile))
  366. {
  367. NextURI = Queue->Uri;
  368. return TRY_AGAIN_OR_REDIRECT;
  369. }
  370. }
  371. }
  372. /* We have a reply we don't handle. This should indicate a perm server
  373. failure */
  374. if (Server->Result < 200 || Server->Result >= 300)
  375. {
  376. if (_error->PendingError() == false)
  377. {
  378. std::string err;
  379. strprintf(err, "HttpError%u", Server->Result);
  380. SetFailReason(err);
  381. _error->Error("%u %s", Server->Result, Server->Code);
  382. }
  383. if (Server->HaveContent == true)
  384. return ERROR_WITH_CONTENT_PAGE;
  385. return ERROR_UNRECOVERABLE;
  386. }
  387. // This is some sort of 2xx 'data follows' reply
  388. Res.LastModified = Server->Date;
  389. Res.Size = Server->TotalFileSize;
  390. return FILE_IS_OPEN;
  391. }
  392. /*}}}*/
  393. // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* This closes and timestamps the open file. This is necessary to get
  396. resume behavoir on user abort */
  397. void ServerMethod::SigTerm(int)
  398. {
  399. if (FailFd == -1)
  400. _exit(100);
  401. struct timeval times[2];
  402. times[0].tv_sec = FailTime;
  403. times[1].tv_sec = FailTime;
  404. times[0].tv_usec = times[1].tv_usec = 0;
  405. utimes(FailFile.c_str(), times);
  406. close(FailFd);
  407. _exit(100);
  408. }
  409. /*}}}*/
  410. // ServerMethod::Fetch - Fetch an item /*{{{*/
  411. // ---------------------------------------------------------------------
  412. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  413. depth. */
  414. bool ServerMethod::Fetch(FetchItem *)
  415. {
  416. if (Server == nullptr || QueueBack == nullptr)
  417. return true;
  418. // If pipelining is disabled, we only queue 1 request
  419. auto const AllowedDepth = Server->Pipeline ? PipelineDepth : 0;
  420. // how deep is our pipeline currently?
  421. decltype(PipelineDepth) CurrentDepth = 0;
  422. for (FetchItem const *I = Queue; I != QueueBack; I = I->Next)
  423. ++CurrentDepth;
  424. if (CurrentDepth > AllowedDepth)
  425. return true;
  426. do {
  427. // Make sure we stick with the same server
  428. if (Server->Comp(QueueBack->Uri) == false)
  429. break;
  430. bool const UsableHashes = QueueBack->ExpectedHashes.usable();
  431. // if we have no hashes, do at most one such request
  432. // as we can't fixup pipeling misbehaviors otherwise
  433. if (CurrentDepth != 0 && UsableHashes == false)
  434. break;
  435. if (UsableHashes && FileExists(QueueBack->DestFile))
  436. {
  437. FileFd partial(QueueBack->DestFile, FileFd::ReadOnly);
  438. Hashes wehave(QueueBack->ExpectedHashes);
  439. if (QueueBack->ExpectedHashes.FileSize() == partial.FileSize())
  440. {
  441. if (wehave.AddFD(partial) &&
  442. wehave.GetHashStringList() == QueueBack->ExpectedHashes)
  443. {
  444. FetchResult Res;
  445. Res.Filename = QueueBack->DestFile;
  446. Res.ResumePoint = QueueBack->ExpectedHashes.FileSize();
  447. URIStart(Res);
  448. // move item to the start of the queue as URIDone will
  449. // always dequeued the first item in the queue
  450. if (Queue != QueueBack)
  451. {
  452. FetchItem *Prev = Queue;
  453. for (; Prev->Next != QueueBack; Prev = Prev->Next)
  454. /* look for the previous queue item */;
  455. Prev->Next = QueueBack->Next;
  456. QueueBack->Next = Queue;
  457. Queue = QueueBack;
  458. QueueBack = Prev->Next;
  459. }
  460. Res.TakeHashes(wehave);
  461. URIDone(Res);
  462. continue;
  463. }
  464. else
  465. RemoveFile("Fetch-Partial", QueueBack->DestFile);
  466. }
  467. }
  468. auto const Tmp = QueueBack;
  469. QueueBack = QueueBack->Next;
  470. SendReq(Tmp);
  471. ++CurrentDepth;
  472. } while (CurrentDepth <= AllowedDepth && QueueBack != nullptr);
  473. return true;
  474. }
  475. /*}}}*/
  476. // ServerMethod::Loop - Main loop /*{{{*/
  477. int ServerMethod::Loop()
  478. {
  479. signal(SIGTERM,SigTerm);
  480. signal(SIGINT,SigTerm);
  481. Server = 0;
  482. int FailCounter = 0;
  483. while (1)
  484. {
  485. // We have no commands, wait for some to arrive
  486. if (Queue == 0)
  487. {
  488. if (WaitFd(STDIN_FILENO) == false)
  489. return 0;
  490. }
  491. /* Run messages, we can accept 0 (no message) if we didn't
  492. do a WaitFd above.. Otherwise the FD is closed. */
  493. int Result = Run(true);
  494. if (Result != -1 && (Result != 0 || Queue == 0))
  495. {
  496. if(FailReason.empty() == false ||
  497. ConfigFindB("DependOnSTDIN", true) == true)
  498. return 100;
  499. else
  500. return 0;
  501. }
  502. if (Queue == 0)
  503. continue;
  504. // Connect to the server
  505. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  506. {
  507. Server = CreateServerState(Queue->Uri);
  508. setPostfixForMethodNames(::URI(Queue->Uri).Host.c_str());
  509. AllowRedirect = ConfigFindB("AllowRedirect", true);
  510. PipelineDepth = ConfigFindI("Pipeline-Depth", 10);
  511. Debug = DebugEnabled();
  512. }
  513. /* If the server has explicitly said this is the last connection
  514. then we pre-emptively shut down the pipeline and tear down
  515. the connection. This will speed up HTTP/1.0 servers a tad
  516. since we don't have to wait for the close sequence to
  517. complete */
  518. if (Server->Persistent == false)
  519. Server->Close();
  520. // Reset the pipeline
  521. if (Server->IsOpen() == false)
  522. QueueBack = Queue;
  523. // Connnect to the host
  524. if (Server->Open() == false)
  525. {
  526. Fail(true);
  527. Server = nullptr;
  528. continue;
  529. }
  530. // Fill the pipeline.
  531. Fetch(0);
  532. // Fetch the next URL header data from the server.
  533. switch (Server->RunHeaders(File, Queue->Uri))
  534. {
  535. case ServerState::RUN_HEADERS_OK:
  536. break;
  537. // The header data is bad
  538. case ServerState::RUN_HEADERS_PARSE_ERROR:
  539. {
  540. _error->Error(_("Bad header data"));
  541. Fail(true);
  542. Server->Close();
  543. RotateDNS();
  544. continue;
  545. }
  546. // The server closed a connection during the header get..
  547. default:
  548. case ServerState::RUN_HEADERS_IO_ERROR:
  549. {
  550. FailCounter++;
  551. _error->Discard();
  552. Server->Close();
  553. Server->Pipeline = false;
  554. Server->PipelineAllowed = false;
  555. if (FailCounter >= 2)
  556. {
  557. Fail(_("Connection failed"),true);
  558. FailCounter = 0;
  559. }
  560. RotateDNS();
  561. continue;
  562. }
  563. };
  564. // Decide what to do.
  565. FetchResult Res;
  566. Res.Filename = Queue->DestFile;
  567. switch (DealWithHeaders(Res))
  568. {
  569. // Ok, the file is Open
  570. case FILE_IS_OPEN:
  571. {
  572. URIStart(Res);
  573. // Run the data
  574. bool Result = true;
  575. // ensure we don't fetch too much
  576. // we could do "Server->MaximumSize = Queue->MaximumSize" here
  577. // but that would break the clever pipeline messup detection
  578. // so instead we use the size of the biggest item in the queue
  579. Server->MaximumSize = FindMaximumObjectSizeInQueue();
  580. if (Server->HaveContent)
  581. Result = Server->RunData(File);
  582. /* If the server is sending back sizeless responses then fill in
  583. the size now */
  584. if (Res.Size == 0)
  585. Res.Size = File->Size();
  586. // Close the file, destroy the FD object and timestamp it
  587. FailFd = -1;
  588. delete File;
  589. File = 0;
  590. // Timestamp
  591. struct timeval times[2];
  592. times[0].tv_sec = times[1].tv_sec = Server->Date;
  593. times[0].tv_usec = times[1].tv_usec = 0;
  594. utimes(Queue->DestFile.c_str(), times);
  595. // Send status to APT
  596. if (Result == true)
  597. {
  598. Hashes * const resultHashes = Server->GetHashes();
  599. HashStringList const hashList = resultHashes->GetHashStringList();
  600. if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
  601. {
  602. // we did not get the expected hash… mhhh:
  603. // could it be that server/proxy messed up pipelining?
  604. FetchItem * BeforeI = Queue;
  605. for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
  606. {
  607. if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
  608. {
  609. // yes, he did! Disable pipelining and rewrite queue
  610. if (Server->Pipeline == true)
  611. {
  612. Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
  613. Server->Pipeline = false;
  614. Server->PipelineAllowed = false;
  615. // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
  616. }
  617. Rename(Res.Filename, I->DestFile);
  618. Res.Filename = I->DestFile;
  619. BeforeI->Next = I->Next;
  620. I->Next = Queue;
  621. Queue = I;
  622. break;
  623. }
  624. BeforeI = I;
  625. }
  626. }
  627. Res.TakeHashes(*resultHashes);
  628. URIDone(Res);
  629. }
  630. else
  631. {
  632. if (Server->IsOpen() == false)
  633. {
  634. FailCounter++;
  635. _error->Discard();
  636. Server->Close();
  637. if (FailCounter >= 2)
  638. {
  639. Fail(_("Connection failed"),true);
  640. FailCounter = 0;
  641. }
  642. QueueBack = Queue;
  643. }
  644. else
  645. {
  646. Server->Close();
  647. Fail(true);
  648. }
  649. }
  650. break;
  651. }
  652. // IMS hit
  653. case IMS_HIT:
  654. {
  655. URIDone(Res);
  656. break;
  657. }
  658. // Hard server error, not found or something
  659. case ERROR_UNRECOVERABLE:
  660. {
  661. Fail();
  662. break;
  663. }
  664. // Hard internal error, kill the connection and fail
  665. case ERROR_NOT_FROM_SERVER:
  666. {
  667. delete File;
  668. File = 0;
  669. Fail();
  670. RotateDNS();
  671. Server->Close();
  672. break;
  673. }
  674. // We need to flush the data, the header is like a 404 w/ error text
  675. case ERROR_WITH_CONTENT_PAGE:
  676. {
  677. Fail();
  678. Server->RunDataToDevNull();
  679. break;
  680. }
  681. // Try again with a new URL
  682. case TRY_AGAIN_OR_REDIRECT:
  683. {
  684. // Clear rest of response if there is content
  685. if (Server->HaveContent)
  686. Server->RunDataToDevNull();
  687. Redirect(NextURI);
  688. break;
  689. }
  690. default:
  691. Fail(_("Internal error"));
  692. break;
  693. }
  694. FailCounter = 0;
  695. }
  696. return 0;
  697. }
  698. /*}}}*/
  699. unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
  700. {
  701. unsigned long long MaxSizeInQueue = 0;
  702. for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
  703. MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
  704. return MaxSizeInQueue;
  705. }
  706. /*}}}*/
  707. ServerMethod::ServerMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
  708. aptMethod(std::move(Binary), Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
  709. AllowRedirect(false), Debug(false)
  710. {
  711. }
  712. /*}}}*/
  713. bool ServerMethod::Configuration(std::string Message) /*{{{*/
  714. {
  715. if (aptMethod::Configuration(Message) == false)
  716. return false;
  717. _config->CndSet("Acquire::tor::Proxy",
  718. "socks5h://apt-transport-tor@localhost:9050");
  719. return true;
  720. }
  721. /*}}}*/
  722. bool ServerMethod::AddProxyAuth(URI &Proxy, URI const &Server) const /*{{{*/
  723. {
  724. if (std::find(methodNames.begin(), methodNames.end(), "tor") != methodNames.end() &&
  725. Proxy.User == "apt-transport-tor" && Proxy.Password.empty())
  726. {
  727. std::string pass = Server.Host;
  728. pass.erase(std::remove_if(pass.begin(), pass.end(), [](char const c) { return std::isalnum(c) == 0; }), pass.end());
  729. if (pass.length() > 255)
  730. Proxy.Password = pass.substr(0, 255);
  731. else
  732. Proxy.Password = std::move(pass);
  733. }
  734. // FIXME: should we support auth.conf for proxies?
  735. return true;
  736. }
  737. /*}}}*/