* [ABI break] merge support for http redirects, thanks to
[ntk/apt.git] / methods / http.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
5
6 HTTP Acquire Method - This is the HTTP aquire method for APT.
7
8 It uses HTTP/1.1 and many of the fancy options there-in, such as
9 pipelining, range, if-range and so on.
10
11 It is based on a doubly buffered select loop. A groupe of requests are
12 fed into a single output buffer that is constantly fed out the
13 socket. This provides ideal pipelining as in many cases all of the
14 requests will fit into a single packet. The input socket is buffered
15 the same way and fed into the fd for the file (may be a pipe in future).
16
17 This double buffering provides fairly substantial transfer rates,
18 compared to wget the http method is about 4% faster. Most importantly,
19 when HTTP is compared with FTP as a protocol the speed difference is
20 huge. In tests over the internet from two sites to llug (via ATM) this
21 program got 230k/s sustained http transfer rates. FTP on the other
22 hand topped out at 170k/s. That combined with the time to setup the
23 FTP connection makes HTTP a vastly superior protocol.
24
25 ##################################################################### */
26 /*}}}*/
27 // Include Files /*{{{*/
28 #include <apt-pkg/fileutl.h>
29 #include <apt-pkg/acquire-method.h>
30 #include <apt-pkg/error.h>
31 #include <apt-pkg/hashes.h>
32
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <utime.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <map>
42 #include <apti18n.h>
43
44 // Internet stuff
45 #include <netdb.h>
46
47 #include "config.h"
48 #include "connect.h"
49 #include "rfc2553emu.h"
50 #include "http.h"
51
52 /*}}}*/
53 using namespace std;
54
55 string HttpMethod::FailFile;
56 int HttpMethod::FailFd = -1;
57 time_t HttpMethod::FailTime = 0;
58 unsigned long PipelineDepth = 10;
59 unsigned long TimeOut = 120;
60 bool AllowRedirect = false;
61 bool Debug = false;
62 URI Proxy;
63
64 unsigned long CircleBuf::BwReadLimit=0;
65 unsigned long CircleBuf::BwTickReadData=0;
66 struct timeval CircleBuf::BwReadTick={0,0};
67 const unsigned int CircleBuf::BW_HZ=10;
68
69 // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
70 // ---------------------------------------------------------------------
71 /* */
72 CircleBuf::CircleBuf(unsigned long Size) : Size(Size), Hash(0)
73 {
74 Buf = new unsigned char[Size];
75 Reset();
76
77 CircleBuf::BwReadLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024;
78 }
79 /*}}}*/
80 // CircleBuf::Reset - Reset to the default state /*{{{*/
81 // ---------------------------------------------------------------------
82 /* */
83 void CircleBuf::Reset()
84 {
85 InP = 0;
86 OutP = 0;
87 StrPos = 0;
88 MaxGet = (unsigned int)-1;
89 OutQueue = string();
90 if (Hash != 0)
91 {
92 delete Hash;
93 Hash = new Hashes;
94 }
95 };
96 /*}}}*/
97 // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
98 // ---------------------------------------------------------------------
99 /* This fills up the buffer with as much data as is in the FD, assuming it
100 is non-blocking.. */
101 bool CircleBuf::Read(int Fd)
102 {
103 unsigned long BwReadMax;
104
105 while (1)
106 {
107 // Woops, buffer is full
108 if (InP - OutP == Size)
109 return true;
110
111 // what's left to read in this tick
112 BwReadMax = CircleBuf::BwReadLimit/BW_HZ;
113
114 if(CircleBuf::BwReadLimit) {
115 struct timeval now;
116 gettimeofday(&now,0);
117
118 unsigned long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 +
119 now.tv_usec-CircleBuf::BwReadTick.tv_usec;
120 if(d > 1000000/BW_HZ) {
121 CircleBuf::BwReadTick = now;
122 CircleBuf::BwTickReadData = 0;
123 }
124
125 if(CircleBuf::BwTickReadData >= BwReadMax) {
126 usleep(1000000/BW_HZ);
127 return true;
128 }
129 }
130
131 // Write the buffer segment
132 int Res;
133 if(CircleBuf::BwReadLimit) {
134 Res = read(Fd,Buf + (InP%Size),
135 BwReadMax > LeftRead() ? LeftRead() : BwReadMax);
136 } else
137 Res = read(Fd,Buf + (InP%Size),LeftRead());
138
139 if(Res > 0 && BwReadLimit > 0)
140 CircleBuf::BwTickReadData += Res;
141
142 if (Res == 0)
143 return false;
144 if (Res < 0)
145 {
146 if (errno == EAGAIN)
147 return true;
148 return false;
149 }
150
151 if (InP == 0)
152 gettimeofday(&Start,0);
153 InP += Res;
154 }
155 }
156 /*}}}*/
157 // CircleBuf::Read - Put the string into the buffer /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This will hold the string in and fill the buffer with it as it empties */
160 bool CircleBuf::Read(string Data)
161 {
162 OutQueue += Data;
163 FillOut();
164 return true;
165 }
166 /*}}}*/
167 // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
168 // ---------------------------------------------------------------------
169 /* */
170 void CircleBuf::FillOut()
171 {
172 if (OutQueue.empty() == true)
173 return;
174 while (1)
175 {
176 // Woops, buffer is full
177 if (InP - OutP == Size)
178 return;
179
180 // Write the buffer segment
181 unsigned long Sz = LeftRead();
182 if (OutQueue.length() - StrPos < Sz)
183 Sz = OutQueue.length() - StrPos;
184 memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz);
185
186 // Advance
187 StrPos += Sz;
188 InP += Sz;
189 if (OutQueue.length() == StrPos)
190 {
191 StrPos = 0;
192 OutQueue = "";
193 return;
194 }
195 }
196 }
197 /*}}}*/
198 // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This empties the buffer into the FD. */
201 bool CircleBuf::Write(int Fd)
202 {
203 while (1)
204 {
205 FillOut();
206
207 // Woops, buffer is empty
208 if (OutP == InP)
209 return true;
210
211 if (OutP == MaxGet)
212 return true;
213
214 // Write the buffer segment
215 int Res;
216 Res = write(Fd,Buf + (OutP%Size),LeftWrite());
217
218 if (Res == 0)
219 return false;
220 if (Res < 0)
221 {
222 if (errno == EAGAIN)
223 return true;
224
225 return false;
226 }
227
228 if (Hash != 0)
229 Hash->Add(Buf + (OutP%Size),Res);
230
231 OutP += Res;
232 }
233 }
234 /*}}}*/
235 // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
236 // ---------------------------------------------------------------------
237 /* This copies till the first empty line */
238 bool CircleBuf::WriteTillEl(string &Data,bool Single)
239 {
240 // We cheat and assume it is unneeded to have more than one buffer load
241 for (unsigned long I = OutP; I < InP; I++)
242 {
243 if (Buf[I%Size] != '\n')
244 continue;
245 ++I;
246
247 if (Single == false)
248 {
249 if (I < InP && Buf[I%Size] == '\r')
250 ++I;
251 if (I >= InP || Buf[I%Size] != '\n')
252 continue;
253 ++I;
254 }
255
256 Data = "";
257 while (OutP < I)
258 {
259 unsigned long Sz = LeftWrite();
260 if (Sz == 0)
261 return false;
262 if (I - OutP < Sz)
263 Sz = I - OutP;
264 Data += string((char *)(Buf + (OutP%Size)),Sz);
265 OutP += Sz;
266 }
267 return true;
268 }
269 return false;
270 }
271 /*}}}*/
272 // CircleBuf::Stats - Print out stats information /*{{{*/
273 // ---------------------------------------------------------------------
274 /* */
275 void CircleBuf::Stats()
276 {
277 if (InP == 0)
278 return;
279
280 struct timeval Stop;
281 gettimeofday(&Stop,0);
282 /* float Diff = Stop.tv_sec - Start.tv_sec +
283 (float)(Stop.tv_usec - Start.tv_usec)/1000000;
284 clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
285 }
286 /*}}}*/
287
288 // ServerState::ServerState - Constructor /*{{{*/
289 // ---------------------------------------------------------------------
290 /* */
291 ServerState::ServerState(URI Srv,HttpMethod *Owner) : Owner(Owner),
292 In(64*1024), Out(4*1024),
293 ServerName(Srv)
294 {
295 Reset();
296 }
297 /*}}}*/
298 // ServerState::Open - Open a connection to the server /*{{{*/
299 // ---------------------------------------------------------------------
300 /* This opens a connection to the server. */
301 bool ServerState::Open()
302 {
303 // Use the already open connection if possible.
304 if (ServerFd != -1)
305 return true;
306
307 Close();
308 In.Reset();
309 Out.Reset();
310 Persistent = true;
311
312 // Determine the proxy setting
313 if (getenv("http_proxy") == 0)
314 {
315 string DefProxy = _config->Find("Acquire::http::Proxy");
316 string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
317 if (SpecificProxy.empty() == false)
318 {
319 if (SpecificProxy == "DIRECT")
320 Proxy = "";
321 else
322 Proxy = SpecificProxy;
323 }
324 else
325 Proxy = DefProxy;
326 }
327 else
328 Proxy = getenv("http_proxy");
329
330 // Parse no_proxy, a , separated list of domains
331 if (getenv("no_proxy") != 0)
332 {
333 if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
334 Proxy = "";
335 }
336
337 // Determine what host and port to use based on the proxy settings
338 int Port = 0;
339 string Host;
340 if (Proxy.empty() == true || Proxy.Host.empty() == true)
341 {
342 if (ServerName.Port != 0)
343 Port = ServerName.Port;
344 Host = ServerName.Host;
345 }
346 else
347 {
348 if (Proxy.Port != 0)
349 Port = Proxy.Port;
350 Host = Proxy.Host;
351 }
352
353 // Connect to the remote server
354 if (Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner) == false)
355 return false;
356
357 return true;
358 }
359 /*}}}*/
360 // ServerState::Close - Close a connection to the server /*{{{*/
361 // ---------------------------------------------------------------------
362 /* */
363 bool ServerState::Close()
364 {
365 close(ServerFd);
366 ServerFd = -1;
367 return true;
368 }
369 /*}}}*/
370 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
371 // ---------------------------------------------------------------------
372 /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
373 parse error occurred */
374 int ServerState::RunHeaders()
375 {
376 State = Header;
377
378 Owner->Status(_("Waiting for headers"));
379
380 Major = 0;
381 Minor = 0;
382 Result = 0;
383 Size = 0;
384 StartPos = 0;
385 Encoding = Closes;
386 HaveContent = false;
387 time(&Date);
388
389 do
390 {
391 string Data;
392 if (In.WriteTillEl(Data) == false)
393 continue;
394
395 if (Debug == true)
396 clog << Data;
397
398 for (string::const_iterator I = Data.begin(); I < Data.end(); I++)
399 {
400 string::const_iterator J = I;
401 for (; J != Data.end() && *J != '\n' && *J != '\r';J++);
402 if (HeaderLine(string(I,J)) == false)
403 return 2;
404 I = J;
405 }
406
407 // 100 Continue is a Nop...
408 if (Result == 100)
409 continue;
410
411 // Tidy up the connection persistance state.
412 if (Encoding == Closes && HaveContent == true)
413 Persistent = false;
414
415 return 0;
416 }
417 while (Owner->Go(false,this) == true);
418
419 return 1;
420 }
421 /*}}}*/
422 // ServerState::RunData - Transfer the data from the socket /*{{{*/
423 // ---------------------------------------------------------------------
424 /* */
425 bool ServerState::RunData()
426 {
427 State = Data;
428
429 // Chunked transfer encoding is fun..
430 if (Encoding == Chunked)
431 {
432 while (1)
433 {
434 // Grab the block size
435 bool Last = true;
436 string Data;
437 In.Limit(-1);
438 do
439 {
440 if (In.WriteTillEl(Data,true) == true)
441 break;
442 }
443 while ((Last = Owner->Go(false,this)) == true);
444
445 if (Last == false)
446 return false;
447
448 // See if we are done
449 unsigned long Len = strtol(Data.c_str(),0,16);
450 if (Len == 0)
451 {
452 In.Limit(-1);
453
454 // We have to remove the entity trailer
455 Last = true;
456 do
457 {
458 if (In.WriteTillEl(Data,true) == true && Data.length() <= 2)
459 break;
460 }
461 while ((Last = Owner->Go(false,this)) == true);
462 if (Last == false)
463 return false;
464 return !_error->PendingError();
465 }
466
467 // Transfer the block
468 In.Limit(Len);
469 while (Owner->Go(true,this) == true)
470 if (In.IsLimit() == true)
471 break;
472
473 // Error
474 if (In.IsLimit() == false)
475 return false;
476
477 // The server sends an extra new line before the next block specifier..
478 In.Limit(-1);
479 Last = true;
480 do
481 {
482 if (In.WriteTillEl(Data,true) == true)
483 break;
484 }
485 while ((Last = Owner->Go(false,this)) == true);
486 if (Last == false)
487 return false;
488 }
489 }
490 else
491 {
492 /* Closes encoding is used when the server did not specify a size, the
493 loss of the connection means we are done */
494 if (Encoding == Closes)
495 In.Limit(-1);
496 else
497 In.Limit(Size - StartPos);
498
499 // Just transfer the whole block.
500 do
501 {
502 if (In.IsLimit() == false)
503 continue;
504
505 In.Limit(-1);
506 return !_error->PendingError();
507 }
508 while (Owner->Go(true,this) == true);
509 }
510
511 return Owner->Flush(this) && !_error->PendingError();
512 }
513 /*}}}*/
514 // ServerState::HeaderLine - Process a header line /*{{{*/
515 // ---------------------------------------------------------------------
516 /* */
517 bool ServerState::HeaderLine(string Line)
518 {
519 if (Line.empty() == true)
520 return true;
521
522 // The http server might be trying to do something evil.
523 if (Line.length() >= MAXLEN)
524 return _error->Error(_("Got a single header line over %u chars"),MAXLEN);
525
526 string::size_type Pos = Line.find(' ');
527 if (Pos == string::npos || Pos+1 > Line.length())
528 {
529 // Blah, some servers use "connection:closes", evil.
530 Pos = Line.find(':');
531 if (Pos == string::npos || Pos + 2 > Line.length())
532 return _error->Error(_("Bad header line"));
533 Pos++;
534 }
535
536 // Parse off any trailing spaces between the : and the next word.
537 string::size_type Pos2 = Pos;
538 while (Pos2 < Line.length() && isspace(Line[Pos2]) != 0)
539 Pos2++;
540
541 string Tag = string(Line,0,Pos);
542 string Val = string(Line,Pos2);
543
544 if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
545 {
546 // Evil servers return no version
547 if (Line[4] == '/')
548 {
549 if (sscanf(Line.c_str(),"HTTP/%u.%u %u %[^\n]",&Major,&Minor,
550 &Result,Code) != 4)
551 return _error->Error(_("The HTTP server sent an invalid reply header"));
552 }
553 else
554 {
555 Major = 0;
556 Minor = 9;
557 if (sscanf(Line.c_str(),"HTTP %u %[^\n]",&Result,Code) != 2)
558 return _error->Error(_("The HTTP server sent an invalid reply header"));
559 }
560
561 /* Check the HTTP response header to get the default persistance
562 state. */
563 if (Major < 1)
564 Persistent = false;
565 else
566 {
567 if (Major == 1 && Minor <= 0)
568 Persistent = false;
569 else
570 Persistent = true;
571 }
572
573 return true;
574 }
575
576 if (stringcasecmp(Tag,"Content-Length:") == 0)
577 {
578 if (Encoding == Closes)
579 Encoding = Stream;
580 HaveContent = true;
581
582 // The length is already set from the Content-Range header
583 if (StartPos != 0)
584 return true;
585
586 if (sscanf(Val.c_str(),"%lu",&Size) != 1)
587 return _error->Error(_("The HTTP server sent an invalid Content-Length header"));
588 return true;
589 }
590
591 if (stringcasecmp(Tag,"Content-Type:") == 0)
592 {
593 HaveContent = true;
594 return true;
595 }
596
597 if (stringcasecmp(Tag,"Content-Range:") == 0)
598 {
599 HaveContent = true;
600
601 if (sscanf(Val.c_str(),"bytes %lu-%*u/%lu",&StartPos,&Size) != 2)
602 return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
603 if ((unsigned)StartPos > Size)
604 return _error->Error(_("This HTTP server has broken range support"));
605 return true;
606 }
607
608 if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
609 {
610 HaveContent = true;
611 if (stringcasecmp(Val,"chunked") == 0)
612 Encoding = Chunked;
613 return true;
614 }
615
616 if (stringcasecmp(Tag,"Connection:") == 0)
617 {
618 if (stringcasecmp(Val,"close") == 0)
619 Persistent = false;
620 if (stringcasecmp(Val,"keep-alive") == 0)
621 Persistent = true;
622 return true;
623 }
624
625 if (stringcasecmp(Tag,"Last-Modified:") == 0)
626 {
627 if (StrToTime(Val,Date) == false)
628 return _error->Error(_("Unknown date format"));
629 return true;
630 }
631
632 if (stringcasecmp(Tag,"Location:") == 0)
633 {
634 Location = Val;
635 return true;
636 }
637
638 return true;
639 }
640 /*}}}*/
641
642 // HttpMethod::SendReq - Send the HTTP request /*{{{*/
643 // ---------------------------------------------------------------------
644 /* This places the http request in the outbound buffer */
645 void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out)
646 {
647 URI Uri = Itm->Uri;
648
649 // The HTTP server expects a hostname with a trailing :port
650 char Buf[1000];
651 string ProperHost = Uri.Host;
652 if (Uri.Port != 0)
653 {
654 sprintf(Buf,":%u",Uri.Port);
655 ProperHost += Buf;
656 }
657
658 // Just in case.
659 if (Itm->Uri.length() >= sizeof(Buf))
660 abort();
661
662 /* Build the request. We include a keep-alive header only for non-proxy
663 requests. This is to tweak old http/1.0 servers that do support keep-alive
664 but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server
665 will glitch HTTP/1.0 proxies because they do not filter it out and
666 pass it on, HTTP/1.1 says the connection should default to keep alive
667 and we expect the proxy to do this */
668 if (Proxy.empty() == true || Proxy.Host.empty())
669 sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n",
670 QuoteString(Uri.Path,"~").c_str(),ProperHost.c_str());
671 else
672 {
673 /* Generate a cache control header if necessary. We place a max
674 cache age on index files, optionally set a no-cache directive
675 and a no-store directive for archives. */
676 sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\n",
677 Itm->Uri.c_str(),ProperHost.c_str());
678 // only generate a cache control header if we actually want to
679 // use a cache
680 if (_config->FindB("Acquire::http::No-Cache",false) == false)
681 {
682 if (Itm->IndexFile == true)
683 sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n",
684 _config->FindI("Acquire::http::Max-Age",0));
685 else
686 {
687 if (_config->FindB("Acquire::http::No-Store",false) == true)
688 strcat(Buf,"Cache-Control: no-store\r\n");
689 }
690 }
691 }
692 // generate a no-cache header if needed
693 if (_config->FindB("Acquire::http::No-Cache",false) == true)
694 strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
695
696
697 string Req = Buf;
698
699 // Check for a partial file
700 struct stat SBuf;
701 if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
702 {
703 // In this case we send an if-range query with a range header
704 sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf.st_size - 1,
705 TimeRFC1123(SBuf.st_mtime).c_str());
706 Req += Buf;
707 }
708 else
709 {
710 if (Itm->LastModified != 0)
711 {
712 sprintf(Buf,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm->LastModified).c_str());
713 Req += Buf;
714 }
715 }
716
717 if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
718 Req += string("Proxy-Authorization: Basic ") +
719 Base64Encode(Proxy.User + ":" + Proxy.Password) + "\r\n";
720
721 if (Uri.User.empty() == false || Uri.Password.empty() == false)
722 Req += string("Authorization: Basic ") +
723 Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n";
724
725 Req += "User-Agent: Ubuntu APT-HTTP/1.3 ("VERSION")\r\n\r\n";
726
727 if (Debug == true)
728 cerr << Req << endl;
729
730 Out.Read(Req);
731 }
732 /*}}}*/
733 // HttpMethod::Go - Run a single loop /*{{{*/
734 // ---------------------------------------------------------------------
735 /* This runs the select loop over the server FDs, Output file FDs and
736 stdin. */
737 bool HttpMethod::Go(bool ToFile,ServerState *Srv)
738 {
739 // Server has closed the connection
740 if (Srv->ServerFd == -1 && (Srv->In.WriteSpace() == false ||
741 ToFile == false))
742 return false;
743
744 fd_set rfds,wfds;
745 FD_ZERO(&rfds);
746 FD_ZERO(&wfds);
747
748 /* Add the server. We only send more requests if the connection will
749 be persisting */
750 if (Srv->Out.WriteSpace() == true && Srv->ServerFd != -1
751 && Srv->Persistent == true)
752 FD_SET(Srv->ServerFd,&wfds);
753 if (Srv->In.ReadSpace() == true && Srv->ServerFd != -1)
754 FD_SET(Srv->ServerFd,&rfds);
755
756 // Add the file
757 int FileFD = -1;
758 if (File != 0)
759 FileFD = File->Fd();
760
761 if (Srv->In.WriteSpace() == true && ToFile == true && FileFD != -1)
762 FD_SET(FileFD,&wfds);
763
764 // Add stdin
765 FD_SET(STDIN_FILENO,&rfds);
766
767 // Figure out the max fd
768 int MaxFd = FileFD;
769 if (MaxFd < Srv->ServerFd)
770 MaxFd = Srv->ServerFd;
771
772 // Select
773 struct timeval tv;
774 tv.tv_sec = TimeOut;
775 tv.tv_usec = 0;
776 int Res = 0;
777 if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0)
778 {
779 if (errno == EINTR)
780 return true;
781 return _error->Errno("select",_("Select failed"));
782 }
783
784 if (Res == 0)
785 {
786 _error->Error(_("Connection timed out"));
787 return ServerDie(Srv);
788 }
789
790 // Handle server IO
791 if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&rfds))
792 {
793 errno = 0;
794 if (Srv->In.Read(Srv->ServerFd) == false)
795 return ServerDie(Srv);
796 }
797
798 if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&wfds))
799 {
800 errno = 0;
801 if (Srv->Out.Write(Srv->ServerFd) == false)
802 return ServerDie(Srv);
803 }
804
805 // Send data to the file
806 if (FileFD != -1 && FD_ISSET(FileFD,&wfds))
807 {
808 if (Srv->In.Write(FileFD) == false)
809 return _error->Errno("write",_("Error writing to output file"));
810 }
811
812 // Handle commands from APT
813 if (FD_ISSET(STDIN_FILENO,&rfds))
814 {
815 if (Run(true) != -1)
816 exit(100);
817 }
818
819 return true;
820 }
821 /*}}}*/
822 // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
823 // ---------------------------------------------------------------------
824 /* This takes the current input buffer from the Server FD and writes it
825 into the file */
826 bool HttpMethod::Flush(ServerState *Srv)
827 {
828 if (File != 0)
829 {
830 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
831 // can't be set
832 if (File->Name() != "/dev/null")
833 SetNonBlock(File->Fd(),false);
834 if (Srv->In.WriteSpace() == false)
835 return true;
836
837 while (Srv->In.WriteSpace() == true)
838 {
839 if (Srv->In.Write(File->Fd()) == false)
840 return _error->Errno("write",_("Error writing to file"));
841 if (Srv->In.IsLimit() == true)
842 return true;
843 }
844
845 if (Srv->In.IsLimit() == true || Srv->Encoding == ServerState::Closes)
846 return true;
847 }
848 return false;
849 }
850 /*}}}*/
851 // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
852 // ---------------------------------------------------------------------
853 /* */
854 bool HttpMethod::ServerDie(ServerState *Srv)
855 {
856 unsigned int LErrno = errno;
857
858 // Dump the buffer to the file
859 if (Srv->State == ServerState::Data)
860 {
861 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
862 // can't be set
863 if (File->Name() != "/dev/null")
864 SetNonBlock(File->Fd(),false);
865 while (Srv->In.WriteSpace() == true)
866 {
867 if (Srv->In.Write(File->Fd()) == false)
868 return _error->Errno("write",_("Error writing to the file"));
869
870 // Done
871 if (Srv->In.IsLimit() == true)
872 return true;
873 }
874 }
875
876 // See if this is because the server finished the data stream
877 if (Srv->In.IsLimit() == false && Srv->State != ServerState::Header &&
878 Srv->Encoding != ServerState::Closes)
879 {
880 Srv->Close();
881 if (LErrno == 0)
882 return _error->Error(_("Error reading from server. Remote end closed connection"));
883 errno = LErrno;
884 return _error->Errno("read",_("Error reading from server"));
885 }
886 else
887 {
888 Srv->In.Limit(-1);
889
890 // Nothing left in the buffer
891 if (Srv->In.WriteSpace() == false)
892 return false;
893
894 // We may have got multiple responses back in one packet..
895 Srv->Close();
896 return true;
897 }
898
899 return false;
900 }
901 /*}}}*/
902 // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
903 // ---------------------------------------------------------------------
904 /* We look at the header data we got back from the server and decide what
905 to do. Returns
906 0 - File is open,
907 1 - IMS hit
908 3 - Unrecoverable error
909 4 - Error with error content page
910 5 - Unrecoverable non-server error (close the connection)
911 6 - Try again with a new or changed URI
912 */
913 int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
914 {
915 // Not Modified
916 if (Srv->Result == 304)
917 {
918 unlink(Queue->DestFile.c_str());
919 Res.IMSHit = true;
920 Res.LastModified = Queue->LastModified;
921 return 1;
922 }
923
924 /* Redirect
925 *
926 * Note that it is only OK for us to treat all redirection the same
927 * because we *always* use GET, not other HTTP methods. There are
928 * three redirection codes for which it is not appropriate that we
929 * redirect. Pass on those codes so the error handling kicks in.
930 */
931 if (AllowRedirect
932 && (Srv->Result > 300 && Srv->Result < 400)
933 && (Srv->Result != 300 // Multiple Choices
934 && Srv->Result != 304 // Not Modified
935 && Srv->Result != 306)) // (Not part of HTTP/1.1, reserved)
936 {
937 if (!Srv->Location.empty())
938 {
939 NextURI = Srv->Location;
940 return 6;
941 }
942 /* else pass through for error message */
943 }
944
945 /* We have a reply we dont handle. This should indicate a perm server
946 failure */
947 if (Srv->Result < 200 || Srv->Result >= 300)
948 {
949 char err[255];
950 snprintf(err,sizeof(err)-1,"HttpError%i",Srv->Result);
951 SetFailReason(err);
952 _error->Error("%u %s",Srv->Result,Srv->Code);
953 if (Srv->HaveContent == true)
954 return 4;
955 return 3;
956 }
957
958 // This is some sort of 2xx 'data follows' reply
959 Res.LastModified = Srv->Date;
960 Res.Size = Srv->Size;
961
962 // Open the file
963 delete File;
964 File = new FileFd(Queue->DestFile,FileFd::WriteAny);
965 if (_error->PendingError() == true)
966 return 5;
967
968 FailFile = Queue->DestFile;
969 FailFile.c_str(); // Make sure we dont do a malloc in the signal handler
970 FailFd = File->Fd();
971 FailTime = Srv->Date;
972
973 // Set the expected size
974 if (Srv->StartPos >= 0)
975 {
976 Res.ResumePoint = Srv->StartPos;
977 if (ftruncate(File->Fd(),Srv->StartPos) < 0)
978 _error->Errno("ftruncate", _("Failed to truncate file"));
979 }
980
981 // Set the start point
982 lseek(File->Fd(),0,SEEK_END);
983
984 delete Srv->In.Hash;
985 Srv->In.Hash = new Hashes;
986
987 // Fill the Hash if the file is non-empty (resume)
988 if (Srv->StartPos > 0)
989 {
990 lseek(File->Fd(),0,SEEK_SET);
991 if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false)
992 {
993 _error->Errno("read",_("Problem hashing file"));
994 return 5;
995 }
996 lseek(File->Fd(),0,SEEK_END);
997 }
998
999 SetNonBlock(File->Fd(),true);
1000 return 0;
1001 }
1002 /*}}}*/
1003 // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
1004 // ---------------------------------------------------------------------
1005 /* This closes and timestamps the open file. This is neccessary to get
1006 resume behavoir on user abort */
1007 void HttpMethod::SigTerm(int)
1008 {
1009 if (FailFd == -1)
1010 _exit(100);
1011 close(FailFd);
1012
1013 // Timestamp
1014 struct utimbuf UBuf;
1015 UBuf.actime = FailTime;
1016 UBuf.modtime = FailTime;
1017 utime(FailFile.c_str(),&UBuf);
1018
1019 _exit(100);
1020 }
1021 /*}}}*/
1022 // HttpMethod::Fetch - Fetch an item /*{{{*/
1023 // ---------------------------------------------------------------------
1024 /* This adds an item to the pipeline. We keep the pipeline at a fixed
1025 depth. */
1026 bool HttpMethod::Fetch(FetchItem *)
1027 {
1028 if (Server == 0)
1029 return true;
1030
1031 // Queue the requests
1032 int Depth = -1;
1033 for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
1034 I = I->Next, Depth++)
1035 {
1036 // If pipelining is disabled, we only queue 1 request
1037 if (Server->Pipeline == false && Depth >= 0)
1038 break;
1039
1040 // Make sure we stick with the same server
1041 if (Server->Comp(I->Uri) == false)
1042 break;
1043 if (QueueBack == I)
1044 {
1045 QueueBack = I->Next;
1046 SendReq(I,Server->Out);
1047 continue;
1048 }
1049 }
1050
1051 return true;
1052 };
1053 /*}}}*/
1054 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
1055 // ---------------------------------------------------------------------
1056 /* We stash the desired pipeline depth */
1057 bool HttpMethod::Configuration(string Message)
1058 {
1059 if (pkgAcqMethod::Configuration(Message) == false)
1060 return false;
1061
1062 AllowRedirect = _config->FindB("Acquire::http::AllowRedirect",true);
1063 TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut);
1064 PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
1065 PipelineDepth);
1066 Debug = _config->FindB("Debug::Acquire::http",false);
1067
1068 return true;
1069 }
1070 /*}}}*/
1071 // HttpMethod::Loop - Main loop /*{{{*/
1072 // ---------------------------------------------------------------------
1073 /* */
1074 int HttpMethod::Loop()
1075 {
1076 typedef vector<string> StringVector;
1077 typedef vector<string>::iterator StringVectorIterator;
1078 map<string, StringVector> Redirected;
1079
1080 signal(SIGTERM,SigTerm);
1081 signal(SIGINT,SigTerm);
1082
1083 Server = 0;
1084
1085 int FailCounter = 0;
1086 while (1)
1087 {
1088 // We have no commands, wait for some to arrive
1089 if (Queue == 0)
1090 {
1091 if (WaitFd(STDIN_FILENO) == false)
1092 return 0;
1093 }
1094
1095 /* Run messages, we can accept 0 (no message) if we didn't
1096 do a WaitFd above.. Otherwise the FD is closed. */
1097 int Result = Run(true);
1098 if (Result != -1 && (Result != 0 || Queue == 0))
1099 return 100;
1100
1101 if (Queue == 0)
1102 continue;
1103
1104 // Connect to the server
1105 if (Server == 0 || Server->Comp(Queue->Uri) == false)
1106 {
1107 delete Server;
1108 Server = new ServerState(Queue->Uri,this);
1109 }
1110 /* If the server has explicitly said this is the last connection
1111 then we pre-emptively shut down the pipeline and tear down
1112 the connection. This will speed up HTTP/1.0 servers a tad
1113 since we don't have to wait for the close sequence to
1114 complete */
1115 if (Server->Persistent == false)
1116 Server->Close();
1117
1118 // Reset the pipeline
1119 if (Server->ServerFd == -1)
1120 QueueBack = Queue;
1121
1122 // Connnect to the host
1123 if (Server->Open() == false)
1124 {
1125 Fail(true);
1126 delete Server;
1127 Server = 0;
1128 continue;
1129 }
1130
1131 // Fill the pipeline.
1132 Fetch(0);
1133
1134 // Fetch the next URL header data from the server.
1135 switch (Server->RunHeaders())
1136 {
1137 case 0:
1138 break;
1139
1140 // The header data is bad
1141 case 2:
1142 {
1143 _error->Error(_("Bad header data"));
1144 Fail(true);
1145 RotateDNS();
1146 continue;
1147 }
1148
1149 // The server closed a connection during the header get..
1150 default:
1151 case 1:
1152 {
1153 FailCounter++;
1154 _error->Discard();
1155 Server->Close();
1156 Server->Pipeline = false;
1157
1158 if (FailCounter >= 2)
1159 {
1160 Fail(_("Connection failed"),true);
1161 FailCounter = 0;
1162 }
1163
1164 RotateDNS();
1165 continue;
1166 }
1167 };
1168
1169 // Decide what to do.
1170 FetchResult Res;
1171 Res.Filename = Queue->DestFile;
1172 switch (DealWithHeaders(Res,Server))
1173 {
1174 // Ok, the file is Open
1175 case 0:
1176 {
1177 URIStart(Res);
1178
1179 // Run the data
1180 bool Result = Server->RunData();
1181
1182 /* If the server is sending back sizeless responses then fill in
1183 the size now */
1184 if (Res.Size == 0)
1185 Res.Size = File->Size();
1186
1187 // Close the file, destroy the FD object and timestamp it
1188 FailFd = -1;
1189 delete File;
1190 File = 0;
1191
1192 // Timestamp
1193 struct utimbuf UBuf;
1194 time(&UBuf.actime);
1195 UBuf.actime = Server->Date;
1196 UBuf.modtime = Server->Date;
1197 utime(Queue->DestFile.c_str(),&UBuf);
1198
1199 // Send status to APT
1200 if (Result == true)
1201 {
1202 Res.TakeHashes(*Server->In.Hash);
1203 URIDone(Res);
1204 }
1205 else
1206 {
1207 if (Server->ServerFd == -1)
1208 {
1209 FailCounter++;
1210 _error->Discard();
1211 Server->Close();
1212
1213 if (FailCounter >= 2)
1214 {
1215 Fail(_("Connection failed"),true);
1216 FailCounter = 0;
1217 }
1218
1219 QueueBack = Queue;
1220 }
1221 else
1222 Fail(true);
1223 }
1224 break;
1225 }
1226
1227 // IMS hit
1228 case 1:
1229 {
1230 URIDone(Res);
1231 break;
1232 }
1233
1234 // Hard server error, not found or something
1235 case 3:
1236 {
1237 Fail();
1238 break;
1239 }
1240
1241 // Hard internal error, kill the connection and fail
1242 case 5:
1243 {
1244 delete File;
1245 File = 0;
1246
1247 Fail();
1248 RotateDNS();
1249 Server->Close();
1250 break;
1251 }
1252
1253 // We need to flush the data, the header is like a 404 w/ error text
1254 case 4:
1255 {
1256 Fail();
1257
1258 // Send to content to dev/null
1259 File = new FileFd("/dev/null",FileFd::WriteExists);
1260 Server->RunData();
1261 delete File;
1262 File = 0;
1263 break;
1264 }
1265
1266 // Try again with a new URL
1267 case 6:
1268 {
1269 // Clear rest of response if there is content
1270 if (Server->HaveContent)
1271 {
1272 File = new FileFd("/dev/null",FileFd::WriteExists);
1273 Server->RunData();
1274 delete File;
1275 File = 0;
1276 }
1277
1278 /* Detect redirect loops. No more redirects are allowed
1279 after the same URI is seen twice in a queue item. */
1280 StringVector &R = Redirected[Queue->DestFile];
1281 bool StopRedirects = false;
1282 if (R.size() == 0)
1283 R.push_back(Queue->Uri);
1284 else if (R[0] == "STOP" || R.size() > 10)
1285 StopRedirects = true;
1286 else
1287 {
1288 for (StringVectorIterator I = R.begin(); I != R.end(); I++)
1289 if (Queue->Uri == *I)
1290 {
1291 R[0] = "STOP";
1292 break;
1293 }
1294
1295 R.push_back(Queue->Uri);
1296 }
1297
1298 if (StopRedirects == false)
1299 Redirect(NextURI);
1300 else
1301 Fail();
1302
1303 break;
1304 }
1305
1306 default:
1307 Fail(_("Internal error"));
1308 break;
1309 }
1310
1311 FailCounter = 0;
1312 }
1313
1314 return 0;
1315 }
1316 /*}}}*/
1317
1318
1319