]>
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 if (Line
.size() > 4 && stringcasecmp(Line
.data(), Line
.data()+4, "HTTP") == 0)
106 // Evil servers return no version
109 int const elements
= sscanf(Line
.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major
,&Minor
,&Result
,Code
);
113 if (Owner
!= NULL
&& Owner
->Debug
== true)
114 clog
<< "HTTP server doesn't give Reason-Phrase for " << std::to_string(Result
) << std::endl
;
116 else if (elements
!= 4)
117 return _error
->Error(_("The HTTP server sent an invalid reply header"));
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"));
127 /* Check the HTTP response header to get the default persistence
133 if (Major
== 1 && Minor
== 0)
148 // Blah, some servers use "connection:closes", evil.
149 // and some even send empty header fields…
150 string::size_type Pos
= Line
.find(':');
151 if (Pos
== string::npos
)
152 return _error
->Error(_("Bad header line"));
155 // Parse off any trailing spaces between the : and the next word.
156 string::size_type Pos2
= Pos
;
157 while (Pos2
< Line
.length() && isspace_ascii(Line
[Pos2
]) != 0)
160 string
const Tag(Line
,0,Pos
);
161 string
const Val(Line
,Pos2
);
163 if (stringcasecmp(Tag
,"Content-Length:") == 0)
165 if (Encoding
== Closes
)
169 unsigned long long * DownloadSizePtr
= &DownloadSize
;
170 if (Result
== 416 || (Result
>= 300 && Result
< 400))
171 DownloadSizePtr
= &JunkSize
;
173 *DownloadSizePtr
= strtoull(Val
.c_str(), NULL
, 10);
174 if (*DownloadSizePtr
>= std::numeric_limits
<unsigned long long>::max())
175 return _error
->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
176 else if (*DownloadSizePtr
== 0)
179 // On partial content (206) the Content-Length less than the real
180 // size, so do not set it here but leave that to the Content-Range
182 if(Result
!= 206 && TotalFileSize
== 0)
183 TotalFileSize
= DownloadSize
;
188 if (stringcasecmp(Tag
,"Content-Type:") == 0)
194 if (stringcasecmp(Tag
,"Content-Range:") == 0)
198 // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
199 if (Result
== 416 && sscanf(Val
.c_str(), "bytes */%llu",&TotalFileSize
) == 1)
200 ; // we got the expected filesize which is all we wanted
201 else if (sscanf(Val
.c_str(),"bytes %llu-%*u/%llu",&StartPos
,&TotalFileSize
) != 2)
202 return _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
203 if ((unsigned long long)StartPos
> TotalFileSize
)
204 return _error
->Error(_("This HTTP server has broken range support"));
206 // figure out what we will download
207 DownloadSize
= TotalFileSize
- StartPos
;
211 if (stringcasecmp(Tag
,"Transfer-Encoding:") == 0)
214 if (stringcasecmp(Val
,"chunked") == 0)
219 if (stringcasecmp(Tag
,"Connection:") == 0)
221 if (stringcasecmp(Val
,"close") == 0)
223 if (stringcasecmp(Val
,"keep-alive") == 0)
228 if (stringcasecmp(Tag
,"Last-Modified:") == 0)
230 if (RFC1123StrToTime(Val
.c_str(), Date
) == false)
231 return _error
->Error(_("Unknown date format"));
235 if (stringcasecmp(Tag
,"Location:") == 0)
244 // ServerState::ServerState - Constructor /*{{{*/
245 ServerState::ServerState(URI Srv
, ServerMethod
*Owner
) :
246 DownloadSize(0), ServerName(Srv
), TimeOut(120), Owner(Owner
)
251 bool ServerState::AddPartialFileToHashes(FileFd
&File
) /*{{{*/
253 File
.Truncate(StartPos
);
254 return GetHashes()->AddFD(File
, StartPos
);
258 // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
259 // ---------------------------------------------------------------------
260 /* We look at the header data we got back from the server and decide what
261 to do. Returns DealWithHeadersResult (see http.h for details).
263 ServerMethod::DealWithHeadersResult
264 ServerMethod::DealWithHeaders(FetchResult
&Res
)
267 if (Server
->Result
== 304)
269 RemoveFile("server", Queue
->DestFile
);
271 Res
.LastModified
= Queue
->LastModified
;
278 * Note that it is only OK for us to treat all redirection the same
279 * because we *always* use GET, not other HTTP methods. There are
280 * three redirection codes for which it is not appropriate that we
281 * redirect. Pass on those codes so the error handling kicks in.
284 && (Server
->Result
> 300 && Server
->Result
< 400)
285 && (Server
->Result
!= 300 // Multiple Choices
286 && Server
->Result
!= 304 // Not Modified
287 && Server
->Result
!= 306)) // (Not part of HTTP/1.1, reserved)
289 if (Server
->Location
.empty() == true)
291 else if (Server
->Location
[0] == '/' && Queue
->Uri
.empty() == false)
293 URI Uri
= Queue
->Uri
;
294 if (Uri
.Host
.empty() == false)
295 NextURI
= URI::SiteOnly(Uri
);
298 NextURI
.append(DeQuoteString(Server
->Location
));
299 if (Queue
->Uri
== NextURI
)
301 SetFailReason("RedirectionLoop");
302 _error
->Error("Redirection loop encountered");
303 if (Server
->HaveContent
== true)
304 return ERROR_WITH_CONTENT_PAGE
;
305 return ERROR_UNRECOVERABLE
;
307 return TRY_AGAIN_OR_REDIRECT
;
311 NextURI
= DeQuoteString(Server
->Location
);
312 URI tmpURI
= NextURI
;
313 if (tmpURI
.Access
.find('+') != std::string::npos
)
315 _error
->Error("Server tried to trick us into using a specific implementation: %s", tmpURI
.Access
.c_str());
316 if (Server
->HaveContent
== true)
317 return ERROR_WITH_CONTENT_PAGE
;
318 return ERROR_UNRECOVERABLE
;
320 URI Uri
= Queue
->Uri
;
321 if (Binary
.find('+') != std::string::npos
)
323 auto base
= Binary
.substr(0, Binary
.find('+'));
324 if (base
!= tmpURI
.Access
)
326 tmpURI
.Access
= base
+ '+' + tmpURI
.Access
;
327 if (tmpURI
.Access
== Binary
)
329 std::string tmpAccess
= Uri
.Access
;
330 std::swap(tmpURI
.Access
, Uri
.Access
);
332 std::swap(tmpURI
.Access
, Uri
.Access
);
338 if (Queue
->Uri
== NextURI
)
340 SetFailReason("RedirectionLoop");
341 _error
->Error("Redirection loop encountered");
342 if (Server
->HaveContent
== true)
343 return ERROR_WITH_CONTENT_PAGE
;
344 return ERROR_UNRECOVERABLE
;
347 // same protocol redirects are okay
348 if (tmpURI
.Access
== Uri
.Access
)
349 return TRY_AGAIN_OR_REDIRECT
;
350 // as well as http to https
351 else if ((Uri
.Access
== "http" || Uri
.Access
== "https+http") && tmpURI
.Access
== "https")
352 return TRY_AGAIN_OR_REDIRECT
;
355 auto const tmpplus
= tmpURI
.Access
.find('+');
356 if (tmpplus
!= std::string::npos
&& tmpURI
.Access
.substr(tmpplus
+ 1) == "https")
358 auto const uriplus
= Uri
.Access
.find('+');
359 if (uriplus
== std::string::npos
)
361 if (Uri
.Access
== tmpURI
.Access
.substr(0, tmpplus
)) // foo -> foo+https
362 return TRY_AGAIN_OR_REDIRECT
;
364 else if (Uri
.Access
.substr(uriplus
+ 1) == "http" &&
365 Uri
.Access
.substr(0, uriplus
) == tmpURI
.Access
.substr(0, tmpplus
)) // foo+http -> foo+https
366 return TRY_AGAIN_OR_REDIRECT
;
369 _error
->Error("Redirection from %s to '%s' is forbidden", Uri
.Access
.c_str(), NextURI
.c_str());
371 /* else pass through for error message */
373 // retry after an invalid range response without partial data
374 else if (Server
->Result
== 416)
377 if (stat(Queue
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
379 bool partialHit
= false;
380 if (Queue
->ExpectedHashes
.usable() == true)
382 Hashes
resultHashes(Queue
->ExpectedHashes
);
383 FileFd
file(Queue
->DestFile
, FileFd::ReadOnly
);
384 Server
->TotalFileSize
= file
.FileSize();
385 Server
->Date
= file
.ModificationTime();
386 resultHashes
.AddFD(file
);
387 HashStringList
const hashList
= resultHashes
.GetHashStringList();
388 partialHit
= (Queue
->ExpectedHashes
== hashList
);
390 else if ((unsigned long long)SBuf
.st_size
== Server
->TotalFileSize
)
392 if (partialHit
== true)
394 // the file is completely downloaded, but was not moved
395 if (Server
->HaveContent
== true)
397 // nuke the sent error page
398 Server
->RunDataToDevNull();
399 Server
->HaveContent
= false;
401 Server
->StartPos
= Server
->TotalFileSize
;
402 Server
->Result
= 200;
404 else if (RemoveFile("server", Queue
->DestFile
))
406 NextURI
= Queue
->Uri
;
407 return TRY_AGAIN_OR_REDIRECT
;
412 /* We have a reply we don't handle. This should indicate a perm server
414 if (Server
->Result
< 200 || Server
->Result
>= 300)
416 if (_error
->PendingError() == false)
419 strprintf(err
, "HttpError%u", Server
->Result
);
421 _error
->Error("%u %s", Server
->Result
, Server
->Code
);
423 if (Server
->HaveContent
== true)
424 return ERROR_WITH_CONTENT_PAGE
;
425 return ERROR_UNRECOVERABLE
;
428 // This is some sort of 2xx 'data follows' reply
429 Res
.LastModified
= Server
->Date
;
430 Res
.Size
= Server
->TotalFileSize
;
434 // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
435 // ---------------------------------------------------------------------
436 /* This closes and timestamps the open file. This is necessary to get
437 resume behavoir on user abort */
438 void ServerMethod::SigTerm(int)
443 struct timeval times
[2];
444 times
[0].tv_sec
= FailTime
;
445 times
[1].tv_sec
= FailTime
;
446 times
[0].tv_usec
= times
[1].tv_usec
= 0;
447 utimes(FailFile
.c_str(), times
);
453 // ServerMethod::Fetch - Fetch an item /*{{{*/
454 // ---------------------------------------------------------------------
455 /* This adds an item to the pipeline. We keep the pipeline at a fixed
457 bool ServerMethod::Fetch(FetchItem
*)
459 if (Server
== nullptr || QueueBack
== nullptr)
462 // If pipelining is disabled, we only queue 1 request
463 auto const AllowedDepth
= Server
->Pipeline
? PipelineDepth
: 0;
464 // how deep is our pipeline currently?
465 decltype(PipelineDepth
) CurrentDepth
= 0;
466 for (FetchItem
const *I
= Queue
; I
!= QueueBack
; I
= I
->Next
)
468 if (CurrentDepth
> AllowedDepth
)
472 // Make sure we stick with the same server
473 if (Server
->Comp(QueueBack
->Uri
) == false)
476 bool const UsableHashes
= QueueBack
->ExpectedHashes
.usable();
477 // if we have no hashes, do at most one such request
478 // as we can't fixup pipeling misbehaviors otherwise
479 if (CurrentDepth
!= 0 && UsableHashes
== false)
482 if (UsableHashes
&& FileExists(QueueBack
->DestFile
))
484 FileFd
partial(QueueBack
->DestFile
, FileFd::ReadOnly
);
485 Hashes
wehave(QueueBack
->ExpectedHashes
);
486 if (QueueBack
->ExpectedHashes
.FileSize() == partial
.FileSize())
488 if (wehave
.AddFD(partial
) &&
489 wehave
.GetHashStringList() == QueueBack
->ExpectedHashes
)
492 Res
.Filename
= QueueBack
->DestFile
;
493 Res
.ResumePoint
= QueueBack
->ExpectedHashes
.FileSize();
495 // move item to the start of the queue as URIDone will
496 // always dequeued the first item in the queue
497 if (Queue
!= QueueBack
)
499 FetchItem
*Prev
= Queue
;
500 for (; Prev
->Next
!= QueueBack
; Prev
= Prev
->Next
)
501 /* look for the previous queue item */;
502 Prev
->Next
= QueueBack
->Next
;
503 QueueBack
->Next
= Queue
;
505 QueueBack
= Prev
->Next
;
507 Res
.TakeHashes(wehave
);
512 RemoveFile("Fetch-Partial", QueueBack
->DestFile
);
515 auto const Tmp
= QueueBack
;
516 QueueBack
= QueueBack
->Next
;
519 } while (CurrentDepth
<= AllowedDepth
&& QueueBack
!= nullptr);
524 // ServerMethod::Loop - Main loop /*{{{*/
525 int ServerMethod::Loop()
527 signal(SIGTERM
,SigTerm
);
528 signal(SIGINT
,SigTerm
);
535 // We have no commands, wait for some to arrive
538 if (WaitFd(STDIN_FILENO
) == false)
542 /* Run messages, we can accept 0 (no message) if we didn't
543 do a WaitFd above.. Otherwise the FD is closed. */
544 int Result
= Run(true);
545 if (Result
!= -1 && (Result
!= 0 || Queue
== 0))
547 if(FailReason
.empty() == false ||
548 ConfigFindB("DependOnSTDIN", true) == true)
557 // Connect to the server
558 if (Server
== 0 || Server
->Comp(Queue
->Uri
) == false)
560 Server
= CreateServerState(Queue
->Uri
);
561 setPostfixForMethodNames(::URI(Queue
->Uri
).Host
.c_str());
562 AllowRedirect
= ConfigFindB("AllowRedirect", true);
563 PipelineDepth
= ConfigFindI("Pipeline-Depth", 10);
564 Debug
= DebugEnabled();
567 /* If the server has explicitly said this is the last connection
568 then we pre-emptively shut down the pipeline and tear down
569 the connection. This will speed up HTTP/1.0 servers a tad
570 since we don't have to wait for the close sequence to
572 if (Server
->Persistent
== false)
575 // Reset the pipeline
576 if (Server
->IsOpen() == false)
579 // Connnect to the host
580 if (Server
->Open() == false)
587 // Fill the pipeline.
590 // Fetch the next URL header data from the server.
591 switch (Server
->RunHeaders(File
, Queue
->Uri
))
593 case ServerState::RUN_HEADERS_OK
:
596 // The header data is bad
597 case ServerState::RUN_HEADERS_PARSE_ERROR
:
599 _error
->Error(_("Bad header data"));
606 // The server closed a connection during the header get..
608 case ServerState::RUN_HEADERS_IO_ERROR
:
613 Server
->Pipeline
= false;
614 Server
->PipelineAllowed
= false;
616 if (FailCounter
>= 2)
618 Fail(_("Connection failed"),true);
627 // Decide what to do.
629 Res
.Filename
= Queue
->DestFile
;
630 switch (DealWithHeaders(Res
))
632 // Ok, the file is Open
640 // ensure we don't fetch too much
641 // we could do "Server->MaximumSize = Queue->MaximumSize" here
642 // but that would break the clever pipeline messup detection
643 // so instead we use the size of the biggest item in the queue
644 Server
->MaximumSize
= FindMaximumObjectSizeInQueue();
646 if (Server
->HaveContent
)
647 Result
= Server
->RunData(File
);
649 /* If the server is sending back sizeless responses then fill in
652 Res
.Size
= File
->Size();
654 // Close the file, destroy the FD object and timestamp it
660 struct timeval times
[2];
661 times
[0].tv_sec
= times
[1].tv_sec
= Server
->Date
;
662 times
[0].tv_usec
= times
[1].tv_usec
= 0;
663 utimes(Queue
->DestFile
.c_str(), times
);
665 // Send status to APT
668 Hashes
* const resultHashes
= Server
->GetHashes();
669 HashStringList
const hashList
= resultHashes
->GetHashStringList();
670 if (PipelineDepth
!= 0 && Queue
->ExpectedHashes
.usable() == true && Queue
->ExpectedHashes
!= hashList
)
672 // we did not get the expected hash… mhhh:
673 // could it be that server/proxy messed up pipelining?
674 FetchItem
* BeforeI
= Queue
;
675 for (FetchItem
*I
= Queue
->Next
; I
!= 0 && I
!= QueueBack
; I
= I
->Next
)
677 if (I
->ExpectedHashes
.usable() == true && I
->ExpectedHashes
== hashList
)
679 // yes, he did! Disable pipelining and rewrite queue
680 if (Server
->Pipeline
== true)
682 Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
683 Server
->Pipeline
= false;
684 Server
->PipelineAllowed
= false;
685 // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
687 Rename(Res
.Filename
, I
->DestFile
);
688 Res
.Filename
= I
->DestFile
;
689 BeforeI
->Next
= I
->Next
;
697 Res
.TakeHashes(*resultHashes
);
702 if (Server
->IsOpen() == false)
708 if (FailCounter
>= 2)
710 Fail(_("Connection failed"),true);
732 // Hard server error, not found or something
733 case ERROR_UNRECOVERABLE
:
739 // Hard internal error, kill the connection and fail
740 case ERROR_NOT_FROM_SERVER
:
751 // We need to flush the data, the header is like a 404 w/ error text
752 case ERROR_WITH_CONTENT_PAGE
:
755 Server
->RunDataToDevNull();
759 // Try again with a new URL
760 case TRY_AGAIN_OR_REDIRECT
:
762 // Clear rest of response if there is content
763 if (Server
->HaveContent
)
764 Server
->RunDataToDevNull();
770 Fail(_("Internal error"));
780 unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
782 unsigned long long MaxSizeInQueue
= 0;
783 for (FetchItem
*I
= Queue
; I
!= 0 && I
!= QueueBack
; I
= I
->Next
)
784 MaxSizeInQueue
= std::max(MaxSizeInQueue
, I
->MaximumSize
);
785 return MaxSizeInQueue
;
788 ServerMethod::ServerMethod(std::string
&&Binary
, char const * const Ver
,unsigned long const Flags
) :/*{{{*/
789 aptMethod(std::move(Binary
), Ver
, Flags
), Server(nullptr), File(NULL
), PipelineDepth(10),
790 AllowRedirect(false), Debug(false)
794 bool ServerMethod::Configuration(std::string Message
) /*{{{*/
796 if (aptMethod::Configuration(Message
) == false)
799 _config
->CndSet("Acquire::tor::Proxy",
800 "socks5h://apt-transport-tor@localhost:9050");
804 bool ServerMethod::AddProxyAuth(URI
&Proxy
, URI
const &Server
) const /*{{{*/
806 if (std::find(methodNames
.begin(), methodNames
.end(), "tor") != methodNames
.end() &&
807 Proxy
.User
== "apt-transport-tor" && Proxy
.Password
.empty())
809 std::string pass
= Server
.Host
;
810 pass
.erase(std::remove_if(pass
.begin(), pass
.end(), [](char const c
) { return std::isalnum(c
) == 0; }), pass
.end());
811 if (pass
.length() > 255)
812 Proxy
.Password
= pass
.substr(0, 255);
814 Proxy
.Password
= std::move(pass
);
816 // FIXME: should we support auth.conf for proxies?