]> git.saurik.com Git - apt.git/blob - methods/server.cc
CMake: Install bash completions via cmake
[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/configuration.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/strutl.h>
17
18 #include <ctype.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <iostream>
27 #include <limits>
28 #include <map>
29 #include <string>
30 #include <vector>
31
32 #include "server.h"
33
34 #include <apti18n.h>
35 /*}}}*/
36 using namespace std;
37
38 string ServerMethod::FailFile;
39 int ServerMethod::FailFd = -1;
40 time_t ServerMethod::FailTime = 0;
41
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)
48 {
49 State = Header;
50
51 Owner->Status(_("Waiting for headers"));
52
53 Major = 0;
54 Minor = 0;
55 Result = 0;
56 TotalFileSize = 0;
57 JunkSize = 0;
58 StartPos = 0;
59 Encoding = Closes;
60 HaveContent = false;
61 time(&Date);
62
63 do
64 {
65 string Data;
66 if (ReadHeaderLines(Data) == false)
67 continue;
68
69 if (Owner->Debug == true)
70 clog << "Answer for: " << Uri << endl << Data;
71
72 for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
73 {
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;
78 I = J;
79 }
80
81 // 100 Continue is a Nop...
82 if (Result == 100)
83 continue;
84
85 // Tidy up the connection persistence state.
86 if (Encoding == Closes && HaveContent == true)
87 Persistent = false;
88
89 return RUN_HEADERS_OK;
90 }
91 while (LoadNextResponse(false, File) == true);
92
93 return RUN_HEADERS_IO_ERROR;
94 }
95 /*}}}*/
96 // ServerState::HeaderLine - Process a header line /*{{{*/
97 // ---------------------------------------------------------------------
98 /* */
99 bool ServerState::HeaderLine(string Line)
100 {
101 if (Line.empty() == true)
102 return true;
103
104 if (Line.size() > 4 && stringcasecmp(Line.data(), Line.data()+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 != NULL && Owner->Debug == true)
114 clog << "HTTP server doesn't give Reason-Phrase for " << std::to_string(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
127 /* Check the HTTP response header to get the default persistence
128 state. */
129 if (Major < 1)
130 Persistent = false;
131 else
132 {
133 if (Major == 1 && Minor == 0)
134 {
135 Persistent = false;
136 }
137 else
138 {
139 Persistent = true;
140 if (PipelineAllowed)
141 Pipeline = true;
142 }
143 }
144
145 return true;
146 }
147
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"));
153 ++Pos;
154
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)
158 Pos2++;
159
160 string const Tag(Line,0,Pos);
161 string const Val(Line,Pos2);
162
163 if (stringcasecmp(Tag,"Content-Length:") == 0)
164 {
165 if (Encoding == Closes)
166 Encoding = Stream;
167 HaveContent = true;
168
169 unsigned long long * DownloadSizePtr = &DownloadSize;
170 if (Result == 416 || (Result >= 300 && Result < 400))
171 DownloadSizePtr = &JunkSize;
172
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)
177 HaveContent = false;
178
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
181 // header instead
182 if(Result != 206 && TotalFileSize == 0)
183 TotalFileSize = DownloadSize;
184
185 return true;
186 }
187
188 if (stringcasecmp(Tag,"Content-Type:") == 0)
189 {
190 HaveContent = true;
191 return true;
192 }
193
194 if (stringcasecmp(Tag,"Content-Range:") == 0)
195 {
196 HaveContent = true;
197
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"));
205
206 // figure out what we will download
207 DownloadSize = TotalFileSize - StartPos;
208 return true;
209 }
210
211 if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
212 {
213 HaveContent = true;
214 if (stringcasecmp(Val,"chunked") == 0)
215 Encoding = Chunked;
216 return true;
217 }
218
219 if (stringcasecmp(Tag,"Connection:") == 0)
220 {
221 if (stringcasecmp(Val,"close") == 0)
222 Persistent = false;
223 if (stringcasecmp(Val,"keep-alive") == 0)
224 Persistent = true;
225 return true;
226 }
227
228 if (stringcasecmp(Tag,"Last-Modified:") == 0)
229 {
230 if (RFC1123StrToTime(Val.c_str(), Date) == false)
231 return _error->Error(_("Unknown date format"));
232 return true;
233 }
234
235 if (stringcasecmp(Tag,"Location:") == 0)
236 {
237 Location = Val;
238 return true;
239 }
240
241 return true;
242 }
243 /*}}}*/
244 // ServerState::ServerState - Constructor /*{{{*/
245 ServerState::ServerState(URI Srv, ServerMethod *Owner) :
246 DownloadSize(0), ServerName(Srv), TimeOut(120), Owner(Owner)
247 {
248 Reset();
249 }
250 /*}}}*/
251 bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/
252 {
253 File.Truncate(StartPos);
254 return GetHashes()->AddFD(File, StartPos);
255 }
256 /*}}}*/
257
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).
262 */
263 ServerMethod::DealWithHeadersResult
264 ServerMethod::DealWithHeaders(FetchResult &Res)
265 {
266 // Not Modified
267 if (Server->Result == 304)
268 {
269 RemoveFile("server", Queue->DestFile);
270 Res.IMSHit = true;
271 Res.LastModified = Queue->LastModified;
272 Res.Size = 0;
273 return IMS_HIT;
274 }
275
276 /* Redirect
277 *
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.
282 */
283 if (AllowRedirect
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)
288 {
289 if (Server->Location.empty() == true)
290 ;
291 else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
292 {
293 URI Uri = Queue->Uri;
294 if (Uri.Host.empty() == false)
295 NextURI = URI::SiteOnly(Uri);
296 else
297 NextURI.clear();
298 NextURI.append(DeQuoteString(Server->Location));
299 if (Queue->Uri == NextURI)
300 {
301 SetFailReason("RedirectionLoop");
302 _error->Error("Redirection loop encountered");
303 if (Server->HaveContent == true)
304 return ERROR_WITH_CONTENT_PAGE;
305 return ERROR_UNRECOVERABLE;
306 }
307 return TRY_AGAIN_OR_REDIRECT;
308 }
309 else
310 {
311 NextURI = DeQuoteString(Server->Location);
312 URI tmpURI = NextURI;
313 if (tmpURI.Access.find('+') != std::string::npos)
314 {
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;
319 }
320 URI Uri = Queue->Uri;
321 if (Binary.find('+') != std::string::npos)
322 {
323 auto base = Binary.substr(0, Binary.find('+'));
324 if (base != tmpURI.Access)
325 {
326 tmpURI.Access = base + '+' + tmpURI.Access;
327 if (tmpURI.Access == Binary)
328 {
329 std::string tmpAccess = Uri.Access;
330 std::swap(tmpURI.Access, Uri.Access);
331 NextURI = tmpURI;
332 std::swap(tmpURI.Access, Uri.Access);
333 }
334 else
335 NextURI = tmpURI;
336 }
337 }
338 if (Queue->Uri == NextURI)
339 {
340 SetFailReason("RedirectionLoop");
341 _error->Error("Redirection loop encountered");
342 if (Server->HaveContent == true)
343 return ERROR_WITH_CONTENT_PAGE;
344 return ERROR_UNRECOVERABLE;
345 }
346 Uri.Access = Binary;
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;
353 else
354 {
355 auto const tmpplus = tmpURI.Access.find('+');
356 if (tmpplus != std::string::npos && tmpURI.Access.substr(tmpplus + 1) == "https")
357 {
358 auto const uriplus = Uri.Access.find('+');
359 if (uriplus == std::string::npos)
360 {
361 if (Uri.Access == tmpURI.Access.substr(0, tmpplus)) // foo -> foo+https
362 return TRY_AGAIN_OR_REDIRECT;
363 }
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;
367 }
368 }
369 _error->Error("Redirection from %s to '%s' is forbidden", Uri.Access.c_str(), NextURI.c_str());
370 }
371 /* else pass through for error message */
372 }
373 // retry after an invalid range response without partial data
374 else if (Server->Result == 416)
375 {
376 struct stat SBuf;
377 if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
378 {
379 bool partialHit = false;
380 if (Queue->ExpectedHashes.usable() == true)
381 {
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);
389 }
390 else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
391 partialHit = true;
392 if (partialHit == true)
393 {
394 // the file is completely downloaded, but was not moved
395 if (Server->HaveContent == true)
396 {
397 // nuke the sent error page
398 Server->RunDataToDevNull();
399 Server->HaveContent = false;
400 }
401 Server->StartPos = Server->TotalFileSize;
402 Server->Result = 200;
403 }
404 else if (RemoveFile("server", Queue->DestFile))
405 {
406 NextURI = Queue->Uri;
407 return TRY_AGAIN_OR_REDIRECT;
408 }
409 }
410 }
411
412 /* We have a reply we don't handle. This should indicate a perm server
413 failure */
414 if (Server->Result < 200 || Server->Result >= 300)
415 {
416 if (_error->PendingError() == false)
417 {
418 std::string err;
419 strprintf(err, "HttpError%u", Server->Result);
420 SetFailReason(err);
421 _error->Error("%u %s", Server->Result, Server->Code);
422 }
423 if (Server->HaveContent == true)
424 return ERROR_WITH_CONTENT_PAGE;
425 return ERROR_UNRECOVERABLE;
426 }
427
428 // This is some sort of 2xx 'data follows' reply
429 Res.LastModified = Server->Date;
430 Res.Size = Server->TotalFileSize;
431 return FILE_IS_OPEN;
432 }
433 /*}}}*/
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)
439 {
440 if (FailFd == -1)
441 _exit(100);
442
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);
448 close(FailFd);
449
450 _exit(100);
451 }
452 /*}}}*/
453 // ServerMethod::Fetch - Fetch an item /*{{{*/
454 // ---------------------------------------------------------------------
455 /* This adds an item to the pipeline. We keep the pipeline at a fixed
456 depth. */
457 bool ServerMethod::Fetch(FetchItem *)
458 {
459 if (Server == nullptr || QueueBack == nullptr)
460 return true;
461
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)
467 ++CurrentDepth;
468 if (CurrentDepth > AllowedDepth)
469 return true;
470
471 do {
472 // Make sure we stick with the same server
473 if (Server->Comp(QueueBack->Uri) == false)
474 break;
475
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)
480 break;
481
482 if (UsableHashes && FileExists(QueueBack->DestFile))
483 {
484 FileFd partial(QueueBack->DestFile, FileFd::ReadOnly);
485 Hashes wehave(QueueBack->ExpectedHashes);
486 if (QueueBack->ExpectedHashes.FileSize() == partial.FileSize())
487 {
488 if (wehave.AddFD(partial) &&
489 wehave.GetHashStringList() == QueueBack->ExpectedHashes)
490 {
491 FetchResult Res;
492 Res.Filename = QueueBack->DestFile;
493 Res.ResumePoint = QueueBack->ExpectedHashes.FileSize();
494 URIStart(Res);
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)
498 {
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;
504 Queue = QueueBack;
505 QueueBack = Prev->Next;
506 }
507 Res.TakeHashes(wehave);
508 URIDone(Res);
509 continue;
510 }
511 else
512 RemoveFile("Fetch-Partial", QueueBack->DestFile);
513 }
514 }
515 auto const Tmp = QueueBack;
516 QueueBack = QueueBack->Next;
517 SendReq(Tmp);
518 ++CurrentDepth;
519 } while (CurrentDepth <= AllowedDepth && QueueBack != nullptr);
520
521 return true;
522 }
523 /*}}}*/
524 // ServerMethod::Loop - Main loop /*{{{*/
525 int ServerMethod::Loop()
526 {
527 signal(SIGTERM,SigTerm);
528 signal(SIGINT,SigTerm);
529
530 Server = 0;
531
532 int FailCounter = 0;
533 while (1)
534 {
535 // We have no commands, wait for some to arrive
536 if (Queue == 0)
537 {
538 if (WaitFd(STDIN_FILENO) == false)
539 return 0;
540 }
541
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))
546 {
547 if(FailReason.empty() == false ||
548 ConfigFindB("DependOnSTDIN", true) == true)
549 return 100;
550 else
551 return 0;
552 }
553
554 if (Queue == 0)
555 continue;
556
557 // Connect to the server
558 if (Server == 0 || Server->Comp(Queue->Uri) == false)
559 {
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();
565 }
566
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
571 complete */
572 if (Server->Persistent == false)
573 Server->Close();
574
575 // Reset the pipeline
576 if (Server->IsOpen() == false)
577 QueueBack = Queue;
578
579 // Connnect to the host
580 if (Server->Open() == false)
581 {
582 Fail(true);
583 Server = nullptr;
584 continue;
585 }
586
587 // Fill the pipeline.
588 Fetch(0);
589
590 // Fetch the next URL header data from the server.
591 switch (Server->RunHeaders(File, Queue->Uri))
592 {
593 case ServerState::RUN_HEADERS_OK:
594 break;
595
596 // The header data is bad
597 case ServerState::RUN_HEADERS_PARSE_ERROR:
598 {
599 _error->Error(_("Bad header data"));
600 Fail(true);
601 Server->Close();
602 RotateDNS();
603 continue;
604 }
605
606 // The server closed a connection during the header get..
607 default:
608 case ServerState::RUN_HEADERS_IO_ERROR:
609 {
610 FailCounter++;
611 _error->Discard();
612 Server->Close();
613 Server->Pipeline = false;
614 Server->PipelineAllowed = false;
615
616 if (FailCounter >= 2)
617 {
618 Fail(_("Connection failed"),true);
619 FailCounter = 0;
620 }
621
622 RotateDNS();
623 continue;
624 }
625 };
626
627 // Decide what to do.
628 FetchResult Res;
629 Res.Filename = Queue->DestFile;
630 switch (DealWithHeaders(Res))
631 {
632 // Ok, the file is Open
633 case FILE_IS_OPEN:
634 {
635 URIStart(Res);
636
637 // Run the data
638 bool Result = true;
639
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();
645
646 if (Server->HaveContent)
647 Result = Server->RunData(File);
648
649 /* If the server is sending back sizeless responses then fill in
650 the size now */
651 if (Res.Size == 0)
652 Res.Size = File->Size();
653
654 // Close the file, destroy the FD object and timestamp it
655 FailFd = -1;
656 delete File;
657 File = 0;
658
659 // Timestamp
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);
664
665 // Send status to APT
666 if (Result == true)
667 {
668 Hashes * const resultHashes = Server->GetHashes();
669 HashStringList const hashList = resultHashes->GetHashStringList();
670 if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
671 {
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)
676 {
677 if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
678 {
679 // yes, he did! Disable pipelining and rewrite queue
680 if (Server->Pipeline == true)
681 {
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
686 }
687 Rename(Res.Filename, I->DestFile);
688 Res.Filename = I->DestFile;
689 BeforeI->Next = I->Next;
690 I->Next = Queue;
691 Queue = I;
692 break;
693 }
694 BeforeI = I;
695 }
696 }
697 Res.TakeHashes(*resultHashes);
698 URIDone(Res);
699 }
700 else
701 {
702 if (Server->IsOpen() == false)
703 {
704 FailCounter++;
705 _error->Discard();
706 Server->Close();
707
708 if (FailCounter >= 2)
709 {
710 Fail(_("Connection failed"),true);
711 FailCounter = 0;
712 }
713
714 QueueBack = Queue;
715 }
716 else
717 {
718 Server->Close();
719 Fail(true);
720 }
721 }
722 break;
723 }
724
725 // IMS hit
726 case IMS_HIT:
727 {
728 URIDone(Res);
729 break;
730 }
731
732 // Hard server error, not found or something
733 case ERROR_UNRECOVERABLE:
734 {
735 Fail();
736 break;
737 }
738
739 // Hard internal error, kill the connection and fail
740 case ERROR_NOT_FROM_SERVER:
741 {
742 delete File;
743 File = 0;
744
745 Fail();
746 RotateDNS();
747 Server->Close();
748 break;
749 }
750
751 // We need to flush the data, the header is like a 404 w/ error text
752 case ERROR_WITH_CONTENT_PAGE:
753 {
754 Fail();
755 Server->RunDataToDevNull();
756 break;
757 }
758
759 // Try again with a new URL
760 case TRY_AGAIN_OR_REDIRECT:
761 {
762 // Clear rest of response if there is content
763 if (Server->HaveContent)
764 Server->RunDataToDevNull();
765 Redirect(NextURI);
766 break;
767 }
768
769 default:
770 Fail(_("Internal error"));
771 break;
772 }
773
774 FailCounter = 0;
775 }
776
777 return 0;
778 }
779 /*}}}*/
780 unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/
781 {
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;
786 }
787 /*}}}*/
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)
791 {
792 }
793 /*}}}*/
794 bool ServerMethod::Configuration(std::string Message) /*{{{*/
795 {
796 if (aptMethod::Configuration(Message) == false)
797 return false;
798
799 _config->CndSet("Acquire::tor::Proxy",
800 "socks5h://apt-transport-tor@localhost:9050");
801 return true;
802 }
803 /*}}}*/
804 bool ServerMethod::AddProxyAuth(URI &Proxy, URI const &Server) const /*{{{*/
805 {
806 if (std::find(methodNames.begin(), methodNames.end(), "tor") != methodNames.end() &&
807 Proxy.User == "apt-transport-tor" && Proxy.Password.empty())
808 {
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);
813 else
814 Proxy.Password = std::move(pass);
815 }
816 // FIXME: should we support auth.conf for proxies?
817 return true;
818 }
819 /*}}}*/