server.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 || (Result >= 300 && Result < 400))
  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. Res.Size = 0;
  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. ;
  255. else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
  256. {
  257. URI Uri = Queue->Uri;
  258. if (Uri.Host.empty() == false)
  259. NextURI = URI::SiteOnly(Uri);
  260. else
  261. NextURI.clear();
  262. NextURI.append(DeQuoteString(Server->Location));
  263. if (Queue->Uri == NextURI)
  264. {
  265. SetFailReason("RedirectionLoop");
  266. _error->Error("Redirection loop encountered");
  267. if (Server->HaveContent == true)
  268. return ERROR_WITH_CONTENT_PAGE;
  269. return ERROR_UNRECOVERABLE;
  270. }
  271. return TRY_AGAIN_OR_REDIRECT;
  272. }
  273. else
  274. {
  275. NextURI = DeQuoteString(Server->Location);
  276. URI tmpURI = NextURI;
  277. if (tmpURI.Access.find('+') != std::string::npos)
  278. {
  279. _error->Error("Server tried to trick us into using a specific implementation: %s", tmpURI.Access.c_str());
  280. if (Server->HaveContent == true)
  281. return ERROR_WITH_CONTENT_PAGE;
  282. return ERROR_UNRECOVERABLE;
  283. }
  284. URI Uri = Queue->Uri;
  285. if (Binary.find('+') != std::string::npos)
  286. {
  287. auto base = Binary.substr(0, Binary.find('+'));
  288. if (base != tmpURI.Access)
  289. {
  290. tmpURI.Access = base + '+' + tmpURI.Access;
  291. if (tmpURI.Access == Binary)
  292. {
  293. std::string tmpAccess = Uri.Access;
  294. std::swap(tmpURI.Access, Uri.Access);
  295. NextURI = tmpURI;
  296. std::swap(tmpURI.Access, Uri.Access);
  297. }
  298. else
  299. NextURI = tmpURI;
  300. }
  301. }
  302. if (Queue->Uri == NextURI)
  303. {
  304. SetFailReason("RedirectionLoop");
  305. _error->Error("Redirection loop encountered");
  306. if (Server->HaveContent == true)
  307. return ERROR_WITH_CONTENT_PAGE;
  308. return ERROR_UNRECOVERABLE;
  309. }
  310. Uri.Access = Binary;
  311. // same protocol redirects are okay
  312. if (tmpURI.Access == Uri.Access)
  313. return TRY_AGAIN_OR_REDIRECT;
  314. // as well as http to https
  315. else if ((Uri.Access == "http" || Uri.Access == "https+http") && tmpURI.Access == "https")
  316. return TRY_AGAIN_OR_REDIRECT;
  317. else
  318. {
  319. auto const tmpplus = tmpURI.Access.find('+');
  320. if (tmpplus != std::string::npos && tmpURI.Access.substr(tmpplus + 1) == "https")
  321. {
  322. auto const uriplus = Uri.Access.find('+');
  323. if (uriplus == std::string::npos)
  324. {
  325. if (Uri.Access == tmpURI.Access.substr(0, tmpplus)) // foo -> foo+https
  326. return TRY_AGAIN_OR_REDIRECT;
  327. }
  328. else if (Uri.Access.substr(uriplus + 1) == "http" &&
  329. Uri.Access.substr(0, uriplus) == tmpURI.Access.substr(0, tmpplus)) // foo+http -> foo+https
  330. return TRY_AGAIN_OR_REDIRECT;
  331. }
  332. }
  333. _error->Error("Redirection from %s to '%s' is forbidden", Uri.Access.c_str(), NextURI.c_str());
  334. }
  335. /* else pass through for error message */
  336. }
  337. // retry after an invalid range response without partial data
  338. else if (Server->Result == 416)
  339. {
  340. struct stat SBuf;
  341. if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
  342. {
  343. bool partialHit = false;
  344. if (Queue->ExpectedHashes.usable() == true)
  345. {
  346. Hashes resultHashes(Queue->ExpectedHashes);
  347. FileFd file(Queue->DestFile, FileFd::ReadOnly);
  348. Server->TotalFileSize = file.FileSize();
  349. Server->Date = file.ModificationTime();
  350. resultHashes.AddFD(file);
  351. HashStringList const hashList = resultHashes.GetHashStringList();
  352. partialHit = (Queue->ExpectedHashes == hashList);
  353. }
  354. else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
  355. partialHit = true;
  356. if (partialHit == true)
  357. {
  358. // the file is completely downloaded, but was not moved
  359. if (Server->HaveContent == true)
  360. {
  361. // nuke the sent error page
  362. Server->RunDataToDevNull();
  363. Server->HaveContent = false;
  364. }
  365. Server->StartPos = Server->TotalFileSize;
  366. Server->Result = 200;
  367. }
  368. else if (RemoveFile("server", Queue->DestFile))
  369. {
  370. NextURI = Queue->Uri;
  371. return TRY_AGAIN_OR_REDIRECT;
  372. }
  373. }
  374. }
  375. /* We have a reply we don't handle. This should indicate a perm server
  376. failure */
  377. if (Server->Result < 200 || Server->Result >= 300)
  378. {
  379. if (_error->PendingError() == false)
  380. {
  381. std::string err;
  382. strprintf(err, "HttpError%u", Server->Result);
  383. SetFailReason(err);
  384. _error->Error("%u %s", Server->Result, Server->Code);
  385. }
  386. if (Server->HaveContent == true)
  387. return ERROR_WITH_CONTENT_PAGE;
  388. return ERROR_UNRECOVERABLE;
  389. }
  390. // This is some sort of 2xx 'data follows' reply
  391. Res.LastModified = Server->Date;
  392. Res.Size = Server->TotalFileSize;
  393. return FILE_IS_OPEN;
  394. }
  395. /*}}}*/
  396. // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* This closes and timestamps the open file. This is necessary to get
  399. resume behavoir on user abort */
  400. void ServerMethod::SigTerm(int)
  401. {
  402. if (FailFd == -1)
  403. _exit(100);
  404. struct timeval times[2];
  405. times[0].tv_sec = FailTime;
  406. times[1].tv_sec = FailTime;
  407. times[0].tv_usec = times[1].tv_usec = 0;
  408. utimes(FailFile.c_str(), times);
  409. close(FailFd);
  410. _exit(100);
  411. }
  412. /*}}}*/
  413. // ServerMethod::Fetch - Fetch an item /*{{{*/
  414. // ---------------------------------------------------------------------
  415. /* This adds an item to the pipeline. We keep the pipeline at a fixed
  416. depth. */
  417. bool ServerMethod::Fetch(FetchItem *)
  418. {
  419. if (Server == nullptr || QueueBack == nullptr)
  420. return true;
  421. // If pipelining is disabled, we only queue 1 request
  422. auto const AllowedDepth = Server->Pipeline ? PipelineDepth : 0;
  423. // how deep is our pipeline currently?
  424. decltype(PipelineDepth) CurrentDepth = 0;
  425. for (FetchItem const *I = Queue; I != QueueBack; I = I->Next)
  426. ++CurrentDepth;
  427. if (CurrentDepth > AllowedDepth)
  428. return true;
  429. do {
  430. // Make sure we stick with the same server
  431. if (Server->Comp(QueueBack->Uri) == false)
  432. break;
  433. bool const UsableHashes = QueueBack->ExpectedHashes.usable();
  434. // if we have no hashes, do at most one such request
  435. // as we can't fixup pipeling misbehaviors otherwise
  436. if (CurrentDepth != 0 && UsableHashes == false)
  437. break;
  438. if (UsableHashes && FileExists(QueueBack->DestFile))
  439. {
  440. FileFd partial(QueueBack->DestFile, FileFd::ReadOnly);
  441. Hashes wehave(QueueBack->ExpectedHashes);
  442. if (QueueBack->ExpectedHashes.FileSize() == partial.FileSize())
  443. {
  444. if (wehave.AddFD(partial) &&
  445. wehave.GetHashStringList() == QueueBack->ExpectedHashes)
  446. {
  447. FetchResult Res;
  448. Res.Filename = QueueBack->DestFile;
  449. Res.ResumePoint = QueueBack->ExpectedHashes.FileSize();
  450. URIStart(Res);
  451. // move item to the start of the queue as URIDone will
  452. // always dequeued the first item in the queue
  453. if (Queue != QueueBack)
  454. {
  455. FetchItem *Prev = Queue;
  456. for (; Prev->Next != QueueBack; Prev = Prev->Next)
  457. /* look for the previous queue item */;
  458. Prev->Next = QueueBack->Next;
  459. QueueBack->Next = Queue;
  460. Queue = QueueBack;
  461. QueueBack = Prev->Next;
  462. }
  463. Res.TakeHashes(wehave);
  464. URIDone(Res);
  465. continue;
  466. }
  467. else
  468. RemoveFile("Fetch-Partial", QueueBack->DestFile);
  469. }
  470. }
  471. auto const Tmp = QueueBack;
  472. QueueBack = QueueBack->Next;
  473. SendReq(Tmp);
  474. ++CurrentDepth;
  475. } while (CurrentDepth <= AllowedDepth && QueueBack != nullptr);
  476. return true;
  477. }
  478. /*}}}*/
  479. // ServerMethod::Loop - Main loop /*{{{*/
  480. int ServerMethod::Loop()
  481. {
  482. signal(SIGTERM,SigTerm);
  483. signal(SIGINT,SigTerm);
  484. Server = 0;
  485. int FailCounter = 0;
  486. while (1)
  487. {
  488. // We have no commands, wait for some to arrive
  489. if (Queue == 0)
  490. {
  491. if (WaitFd(STDIN_FILENO) == false)
  492. return 0;
  493. }
  494. /* Run messages, we can accept 0 (no message) if we didn't
  495. do a WaitFd above.. Otherwise the FD is closed. */
  496. int Result = Run(true);
  497. if (Result != -1 && (Result != 0 || Queue == 0))
  498. {
  499. if(FailReason.empty() == false ||
  500. ConfigFindB("DependOnSTDIN", true) == true)
  501. return 100;
  502. else
  503. return 0;
  504. }
  505. if (Queue == 0)
  506. continue;
  507. // Connect to the server
  508. if (Server == 0 || Server->Comp(Queue->Uri) == false)
  509. {
  510. Server = CreateServerState(Queue->Uri);
  511. setPostfixForMethodNames(::URI(Queue->Uri).Host.c_str());
  512. AllowRedirect = ConfigFindB("AllowRedirect", true);
  513. PipelineDepth = ConfigFindI("Pipeline-Depth", 10);
  514. Debug = DebugEnabled();
  515. }
  516. /* If the server has explicitly said this is the last connection
  517. then we pre-emptively shut down the pipeline and tear down
  518. the connection. This will speed up HTTP/1.0 servers a tad
  519. since we don't have to wait for the close sequence to
  520. complete */
  521. if (Server->Persistent == false)
  522. Server->Close();
  523. // Reset the pipeline
  524. if (Server->IsOpen() == false)
  525. QueueBack = Queue;
  526. // Connnect to the host
  527. if (Server->Open() == false)
  528. {
  529. Fail(true);
  530. Server = nullptr;
  531. continue;
  532. }
  533. // Fill the pipeline.
  534. Fetch(0);
  535. // Fetch the next URL header data from the server.
  536. switch (Server->RunHeaders(File, Queue->Uri))
  537. {
  538. case ServerState::RUN_HEADERS_OK:
  539. break;
  540. // The header data is bad
  541. case ServerState::RUN_HEADERS_PARSE_ERROR:
  542. {
  543. _error->Error(_("Bad header data"));
  544. Fail(true);
  545. Server->Close();
  546. RotateDNS();
  547. continue;
  548. }
  549. // The server closed a connection during the header get..
  550. default:
  551. case ServerState::RUN_HEADERS_IO_ERROR:
  552. {
  553. FailCounter++;
  554. _error->Discard();
  555. Server->Close();
  556. Server->Pipeline = false;
  557. Server->PipelineAllowed = false;
  558. if (FailCounter >= 2)
  559. {
  560. Fail(_("Connection failed"),true);
  561. FailCounter = 0;
  562. }
  563. RotateDNS();
  564. continue;
  565. }
  566. };
  567. // Decide what to do.
  568. FetchResult Res;
  569. Res.Filename = Queue->DestFile;
  570. switch (DealWithHeaders(Res))
  571. {
  572. // Ok, the file is Open
  573. case FILE_IS_OPEN:
  574. {
  575. URIStart(Res);
  576. // Run the data
  577. bool Result = true;
  578. // ensure we don't fetch too much
  579. // we could do "Server->MaximumSize = Queue->MaximumSize" here
  580. // but that would break the clever pipeline messup detection
  581. // so instead we use the size of the biggest item in the queue
  582. Server->MaximumSize = FindMaximumObjectSizeInQueue();
  583. if (Server->HaveContent)
  584. Result = Server->RunData(File);
  585. /* If the server is sending back sizeless responses then fill in
  586. the size now */
  587. if (Res.Size == 0)
  588. Res.Size = File->Size();
  589. // Close the file, destroy the FD object and timestamp it
  590. FailFd = -1;
  591. delete File;
  592. File = 0;
  593. // Timestamp
  594. struct timeval times[2];
  595. times[0].tv_sec = times[1].tv_sec = Server->Date;
  596. times[0].tv_usec = times[1].tv_usec = 0;
  597. utimes(Queue->DestFile.c_str(), times);
  598. // Send status to APT
  599. if (Result == true)
  600. {
  601. Hashes * const resultHashes = Server->GetHashes();
  602. HashStringList const hashList = resultHashes->GetHashStringList();
  603. if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
  604. {
  605. // we did not get the expected hash… mhhh:
  606. // could it be that server/proxy messed up pipelining?
  607. FetchItem * BeforeI = Queue;
  608. for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
  609. {
  610. if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
  611. {
  612. // yes, he did! Disable pipelining and rewrite queue
  613. if (Server->Pipeline == true)
  614. {
  615. Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
  616. Server->Pipeline = false;
  617. Server->PipelineAllowed = false;
  618. // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
  619. }
  620. Rename(Res.Filename, I->DestFile);
  621. Res.Filename = I->DestFile;
  622. BeforeI->Next = I->Next;
  623. I->Next = Queue;
  624. Queue = I;
  625. break;
  626. }
  627. BeforeI = I;
  628. }
  629. }
  630. Res.TakeHashes(*resultHashes);
  631. URIDone(Res);
  632. }
  633. else
  634. {
  635. if (Server->IsOpen() == false)
  636. {
  637. FailCounter++;
  638. _error->Discard();
  639. Server->Close();
  640. if (FailCounter >= 2)
  641. {
  642. Fail(_("Connection failed"),true);
  643. FailCounter = 0;
  644. }
  645. QueueBack = Queue;
  646. }
  647. else
  648. {
  649. Server->Close();
  650. Fail(true);
  651. }
  652. }
  653. break;
  654. }
  655. // IMS hit
  656. case IMS_HIT:
  657. {
  658. URIDone(Res);
  659. break;
  660. }
  661. // Hard server error, not found or something
  662. case ERROR_UNRECOVERABLE:
  663. {
  664. Fail();
  665. break;
  666. }
  667. // Hard internal error, kill the connection and fail
  668. case ERROR_NOT_FROM_SERVER:
  669. {
  670. delete File;
  671. File = 0;
  672. Fail();
  673. RotateDNS();
  674. Server->Close();
  675. break;
  676. }
  677. // We need to flush the data, the header is like a 404 w/ error text
  678. case ERROR_WITH_CONTENT_PAGE:
  679. {
  680. Fail();
  681. Server->RunDataToDevNull();
  682. break;
  683. }
  684. // Try again with a new URL
  685. case TRY_AGAIN_OR_REDIRECT:
  686. {
  687. // Clear rest of response if there is content
  688. if (Server->HaveContent)
  689. Server->RunDataToDevNull();
  690. Redirect(NextURI);
  691. break;
  692. }
  693. default:
  694. Fail(_("Internal error"));
  695. break;
  696. }
  697. FailCounter = 0;
  698. }
  699. return 0;
  700. }
  701. /*}}}*/
  702. unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
  703. {
  704. unsigned long long MaxSizeInQueue = 0;
  705. for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
  706. MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
  707. return MaxSizeInQueue;
  708. }
  709. /*}}}*/
  710. ServerMethod::ServerMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
  711. aptMethod(std::move(Binary), Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
  712. AllowRedirect(false), Debug(false)
  713. {
  714. }
  715. /*}}}*/
  716. bool ServerMethod::Configuration(std::string Message) /*{{{*/
  717. {
  718. if (aptMethod::Configuration(Message) == false)
  719. return false;
  720. _config->CndSet("Acquire::tor::Proxy",
  721. "socks5h://apt-transport-tor@localhost:9050");
  722. return true;
  723. }
  724. /*}}}*/
  725. bool ServerMethod::AddProxyAuth(URI &Proxy, URI const &Server) const /*{{{*/
  726. {
  727. if (std::find(methodNames.begin(), methodNames.end(), "tor") != methodNames.end() &&
  728. Proxy.User == "apt-transport-tor" && Proxy.Password.empty())
  729. {
  730. std::string pass = Server.Host;
  731. pass.erase(std::remove_if(pass.begin(), pass.end(), [](char const c) { return std::isalnum(c) == 0; }), pass.end());
  732. if (pass.length() > 255)
  733. Proxy.Password = pass.substr(0, 255);
  734. else
  735. Proxy.Password = std::move(pass);
  736. }
  737. // FIXME: should we support auth.conf for proxies?
  738. return true;
  739. }
  740. /*}}}*/