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