server.cc 17 KB

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