]>
git.saurik.com Git - apt.git/blob - methods/server.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 HTTP and HTTPS share a lot of common code and these classes are
6 exactly the dumping ground for this common code
8 ##################################################################### */
10 // Include Files /*{{{*/
13 #include <apt-pkg/configuration.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/strutl.h>
38 string
ServerMethod::FailFile
;
39 int ServerMethod::FailFd
= -1;
40 time_t ServerMethod::FailTime
= 0;
42 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
43 // ---------------------------------------------------------------------
44 /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
45 parse error occurred */
46 ServerState::RunHeadersResult
ServerState::RunHeaders(FileFd
* const File
,
47 const std::string
&Uri
)
51 Owner
->Status(_("Waiting for headers"));
66 if (ReadHeaderLines(Data
) == false)
69 if (Owner
->Debug
== true)
70 clog
<< "Answer for: " << Uri
<< endl
<< Data
;
72 for (string::const_iterator I
= Data
.begin(); I
< Data
.end(); ++I
)
74 string::const_iterator J
= I
;
75 for (; J
!= Data
.end() && *J
!= '\n' && *J
!= '\r'; ++J
);
76 if (HeaderLine(string(I
,J
)) == false)
77 return RUN_HEADERS_PARSE_ERROR
;
81 // 100 Continue is a Nop...
85 // Tidy up the connection persistence state.
86 if (Encoding
== Closes
&& HaveContent
== true)
89 return RUN_HEADERS_OK
;
91 while (LoadNextResponse(false, File
) == true);
93 return RUN_HEADERS_IO_ERROR
;
96 // ServerState::HeaderLine - Process a header line /*{{{*/
97 // ---------------------------------------------------------------------
99 bool ServerState::HeaderLine(string Line
)
101 if (Line
.empty() == true)
104 string::size_type Pos
= Line
.find(' ');
105 if (Pos
== string::npos
|| Pos
+1 > Line
.length())
107 // Blah, some servers use "connection:closes", evil.
108 Pos
= Line
.find(':');
109 if (Pos
== string::npos
|| Pos
+ 2 > Line
.length())
110 return _error
->Error(_("Bad header line"));
114 // Parse off any trailing spaces between the : and the next word.
115 string::size_type Pos2
= Pos
;
116 while (Pos2
< Line
.length() && isspace_ascii(Line
[Pos2
]) != 0)
119 string Tag
= string(Line
,0,Pos
);
120 string Val
= string(Line
,Pos2
);
122 if (stringcasecmp(Tag
.c_str(),Tag
.c_str()+4,"HTTP") == 0)
124 // Evil servers return no version
127 int const elements
= sscanf(Line
.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major
,&Minor
,&Result
,Code
);
131 if (Owner
!= NULL
&& Owner
->Debug
== true)
132 clog
<< "HTTP server doesn't give Reason-Phrase for " << std::to_string(Result
) << std::endl
;
134 else if (elements
!= 4)
135 return _error
->Error(_("The HTTP server sent an invalid reply header"));
141 if (sscanf(Line
.c_str(),"HTTP %3u%359[^\n]",&Result
,Code
) != 2)
142 return _error
->Error(_("The HTTP server sent an invalid reply header"));
145 /* Check the HTTP response header to get the default persistence
151 if (Major
== 1 && Minor
== 0)
166 if (stringcasecmp(Tag
,"Content-Length:") == 0)
168 if (Encoding
== Closes
)
172 unsigned long long * DownloadSizePtr
= &DownloadSize
;
173 if (Result
== 416 || (Result
>= 300 && Result
< 400))
174 DownloadSizePtr
= &JunkSize
;
176 *DownloadSizePtr
= strtoull(Val
.c_str(), NULL
, 10);
177 if (*DownloadSizePtr
>= std::numeric_limits
<unsigned long long>::max())
178 return _error
->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
179 else if (*DownloadSizePtr
== 0)
182 // On partial content (206) the Content-Length less than the real
183 // size, so do not set it here but leave that to the Content-Range
185 if(Result
!= 206 && TotalFileSize
== 0)
186 TotalFileSize
= DownloadSize
;
191 if (stringcasecmp(Tag
,"Content-Type:") == 0)
197 if (stringcasecmp(Tag
,"Content-Range:") == 0)
201 // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
202 if (Result
== 416 && sscanf(Val
.c_str(), "bytes */%llu",&TotalFileSize
) == 1)
203 ; // we got the expected filesize which is all we wanted
204 else if (sscanf(Val
.c_str(),"bytes %llu-%*u/%llu",&StartPos
,&TotalFileSize
) != 2)
205 return _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
206 if ((unsigned long long)StartPos
> TotalFileSize
)
207 return _error
->Error(_("This HTTP server has broken range support"));
209 // figure out what we will download
210 DownloadSize
= TotalFileSize
- StartPos
;
214 if (stringcasecmp(Tag
,"Transfer-Encoding:") == 0)
217 if (stringcasecmp(Val
,"chunked") == 0)
222 if (stringcasecmp(Tag
,"Connection:") == 0)
224 if (stringcasecmp(Val
,"close") == 0)
226 if (stringcasecmp(Val
,"keep-alive") == 0)
231 if (stringcasecmp(Tag
,"Last-Modified:") == 0)
233 if (RFC1123StrToTime(Val
.c_str(), Date
) == false)
234 return _error
->Error(_("Unknown date format"));
238 if (stringcasecmp(Tag
,"Location:") == 0)
247 // ServerState::ServerState - Constructor /*{{{*/
248 ServerState::ServerState(URI Srv
, ServerMethod
*Owner
) :
249 DownloadSize(0), ServerName(Srv
), TimeOut(120), Owner(Owner
)
254 bool ServerState::AddPartialFileToHashes(FileFd
&File
) /*{{{*/
256 File
.Truncate(StartPos
);
257 return GetHashes()->AddFD(File
, StartPos
);
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).
266 ServerMethod::DealWithHeadersResult
267 ServerMethod::DealWithHeaders(FetchResult
&Res
)
270 if (Server
->Result
== 304)
272 RemoveFile("server", Queue
->DestFile
);
274 Res
.LastModified
= Queue
->LastModified
;
281 * Note that it is only OK for us to treat all redirection the same
282 * because we *always* use GET, not other HTTP methods. There are
283 * three redirection codes for which it is not appropriate that we
284 * redirect. Pass on those codes so the error handling kicks in.
287 && (Server
->Result
> 300 && Server
->Result
< 400)
288 && (Server
->Result
!= 300 // Multiple Choices
289 && Server
->Result
!= 304 // Not Modified
290 && Server
->Result
!= 306)) // (Not part of HTTP/1.1, reserved)
292 if (Server
->Location
.empty() == true)
294 else if (Server
->Location
[0] == '/' && Queue
->Uri
.empty() == false)
296 URI Uri
= Queue
->Uri
;
297 if (Uri
.Host
.empty() == false)
298 NextURI
= URI::SiteOnly(Uri
);
301 NextURI
.append(DeQuoteString(Server
->Location
));
302 if (Queue
->Uri
== NextURI
)
304 SetFailReason("RedirectionLoop");
305 _error
->Error("Redirection loop encountered");
306 if (Server
->HaveContent
== true)
307 return ERROR_WITH_CONTENT_PAGE
;
308 return ERROR_UNRECOVERABLE
;
310 return TRY_AGAIN_OR_REDIRECT
;
314 NextURI
= DeQuoteString(Server
->Location
);
315 URI tmpURI
= NextURI
;
316 if (tmpURI
.Access
.find('+') != std::string::npos
)
318 _error
->Error("Server tried to trick us into using a specific implementation: %s", tmpURI
.Access
.c_str());
319 if (Server
->HaveContent
== true)
320 return ERROR_WITH_CONTENT_PAGE
;
321 return ERROR_UNRECOVERABLE
;
323 URI Uri
= Queue
->Uri
;
324 if (Binary
.find('+') != std::string::npos
)
326 auto base
= Binary
.substr(0, Binary
.find('+'));
327 if (base
!= tmpURI
.Access
)
329 tmpURI
.Access
= base
+ '+' + tmpURI
.Access
;
330 if (tmpURI
.Access
== Binary
)
332 std::string tmpAccess
= Uri
.Access
;
333 std::swap(tmpURI
.Access
, Uri
.Access
);
335 std::swap(tmpURI
.Access
, Uri
.Access
);
341 if (Queue
->Uri
== NextURI
)
343 SetFailReason("RedirectionLoop");
344 _error
->Error("Redirection loop encountered");
345 if (Server
->HaveContent
== true)
346 return ERROR_WITH_CONTENT_PAGE
;
347 return ERROR_UNRECOVERABLE
;
350 // same protocol redirects are okay
351 if (tmpURI
.Access
== Uri
.Access
)
352 return TRY_AGAIN_OR_REDIRECT
;
353 // as well as http to https
354 else if ((Uri
.Access
== "http" || Uri
.Access
== "https+http") && tmpURI
.Access
== "https")
355 return TRY_AGAIN_OR_REDIRECT
;
358 auto const tmpplus
= tmpURI
.Access
.find('+');
359 if (tmpplus
!= std::string::npos
&& tmpURI
.Access
.substr(tmpplus
+ 1) == "https")
361 auto const uriplus
= Uri
.Access
.find('+');
362 if (uriplus
== std::string::npos
)
364 if (Uri
.Access
== tmpURI
.Access
.substr(0, tmpplus
)) // foo -> foo+https
365 return TRY_AGAIN_OR_REDIRECT
;
367 else if (Uri
.Access
.substr(uriplus
+ 1) == "http" &&
368 Uri
.Access
.substr(0, uriplus
) == tmpURI
.Access
.substr(0, tmpplus
)) // foo+http -> foo+https
369 return TRY_AGAIN_OR_REDIRECT
;
372 _error
->Error("Redirection from %s to '%s' is forbidden", Uri
.Access
.c_str(), NextURI
.c_str());
374 /* else pass through for error message */
376 // retry after an invalid range response without partial data
377 else if (Server
->Result
== 416)
380 if (stat(Queue
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
382 bool partialHit
= false;
383 if (Queue
->ExpectedHashes
.usable() == true)
385 Hashes
resultHashes(Queue
->ExpectedHashes
);
386 FileFd
file(Queue
->DestFile
, FileFd::ReadOnly
);
387 Server
->TotalFileSize
= file
.FileSize();
388 Server
->Date
= file
.ModificationTime();
389 resultHashes
.AddFD(file
);
390 HashStringList
const hashList
= resultHashes
.GetHashStringList();
391 partialHit
= (Queue
->ExpectedHashes
== hashList
);
393 else if ((unsigned long long)SBuf
.st_size
== Server
->TotalFileSize
)
395 if (partialHit
== true)
397 // the file is completely downloaded, but was not moved
398 if (Server
->HaveContent
== true)
400 // nuke the sent error page
401 Server
->RunDataToDevNull();
402 Server
->HaveContent
= false;
404 Server
->StartPos
= Server
->TotalFileSize
;
405 Server
->Result
= 200;
407 else if (RemoveFile("server", Queue
->DestFile
))
409 NextURI
= Queue
->Uri
;
410 return TRY_AGAIN_OR_REDIRECT
;
415 /* We have a reply we don't handle. This should indicate a perm server
417 if (Server
->Result
< 200 || Server
->Result
>= 300)
419 if (_error
->PendingError() == false)
422 strprintf(err
, "HttpError%u", Server
->Result
);
424 _error
->Error("%u %s", Server
->Result
, Server
->Code
);
426 if (Server
->HaveContent
== true)
427 return ERROR_WITH_CONTENT_PAGE
;
428 return ERROR_UNRECOVERABLE
;
431 // This is some sort of 2xx 'data follows' reply
432 Res
.LastModified
= Server
->Date
;
433 Res
.Size
= Server
->TotalFileSize
;
437 // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
438 // ---------------------------------------------------------------------
439 /* This closes and timestamps the open file. This is necessary to get
440 resume behavoir on user abort */
441 void ServerMethod::SigTerm(int)
446 struct timeval times
[2];
447 times
[0].tv_sec
= FailTime
;
448 times
[1].tv_sec
= FailTime
;
449 times
[0].tv_usec
= times
[1].tv_usec
= 0;
450 utimes(FailFile
.c_str(), times
);
456 // ServerMethod::Fetch - Fetch an item /*{{{*/
457 // ---------------------------------------------------------------------
458 /* This adds an item to the pipeline. We keep the pipeline at a fixed
460 bool ServerMethod::Fetch(FetchItem
*)
462 if (Server
== nullptr || QueueBack
== nullptr)
465 // If pipelining is disabled, we only queue 1 request
466 auto const AllowedDepth
= Server
->Pipeline
? PipelineDepth
: 0;
467 // how deep is our pipeline currently?
468 decltype(PipelineDepth
) CurrentDepth
= 0;
469 for (FetchItem
const *I
= Queue
; I
!= QueueBack
; I
= I
->Next
)
471 if (CurrentDepth
> AllowedDepth
)
475 // Make sure we stick with the same server
476 if (Server
->Comp(QueueBack
->Uri
) == false)
479 bool const UsableHashes
= QueueBack
->ExpectedHashes
.usable();
480 // if we have no hashes, do at most one such request
481 // as we can't fixup pipeling misbehaviors otherwise
482 if (CurrentDepth
!= 0 && UsableHashes
== false)
485 if (UsableHashes
&& FileExists(QueueBack
->DestFile
))
487 FileFd
partial(QueueBack
->DestFile
, FileFd::ReadOnly
);
488 Hashes
wehave(QueueBack
->ExpectedHashes
);
489 if (QueueBack
->ExpectedHashes
.FileSize() == partial
.FileSize())
491 if (wehave
.AddFD(partial
) &&
492 wehave
.GetHashStringList() == QueueBack
->ExpectedHashes
)
495 Res
.Filename
= QueueBack
->DestFile
;
496 Res
.ResumePoint
= QueueBack
->ExpectedHashes
.FileSize();
498 // move item to the start of the queue as URIDone will
499 // always dequeued the first item in the queue
500 if (Queue
!= QueueBack
)
502 FetchItem
*Prev
= Queue
;
503 for (; Prev
->Next
!= QueueBack
; Prev
= Prev
->Next
)
504 /* look for the previous queue item */;
505 Prev
->Next
= QueueBack
->Next
;
506 QueueBack
->Next
= Queue
;
508 QueueBack
= Prev
->Next
;
510 Res
.TakeHashes(wehave
);
515 RemoveFile("Fetch-Partial", QueueBack
->DestFile
);
518 auto const Tmp
= QueueBack
;
519 QueueBack
= QueueBack
->Next
;
522 } while (CurrentDepth
<= AllowedDepth
&& QueueBack
!= nullptr);
527 // ServerMethod::Loop - Main loop /*{{{*/
528 int ServerMethod::Loop()
530 signal(SIGTERM
,SigTerm
);
531 signal(SIGINT
,SigTerm
);
538 // We have no commands, wait for some to arrive
541 if (WaitFd(STDIN_FILENO
) == false)
545 /* Run messages, we can accept 0 (no message) if we didn't
546 do a WaitFd above.. Otherwise the FD is closed. */
547 int Result
= Run(true);
548 if (Result
!= -1 && (Result
!= 0 || Queue
== 0))
550 if(FailReason
.empty() == false ||
551 ConfigFindB("DependOnSTDIN", true) == true)
560 // Connect to the server
561 if (Server
== 0 || Server
->Comp(Queue
->Uri
) == false)
563 Server
= CreateServerState(Queue
->Uri
);
564 setPostfixForMethodNames(::URI(Queue
->Uri
).Host
.c_str());
565 AllowRedirect
= ConfigFindB("AllowRedirect", true);
566 PipelineDepth
= ConfigFindI("Pipeline-Depth", 10);
567 Debug
= DebugEnabled();
570 /* If the server has explicitly said this is the last connection
571 then we pre-emptively shut down the pipeline and tear down
572 the connection. This will speed up HTTP/1.0 servers a tad
573 since we don't have to wait for the close sequence to
575 if (Server
->Persistent
== false)
578 // Reset the pipeline
579 if (Server
->IsOpen() == false)
582 // Connnect to the host
583 if (Server
->Open() == false)
590 // Fill the pipeline.
593 // Fetch the next URL header data from the server.
594 switch (Server
->RunHeaders(File
, Queue
->Uri
))
596 case ServerState::RUN_HEADERS_OK
:
599 // The header data is bad
600 case ServerState::RUN_HEADERS_PARSE_ERROR
:
602 _error
->Error(_("Bad header data"));
609 // The server closed a connection during the header get..
611 case ServerState::RUN_HEADERS_IO_ERROR
:
616 Server
->Pipeline
= false;
617 Server
->PipelineAllowed
= false;
619 if (FailCounter
>= 2)
621 Fail(_("Connection failed"),true);
630 // Decide what to do.
632 Res
.Filename
= Queue
->DestFile
;
633 switch (DealWithHeaders(Res
))
635 // Ok, the file is Open
643 // ensure we don't fetch too much
644 // we could do "Server->MaximumSize = Queue->MaximumSize" here
645 // but that would break the clever pipeline messup detection
646 // so instead we use the size of the biggest item in the queue
647 Server
->MaximumSize
= FindMaximumObjectSizeInQueue();
649 if (Server
->HaveContent
)
650 Result
= Server
->RunData(File
);
652 /* If the server is sending back sizeless responses then fill in
655 Res
.Size
= File
->Size();
657 // Close the file, destroy the FD object and timestamp it
663 struct timeval times
[2];
664 times
[0].tv_sec
= times
[1].tv_sec
= Server
->Date
;
665 times
[0].tv_usec
= times
[1].tv_usec
= 0;
666 utimes(Queue
->DestFile
.c_str(), times
);
668 // Send status to APT
671 Hashes
* const resultHashes
= Server
->GetHashes();
672 HashStringList
const hashList
= resultHashes
->GetHashStringList();
673 if (PipelineDepth
!= 0 && Queue
->ExpectedHashes
.usable() == true && Queue
->ExpectedHashes
!= hashList
)
675 // we did not get the expected hash… mhhh:
676 // could it be that server/proxy messed up pipelining?
677 FetchItem
* BeforeI
= Queue
;
678 for (FetchItem
*I
= Queue
->Next
; I
!= 0 && I
!= QueueBack
; I
= I
->Next
)
680 if (I
->ExpectedHashes
.usable() == true && I
->ExpectedHashes
== hashList
)
682 // yes, he did! Disable pipelining and rewrite queue
683 if (Server
->Pipeline
== true)
685 Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
686 Server
->Pipeline
= false;
687 Server
->PipelineAllowed
= false;
688 // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
690 Rename(Res
.Filename
, I
->DestFile
);
691 Res
.Filename
= I
->DestFile
;
692 BeforeI
->Next
= I
->Next
;
700 Res
.TakeHashes(*resultHashes
);
705 if (Server
->IsOpen() == false)
711 if (FailCounter
>= 2)
713 Fail(_("Connection failed"),true);
735 // Hard server error, not found or something
736 case ERROR_UNRECOVERABLE
:
742 // Hard internal error, kill the connection and fail
743 case ERROR_NOT_FROM_SERVER
:
754 // We need to flush the data, the header is like a 404 w/ error text
755 case ERROR_WITH_CONTENT_PAGE
:
758 Server
->RunDataToDevNull();
762 // Try again with a new URL
763 case TRY_AGAIN_OR_REDIRECT
:
765 // Clear rest of response if there is content
766 if (Server
->HaveContent
)
767 Server
->RunDataToDevNull();
773 Fail(_("Internal error"));
783 unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
785 unsigned long long MaxSizeInQueue
= 0;
786 for (FetchItem
*I
= Queue
; I
!= 0 && I
!= QueueBack
; I
= I
->Next
)
787 MaxSizeInQueue
= std::max(MaxSizeInQueue
, I
->MaximumSize
);
788 return MaxSizeInQueue
;
791 ServerMethod::ServerMethod(std::string
&&Binary
, char const * const Ver
,unsigned long const Flags
) :/*{{{*/
792 aptMethod(std::move(Binary
), Ver
, Flags
), Server(nullptr), File(NULL
), PipelineDepth(10),
793 AllowRedirect(false), Debug(false)
797 bool ServerMethod::Configuration(std::string Message
) /*{{{*/
799 if (aptMethod::Configuration(Message
) == false)
802 _config
->CndSet("Acquire::tor::Proxy",
803 "socks5h://apt-transport-tor@localhost:9050");
807 bool ServerMethod::AddProxyAuth(URI
&Proxy
, URI
const &Server
) const /*{{{*/
809 if (std::find(methodNames
.begin(), methodNames
.end(), "tor") != methodNames
.end() &&
810 Proxy
.User
== "apt-transport-tor" && Proxy
.Password
.empty())
812 std::string pass
= Server
.Host
;
813 pass
.erase(std::remove_if(pass
.begin(), pass
.end(), [](char const c
) { return std::isalnum(c
) == 0; }), pass
.end());
814 if (pass
.length() > 255)
815 Proxy
.Password
= pass
.substr(0, 255);
817 Proxy
.Password
= std::move(pass
);
819 // FIXME: should we support auth.conf for proxies?