]>
git.saurik.com Git - apt.git/blob - methods/http.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4 /* ######################################################################
6 HTTP Aquire Method - This is the HTTP aquire method for APT.
8 It uses HTTP/1.1 and many of the fancy options there-in, such as
9 pipelining, range, if-range and so on.
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).
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.
25 ##################################################################### */
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>
48 #include "rfc2553emu.h"
54 string
HttpMethod::FailFile
;
55 int HttpMethod::FailFd
= -1;
56 time_t HttpMethod::FailTime
= 0;
57 unsigned long PipelineDepth
= 10;
58 unsigned long TimeOut
= 120;
61 unsigned long CircleBuf::BwReadLimit
=0;
62 unsigned long CircleBuf::BwTickReadData
=0;
63 struct timeval
CircleBuf::BwReadTick
={0,0};
64 const unsigned int CircleBuf::BW_HZ
=10;
66 // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
67 // ---------------------------------------------------------------------
69 CircleBuf::CircleBuf(unsigned long Size
) : Size(Size
), Hash(0)
71 Buf
= new unsigned char[Size
];
74 CircleBuf::BwReadLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
77 // CircleBuf::Reset - Reset to the default state /*{{{*/
78 // ---------------------------------------------------------------------
80 void CircleBuf::Reset()
85 MaxGet
= (unsigned int)-1;
94 // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This fills up the buffer with as much data as is in the FD, assuming it
98 bool CircleBuf::Read(int Fd
)
100 unsigned long BwReadMax
;
104 // Woops, buffer is full
105 if (InP
- OutP
== Size
)
108 // what's left to read in this tick
109 BwReadMax
= CircleBuf::BwReadLimit
/BW_HZ
;
111 if(CircleBuf::BwReadLimit
) {
113 gettimeofday(&now
,0);
115 unsigned long d
= (now
.tv_sec
-CircleBuf::BwReadTick
.tv_sec
)*1000000 +
116 now
.tv_usec
-CircleBuf::BwReadTick
.tv_usec
;
117 if(d
> 1000000/BW_HZ
) {
118 CircleBuf::BwReadTick
= now
;
119 CircleBuf::BwTickReadData
= 0;
122 if(CircleBuf::BwTickReadData
>= BwReadMax
) {
123 usleep(1000000/BW_HZ
);
128 // Write the buffer segment
130 if(CircleBuf::BwReadLimit
) {
131 Res
= read(Fd
,Buf
+ (InP%Size
),
132 BwReadMax
> LeftRead() ? LeftRead() : BwReadMax
);
134 Res
= read(Fd
,Buf
+ (InP%Size
),LeftRead());
136 if(Res
> 0 && BwReadLimit
> 0)
137 CircleBuf::BwTickReadData
+= Res
;
149 gettimeofday(&Start
,0);
154 // CircleBuf::Read - Put the string into the buffer /*{{{*/
155 // ---------------------------------------------------------------------
156 /* This will hold the string in and fill the buffer with it as it empties */
157 bool CircleBuf::Read(string Data
)
164 // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
165 // ---------------------------------------------------------------------
167 void CircleBuf::FillOut()
169 if (OutQueue
.empty() == true)
173 // Woops, buffer is full
174 if (InP
- OutP
== Size
)
177 // Write the buffer segment
178 unsigned long Sz
= LeftRead();
179 if (OutQueue
.length() - StrPos
< Sz
)
180 Sz
= OutQueue
.length() - StrPos
;
181 memcpy(Buf
+ (InP%Size
),OutQueue
.c_str() + StrPos
,Sz
);
186 if (OutQueue
.length() == StrPos
)
195 // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
196 // ---------------------------------------------------------------------
197 /* This empties the buffer into the FD. */
198 bool CircleBuf::Write(int Fd
)
204 // Woops, buffer is empty
211 // Write the buffer segment
213 Res
= write(Fd
,Buf
+ (OutP%Size
),LeftWrite());
226 Hash
->Add(Buf
+ (OutP%Size
),Res
);
232 // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
233 // ---------------------------------------------------------------------
234 /* This copies till the first empty line */
235 bool CircleBuf::WriteTillEl(string
&Data
,bool Single
)
237 // We cheat and assume it is unneeded to have more than one buffer load
238 for (unsigned long I
= OutP
; I
< InP
; I
++)
240 if (Buf
[I%Size
] != '\n')
246 if (I
< InP
&& Buf
[I%Size
] == '\r')
248 if (I
>= InP
|| Buf
[I%Size
] != '\n')
256 unsigned long Sz
= LeftWrite();
261 Data
+= string((char *)(Buf
+ (OutP%Size
)),Sz
);
269 // CircleBuf::Stats - Print out stats information /*{{{*/
270 // ---------------------------------------------------------------------
272 void CircleBuf::Stats()
278 gettimeofday(&Stop
,0);
279 /* float Diff = Stop.tv_sec - Start.tv_sec +
280 (float)(Stop.tv_usec - Start.tv_usec)/1000000;
281 clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
285 // ServerState::ServerState - Constructor /*{{{*/
286 // ---------------------------------------------------------------------
288 ServerState::ServerState(URI Srv
,HttpMethod
*Owner
) : Owner(Owner
),
289 In(64*1024), Out(4*1024),
295 // ServerState::Open - Open a connection to the server /*{{{*/
296 // ---------------------------------------------------------------------
297 /* This opens a connection to the server. */
298 bool ServerState::Open()
300 // Use the already open connection if possible.
309 // Determine the proxy setting
310 if (getenv("http_proxy") == 0)
312 string DefProxy
= _config
->Find("Acquire::http::Proxy");
313 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
314 if (SpecificProxy
.empty() == false)
316 if (SpecificProxy
== "DIRECT")
319 Proxy
= SpecificProxy
;
325 Proxy
= getenv("http_proxy");
327 // Parse no_proxy, a , separated list of domains
328 if (getenv("no_proxy") != 0)
330 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
334 // Determine what host and port to use based on the proxy settings
337 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
339 if (ServerName
.Port
!= 0)
340 Port
= ServerName
.Port
;
341 Host
= ServerName
.Host
;
350 // Connect to the remote server
351 if (Connect(Host
,Port
,"http",80,ServerFd
,TimeOut
,Owner
) == false)
357 // ServerState::Close - Close a connection to the server /*{{{*/
358 // ---------------------------------------------------------------------
360 bool ServerState::Close()
367 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
368 // ---------------------------------------------------------------------
369 /* Returns 0 if things are OK, 1 if an IO error occursed and 2 if a header
370 parse error occured */
371 int ServerState::RunHeaders()
375 Owner
->Status(_("Waiting for headers"));
389 if (In
.WriteTillEl(Data
) == false)
395 for (string::const_iterator I
= Data
.begin(); I
< Data
.end(); I
++)
397 string::const_iterator J
= I
;
398 for (; J
!= Data
.end() && *J
!= '\n' && *J
!= '\r';J
++);
399 if (HeaderLine(string(I
,J
)) == false)
404 // 100 Continue is a Nop...
408 // Tidy up the connection persistance state.
409 if (Encoding
== Closes
&& HaveContent
== true)
414 while (Owner
->Go(false,this) == true);
419 // ServerState::RunData - Transfer the data from the socket /*{{{*/
420 // ---------------------------------------------------------------------
422 bool ServerState::RunData()
426 // Chunked transfer encoding is fun..
427 if (Encoding
== Chunked
)
431 // Grab the block size
437 if (In
.WriteTillEl(Data
,true) == true)
440 while ((Last
= Owner
->Go(false,this)) == true);
445 // See if we are done
446 unsigned long Len
= strtol(Data
.c_str(),0,16);
451 // We have to remove the entity trailer
455 if (In
.WriteTillEl(Data
,true) == true && Data
.length() <= 2)
458 while ((Last
= Owner
->Go(false,this)) == true);
461 return !_error
->PendingError();
464 // Transfer the block
466 while (Owner
->Go(true,this) == true)
467 if (In
.IsLimit() == true)
471 if (In
.IsLimit() == false)
474 // The server sends an extra new line before the next block specifier..
479 if (In
.WriteTillEl(Data
,true) == true)
482 while ((Last
= Owner
->Go(false,this)) == true);
489 /* Closes encoding is used when the server did not specify a size, the
490 loss of the connection means we are done */
491 if (Encoding
== Closes
)
494 In
.Limit(Size
- StartPos
);
496 // Just transfer the whole block.
499 if (In
.IsLimit() == false)
503 return !_error
->PendingError();
505 while (Owner
->Go(true,this) == true);
508 return Owner
->Flush(this) && !_error
->PendingError();
511 // ServerState::HeaderLine - Process a header line /*{{{*/
512 // ---------------------------------------------------------------------
514 bool ServerState::HeaderLine(string Line
)
516 if (Line
.empty() == true)
519 // The http server might be trying to do something evil.
520 if (Line
.length() >= MAXLEN
)
521 return _error
->Error(_("Got a single header line over %u chars"),MAXLEN
);
523 string::size_type Pos
= Line
.find(' ');
524 if (Pos
== string::npos
|| Pos
+1 > Line
.length())
526 // Blah, some servers use "connection:closes", evil.
527 Pos
= Line
.find(':');
528 if (Pos
== string::npos
|| Pos
+ 2 > Line
.length())
529 return _error
->Error(_("Bad header line"));
533 // Parse off any trailing spaces between the : and the next word.
534 string::size_type Pos2
= Pos
;
535 while (Pos2
< Line
.length() && isspace(Line
[Pos2
]) != 0)
538 string Tag
= string(Line
,0,Pos
);
539 string Val
= string(Line
,Pos2
);
541 if (stringcasecmp(Tag
.c_str(),Tag
.c_str()+4,"HTTP") == 0)
543 // Evil servers return no version
546 if (sscanf(Line
.c_str(),"HTTP/%u.%u %u %[^\n]",&Major
,&Minor
,
548 return _error
->Error(_("The HTTP server sent an invalid reply header"));
554 if (sscanf(Line
.c_str(),"HTTP %u %[^\n]",&Result
,Code
) != 2)
555 return _error
->Error(_("The HTTP server sent an invalid reply header"));
558 /* Check the HTTP response header to get the default persistance
564 if (Major
== 1 && Minor
<= 0)
573 if (stringcasecmp(Tag
,"Content-Length:") == 0)
575 if (Encoding
== Closes
)
579 // The length is already set from the Content-Range header
583 if (sscanf(Val
.c_str(),"%lu",&Size
) != 1)
584 return _error
->Error(_("The HTTP server sent an invalid Content-Length header"));
588 if (stringcasecmp(Tag
,"Content-Type:") == 0)
594 if (stringcasecmp(Tag
,"Content-Range:") == 0)
598 if (sscanf(Val
.c_str(),"bytes %lu-%*u/%lu",&StartPos
,&Size
) != 2)
599 return _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
600 if ((unsigned)StartPos
> Size
)
601 return _error
->Error(_("This HTTP server has broken range support"));
605 if (stringcasecmp(Tag
,"Transfer-Encoding:") == 0)
608 if (stringcasecmp(Val
,"chunked") == 0)
613 if (stringcasecmp(Tag
,"Connection:") == 0)
615 if (stringcasecmp(Val
,"close") == 0)
617 if (stringcasecmp(Val
,"keep-alive") == 0)
622 if (stringcasecmp(Tag
,"Last-Modified:") == 0)
624 if (StrToTime(Val
,Date
) == false)
625 return _error
->Error(_("Unknown date format"));
633 // HttpMethod::SendReq - Send the HTTP request /*{{{*/
634 // ---------------------------------------------------------------------
635 /* This places the http request in the outbound buffer */
636 void HttpMethod::SendReq(FetchItem
*Itm
,CircleBuf
&Out
)
640 // The HTTP server expects a hostname with a trailing :port
642 string ProperHost
= Uri
.Host
;
645 sprintf(Buf
,":%u",Uri
.Port
);
650 if (Itm
->Uri
.length() >= sizeof(Buf
))
653 /* Build the request. We include a keep-alive header only for non-proxy
654 requests. This is to tweak old http/1.0 servers that do support keep-alive
655 but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server
656 will glitch HTTP/1.0 proxies because they do not filter it out and
657 pass it on, HTTP/1.1 says the connection should default to keep alive
658 and we expect the proxy to do this */
659 if (Proxy
.empty() == true)
660 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n",
661 QuoteString(Uri
.Path
,"~").c_str(),ProperHost
.c_str());
664 /* Generate a cache control header if necessary. We place a max
665 cache age on index files, optionally set a no-cache directive
666 and a no-store directive for archives. */
667 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\n",
668 Itm
->Uri
.c_str(),ProperHost
.c_str());
669 // only generate a cache control header if we actually want to
671 if (_config
->FindB("Acquire::http::No-Cache",false) == false)
673 if (Itm
->IndexFile
== true)
674 sprintf(Buf
+strlen(Buf
),"Cache-Control: max-age=%u\r\n",
675 _config
->FindI("Acquire::http::Max-Age",0));
678 if (_config
->FindB("Acquire::http::No-Store",false) == true)
679 strcat(Buf
,"Cache-Control: no-store\r\n");
683 // generate a no-cache header if needed
684 if (_config
->FindB("Acquire::http::No-Cache",false) == true)
685 strcat(Buf
,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
690 // Check for a partial file
692 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
694 // In this case we send an if-range query with a range header
695 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf
.st_size
- 1,
696 TimeRFC1123(SBuf
.st_mtime
).c_str());
701 if (Itm
->LastModified
!= 0)
703 sprintf(Buf
,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm
->LastModified
).c_str());
708 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
709 Req
+= string("Proxy-Authorization: Basic ") +
710 Base64Encode(Proxy
.User
+ ":" + Proxy
.Password
) + "\r\n";
712 if (Uri
.User
.empty() == false || Uri
.Password
.empty() == false)
713 Req
+= string("Authorization: Basic ") +
714 Base64Encode(Uri
.User
+ ":" + Uri
.Password
) + "\r\n";
716 Req
+= "User-Agent: Debian APT-HTTP/1.3\r\n\r\n";
724 // HttpMethod::Go - Run a single loop /*{{{*/
725 // ---------------------------------------------------------------------
726 /* This runs the select loop over the server FDs, Output file FDs and
728 bool HttpMethod::Go(bool ToFile
,ServerState
*Srv
)
730 // Server has closed the connection
731 if (Srv
->ServerFd
== -1 && (Srv
->In
.WriteSpace() == false ||
739 /* Add the server. We only send more requests if the connection will
741 if (Srv
->Out
.WriteSpace() == true && Srv
->ServerFd
!= -1
742 && Srv
->Persistent
== true)
743 FD_SET(Srv
->ServerFd
,&wfds
);
744 if (Srv
->In
.ReadSpace() == true && Srv
->ServerFd
!= -1)
745 FD_SET(Srv
->ServerFd
,&rfds
);
752 if (Srv
->In
.WriteSpace() == true && ToFile
== true && FileFD
!= -1)
753 FD_SET(FileFD
,&wfds
);
756 FD_SET(STDIN_FILENO
,&rfds
);
758 // Figure out the max fd
760 if (MaxFd
< Srv
->ServerFd
)
761 MaxFd
= Srv
->ServerFd
;
768 if ((Res
= select(MaxFd
+1,&rfds
,&wfds
,0,&tv
)) < 0)
772 return _error
->Errno("select",_("Select failed"));
777 _error
->Error(_("Connection timed out"));
778 return ServerDie(Srv
);
782 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&rfds
))
785 if (Srv
->In
.Read(Srv
->ServerFd
) == false)
786 return ServerDie(Srv
);
789 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&wfds
))
792 if (Srv
->Out
.Write(Srv
->ServerFd
) == false)
793 return ServerDie(Srv
);
796 // Send data to the file
797 if (FileFD
!= -1 && FD_ISSET(FileFD
,&wfds
))
799 if (Srv
->In
.Write(FileFD
) == false)
800 return _error
->Errno("write",_("Error writing to output file"));
803 // Handle commands from APT
804 if (FD_ISSET(STDIN_FILENO
,&rfds
))
813 // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
814 // ---------------------------------------------------------------------
815 /* This takes the current input buffer from the Server FD and writes it
817 bool HttpMethod::Flush(ServerState
*Srv
)
821 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
823 if (File
->Name() != "/dev/null")
824 SetNonBlock(File
->Fd(),false);
825 if (Srv
->In
.WriteSpace() == false)
828 while (Srv
->In
.WriteSpace() == true)
830 if (Srv
->In
.Write(File
->Fd()) == false)
831 return _error
->Errno("write",_("Error writing to file"));
832 if (Srv
->In
.IsLimit() == true)
836 if (Srv
->In
.IsLimit() == true || Srv
->Encoding
== ServerState::Closes
)
842 // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
843 // ---------------------------------------------------------------------
845 bool HttpMethod::ServerDie(ServerState
*Srv
)
847 unsigned int LErrno
= errno
;
849 // Dump the buffer to the file
850 if (Srv
->State
== ServerState::Data
)
852 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
854 if (File
->Name() != "/dev/null")
855 SetNonBlock(File
->Fd(),false);
856 while (Srv
->In
.WriteSpace() == true)
858 if (Srv
->In
.Write(File
->Fd()) == false)
859 return _error
->Errno("write",_("Error writing to the file"));
862 if (Srv
->In
.IsLimit() == true)
867 // See if this is because the server finished the data stream
868 if (Srv
->In
.IsLimit() == false && Srv
->State
!= ServerState::Header
&&
869 Srv
->Encoding
!= ServerState::Closes
)
873 return _error
->Error(_("Error reading from server. Remote end closed connection"));
875 return _error
->Errno("read",_("Error reading from server"));
881 // Nothing left in the buffer
882 if (Srv
->In
.WriteSpace() == false)
885 // We may have got multiple responses back in one packet..
893 // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
894 // ---------------------------------------------------------------------
895 /* We look at the header data we got back from the server and decide what
899 3 - Unrecoverable error
900 4 - Error with error content page
901 5 - Unrecoverable non-server error (close the connection) */
902 int HttpMethod::DealWithHeaders(FetchResult
&Res
,ServerState
*Srv
)
905 if (Srv
->Result
== 304)
907 unlink(Queue
->DestFile
.c_str());
909 Res
.LastModified
= Queue
->LastModified
;
913 /* We have a reply we dont handle. This should indicate a perm server
915 if (Srv
->Result
< 200 || Srv
->Result
>= 300)
917 _error
->Error("%u %s",Srv
->Result
,Srv
->Code
);
918 if (Srv
->HaveContent
== true)
923 // This is some sort of 2xx 'data follows' reply
924 Res
.LastModified
= Srv
->Date
;
925 Res
.Size
= Srv
->Size
;
929 File
= new FileFd(Queue
->DestFile
,FileFd::WriteAny
);
930 if (_error
->PendingError() == true)
933 FailFile
= Queue
->DestFile
;
934 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
936 FailTime
= Srv
->Date
;
938 // Set the expected size
939 if (Srv
->StartPos
>= 0)
941 Res
.ResumePoint
= Srv
->StartPos
;
942 ftruncate(File
->Fd(),Srv
->StartPos
);
945 // Set the start point
946 lseek(File
->Fd(),0,SEEK_END
);
949 Srv
->In
.Hash
= new Hashes
;
951 // Fill the Hash if the file is non-empty (resume)
952 if (Srv
->StartPos
> 0)
954 lseek(File
->Fd(),0,SEEK_SET
);
955 if (Srv
->In
.Hash
->AddFD(File
->Fd(),Srv
->StartPos
) == false)
957 _error
->Errno("read",_("Problem hashing file"));
960 lseek(File
->Fd(),0,SEEK_END
);
963 SetNonBlock(File
->Fd(),true);
967 // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
968 // ---------------------------------------------------------------------
969 /* This closes and timestamps the open file. This is neccessary to get
970 resume behavoir on user abort */
971 void HttpMethod::SigTerm(int)
979 UBuf
.actime
= FailTime
;
980 UBuf
.modtime
= FailTime
;
981 utime(FailFile
.c_str(),&UBuf
);
986 // HttpMethod::Fetch - Fetch an item /*{{{*/
987 // ---------------------------------------------------------------------
988 /* This adds an item to the pipeline. We keep the pipeline at a fixed
990 bool HttpMethod::Fetch(FetchItem
*)
995 // Queue the requests
998 for (FetchItem
*I
= Queue
; I
!= 0 && Depth
< (signed)PipelineDepth
;
999 I
= I
->Next
, Depth
++)
1001 // If pipelining is disabled, we only queue 1 request
1002 if (Server
->Pipeline
== false && Depth
>= 0)
1005 // Make sure we stick with the same server
1006 if (Server
->Comp(I
->Uri
) == false)
1012 QueueBack
= I
->Next
;
1013 SendReq(I
,Server
->Out
);
1021 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* We stash the desired pipeline depth */
1024 bool HttpMethod::Configuration(string Message
)
1026 if (pkgAcqMethod::Configuration(Message
) == false)
1029 TimeOut
= _config
->FindI("Acquire::http::Timeout",TimeOut
);
1030 PipelineDepth
= _config
->FindI("Acquire::http::Pipeline-Depth",
1032 Debug
= _config
->FindB("Debug::Acquire::http",false);
1037 // HttpMethod::Loop - Main loop /*{{{*/
1038 // ---------------------------------------------------------------------
1040 int HttpMethod::Loop()
1042 signal(SIGTERM
,SigTerm
);
1043 signal(SIGINT
,SigTerm
);
1047 int FailCounter
= 0;
1050 // We have no commands, wait for some to arrive
1053 if (WaitFd(STDIN_FILENO
) == false)
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))
1066 // Connect to the server
1067 if (Server
== 0 || Server
->Comp(Queue
->Uri
) == false)
1070 Server
= new ServerState(Queue
->Uri
,this);
1073 /* If the server has explicitly said this is the last connection
1074 then we pre-emptively shut down the pipeline and tear down
1075 the connection. This will speed up HTTP/1.0 servers a tad
1076 since we don't have to wait for the close sequence to
1078 if (Server
->Persistent
== false)
1081 // Reset the pipeline
1082 if (Server
->ServerFd
== -1)
1085 // Connnect to the host
1086 if (Server
->Open() == false)
1094 // Fill the pipeline.
1097 // Fetch the next URL header data from the server.
1098 switch (Server
->RunHeaders())
1103 // The header data is bad
1106 _error
->Error(_("Bad header data"));
1112 // The server closed a connection during the header get..
1119 Server
->Pipeline
= false;
1121 if (FailCounter
>= 2)
1123 Fail(_("Connection failed"),true);
1132 // Decide what to do.
1134 Res
.Filename
= Queue
->DestFile
;
1135 switch (DealWithHeaders(Res
,Server
))
1137 // Ok, the file is Open
1143 bool Result
= Server
->RunData();
1145 /* If the server is sending back sizeless responses then fill in
1148 Res
.Size
= File
->Size();
1150 // Close the file, destroy the FD object and timestamp it
1156 struct utimbuf UBuf
;
1158 UBuf
.actime
= Server
->Date
;
1159 UBuf
.modtime
= Server
->Date
;
1160 utime(Queue
->DestFile
.c_str(),&UBuf
);
1162 // Send status to APT
1165 Res
.TakeHashes(*Server
->In
.Hash
);
1181 // Hard server error, not found or something
1188 // Hard internal error, kill the connection and fail
1200 // We need to flush the data, the header is like a 404 w/ error text
1205 // Send to content to dev/null
1206 File
= new FileFd("/dev/null",FileFd::WriteExists
);
1214 Fail(_("Internal error"));
1227 setlocale(LC_ALL
, "");