]> git.saurik.com Git - apt.git/blob - methods/server.cc
Only enable pipelining if server is HTTP/1.1
[apt.git] / methods / server.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 HTTP and HTTPS share a lot of common code and these classes are
6 exactly the dumping ground for this common code
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <config.h>
12
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/strutl.h>
18
19 #include <ctype.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <iostream>
28 #include <limits>
29 #include <map>
30 #include <string>
31 #include <vector>
32
33 #include "server.h"
34
35 #include <apti18n.h>
36 /*}}}*/
37 using namespace std;
38
39 string ServerMethod::FailFile;
40 int ServerMethod::FailFd = -1;
41 time_t ServerMethod::FailTime = 0;
42
43 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
44 // ---------------------------------------------------------------------
45 /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
46 parse error occurred */
47 ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File,
48 const std::string &Uri)
49 {
50 State = Header;
51
52 Owner->Status(_("Waiting for headers"));
53
54 Major = 0;
55 Minor = 0;
56 Result = 0;
57 TotalFileSize = 0;
58 JunkSize = 0;
59 StartPos = 0;
60 Encoding = Closes;
61 HaveContent = false;
62 time(&Date);
63
64 do
65 {
66 string Data;
67 if (ReadHeaderLines(Data) == false)
68 continue;
69
70 if (Owner->Debug == true)
71 clog << "Answer for: " << Uri << endl << Data;
72
73 for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
74 {
75 string::const_iterator J = I;
76 for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J);
77 if (HeaderLine(string(I,J)) == false)
78 return RUN_HEADERS_PARSE_ERROR;
79 I = J;
80 }
81
82 // 100 Continue is a Nop...
83 if (Result == 100)
84 continue;
85
86 // Tidy up the connection persistence state.
87 if (Encoding == Closes && HaveContent == true)
88 Persistent = false;
89
90 return RUN_HEADERS_OK;
91 }
92 while (LoadNextResponse(false, File) == true);
93
94 return RUN_HEADERS_IO_ERROR;
95 }
96 /*}}}*/
97 // ServerState::HeaderLine - Process a header line /*{{{*/
98 // ---------------------------------------------------------------------
99 /* */
100 bool ServerState::HeaderLine(string Line)
101 {
102 if (Line.empty() == true)
103 return true;
104
105 string::size_type Pos = Line.find(' ');
106 if (Pos == string::npos || Pos+1 > Line.length())
107 {
108 // Blah, some servers use "connection:closes", evil.
109 Pos = Line.find(':');
110 if (Pos == string::npos || Pos + 2 > Line.length())
111 return _error->Error(_("Bad header line"));
112 Pos++;
113 }
114
115 // Parse off any trailing spaces between the : and the next word.
116 string::size_type Pos2 = Pos;
117 while (Pos2 < Line.length() && isspace_ascii(Line[Pos2]) != 0)
118 Pos2++;
119
120 string Tag = string(Line,0,Pos);
121 string Val = string(Line,Pos2);
122
123 if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
124 {
125 // Evil servers return no version
126 if (Line[4] == '/')
127 {
128 int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code);
129 if (elements == 3)
130 {
131 Code[0] = '\0';
132 if (Owner != NULL && Owner->Debug == true)
133 clog << "HTTP server doesn't give Reason-Phrase for " << Result << std::endl;
134 }
135 else if (elements != 4)
136 return _error->Error(_("The HTTP server sent an invalid reply header"));
137 }
138 else
139 {
140 Major = 0;
141 Minor = 9;
142 if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2)
143 return _error->Error(_("The HTTP server sent an invalid reply header"));
144 }
145
146 /* Check the HTTP response header to get the default persistence
147 state. */
148 if (Major < 1)
149 Persistent = false;
150 else
151 {
152 if (Major == 1 && Minor == 0)
153 {
154 Persistent = false;
155 }
156 else
157 {
158 Persistent = true;
159 if (PipelineAllowed)
160 Pipeline = true;
161 }
162 }
163
164 return true;
165 }
166
167 if (stringcasecmp(Tag,"Content-Length:") == 0)
168 {
169 if (Encoding == Closes)
170 Encoding = Stream;
171 HaveContent = true;
172
173 unsigned long long * DownloadSizePtr = &DownloadSize;
174 if (Result == 416)
175 DownloadSizePtr = &JunkSize;
176
177 *DownloadSizePtr = strtoull(Val.c_str(), NULL, 10);
178 if (*DownloadSizePtr >= std::numeric_limits<unsigned long long>::max())
179 return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
180 else if (*DownloadSizePtr == 0)
181 HaveContent = false;
182
183 // On partial content (206) the Content-Length less than the real
184 // size, so do not set it here but leave that to the Content-Range
185 // header instead
186 if(Result != 206 && TotalFileSize == 0)
187 TotalFileSize = DownloadSize;
188
189 return true;
190 }
191
192 if (stringcasecmp(Tag,"Content-Type:") == 0)
193 {
194 HaveContent = true;
195 return true;
196 }
197
198 if (stringcasecmp(Tag,"Content-Range:") == 0)
199 {
200 HaveContent = true;
201
202 // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
203 if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&TotalFileSize) == 1)
204 ; // we got the expected filesize which is all we wanted
205 else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2)
206 return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
207 if ((unsigned long long)StartPos > TotalFileSize)
208 return _error->Error(_("This HTTP server has broken range support"));
209
210 // figure out what we will download
211 DownloadSize = TotalFileSize - StartPos;
212 return true;
213 }
214
215 if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
216 {
217 HaveContent = true;
218 if (stringcasecmp(Val,"chunked") == 0)
219 Encoding = Chunked;
220 return true;
221 }
222
223 if (stringcasecmp(Tag,"Connection:") == 0)
224 {
225 if (stringcasecmp(Val,"close") == 0)
226 Persistent = false;
227 if (stringcasecmp(Val,"keep-alive") == 0)
228 Persistent = true;
229 return true;
230 }
231
232 if (stringcasecmp(Tag,"Last-Modified:") == 0)
233 {
234 if (RFC1123StrToTime(Val.c_str(), Date) == false)
235 return _error->Error(_("Unknown date format"));
236 return true;
237 }
238
239 if (stringcasecmp(Tag,"Location:") == 0)
240 {
241 Location = Val;
242 return true;
243 }
244
245 return true;
246 }
247 /*}}}*/
248 // ServerState::ServerState - Constructor /*{{{*/
249 ServerState::ServerState(URI Srv, ServerMethod *Owner) : ServerName(Srv), TimeOut(120), Owner(Owner)
250 {
251 Reset();
252 }
253 /*}}}*/
254 bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/
255 {
256 File.Truncate(StartPos);
257 return GetHashes()->AddFD(File, StartPos);
258 }
259 /*}}}*/
260
261 // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
262 // ---------------------------------------------------------------------
263 /* We look at the header data we got back from the server and decide what
264 to do. Returns DealWithHeadersResult (see http.h for details).
265 */
266 ServerMethod::DealWithHeadersResult
267 ServerMethod::DealWithHeaders(FetchResult &Res)
268 {
269 // Not Modified
270 if (Server->Result == 304)
271 {
272 RemoveFile("server", Queue->DestFile);
273 Res.IMSHit = true;
274 Res.LastModified = Queue->LastModified;
275 return IMS_HIT;
276 }
277
278 /* Redirect
279 *
280 * Note that it is only OK for us to treat all redirection the same
281 * because we *always* use GET, not other HTTP methods. There are
282 * three redirection codes for which it is not appropriate that we
283 * redirect. Pass on those codes so the error handling kicks in.
284 */
285 if (AllowRedirect
286 && (Server->Result > 300 && Server->Result < 400)
287 && (Server->Result != 300 // Multiple Choices
288 && Server->Result != 304 // Not Modified
289 && Server->Result != 306)) // (Not part of HTTP/1.1, reserved)
290 {
291 if (Server->Location.empty() == true);
292 else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
293 {
294 URI Uri = Queue->Uri;
295 if (Uri.Host.empty() == false)
296 NextURI = URI::SiteOnly(Uri);
297 else
298 NextURI.clear();
299 NextURI.append(DeQuoteString(Server->Location));
300 return TRY_AGAIN_OR_REDIRECT;
301 }
302 else
303 {
304 NextURI = DeQuoteString(Server->Location);
305 URI tmpURI = NextURI;
306 URI Uri = Queue->Uri;
307 // same protocol redirects are okay
308 if (tmpURI.Access == Uri.Access)
309 return TRY_AGAIN_OR_REDIRECT;
310 // as well as http to https
311 else if (Uri.Access == "http" && tmpURI.Access == "https")
312 return TRY_AGAIN_OR_REDIRECT;
313 }
314 /* else pass through for error message */
315 }
316 // retry after an invalid range response without partial data
317 else if (Server->Result == 416)
318 {
319 struct stat SBuf;
320 if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
321 {
322 bool partialHit = false;
323 if (Queue->ExpectedHashes.usable() == true)
324 {
325 Hashes resultHashes(Queue->ExpectedHashes);
326 FileFd file(Queue->DestFile, FileFd::ReadOnly);
327 Server->TotalFileSize = file.FileSize();
328 Server->Date = file.ModificationTime();
329 resultHashes.AddFD(file);
330 HashStringList const hashList = resultHashes.GetHashStringList();
331 partialHit = (Queue->ExpectedHashes == hashList);
332 }
333 else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
334 partialHit = true;
335 if (partialHit == true)
336 {
337 // the file is completely downloaded, but was not moved
338 if (Server->HaveContent == true)
339 {
340 // Send to error page to dev/null
341 FileFd DevNull("/dev/null",FileFd::WriteExists);
342 Server->RunData(&DevNull);
343 }
344 Server->HaveContent = false;
345 Server->StartPos = Server->TotalFileSize;
346 Server->Result = 200;
347 }
348 else if (RemoveFile("server", Queue->DestFile))
349 {
350 NextURI = Queue->Uri;
351 return TRY_AGAIN_OR_REDIRECT;
352 }
353 }
354 }
355
356 /* We have a reply we don't handle. This should indicate a perm server
357 failure */
358 if (Server->Result < 200 || Server->Result >= 300)
359 {
360 std::string err;
361 strprintf(err, "HttpError%u", Server->Result);
362 SetFailReason(err);
363 _error->Error("%u %s", Server->Result, Server->Code);
364 if (Server->HaveContent == true)
365 return ERROR_WITH_CONTENT_PAGE;
366 return ERROR_UNRECOVERABLE;
367 }
368
369 // This is some sort of 2xx 'data follows' reply
370 Res.LastModified = Server->Date;
371 Res.Size = Server->TotalFileSize;
372
373 // Open the file
374 delete File;
375 File = new FileFd(Queue->DestFile,FileFd::WriteAny);
376 if (_error->PendingError() == true)
377 return ERROR_NOT_FROM_SERVER;
378
379 FailFile = Queue->DestFile;
380 FailFile.c_str(); // Make sure we don't do a malloc in the signal handler
381 FailFd = File->Fd();
382 FailTime = Server->Date;
383
384 if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false)
385 {
386 _error->Errno("read",_("Problem hashing file"));
387 return ERROR_NOT_FROM_SERVER;
388 }
389 if (Server->StartPos > 0)
390 Res.ResumePoint = Server->StartPos;
391
392 SetNonBlock(File->Fd(),true);
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
405 struct timeval times[2];
406 times[0].tv_sec = FailTime;
407 times[1].tv_sec = FailTime;
408 times[0].tv_usec = times[1].tv_usec = 0;
409 utimes(FailFile.c_str(), times);
410 close(FailFd);
411
412 _exit(100);
413 }
414 /*}}}*/
415 // ServerMethod::Fetch - Fetch an item /*{{{*/
416 // ---------------------------------------------------------------------
417 /* This adds an item to the pipeline. We keep the pipeline at a fixed
418 depth. */
419 bool ServerMethod::Fetch(FetchItem *)
420 {
421 if (Server == 0)
422 return true;
423
424 // Queue the requests
425 int Depth = -1;
426 for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
427 I = I->Next, Depth++)
428 {
429 if (Depth >= 0)
430 {
431 // If pipelining is disabled, we only queue 1 request
432 if (Server->Pipeline == false)
433 break;
434 // if we have no hashes, do at most one such request
435 // as we can't fixup pipeling misbehaviors otherwise
436 else if (I->ExpectedHashes.usable() == false)
437 break;
438 }
439
440 // Make sure we stick with the same server
441 if (Server->Comp(I->Uri) == false)
442 break;
443 if (QueueBack == I)
444 {
445 QueueBack = I->Next;
446 SendReq(I);
447 continue;
448 }
449 }
450
451 return true;
452 }
453 /*}}}*/
454 // ServerMethod::Loop - Main loop /*{{{*/
455 int ServerMethod::Loop()
456 {
457 typedef vector<string> StringVector;
458 typedef vector<string>::iterator StringVectorIterator;
459 map<string, StringVector> Redirected;
460
461 signal(SIGTERM,SigTerm);
462 signal(SIGINT,SigTerm);
463
464 Server = 0;
465
466 int FailCounter = 0;
467 while (1)
468 {
469 // We have no commands, wait for some to arrive
470 if (Queue == 0)
471 {
472 if (WaitFd(STDIN_FILENO) == false)
473 return 0;
474 }
475
476 /* Run messages, we can accept 0 (no message) if we didn't
477 do a WaitFd above.. Otherwise the FD is closed. */
478 int Result = Run(true);
479 if (Result != -1 && (Result != 0 || Queue == 0))
480 {
481 if(FailReason.empty() == false ||
482 _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
483 return 100;
484 else
485 return 0;
486 }
487
488 if (Queue == 0)
489 continue;
490
491 // Connect to the server
492 if (Server == 0 || Server->Comp(Queue->Uri) == false)
493 Server = CreateServerState(Queue->Uri);
494
495 /* If the server has explicitly said this is the last connection
496 then we pre-emptively shut down the pipeline and tear down
497 the connection. This will speed up HTTP/1.0 servers a tad
498 since we don't have to wait for the close sequence to
499 complete */
500 if (Server->Persistent == false)
501 Server->Close();
502
503 // Reset the pipeline
504 if (Server->IsOpen() == false)
505 QueueBack = Queue;
506
507 // Connnect to the host
508 if (Server->Open() == false)
509 {
510 Fail(true);
511 Server = nullptr;
512 continue;
513 }
514
515 // Fill the pipeline.
516 Fetch(0);
517
518 // Fetch the next URL header data from the server.
519 switch (Server->RunHeaders(File, Queue->Uri))
520 {
521 case ServerState::RUN_HEADERS_OK:
522 break;
523
524 // The header data is bad
525 case ServerState::RUN_HEADERS_PARSE_ERROR:
526 {
527 _error->Error(_("Bad header data"));
528 Fail(true);
529 RotateDNS();
530 continue;
531 }
532
533 // The server closed a connection during the header get..
534 default:
535 case ServerState::RUN_HEADERS_IO_ERROR:
536 {
537 FailCounter++;
538 _error->Discard();
539 Server->Close();
540 Server->Pipeline = false;
541 Server->PipelineAllowed = false;
542
543 if (FailCounter >= 2)
544 {
545 Fail(_("Connection failed"),true);
546 FailCounter = 0;
547 }
548
549 RotateDNS();
550 continue;
551 }
552 };
553
554 // Decide what to do.
555 FetchResult Res;
556 Res.Filename = Queue->DestFile;
557 switch (DealWithHeaders(Res))
558 {
559 // Ok, the file is Open
560 case FILE_IS_OPEN:
561 {
562 URIStart(Res);
563
564 // Run the data
565 bool Result = true;
566
567 // ensure we don't fetch too much
568 // we could do "Server->MaximumSize = Queue->MaximumSize" here
569 // but that would break the clever pipeline messup detection
570 // so instead we use the size of the biggest item in the queue
571 Server->MaximumSize = FindMaximumObjectSizeInQueue();
572
573 if (Server->HaveContent)
574 Result = Server->RunData(File);
575
576 /* If the server is sending back sizeless responses then fill in
577 the size now */
578 if (Res.Size == 0)
579 Res.Size = File->Size();
580
581 // Close the file, destroy the FD object and timestamp it
582 FailFd = -1;
583 delete File;
584 File = 0;
585
586 // Timestamp
587 struct timeval times[2];
588 times[0].tv_sec = times[1].tv_sec = Server->Date;
589 times[0].tv_usec = times[1].tv_usec = 0;
590 utimes(Queue->DestFile.c_str(), times);
591
592 // Send status to APT
593 if (Result == true)
594 {
595 Hashes * const resultHashes = Server->GetHashes();
596 HashStringList const hashList = resultHashes->GetHashStringList();
597 if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
598 {
599 // we did not get the expected hash… mhhh:
600 // could it be that server/proxy messed up pipelining?
601 FetchItem * BeforeI = Queue;
602 for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
603 {
604 if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
605 {
606 // yes, he did! Disable pipelining and rewrite queue
607 if (Server->Pipeline == true)
608 {
609 // FIXME: fake a warning message as we have no proper way of communicating here
610 std::string out;
611 strprintf(out, _("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::PipelineDepth");
612 std::cerr << "W: " << out << std::endl;
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
638 if (FailCounter >= 2)
639 {
640 Fail(_("Connection failed"),true);
641 FailCounter = 0;
642 }
643
644 QueueBack = Queue;
645 }
646 else
647 {
648 Server->Close();
649 Fail(true);
650 }
651 }
652 break;
653 }
654
655 // IMS hit
656 case IMS_HIT:
657 {
658 URIDone(Res);
659 break;
660 }
661
662 // Hard server error, not found or something
663 case ERROR_UNRECOVERABLE:
664 {
665 Fail();
666 break;
667 }
668
669 // Hard internal error, kill the connection and fail
670 case ERROR_NOT_FROM_SERVER:
671 {
672 delete File;
673 File = 0;
674
675 Fail();
676 RotateDNS();
677 Server->Close();
678 break;
679 }
680
681 // We need to flush the data, the header is like a 404 w/ error text
682 case ERROR_WITH_CONTENT_PAGE:
683 {
684 Fail();
685
686 // Send to content to dev/null
687 File = new FileFd("/dev/null",FileFd::WriteExists);
688 Server->RunData(File);
689 delete File;
690 File = 0;
691 break;
692 }
693
694 // Try again with a new URL
695 case TRY_AGAIN_OR_REDIRECT:
696 {
697 // Clear rest of response if there is content
698 if (Server->HaveContent)
699 {
700 File = new FileFd("/dev/null",FileFd::WriteExists);
701 Server->RunData(File);
702 delete File;
703 File = 0;
704 }
705
706 /* Detect redirect loops. No more redirects are allowed
707 after the same URI is seen twice in a queue item. */
708 StringVector &R = Redirected[Queue->DestFile];
709 bool StopRedirects = false;
710 if (R.empty() == true)
711 R.push_back(Queue->Uri);
712 else if (R[0] == "STOP" || R.size() > 10)
713 StopRedirects = true;
714 else
715 {
716 for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
717 if (Queue->Uri == *I)
718 {
719 R[0] = "STOP";
720 break;
721 }
722
723 R.push_back(Queue->Uri);
724 }
725
726 if (StopRedirects == false)
727 Redirect(NextURI);
728 else
729 Fail();
730
731 break;
732 }
733
734 default:
735 Fail(_("Internal error"));
736 break;
737 }
738
739 FailCounter = 0;
740 }
741
742 return 0;
743 }
744 /*}}}*/
745 unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
746 {
747 unsigned long long MaxSizeInQueue = 0;
748 for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
749 MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
750 return MaxSizeInQueue;
751 }
752 /*}}}*/
753 ServerMethod::ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/
754 aptMethod(Binary, Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10),
755 AllowRedirect(false), Debug(false)
756 {
757 }
758 /*}}}*/