]> git.saurik.com Git - apt.git/blob - methods/http.cc
apt-pkg/deb/debsystem.cc: add missing i18n string
[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 if (ftruncate(File->Fd(),Srv->StartPos) < 0)
945 _error->Errno("ftruncate", _("Failed to truncate file"));
946 }
947
948 // Set the start point
949 lseek(File->Fd(),0,SEEK_END);
950
951 delete Srv->In.Hash;
952 Srv->In.Hash = new Hashes;
953
954 // Fill the Hash if the file is non-empty (resume)
955 if (Srv->StartPos > 0)
956 {
957 lseek(File->Fd(),0,SEEK_SET);
958 if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false)
959 {
960 _error->Errno("read",_("Problem hashing file"));
961 return 5;
962 }
963 lseek(File->Fd(),0,SEEK_END);
964 }
965
966 SetNonBlock(File->Fd(),true);
967 return 0;
968 }
969 /*}}}*/
970 // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
971 // ---------------------------------------------------------------------
972 /* This closes and timestamps the open file. This is neccessary to get
973 resume behavoir on user abort */
974 void HttpMethod::SigTerm(int)
975 {
976 if (FailFd == -1)
977 _exit(100);
978 close(FailFd);
979
980 // Timestamp
981 struct utimbuf UBuf;
982 UBuf.actime = FailTime;
983 UBuf.modtime = FailTime;
984 utime(FailFile.c_str(),&UBuf);
985
986 _exit(100);
987 }
988 /*}}}*/
989 // HttpMethod::Fetch - Fetch an item /*{{{*/
990 // ---------------------------------------------------------------------
991 /* This adds an item to the pipeline. We keep the pipeline at a fixed
992 depth. */
993 bool HttpMethod::Fetch(FetchItem *)
994 {
995 if (Server == 0)
996 return true;
997
998 // Queue the requests
999 int Depth = -1;
1000 for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
1001 I = I->Next, Depth++)
1002 {
1003 // If pipelining is disabled, we only queue 1 request
1004 if (Server->Pipeline == false && Depth >= 0)
1005 break;
1006
1007 // Make sure we stick with the same server
1008 if (Server->Comp(I->Uri) == false)
1009 break;
1010 if (QueueBack == I)
1011 {
1012 QueueBack = I->Next;
1013 SendReq(I,Server->Out);
1014 continue;
1015 }
1016 }
1017
1018 return true;
1019 };
1020 /*}}}*/
1021 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* We stash the desired pipeline depth */
1024 bool HttpMethod::Configuration(string Message)
1025 {
1026 if (pkgAcqMethod::Configuration(Message) == false)
1027 return false;
1028
1029 TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut);
1030 PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
1031 PipelineDepth);
1032 Debug = _config->FindB("Debug::Acquire::http",false);
1033
1034 return true;
1035 }
1036 /*}}}*/
1037 // HttpMethod::Loop - Main loop /*{{{*/
1038 // ---------------------------------------------------------------------
1039 /* */
1040 int HttpMethod::Loop()
1041 {
1042 signal(SIGTERM,SigTerm);
1043 signal(SIGINT,SigTerm);
1044
1045 Server = 0;
1046
1047 int FailCounter = 0;
1048 while (1)
1049 {
1050 // We have no commands, wait for some to arrive
1051 if (Queue == 0)
1052 {
1053 if (WaitFd(STDIN_FILENO) == false)
1054 return 0;
1055 }
1056
1057 /* Run messages, we can accept 0 (no message) if we didn't
1058 do a WaitFd above.. Otherwise the FD is closed. */
1059 int Result = Run(true);
1060 if (Result != -1 && (Result != 0 || Queue == 0))
1061 return 100;
1062
1063 if (Queue == 0)
1064 continue;
1065
1066 // Connect to the server
1067 if (Server == 0 || Server->Comp(Queue->Uri) == false)
1068 {
1069 delete Server;
1070 Server = new ServerState(Queue->Uri,this);
1071 }
1072 /* If the server has explicitly said this is the last connection
1073 then we pre-emptively shut down the pipeline and tear down
1074 the connection. This will speed up HTTP/1.0 servers a tad
1075 since we don't have to wait for the close sequence to
1076 complete */
1077 if (Server->Persistent == false)
1078 Server->Close();
1079
1080 // Reset the pipeline
1081 if (Server->ServerFd == -1)
1082 QueueBack = Queue;
1083
1084 // Connnect to the host
1085 if (Server->Open() == false)
1086 {
1087 Fail(true);
1088 delete Server;
1089 Server = 0;
1090 continue;
1091 }
1092
1093 // Fill the pipeline.
1094 Fetch(0);
1095
1096 // Fetch the next URL header data from the server.
1097 switch (Server->RunHeaders())
1098 {
1099 case 0:
1100 break;
1101
1102 // The header data is bad
1103 case 2:
1104 {
1105 _error->Error(_("Bad header data"));
1106 Fail(true);
1107 RotateDNS();
1108 continue;
1109 }
1110
1111 // The server closed a connection during the header get..
1112 default:
1113 case 1:
1114 {
1115 FailCounter++;
1116 _error->Discard();
1117 Server->Close();
1118 Server->Pipeline = false;
1119
1120 if (FailCounter >= 2)
1121 {
1122 Fail(_("Connection failed"),true);
1123 FailCounter = 0;
1124 }
1125
1126 RotateDNS();
1127 continue;
1128 }
1129 };
1130
1131 // Decide what to do.
1132 FetchResult Res;
1133 Res.Filename = Queue->DestFile;
1134 switch (DealWithHeaders(Res,Server))
1135 {
1136 // Ok, the file is Open
1137 case 0:
1138 {
1139 URIStart(Res);
1140
1141 // Run the data
1142 bool Result = Server->RunData();
1143
1144 /* If the server is sending back sizeless responses then fill in
1145 the size now */
1146 if (Res.Size == 0)
1147 Res.Size = File->Size();
1148
1149 // Close the file, destroy the FD object and timestamp it
1150 FailFd = -1;
1151 delete File;
1152 File = 0;
1153
1154 // Timestamp
1155 struct utimbuf UBuf;
1156 time(&UBuf.actime);
1157 UBuf.actime = Server->Date;
1158 UBuf.modtime = Server->Date;
1159 utime(Queue->DestFile.c_str(),&UBuf);
1160
1161 // Send status to APT
1162 if (Result == true)
1163 {
1164 Res.TakeHashes(*Server->In.Hash);
1165 URIDone(Res);
1166 }
1167 else
1168 {
1169 if (Server->ServerFd == -1)
1170 {
1171 FailCounter++;
1172 _error->Discard();
1173 Server->Close();
1174
1175 if (FailCounter >= 2)
1176 {
1177 Fail(_("Connection failed"),true);
1178 FailCounter = 0;
1179 }
1180
1181 QueueBack = Queue;
1182 }
1183 else
1184 Fail(true);
1185 }
1186 break;
1187 }
1188
1189 // IMS hit
1190 case 1:
1191 {
1192 URIDone(Res);
1193 break;
1194 }
1195
1196 // Hard server error, not found or something
1197 case 3:
1198 {
1199 Fail();
1200 break;
1201 }
1202
1203 // Hard internal error, kill the connection and fail
1204 case 5:
1205 {
1206 delete File;
1207 File = 0;
1208
1209 Fail();
1210 RotateDNS();
1211 Server->Close();
1212 break;
1213 }
1214
1215 // We need to flush the data, the header is like a 404 w/ error text
1216 case 4:
1217 {
1218 Fail();
1219
1220 // Send to content to dev/null
1221 File = new FileFd("/dev/null",FileFd::WriteExists);
1222 Server->RunData();
1223 delete File;
1224 File = 0;
1225 break;
1226 }
1227
1228 default:
1229 Fail(_("Internal error"));
1230 break;
1231 }
1232
1233 FailCounter = 0;
1234 }
1235
1236 return 0;
1237 }
1238 /*}}}*/
1239
1240 int main()
1241 {
1242 setlocale(LC_ALL, "");
1243
1244 HttpMethod Mth;
1245
1246 return Mth.Loop();
1247 }
1248
1249