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