2 #include <mach-o/nlist.h>
5 // -*- mode: cpp; mode: fold -*-
7 // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
8 /* ######################################################################
10 HTTP Aquire Method - This is the HTTP aquire method for APT.
12 It uses HTTP/1.1 and many of the fancy options there-in, such as
13 pipelining, range, if-range and so on.
15 It is based on a doubly buffered select loop. A groupe of requests are
16 fed into a single output buffer that is constantly fed out the
17 socket. This provides ideal pipelining as in many cases all of the
18 requests will fit into a single packet. The input socket is buffered
19 the same way and fed into the fd for the file (may be a pipe in future).
21 This double buffering provides fairly substantial transfer rates,
22 compared to wget the http method is about 4% faster. Most importantly,
23 when HTTP is compared with FTP as a protocol the speed difference is
24 huge. In tests over the internet from two sites to llug (via ATM) this
25 program got 230k/s sustained http transfer rates. FTP on the other
26 hand topped out at 170k/s. That combined with the time to setup the
27 FTP connection makes HTTP a vastly superior protocol.
29 ##################################################################### */
31 // Include Files /*{{{*/
32 #include <apt-pkg/fileutl.h>
33 #include <apt-pkg/acquire-method.h>
34 #include <apt-pkg/error.h>
35 #include <apt-pkg/hashes.h>
37 #include <sys/sysctl.h>
51 #include <arpa/inet.h>
53 #include <CoreFoundation/CoreFoundation.h>
54 #include <CoreServices/CoreServices.h>
55 #include <SystemConfiguration/SystemConfiguration.h>
58 #include "rfc2553emu.h"
64 CFStringRef Firmware_
;
66 const char *SerialNumber_
;
68 void CfrsError(const char *name
, CFReadStreamRef rs
) {
69 CFStreamError se
= CFReadStreamGetError(rs
);
71 if (se
.domain
== kCFStreamErrorDomainCustom
) {
72 } else if (se
.domain
== kCFStreamErrorDomainPOSIX
) {
73 _error
->Error("POSIX: %s", strerror(se
.error
));
74 } else if (se
.domain
== kCFStreamErrorDomainMacOSStatus
) {
75 _error
->Error("MacOSStatus: %ld", se
.error
);
76 } else if (se
.domain
== kCFStreamErrorDomainNetDB
) {
77 _error
->Error("NetDB: %s %s", name
, gai_strerror(se
.error
));
78 } else if (se
.domain
== kCFStreamErrorDomainMach
) {
79 _error
->Error("Mach: %ld", se
.error
);
80 } else if (se
.domain
== kCFStreamErrorDomainHTTP
) {
82 case kCFStreamErrorHTTPParseFailure
:
83 _error
->Error("Parse failure");
86 case kCFStreamErrorHTTPRedirectionLoop
:
87 _error
->Error("Redirection loop");
90 case kCFStreamErrorHTTPBadURL
:
91 _error
->Error("Bad URL");
95 _error
->Error("Unknown HTTP error: %ld", se
.error
);
98 } else if (se
.domain
== kCFStreamErrorDomainSOCKS
) {
99 _error
->Error("SOCKS: %ld", se
.error
);
100 } else if (se
.domain
== kCFStreamErrorDomainSystemConfiguration
) {
101 _error
->Error("SystemConfiguration: %ld", se
.error
);
102 } else if (se
.domain
== kCFStreamErrorDomainSSL
) {
103 _error
->Error("SSL: %ld", se
.error
);
105 _error
->Error("Domain #%ld: %ld", se
.domain
, se
.error
);
109 string
HttpMethod::FailFile
;
110 int HttpMethod::FailFd
= -1;
111 time_t HttpMethod::FailTime
= 0;
112 unsigned long PipelineDepth
= 10;
113 unsigned long TimeOut
= 120;
116 unsigned long CircleBuf::BwReadLimit
=0;
117 unsigned long CircleBuf::BwTickReadData
=0;
118 struct timeval
CircleBuf::BwReadTick
={0,0};
119 const unsigned int CircleBuf::BW_HZ
=10;
121 // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
122 // ---------------------------------------------------------------------
124 CircleBuf::CircleBuf(unsigned long Size
) : Size(Size
), Hash(0)
126 Buf
= new unsigned char[Size
];
129 CircleBuf::BwReadLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
132 // CircleBuf::Reset - Reset to the default state /*{{{*/
133 // ---------------------------------------------------------------------
135 void CircleBuf::Reset()
140 MaxGet
= (unsigned int)-1;
149 // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This fills up the buffer with as much data as is in the FD, assuming it
153 bool CircleBuf::Read(int Fd
)
155 unsigned long BwReadMax
;
159 // Woops, buffer is full
160 if (InP
- OutP
== Size
)
163 // what's left to read in this tick
164 BwReadMax
= CircleBuf::BwReadLimit
/BW_HZ
;
166 if(CircleBuf::BwReadLimit
) {
168 gettimeofday(&now
,0);
170 unsigned long d
= (now
.tv_sec
-CircleBuf::BwReadTick
.tv_sec
)*1000000 +
171 now
.tv_usec
-CircleBuf::BwReadTick
.tv_usec
;
172 if(d
> 1000000/BW_HZ
) {
173 CircleBuf::BwReadTick
= now
;
174 CircleBuf::BwTickReadData
= 0;
177 if(CircleBuf::BwTickReadData
>= BwReadMax
) {
178 usleep(1000000/BW_HZ
);
183 // Write the buffer segment
185 if(CircleBuf::BwReadLimit
) {
186 Res
= read(Fd
,Buf
+ (InP%Size
),
187 BwReadMax
> LeftRead() ? LeftRead() : BwReadMax
);
189 Res
= read(Fd
,Buf
+ (InP%Size
),LeftRead());
191 if(Res
> 0 && BwReadLimit
> 0)
192 CircleBuf::BwTickReadData
+= Res
;
204 gettimeofday(&Start
,0);
209 // CircleBuf::Read - Put the string into the buffer /*{{{*/
210 // ---------------------------------------------------------------------
211 /* This will hold the string in and fill the buffer with it as it empties */
212 bool CircleBuf::Read(string Data
)
219 // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
220 // ---------------------------------------------------------------------
222 void CircleBuf::FillOut()
224 if (OutQueue
.empty() == true)
228 // Woops, buffer is full
229 if (InP
- OutP
== Size
)
232 // Write the buffer segment
233 unsigned long Sz
= LeftRead();
234 if (OutQueue
.length() - StrPos
< Sz
)
235 Sz
= OutQueue
.length() - StrPos
;
236 memcpy(Buf
+ (InP%Size
),OutQueue
.c_str() + StrPos
,Sz
);
241 if (OutQueue
.length() == StrPos
)
250 // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
251 // ---------------------------------------------------------------------
252 /* This empties the buffer into the FD. */
253 bool CircleBuf::Write(int Fd
)
259 // Woops, buffer is empty
266 // Write the buffer segment
268 Res
= write(Fd
,Buf
+ (OutP%Size
),LeftWrite());
281 Hash
->Add(Buf
+ (OutP%Size
),Res
);
287 // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
288 // ---------------------------------------------------------------------
289 /* This copies till the first empty line */
290 bool CircleBuf::WriteTillEl(string
&Data
,bool Single
)
292 // We cheat and assume it is unneeded to have more than one buffer load
293 for (unsigned long I
= OutP
; I
< InP
; I
++)
295 if (Buf
[I%Size
] != '\n')
301 if (I
< InP
&& Buf
[I%Size
] == '\r')
303 if (I
>= InP
|| Buf
[I%Size
] != '\n')
311 unsigned long Sz
= LeftWrite();
316 Data
+= string((char *)(Buf
+ (OutP%Size
)),Sz
);
324 // CircleBuf::Stats - Print out stats information /*{{{*/
325 // ---------------------------------------------------------------------
327 void CircleBuf::Stats()
333 gettimeofday(&Stop
,0);
334 /* float Diff = Stop.tv_sec - Start.tv_sec +
335 (float)(Stop.tv_usec - Start.tv_usec)/1000000;
336 clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
340 // ServerState::ServerState - Constructor /*{{{*/
341 // ---------------------------------------------------------------------
343 ServerState::ServerState(URI Srv
,HttpMethod
*Owner
) : Owner(Owner
),
344 In(64*1024), Out(4*1024),
350 // ServerState::Open - Open a connection to the server /*{{{*/
351 // ---------------------------------------------------------------------
352 /* This opens a connection to the server. */
353 bool ServerState::Open()
355 // Use the already open connection if possible.
364 // Determine the proxy setting
365 if (getenv("http_proxy") == 0)
367 string DefProxy
= _config
->Find("Acquire::http::Proxy");
368 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
369 if (SpecificProxy
.empty() == false)
371 if (SpecificProxy
== "DIRECT")
374 Proxy
= SpecificProxy
;
380 Proxy
= getenv("http_proxy");
382 // Parse no_proxy, a , separated list of domains
383 if (getenv("no_proxy") != 0)
385 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
389 // Determine what host and port to use based on the proxy settings
392 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
394 if (ServerName
.Port
!= 0)
395 Port
= ServerName
.Port
;
396 Host
= ServerName
.Host
;
405 // Connect to the remote server
406 if (Connect(Host
,Port
,"http",80,ServerFd
,TimeOut
,Owner
) == false)
412 // ServerState::Close - Close a connection to the server /*{{{*/
413 // ---------------------------------------------------------------------
415 bool ServerState::Close()
422 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
423 // ---------------------------------------------------------------------
424 /* Returns 0 if things are OK, 1 if an IO error occursed and 2 if a header
425 parse error occured */
426 int ServerState::RunHeaders()
430 Owner
->Status(_("Waiting for headers"));
444 if (In
.WriteTillEl(Data
) == false)
450 for (string::const_iterator I
= Data
.begin(); I
< Data
.end(); I
++)
452 string::const_iterator J
= I
;
453 for (; J
!= Data
.end() && *J
!= '\n' && *J
!= '\r';J
++);
454 if (HeaderLine(string(I
,J
)) == false)
459 // 100 Continue is a Nop...
463 // Tidy up the connection persistance state.
464 if (Encoding
== Closes
&& HaveContent
== true)
469 while (Owner
->Go(false,this) == true);
474 // ServerState::RunData - Transfer the data from the socket /*{{{*/
475 // ---------------------------------------------------------------------
477 bool ServerState::RunData()
481 // Chunked transfer encoding is fun..
482 if (Encoding
== Chunked
)
486 // Grab the block size
492 if (In
.WriteTillEl(Data
,true) == true)
495 while ((Last
= Owner
->Go(false,this)) == true);
500 // See if we are done
501 unsigned long Len
= strtol(Data
.c_str(),0,16);
506 // We have to remove the entity trailer
510 if (In
.WriteTillEl(Data
,true) == true && Data
.length() <= 2)
513 while ((Last
= Owner
->Go(false,this)) == true);
516 return !_error
->PendingError();
519 // Transfer the block
521 while (Owner
->Go(true,this) == true)
522 if (In
.IsLimit() == true)
526 if (In
.IsLimit() == false)
529 // The server sends an extra new line before the next block specifier..
534 if (In
.WriteTillEl(Data
,true) == true)
537 while ((Last
= Owner
->Go(false,this)) == true);
544 /* Closes encoding is used when the server did not specify a size, the
545 loss of the connection means we are done */
546 if (Encoding
== Closes
)
549 In
.Limit(Size
- StartPos
);
551 // Just transfer the whole block.
554 if (In
.IsLimit() == false)
558 return !_error
->PendingError();
560 while (Owner
->Go(true,this) == true);
563 return Owner
->Flush(this) && !_error
->PendingError();
566 // ServerState::HeaderLine - Process a header line /*{{{*/
567 // ---------------------------------------------------------------------
569 bool ServerState::HeaderLine(string Line
)
571 if (Line
.empty() == true)
574 // The http server might be trying to do something evil.
575 if (Line
.length() >= MAXLEN
)
576 return _error
->Error(_("Got a single header line over %u chars"),MAXLEN
);
578 string::size_type Pos
= Line
.find(' ');
579 if (Pos
== string::npos
|| Pos
+1 > Line
.length())
581 // Blah, some servers use "connection:closes", evil.
582 Pos
= Line
.find(':');
583 if (Pos
== string::npos
|| Pos
+ 2 > Line
.length())
584 return _error
->Error(_("Bad header line"));
588 // Parse off any trailing spaces between the : and the next word.
589 string::size_type Pos2
= Pos
;
590 while (Pos2
< Line
.length() && isspace(Line
[Pos2
]) != 0)
593 string Tag
= string(Line
,0,Pos
);
594 string Val
= string(Line
,Pos2
);
596 if (stringcasecmp(Tag
.c_str(),Tag
.c_str()+4,"HTTP") == 0)
598 // Evil servers return no version
601 if (sscanf(Line
.c_str(),"HTTP/%u.%u %u %[^\n]",&Major
,&Minor
,
603 return _error
->Error(_("The HTTP server sent an invalid reply header"));
609 if (sscanf(Line
.c_str(),"HTTP %u %[^\n]",&Result
,Code
) != 2)
610 return _error
->Error(_("The HTTP server sent an invalid reply header"));
613 /* Check the HTTP response header to get the default persistance
619 if (Major
== 1 && Minor
<= 0)
628 if (stringcasecmp(Tag
,"Content-Length:") == 0)
630 if (Encoding
== Closes
)
634 // The length is already set from the Content-Range header
638 if (sscanf(Val
.c_str(),"%lu",&Size
) != 1)
639 return _error
->Error(_("The HTTP server sent an invalid Content-Length header"));
643 if (stringcasecmp(Tag
,"Content-Type:") == 0)
649 if (stringcasecmp(Tag
,"Content-Range:") == 0)
653 if (sscanf(Val
.c_str(),"bytes %lu-%*u/%lu",&StartPos
,&Size
) != 2)
654 return _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
655 if ((unsigned)StartPos
> Size
)
656 return _error
->Error(_("This HTTP server has broken range support"));
660 if (stringcasecmp(Tag
,"Transfer-Encoding:") == 0)
663 if (stringcasecmp(Val
,"chunked") == 0)
668 if (stringcasecmp(Tag
,"Connection:") == 0)
670 if (stringcasecmp(Val
,"close") == 0)
672 if (stringcasecmp(Val
,"keep-alive") == 0)
677 if (stringcasecmp(Tag
,"Last-Modified:") == 0)
679 if (StrToTime(Val
,Date
) == false)
680 return _error
->Error(_("Unknown date format"));
688 // HttpMethod::SendReq - Send the HTTP request /*{{{*/
689 // ---------------------------------------------------------------------
690 /* This places the http request in the outbound buffer */
691 void HttpMethod::SendReq(FetchItem
*Itm
,CircleBuf
&Out
)
695 // The HTTP server expects a hostname with a trailing :port
697 string ProperHost
= Uri
.Host
;
700 sprintf(Buf
,":%u",Uri
.Port
);
705 if (Itm
->Uri
.length() >= sizeof(Buf
))
708 /* Build the request. We include a keep-alive header only for non-proxy
709 requests. This is to tweak old http/1.0 servers that do support keep-alive
710 but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server
711 will glitch HTTP/1.0 proxies because they do not filter it out and
712 pass it on, HTTP/1.1 says the connection should default to keep alive
713 and we expect the proxy to do this */
714 if (Proxy
.empty() == true || Proxy
.Host
.empty())
715 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n",
716 QuoteString(Uri
.Path
,"~").c_str(),ProperHost
.c_str());
719 /* Generate a cache control header if necessary. We place a max
720 cache age on index files, optionally set a no-cache directive
721 and a no-store directive for archives. */
722 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\n",
723 Itm
->Uri
.c_str(),ProperHost
.c_str());
724 // only generate a cache control header if we actually want to
726 if (_config
->FindB("Acquire::http::No-Cache",false) == false)
728 if (Itm
->IndexFile
== true)
729 sprintf(Buf
+strlen(Buf
),"Cache-Control: max-age=%u\r\n",
730 _config
->FindI("Acquire::http::Max-Age",0));
733 if (_config
->FindB("Acquire::http::No-Store",false) == true)
734 strcat(Buf
,"Cache-Control: no-store\r\n");
738 // generate a no-cache header if needed
739 if (_config
->FindB("Acquire::http::No-Cache",false) == true)
740 strcat(Buf
,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
745 // Check for a partial file
747 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
749 // In this case we send an if-range query with a range header
750 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf
.st_size
- 1,
751 TimeRFC1123(SBuf
.st_mtime
).c_str());
756 if (Itm
->LastModified
!= 0)
758 sprintf(Buf
,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm
->LastModified
).c_str());
763 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
764 Req
+= string("Proxy-Authorization: Basic ") +
765 Base64Encode(Proxy
.User
+ ":" + Proxy
.Password
) + "\r\n";
767 if (Uri
.User
.empty() == false || Uri
.Password
.empty() == false)
768 Req
+= string("Authorization: Basic ") +
769 Base64Encode(Uri
.User
+ ":" + Uri
.Password
) + "\r\n";
771 Req
+= "User-Agent: Debian APT-HTTP/1.3\r\n\r\n";
779 // HttpMethod::Go - Run a single loop /*{{{*/
780 // ---------------------------------------------------------------------
781 /* This runs the select loop over the server FDs, Output file FDs and
783 bool HttpMethod::Go(bool ToFile
,ServerState
*Srv
)
785 // Server has closed the connection
786 if (Srv
->ServerFd
== -1 && (Srv
->In
.WriteSpace() == false ||
794 /* Add the server. We only send more requests if the connection will
796 if (Srv
->Out
.WriteSpace() == true && Srv
->ServerFd
!= -1
797 && Srv
->Persistent
== true)
798 FD_SET(Srv
->ServerFd
,&wfds
);
799 if (Srv
->In
.ReadSpace() == true && Srv
->ServerFd
!= -1)
800 FD_SET(Srv
->ServerFd
,&rfds
);
807 if (Srv
->In
.WriteSpace() == true && ToFile
== true && FileFD
!= -1)
808 FD_SET(FileFD
,&wfds
);
811 FD_SET(STDIN_FILENO
,&rfds
);
813 // Figure out the max fd
815 if (MaxFd
< Srv
->ServerFd
)
816 MaxFd
= Srv
->ServerFd
;
823 if ((Res
= select(MaxFd
+1,&rfds
,&wfds
,0,&tv
)) < 0)
827 return _error
->Errno("select",_("Select failed"));
832 _error
->Error(_("Connection timed out"));
833 return ServerDie(Srv
);
837 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&rfds
))
840 if (Srv
->In
.Read(Srv
->ServerFd
) == false)
841 return ServerDie(Srv
);
844 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&wfds
))
847 if (Srv
->Out
.Write(Srv
->ServerFd
) == false)
848 return ServerDie(Srv
);
851 // Send data to the file
852 if (FileFD
!= -1 && FD_ISSET(FileFD
,&wfds
))
854 if (Srv
->In
.Write(FileFD
) == false)
855 return _error
->Errno("write",_("Error writing to output file"));
858 // Handle commands from APT
859 if (FD_ISSET(STDIN_FILENO
,&rfds
))
868 // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
869 // ---------------------------------------------------------------------
870 /* This takes the current input buffer from the Server FD and writes it
872 bool HttpMethod::Flush(ServerState
*Srv
)
876 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
878 if (File
->Name() != "/dev/null")
879 SetNonBlock(File
->Fd(),false);
880 if (Srv
->In
.WriteSpace() == false)
883 while (Srv
->In
.WriteSpace() == true)
885 if (Srv
->In
.Write(File
->Fd()) == false)
886 return _error
->Errno("write",_("Error writing to file"));
887 if (Srv
->In
.IsLimit() == true)
891 if (Srv
->In
.IsLimit() == true || Srv
->Encoding
== ServerState::Closes
)
897 // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
898 // ---------------------------------------------------------------------
900 bool HttpMethod::ServerDie(ServerState
*Srv
)
902 unsigned int LErrno
= errno
;
904 // Dump the buffer to the file
905 if (Srv
->State
== ServerState::Data
)
907 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
909 if (File
->Name() != "/dev/null")
910 SetNonBlock(File
->Fd(),false);
911 while (Srv
->In
.WriteSpace() == true)
913 if (Srv
->In
.Write(File
->Fd()) == false)
914 return _error
->Errno("write",_("Error writing to the file"));
917 if (Srv
->In
.IsLimit() == true)
922 // See if this is because the server finished the data stream
923 if (Srv
->In
.IsLimit() == false && Srv
->State
!= ServerState::Header
&&
924 Srv
->Encoding
!= ServerState::Closes
)
928 return _error
->Error(_("Error reading from server. Remote end closed connection"));
930 return _error
->Errno("read",_("Error reading from server"));
936 // Nothing left in the buffer
937 if (Srv
->In
.WriteSpace() == false)
940 // We may have got multiple responses back in one packet..
948 // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
949 // ---------------------------------------------------------------------
950 /* We look at the header data we got back from the server and decide what
954 3 - Unrecoverable error
955 4 - Error with error content page
956 5 - Unrecoverable non-server error (close the connection) */
957 int HttpMethod::DealWithHeaders(FetchResult
&Res
,ServerState
*Srv
)
960 if (Srv
->Result
== 304)
962 unlink(Queue
->DestFile
.c_str());
964 Res
.LastModified
= Queue
->LastModified
;
968 /* We have a reply we dont handle. This should indicate a perm server
970 if (Srv
->Result
< 200 || Srv
->Result
>= 300)
972 _error
->Error("%u %s",Srv
->Result
,Srv
->Code
);
973 if (Srv
->HaveContent
== true)
978 // This is some sort of 2xx 'data follows' reply
979 Res
.LastModified
= Srv
->Date
;
980 Res
.Size
= Srv
->Size
;
984 File
= new FileFd(Queue
->DestFile
,FileFd::WriteAny
);
985 if (_error
->PendingError() == true)
988 FailFile
= Queue
->DestFile
;
989 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
991 FailTime
= Srv
->Date
;
993 // Set the expected size
994 if (Srv
->StartPos
>= 0)
996 Res
.ResumePoint
= Srv
->StartPos
;
997 ftruncate(File
->Fd(),Srv
->StartPos
);
1000 // Set the start point
1001 lseek(File
->Fd(),0,SEEK_END
);
1003 delete Srv
->In
.Hash
;
1004 Srv
->In
.Hash
= new Hashes
;
1006 // Fill the Hash if the file is non-empty (resume)
1007 if (Srv
->StartPos
> 0)
1009 lseek(File
->Fd(),0,SEEK_SET
);
1010 if (Srv
->In
.Hash
->AddFD(File
->Fd(),Srv
->StartPos
) == false)
1012 _error
->Errno("read",_("Problem hashing file"));
1015 lseek(File
->Fd(),0,SEEK_END
);
1018 SetNonBlock(File
->Fd(),true);
1022 // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
1023 // ---------------------------------------------------------------------
1024 /* This closes and timestamps the open file. This is neccessary to get
1025 resume behavoir on user abort */
1026 void HttpMethod::SigTerm(int)
1033 struct utimbuf UBuf
;
1034 UBuf
.actime
= FailTime
;
1035 UBuf
.modtime
= FailTime
;
1036 utime(FailFile
.c_str(),&UBuf
);
1041 // HttpMethod::Fetch - Fetch an item /*{{{*/
1042 // ---------------------------------------------------------------------
1043 /* This adds an item to the pipeline. We keep the pipeline at a fixed
1045 bool HttpMethod::Fetch(FetchItem
*)
1050 // Queue the requests
1053 for (FetchItem
*I
= Queue
; I
!= 0 && Depth
< (signed)PipelineDepth
;
1054 I
= I
->Next
, Depth
++)
1056 // If pipelining is disabled, we only queue 1 request
1057 if (Server
->Pipeline
== false && Depth
>= 0)
1060 // Make sure we stick with the same server
1061 if (Server
->Comp(I
->Uri
) == false)
1067 QueueBack
= I
->Next
;
1068 SendReq(I
,Server
->Out
);
1076 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
1077 // ---------------------------------------------------------------------
1078 /* We stash the desired pipeline depth */
1079 bool HttpMethod::Configuration(string Message
)
1081 if (pkgAcqMethod::Configuration(Message
) == false)
1084 TimeOut
= _config
->FindI("Acquire::http::Timeout",TimeOut
);
1085 PipelineDepth
= _config
->FindI("Acquire::http::Pipeline-Depth",
1087 Debug
= _config
->FindB("Debug::Acquire::http",false);
1092 // HttpMethod::Loop - Main loop /*{{{*/
1093 // ---------------------------------------------------------------------
1095 int HttpMethod::Loop()
1097 signal(SIGTERM
,SigTerm
);
1098 signal(SIGINT
,SigTerm
);
1102 int FailCounter
= 0;
1105 // We have no commands, wait for some to arrive
1108 if (WaitFd(STDIN_FILENO
) == false)
1112 /* Run messages, we can accept 0 (no message) if we didn't
1113 do a WaitFd above.. Otherwise the FD is closed. */
1114 int Result
= Run(true);
1115 if (Result
!= -1 && (Result
!= 0 || Queue
== 0))
1121 CFStringEncoding se
= kCFStringEncodingUTF8
;
1123 char *url
= strdup(Queue
->Uri
.c_str());
1125 URI uri
= std::string(url
);
1126 std::string hs
= uri
.Host
;
1128 std::string urs
= uri
;
1130 CFStringRef sr
= CFStringCreateWithCString(kCFAllocatorDefault
, urs
.c_str(), se
);
1131 CFURLRef ur
= CFURLCreateWithString(kCFAllocatorDefault
, sr
, NULL
);
1133 CFHTTPMessageRef hm
= CFHTTPMessageCreateRequest(kCFAllocatorDefault
, CFSTR("GET"), ur
, kCFHTTPVersion1_1
);
1137 if (stat(Queue
->DestFile
.c_str(), &SBuf
) >= 0 && SBuf
.st_size
> 0) {
1138 sr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("bytes=%li-"), (long) SBuf
.st_size
- 1);
1139 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("Range"), sr
);
1142 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, TimeRFC1123(SBuf
.st_mtime
).c_str(), se
);
1143 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("If-Range"), sr
);
1145 } else if (Queue
->LastModified
!= 0) {
1146 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, TimeRFC1123(Queue
->LastModified
).c_str(), se
);
1147 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("If-Modified-Since"), sr
);
1151 if (Firmware_
!= NULL
)
1152 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Firmware"), Firmware_
);
1154 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, Machine_
, se
);
1155 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Machine"), sr
);
1158 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, SerialNumber_
, se
);
1159 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Serial-Number"), sr
);
1162 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98"));
1164 CFReadStreamRef rs
= CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault
, hm
);
1167 CFDictionaryRef dr
= SCDynamicStoreCopyProxies(NULL
);
1168 CFReadStreamSetProperty(rs
, kCFStreamPropertyHTTPProxy
, dr
);
1171 //CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
1172 CFReadStreamSetProperty(rs
, kCFStreamPropertyHTTPAttemptPersistentConnection
, kCFBooleanTrue
);
1178 uint8_t data
[10240];
1181 Status("Connecting to %s", hs
.c_str());
1183 if (!CFReadStreamOpen(rs
)) {
1184 CfrsError("Open", rs
);
1189 rd
= CFReadStreamRead(rs
, data
, sizeof(data
));
1192 CfrsError(uri
.Host
.c_str(), rs
);
1197 Res
.Filename
= Queue
->DestFile
;
1199 hm
= (CFHTTPMessageRef
) CFReadStreamCopyProperty(rs
, kCFStreamPropertyHTTPResponseHeader
);
1200 sc
= CFHTTPMessageGetResponseStatusCode(hm
);
1202 if (sc
== 301 || sc
== 302) {
1203 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Location"));
1208 size_t ln
= CFStringGetLength(sr
) + 1;
1210 url
= static_cast<char *>(malloc(ln
));
1212 if (!CFStringGetCString(sr
, url
, ln
, se
)) {
1222 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Content-Range"));
1224 size_t ln
= CFStringGetLength(sr
) + 1;
1227 if (!CFStringGetCString(sr
, cr
, ln
, se
)) {
1234 if (sscanf(cr
, "bytes %lu-%*u/%lu", &offset
, &Res
.Size
) != 2) {
1235 _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
1240 if (offset
> Res
.Size
) {
1241 _error
->Error(_("This HTTP server has broken range support"));
1246 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Content-Length"));
1248 Res
.Size
= CFStringGetIntValue(sr
);
1253 time(&Res
.LastModified
);
1255 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Last-Modified"));
1257 size_t ln
= CFStringGetLength(sr
) + 1;
1260 if (!CFStringGetCString(sr
, cr
, ln
, se
)) {
1267 if (!StrToTime(cr
, Res
.LastModified
)) {
1268 _error
->Error(_("Unknown date format"));
1277 unlink(Queue
->DestFile
.c_str());
1279 Res
.LastModified
= Queue
->LastModified
;
1281 } else if (sc
< 200 || sc
>= 300)
1286 File
= new FileFd(Queue
->DestFile
, FileFd::WriteAny
);
1287 if (_error
->PendingError() == true) {
1294 FailFile
= Queue
->DestFile
;
1295 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
1296 FailFd
= File
->Fd();
1297 FailTime
= Res
.LastModified
;
1299 Res
.ResumePoint
= offset
;
1300 ftruncate(File
->Fd(), offset
);
1303 lseek(File
->Fd(), 0, SEEK_SET
);
1304 if (!hash
.AddFD(File
->Fd(), offset
)) {
1305 _error
->Errno("read", _("Problem hashing file"));
1313 lseek(File
->Fd(), 0, SEEK_END
);
1317 read
: if (rd
== -1) {
1318 CfrsError("rd", rs
);
1320 } else if (rd
== 0) {
1322 Res
.Size
= File
->Size();
1324 struct utimbuf UBuf
;
1326 UBuf
.actime
= Res
.LastModified
;
1327 UBuf
.modtime
= Res
.LastModified
;
1328 utime(Queue
->DestFile
.c_str(), &UBuf
);
1330 Res
.TakeHashes(hash
);
1337 int sz
= write(File
->Fd(), dt
, rd
);
1350 rd
= CFReadStreamRead(rs
, data
, sizeof(data
));
1359 CFReadStreamClose(rs
);
1372 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
1374 memset(nl
, 0, sizeof(nl
));
1375 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
1376 nlist("/usr/lib/libc.dylib", nl
);
1377 if (nl
[0].n_type
!= N_UNDF
)
1378 *(int *) nl
[0].n_value
= 0;
1381 setlocale(LC_ALL
, "");
1386 sysctlbyname("hw.machine", NULL
, &size
, NULL
, 0);
1387 char *machine
= new char[size
];
1388 sysctlbyname("hw.machine", machine
, &size
, NULL
, 0);
1391 const char *path
= "/System/Library/CoreServices/SystemVersion.plist";
1392 CFURLRef url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
, (uint8_t *) path
, strlen(path
), false);
1394 CFPropertyListRef plist
; {
1395 CFReadStreamRef stream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, url
);
1396 CFReadStreamOpen(stream
);
1397 plist
= CFPropertyListCreateFromStream(kCFAllocatorDefault
, stream
, 0, kCFPropertyListImmutable
, NULL
, NULL
);
1398 CFReadStreamClose(stream
);
1403 if (plist
!= NULL
) {
1404 Firmware_
= (CFStringRef
) CFRetain(CFDictionaryGetValue((CFDictionaryRef
) plist
, CFSTR("ProductVersion")));
1408 if (CFMutableDictionaryRef dict
= IOServiceMatching("IOPlatformExpertDevice"))
1409 if (io_service_t service
= IOServiceGetMatchingService(kIOMasterPortDefault
, dict
)) {
1410 if (CFTypeRef serial
= IORegistryEntryCreateCFProperty(service
, CFSTR(kIOPlatformSerialNumberKey
), kCFAllocatorDefault
, 0)) {
1411 SerialNumber_
= strdup(CFStringGetCStringPtr((CFStringRef
) serial
, CFStringGetSystemEncoding()));
1415 IOObjectRelease(service
);