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>
54 #include <CoreFoundation/CoreFoundation.h>
55 #include <CoreServices/CoreServices.h>
56 #include <SystemConfiguration/SystemConfiguration.h>
59 #include "rfc2553emu.h"
65 CFStringRef Firmware_
;
67 CFStringRef UniqueID_
;
69 void CfrsError(const char *name
, CFReadStreamRef rs
) {
70 CFStreamError se
= CFReadStreamGetError(rs
);
72 if (se
.domain
== kCFStreamErrorDomainCustom
) {
73 } else if (se
.domain
== kCFStreamErrorDomainPOSIX
) {
74 _error
->Error("POSIX: %s", strerror(se
.error
));
75 } else if (se
.domain
== kCFStreamErrorDomainMacOSStatus
) {
76 _error
->Error("MacOSStatus: %ld", se
.error
);
77 } else if (se
.domain
== kCFStreamErrorDomainNetDB
) {
78 _error
->Error("NetDB: %s %s", name
, gai_strerror(se
.error
));
79 } else if (se
.domain
== kCFStreamErrorDomainMach
) {
80 _error
->Error("Mach: %ld", se
.error
);
81 } else if (se
.domain
== kCFStreamErrorDomainHTTP
) {
83 case kCFStreamErrorHTTPParseFailure
:
84 _error
->Error("Parse failure");
87 case kCFStreamErrorHTTPRedirectionLoop
:
88 _error
->Error("Redirection loop");
91 case kCFStreamErrorHTTPBadURL
:
92 _error
->Error("Bad URL");
96 _error
->Error("Unknown HTTP error: %ld", se
.error
);
99 } else if (se
.domain
== kCFStreamErrorDomainSOCKS
) {
100 _error
->Error("SOCKS: %ld", se
.error
);
101 } else if (se
.domain
== kCFStreamErrorDomainSystemConfiguration
) {
102 _error
->Error("SystemConfiguration: %ld", se
.error
);
103 } else if (se
.domain
== kCFStreamErrorDomainSSL
) {
104 _error
->Error("SSL: %ld", se
.error
);
106 _error
->Error("Domain #%ld: %ld", se
.domain
, se
.error
);
110 string
HttpMethod::FailFile
;
111 int HttpMethod::FailFd
= -1;
112 time_t HttpMethod::FailTime
= 0;
113 unsigned long PipelineDepth
= 10;
114 unsigned long TimeOut
= 120;
117 unsigned long CircleBuf::BwReadLimit
=0;
118 unsigned long CircleBuf::BwTickReadData
=0;
119 struct timeval
CircleBuf::BwReadTick
={0,0};
120 const unsigned int CircleBuf::BW_HZ
=10;
122 // CircleBuf::CircleBuf - Circular input buffer /*{{{*/
123 // ---------------------------------------------------------------------
125 CircleBuf::CircleBuf(unsigned long Size
) : Size(Size
), Hash(0)
127 Buf
= new unsigned char[Size
];
130 CircleBuf::BwReadLimit
= _config
->FindI("Acquire::http::Dl-Limit",0)*1024;
133 // CircleBuf::Reset - Reset to the default state /*{{{*/
134 // ---------------------------------------------------------------------
136 void CircleBuf::Reset()
141 MaxGet
= (unsigned int)-1;
150 // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/
151 // ---------------------------------------------------------------------
152 /* This fills up the buffer with as much data as is in the FD, assuming it
154 bool CircleBuf::Read(int Fd
)
156 unsigned long BwReadMax
;
160 // Woops, buffer is full
161 if (InP
- OutP
== Size
)
164 // what's left to read in this tick
165 BwReadMax
= CircleBuf::BwReadLimit
/BW_HZ
;
167 if(CircleBuf::BwReadLimit
) {
169 gettimeofday(&now
,0);
171 unsigned long d
= (now
.tv_sec
-CircleBuf::BwReadTick
.tv_sec
)*1000000 +
172 now
.tv_usec
-CircleBuf::BwReadTick
.tv_usec
;
173 if(d
> 1000000/BW_HZ
) {
174 CircleBuf::BwReadTick
= now
;
175 CircleBuf::BwTickReadData
= 0;
178 if(CircleBuf::BwTickReadData
>= BwReadMax
) {
179 usleep(1000000/BW_HZ
);
184 // Write the buffer segment
186 if(CircleBuf::BwReadLimit
) {
187 Res
= read(Fd
,Buf
+ (InP%Size
),
188 BwReadMax
> LeftRead() ? LeftRead() : BwReadMax
);
190 Res
= read(Fd
,Buf
+ (InP%Size
),LeftRead());
192 if(Res
> 0 && BwReadLimit
> 0)
193 CircleBuf::BwTickReadData
+= Res
;
205 gettimeofday(&Start
,0);
210 // CircleBuf::Read - Put the string into the buffer /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This will hold the string in and fill the buffer with it as it empties */
213 bool CircleBuf::Read(string Data
)
220 // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/
221 // ---------------------------------------------------------------------
223 void CircleBuf::FillOut()
225 if (OutQueue
.empty() == true)
229 // Woops, buffer is full
230 if (InP
- OutP
== Size
)
233 // Write the buffer segment
234 unsigned long Sz
= LeftRead();
235 if (OutQueue
.length() - StrPos
< Sz
)
236 Sz
= OutQueue
.length() - StrPos
;
237 memcpy(Buf
+ (InP%Size
),OutQueue
.c_str() + StrPos
,Sz
);
242 if (OutQueue
.length() == StrPos
)
251 // CircleBuf::Write - Write from the buffer into a FD /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This empties the buffer into the FD. */
254 bool CircleBuf::Write(int Fd
)
260 // Woops, buffer is empty
267 // Write the buffer segment
269 Res
= write(Fd
,Buf
+ (OutP%Size
),LeftWrite());
282 Hash
->Add(Buf
+ (OutP%Size
),Res
);
288 // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/
289 // ---------------------------------------------------------------------
290 /* This copies till the first empty line */
291 bool CircleBuf::WriteTillEl(string
&Data
,bool Single
)
293 // We cheat and assume it is unneeded to have more than one buffer load
294 for (unsigned long I
= OutP
; I
< InP
; I
++)
296 if (Buf
[I%Size
] != '\n')
302 if (I
< InP
&& Buf
[I%Size
] == '\r')
304 if (I
>= InP
|| Buf
[I%Size
] != '\n')
312 unsigned long Sz
= LeftWrite();
317 Data
+= string((char *)(Buf
+ (OutP%Size
)),Sz
);
325 // CircleBuf::Stats - Print out stats information /*{{{*/
326 // ---------------------------------------------------------------------
328 void CircleBuf::Stats()
334 gettimeofday(&Stop
,0);
335 /* float Diff = Stop.tv_sec - Start.tv_sec +
336 (float)(Stop.tv_usec - Start.tv_usec)/1000000;
337 clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/
341 // ServerState::ServerState - Constructor /*{{{*/
342 // ---------------------------------------------------------------------
344 ServerState::ServerState(URI Srv
,HttpMethod
*Owner
) : Owner(Owner
),
345 In(64*1024), Out(4*1024),
351 // ServerState::Open - Open a connection to the server /*{{{*/
352 // ---------------------------------------------------------------------
353 /* This opens a connection to the server. */
354 bool ServerState::Open()
356 // Use the already open connection if possible.
365 // Determine the proxy setting
366 if (getenv("http_proxy") == 0)
368 string DefProxy
= _config
->Find("Acquire::http::Proxy");
369 string SpecificProxy
= _config
->Find("Acquire::http::Proxy::" + ServerName
.Host
);
370 if (SpecificProxy
.empty() == false)
372 if (SpecificProxy
== "DIRECT")
375 Proxy
= SpecificProxy
;
381 Proxy
= getenv("http_proxy");
383 // Parse no_proxy, a , separated list of domains
384 if (getenv("no_proxy") != 0)
386 if (CheckDomainList(ServerName
.Host
,getenv("no_proxy")) == true)
390 // Determine what host and port to use based on the proxy settings
393 if (Proxy
.empty() == true || Proxy
.Host
.empty() == true)
395 if (ServerName
.Port
!= 0)
396 Port
= ServerName
.Port
;
397 Host
= ServerName
.Host
;
406 // Connect to the remote server
407 if (Connect(Host
,Port
,"http",80,ServerFd
,TimeOut
,Owner
) == false)
413 // ServerState::Close - Close a connection to the server /*{{{*/
414 // ---------------------------------------------------------------------
416 bool ServerState::Close()
423 // ServerState::RunHeaders - Get the headers before the data /*{{{*/
424 // ---------------------------------------------------------------------
425 /* Returns 0 if things are OK, 1 if an IO error occursed and 2 if a header
426 parse error occured */
427 int ServerState::RunHeaders()
431 Owner
->Status(_("Waiting for headers"));
445 if (In
.WriteTillEl(Data
) == false)
451 for (string::const_iterator I
= Data
.begin(); I
< Data
.end(); I
++)
453 string::const_iterator J
= I
;
454 for (; J
!= Data
.end() && *J
!= '\n' && *J
!= '\r';J
++);
455 if (HeaderLine(string(I
,J
)) == false)
460 // 100 Continue is a Nop...
464 // Tidy up the connection persistance state.
465 if (Encoding
== Closes
&& HaveContent
== true)
470 while (Owner
->Go(false,this) == true);
475 // ServerState::RunData - Transfer the data from the socket /*{{{*/
476 // ---------------------------------------------------------------------
478 bool ServerState::RunData()
482 // Chunked transfer encoding is fun..
483 if (Encoding
== Chunked
)
487 // Grab the block size
493 if (In
.WriteTillEl(Data
,true) == true)
496 while ((Last
= Owner
->Go(false,this)) == true);
501 // See if we are done
502 unsigned long Len
= strtol(Data
.c_str(),0,16);
507 // We have to remove the entity trailer
511 if (In
.WriteTillEl(Data
,true) == true && Data
.length() <= 2)
514 while ((Last
= Owner
->Go(false,this)) == true);
517 return !_error
->PendingError();
520 // Transfer the block
522 while (Owner
->Go(true,this) == true)
523 if (In
.IsLimit() == true)
527 if (In
.IsLimit() == false)
530 // The server sends an extra new line before the next block specifier..
535 if (In
.WriteTillEl(Data
,true) == true)
538 while ((Last
= Owner
->Go(false,this)) == true);
545 /* Closes encoding is used when the server did not specify a size, the
546 loss of the connection means we are done */
547 if (Encoding
== Closes
)
550 In
.Limit(Size
- StartPos
);
552 // Just transfer the whole block.
555 if (In
.IsLimit() == false)
559 return !_error
->PendingError();
561 while (Owner
->Go(true,this) == true);
564 return Owner
->Flush(this) && !_error
->PendingError();
567 // ServerState::HeaderLine - Process a header line /*{{{*/
568 // ---------------------------------------------------------------------
570 bool ServerState::HeaderLine(string Line
)
572 if (Line
.empty() == true)
575 // The http server might be trying to do something evil.
576 if (Line
.length() >= MAXLEN
)
577 return _error
->Error(_("Got a single header line over %u chars"),MAXLEN
);
579 string::size_type Pos
= Line
.find(' ');
580 if (Pos
== string::npos
|| Pos
+1 > Line
.length())
582 // Blah, some servers use "connection:closes", evil.
583 Pos
= Line
.find(':');
584 if (Pos
== string::npos
|| Pos
+ 2 > Line
.length())
585 return _error
->Error(_("Bad header line"));
589 // Parse off any trailing spaces between the : and the next word.
590 string::size_type Pos2
= Pos
;
591 while (Pos2
< Line
.length() && isspace(Line
[Pos2
]) != 0)
594 string Tag
= string(Line
,0,Pos
);
595 string Val
= string(Line
,Pos2
);
597 if (stringcasecmp(Tag
.c_str(),Tag
.c_str()+4,"HTTP") == 0)
599 // Evil servers return no version
602 if (sscanf(Line
.c_str(),"HTTP/%u.%u %u %[^\n]",&Major
,&Minor
,
604 return _error
->Error(_("The HTTP server sent an invalid reply header"));
610 if (sscanf(Line
.c_str(),"HTTP %u %[^\n]",&Result
,Code
) != 2)
611 return _error
->Error(_("The HTTP server sent an invalid reply header"));
614 /* Check the HTTP response header to get the default persistance
620 if (Major
== 1 && Minor
<= 0)
629 if (stringcasecmp(Tag
,"Content-Length:") == 0)
631 if (Encoding
== Closes
)
635 // The length is already set from the Content-Range header
639 if (sscanf(Val
.c_str(),"%lu",&Size
) != 1)
640 return _error
->Error(_("The HTTP server sent an invalid Content-Length header"));
644 if (stringcasecmp(Tag
,"Content-Type:") == 0)
650 if (stringcasecmp(Tag
,"Content-Range:") == 0)
654 if (sscanf(Val
.c_str(),"bytes %lu-%*u/%lu",&StartPos
,&Size
) != 2)
655 return _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
656 if ((unsigned)StartPos
> Size
)
657 return _error
->Error(_("This HTTP server has broken range support"));
661 if (stringcasecmp(Tag
,"Transfer-Encoding:") == 0)
664 if (stringcasecmp(Val
,"chunked") == 0)
669 if (stringcasecmp(Tag
,"Connection:") == 0)
671 if (stringcasecmp(Val
,"close") == 0)
673 if (stringcasecmp(Val
,"keep-alive") == 0)
678 if (stringcasecmp(Tag
,"Last-Modified:") == 0)
680 if (StrToTime(Val
,Date
) == false)
681 return _error
->Error(_("Unknown date format"));
689 // HttpMethod::SendReq - Send the HTTP request /*{{{*/
690 // ---------------------------------------------------------------------
691 /* This places the http request in the outbound buffer */
692 void HttpMethod::SendReq(FetchItem
*Itm
,CircleBuf
&Out
)
696 // The HTTP server expects a hostname with a trailing :port
698 string ProperHost
= Uri
.Host
;
701 sprintf(Buf
,":%u",Uri
.Port
);
706 if (Itm
->Uri
.length() >= sizeof(Buf
))
709 /* Build the request. We include a keep-alive header only for non-proxy
710 requests. This is to tweak old http/1.0 servers that do support keep-alive
711 but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server
712 will glitch HTTP/1.0 proxies because they do not filter it out and
713 pass it on, HTTP/1.1 says the connection should default to keep alive
714 and we expect the proxy to do this */
715 if (Proxy
.empty() == true || Proxy
.Host
.empty())
716 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n",
717 QuoteString(Uri
.Path
,"~").c_str(),ProperHost
.c_str());
720 /* Generate a cache control header if necessary. We place a max
721 cache age on index files, optionally set a no-cache directive
722 and a no-store directive for archives. */
723 sprintf(Buf
,"GET %s HTTP/1.1\r\nHost: %s\r\n",
724 Itm
->Uri
.c_str(),ProperHost
.c_str());
725 // only generate a cache control header if we actually want to
727 if (_config
->FindB("Acquire::http::No-Cache",false) == false)
729 if (Itm
->IndexFile
== true)
730 sprintf(Buf
+strlen(Buf
),"Cache-Control: max-age=%u\r\n",
731 _config
->FindI("Acquire::http::Max-Age",0));
734 if (_config
->FindB("Acquire::http::No-Store",false) == true)
735 strcat(Buf
,"Cache-Control: no-store\r\n");
739 // generate a no-cache header if needed
740 if (_config
->FindB("Acquire::http::No-Cache",false) == true)
741 strcat(Buf
,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
746 // Check for a partial file
748 if (stat(Itm
->DestFile
.c_str(),&SBuf
) >= 0 && SBuf
.st_size
> 0)
750 // In this case we send an if-range query with a range header
751 sprintf(Buf
,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf
.st_size
- 1,
752 TimeRFC1123(SBuf
.st_mtime
).c_str());
757 if (Itm
->LastModified
!= 0)
759 sprintf(Buf
,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm
->LastModified
).c_str());
764 if (Proxy
.User
.empty() == false || Proxy
.Password
.empty() == false)
765 Req
+= string("Proxy-Authorization: Basic ") +
766 Base64Encode(Proxy
.User
+ ":" + Proxy
.Password
) + "\r\n";
768 if (Uri
.User
.empty() == false || Uri
.Password
.empty() == false)
769 Req
+= string("Authorization: Basic ") +
770 Base64Encode(Uri
.User
+ ":" + Uri
.Password
) + "\r\n";
772 Req
+= "User-Agent: Debian APT-HTTP/1.3\r\n\r\n";
780 // HttpMethod::Go - Run a single loop /*{{{*/
781 // ---------------------------------------------------------------------
782 /* This runs the select loop over the server FDs, Output file FDs and
784 bool HttpMethod::Go(bool ToFile
,ServerState
*Srv
)
786 // Server has closed the connection
787 if (Srv
->ServerFd
== -1 && (Srv
->In
.WriteSpace() == false ||
795 /* Add the server. We only send more requests if the connection will
797 if (Srv
->Out
.WriteSpace() == true && Srv
->ServerFd
!= -1
798 && Srv
->Persistent
== true)
799 FD_SET(Srv
->ServerFd
,&wfds
);
800 if (Srv
->In
.ReadSpace() == true && Srv
->ServerFd
!= -1)
801 FD_SET(Srv
->ServerFd
,&rfds
);
808 if (Srv
->In
.WriteSpace() == true && ToFile
== true && FileFD
!= -1)
809 FD_SET(FileFD
,&wfds
);
812 FD_SET(STDIN_FILENO
,&rfds
);
814 // Figure out the max fd
816 if (MaxFd
< Srv
->ServerFd
)
817 MaxFd
= Srv
->ServerFd
;
824 if ((Res
= select(MaxFd
+1,&rfds
,&wfds
,0,&tv
)) < 0)
828 return _error
->Errno("select",_("Select failed"));
833 _error
->Error(_("Connection timed out"));
834 return ServerDie(Srv
);
838 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&rfds
))
841 if (Srv
->In
.Read(Srv
->ServerFd
) == false)
842 return ServerDie(Srv
);
845 if (Srv
->ServerFd
!= -1 && FD_ISSET(Srv
->ServerFd
,&wfds
))
848 if (Srv
->Out
.Write(Srv
->ServerFd
) == false)
849 return ServerDie(Srv
);
852 // Send data to the file
853 if (FileFD
!= -1 && FD_ISSET(FileFD
,&wfds
))
855 if (Srv
->In
.Write(FileFD
) == false)
856 return _error
->Errno("write",_("Error writing to output file"));
859 // Handle commands from APT
860 if (FD_ISSET(STDIN_FILENO
,&rfds
))
869 // HttpMethod::Flush - Dump the buffer into the file /*{{{*/
870 // ---------------------------------------------------------------------
871 /* This takes the current input buffer from the Server FD and writes it
873 bool HttpMethod::Flush(ServerState
*Srv
)
877 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
879 if (File
->Name() != "/dev/null")
880 SetNonBlock(File
->Fd(),false);
881 if (Srv
->In
.WriteSpace() == false)
884 while (Srv
->In
.WriteSpace() == true)
886 if (Srv
->In
.Write(File
->Fd()) == false)
887 return _error
->Errno("write",_("Error writing to file"));
888 if (Srv
->In
.IsLimit() == true)
892 if (Srv
->In
.IsLimit() == true || Srv
->Encoding
== ServerState::Closes
)
898 // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/
899 // ---------------------------------------------------------------------
901 bool HttpMethod::ServerDie(ServerState
*Srv
)
903 unsigned int LErrno
= errno
;
905 // Dump the buffer to the file
906 if (Srv
->State
== ServerState::Data
)
908 // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
910 if (File
->Name() != "/dev/null")
911 SetNonBlock(File
->Fd(),false);
912 while (Srv
->In
.WriteSpace() == true)
914 if (Srv
->In
.Write(File
->Fd()) == false)
915 return _error
->Errno("write",_("Error writing to the file"));
918 if (Srv
->In
.IsLimit() == true)
923 // See if this is because the server finished the data stream
924 if (Srv
->In
.IsLimit() == false && Srv
->State
!= ServerState::Header
&&
925 Srv
->Encoding
!= ServerState::Closes
)
929 return _error
->Error(_("Error reading from server. Remote end closed connection"));
931 return _error
->Errno("read",_("Error reading from server"));
937 // Nothing left in the buffer
938 if (Srv
->In
.WriteSpace() == false)
941 // We may have got multiple responses back in one packet..
949 // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
950 // ---------------------------------------------------------------------
951 /* We look at the header data we got back from the server and decide what
955 3 - Unrecoverable error
956 4 - Error with error content page
957 5 - Unrecoverable non-server error (close the connection) */
958 int HttpMethod::DealWithHeaders(FetchResult
&Res
,ServerState
*Srv
)
961 if (Srv
->Result
== 304)
963 unlink(Queue
->DestFile
.c_str());
965 Res
.LastModified
= Queue
->LastModified
;
969 /* We have a reply we dont handle. This should indicate a perm server
971 if (Srv
->Result
< 200 || Srv
->Result
>= 300)
973 _error
->Error("%u %s",Srv
->Result
,Srv
->Code
);
974 if (Srv
->HaveContent
== true)
979 // This is some sort of 2xx 'data follows' reply
980 Res
.LastModified
= Srv
->Date
;
981 Res
.Size
= Srv
->Size
;
985 File
= new FileFd(Queue
->DestFile
,FileFd::WriteAny
);
986 if (_error
->PendingError() == true)
989 FailFile
= Queue
->DestFile
;
990 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
992 FailTime
= Srv
->Date
;
994 // Set the expected size
995 if (Srv
->StartPos
>= 0)
997 Res
.ResumePoint
= Srv
->StartPos
;
998 ftruncate(File
->Fd(),Srv
->StartPos
);
1001 // Set the start point
1002 lseek(File
->Fd(),0,SEEK_END
);
1004 delete Srv
->In
.Hash
;
1005 Srv
->In
.Hash
= new Hashes
;
1007 // Fill the Hash if the file is non-empty (resume)
1008 if (Srv
->StartPos
> 0)
1010 lseek(File
->Fd(),0,SEEK_SET
);
1011 if (Srv
->In
.Hash
->AddFD(File
->Fd(),Srv
->StartPos
) == false)
1013 _error
->Errno("read",_("Problem hashing file"));
1016 lseek(File
->Fd(),0,SEEK_END
);
1019 SetNonBlock(File
->Fd(),true);
1023 // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/
1024 // ---------------------------------------------------------------------
1025 /* This closes and timestamps the open file. This is neccessary to get
1026 resume behavoir on user abort */
1027 void HttpMethod::SigTerm(int)
1034 struct utimbuf UBuf
;
1035 UBuf
.actime
= FailTime
;
1036 UBuf
.modtime
= FailTime
;
1037 utime(FailFile
.c_str(),&UBuf
);
1042 // HttpMethod::Fetch - Fetch an item /*{{{*/
1043 // ---------------------------------------------------------------------
1044 /* This adds an item to the pipeline. We keep the pipeline at a fixed
1046 bool HttpMethod::Fetch(FetchItem
*)
1051 // Queue the requests
1054 for (FetchItem
*I
= Queue
; I
!= 0 && Depth
< (signed)PipelineDepth
;
1055 I
= I
->Next
, Depth
++)
1057 // If pipelining is disabled, we only queue 1 request
1058 if (Server
->Pipeline
== false && Depth
>= 0)
1061 // Make sure we stick with the same server
1062 if (Server
->Comp(I
->Uri
) == false)
1068 QueueBack
= I
->Next
;
1069 SendReq(I
,Server
->Out
);
1077 // HttpMethod::Configuration - Handle a configuration message /*{{{*/
1078 // ---------------------------------------------------------------------
1079 /* We stash the desired pipeline depth */
1080 bool HttpMethod::Configuration(string Message
)
1082 if (pkgAcqMethod::Configuration(Message
) == false)
1085 TimeOut
= _config
->FindI("Acquire::http::Timeout",TimeOut
);
1086 PipelineDepth
= _config
->FindI("Acquire::http::Pipeline-Depth",
1088 Debug
= _config
->FindB("Debug::Acquire::http",false);
1093 // HttpMethod::Loop - Main loop /*{{{*/
1094 // ---------------------------------------------------------------------
1096 int HttpMethod::Loop()
1098 signal(SIGTERM
,SigTerm
);
1099 signal(SIGINT
,SigTerm
);
1103 int FailCounter
= 0;
1106 // We have no commands, wait for some to arrive
1109 if (WaitFd(STDIN_FILENO
) == false)
1113 /* Run messages, we can accept 0 (no message) if we didn't
1114 do a WaitFd above.. Otherwise the FD is closed. */
1115 int Result
= Run(true);
1116 if (Result
!= -1 && (Result
!= 0 || Queue
== 0))
1122 CFStringEncoding se
= kCFStringEncodingUTF8
;
1124 char *url
= strdup(Queue
->Uri
.c_str());
1126 URI uri
= std::string(url
);
1127 std::string hs
= uri
.Host
;
1129 std::string urs
= uri
;
1132 size_t bad
= urs
.find_first_of("+");
1133 if (bad
== std::string::npos
)
1136 urs
= urs
.substr(0, bad
) + "%2b" + urs
.substr(bad
+ 1);
1139 CFStringRef sr
= CFStringCreateWithCString(kCFAllocatorDefault
, urs
.c_str(), se
);
1140 CFURLRef ur
= CFURLCreateWithString(kCFAllocatorDefault
, sr
, NULL
);
1142 CFHTTPMessageRef hm
= CFHTTPMessageCreateRequest(kCFAllocatorDefault
, CFSTR("GET"), ur
, kCFHTTPVersion1_1
);
1146 if (stat(Queue
->DestFile
.c_str(), &SBuf
) >= 0 && SBuf
.st_size
> 0) {
1147 sr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
, CFSTR("bytes=%li-"), (long) SBuf
.st_size
- 1);
1148 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("Range"), sr
);
1151 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, TimeRFC1123(SBuf
.st_mtime
).c_str(), se
);
1152 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("If-Range"), sr
);
1154 } else if (Queue
->LastModified
!= 0) {
1155 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, TimeRFC1123(Queue
->LastModified
).c_str(), se
);
1156 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("If-Modified-Since"), sr
);
1160 if (Firmware_
!= NULL
)
1161 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Firmware"), Firmware_
);
1163 sr
= CFStringCreateWithCString(kCFAllocatorDefault
, Machine_
, se
);
1164 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Machine"), sr
);
1167 if (UniqueID_
!= NULL
)
1168 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("X-Unique-ID"), UniqueID_
);
1170 CFHTTPMessageSetHeaderFieldValue(hm
, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98"));
1172 CFReadStreamRef rs
= CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault
, hm
);
1175 CFDictionaryRef dr
= SCDynamicStoreCopyProxies(NULL
);
1176 CFReadStreamSetProperty(rs
, kCFStreamPropertyHTTPProxy
, dr
);
1179 //CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
1180 CFReadStreamSetProperty(rs
, kCFStreamPropertyHTTPAttemptPersistentConnection
, kCFBooleanTrue
);
1186 uint8_t data
[10240];
1189 Status("Connecting to %s", hs
.c_str());
1191 if (!CFReadStreamOpen(rs
)) {
1192 CfrsError("Open", rs
);
1197 rd
= CFReadStreamRead(rs
, data
, sizeof(data
));
1200 CfrsError(uri
.Host
.c_str(), rs
);
1205 Res
.Filename
= Queue
->DestFile
;
1207 hm
= (CFHTTPMessageRef
) CFReadStreamCopyProperty(rs
, kCFStreamPropertyHTTPResponseHeader
);
1208 sc
= CFHTTPMessageGetResponseStatusCode(hm
);
1210 if (sc
== 301 || sc
== 302) {
1211 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Location"));
1216 size_t ln
= CFStringGetLength(sr
) + 1;
1218 url
= static_cast<char *>(malloc(ln
));
1220 if (!CFStringGetCString(sr
, url
, ln
, se
)) {
1230 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Content-Range"));
1232 size_t ln
= CFStringGetLength(sr
) + 1;
1235 if (!CFStringGetCString(sr
, cr
, ln
, se
)) {
1242 if (sscanf(cr
, "bytes %lu-%*u/%lu", &offset
, &Res
.Size
) != 2) {
1243 _error
->Error(_("The HTTP server sent an invalid Content-Range header"));
1248 if (offset
> Res
.Size
) {
1249 _error
->Error(_("This HTTP server has broken range support"));
1254 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Content-Length"));
1256 Res
.Size
= CFStringGetIntValue(sr
);
1261 time(&Res
.LastModified
);
1263 sr
= CFHTTPMessageCopyHeaderFieldValue(hm
, CFSTR("Last-Modified"));
1265 size_t ln
= CFStringGetLength(sr
) + 1;
1268 if (!CFStringGetCString(sr
, cr
, ln
, se
)) {
1275 if (!StrToTime(cr
, Res
.LastModified
)) {
1276 _error
->Error(_("Unknown date format"));
1285 unlink(Queue
->DestFile
.c_str());
1287 Res
.LastModified
= Queue
->LastModified
;
1289 } else if (sc
< 200 || sc
>= 300)
1294 File
= new FileFd(Queue
->DestFile
, FileFd::WriteAny
);
1295 if (_error
->PendingError() == true) {
1302 FailFile
= Queue
->DestFile
;
1303 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
1304 FailFd
= File
->Fd();
1305 FailTime
= Res
.LastModified
;
1307 Res
.ResumePoint
= offset
;
1308 ftruncate(File
->Fd(), offset
);
1311 lseek(File
->Fd(), 0, SEEK_SET
);
1312 if (!hash
.AddFD(File
->Fd(), offset
)) {
1313 _error
->Errno("read", _("Problem hashing file"));
1321 lseek(File
->Fd(), 0, SEEK_END
);
1325 read
: if (rd
== -1) {
1326 CfrsError("rd", rs
);
1328 } else if (rd
== 0) {
1330 Res
.Size
= File
->Size();
1332 struct utimbuf UBuf
;
1334 UBuf
.actime
= Res
.LastModified
;
1335 UBuf
.modtime
= Res
.LastModified
;
1336 utime(Queue
->DestFile
.c_str(), &UBuf
);
1338 Res
.TakeHashes(hash
);
1345 int sz
= write(File
->Fd(), dt
, rd
);
1358 rd
= CFReadStreamRead(rs
, data
, sizeof(data
));
1367 CFReadStreamClose(rs
);
1380 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
1382 memset(nl
, 0, sizeof(nl
));
1383 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
1384 nlist("/usr/lib/libc.dylib", nl
);
1385 if (nl
[0].n_type
!= N_UNDF
)
1386 *(int *) nl
[0].n_value
= 0;
1389 setlocale(LC_ALL
, "");
1394 sysctlbyname("hw.machine", NULL
, &size
, NULL
, 0);
1395 char *machine
= new char[size
];
1396 sysctlbyname("hw.machine", machine
, &size
, NULL
, 0);
1399 const char *path
= "/System/Library/CoreServices/SystemVersion.plist";
1400 CFURLRef url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
, (uint8_t *) path
, strlen(path
), false);
1402 CFPropertyListRef plist
; {
1403 CFReadStreamRef stream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, url
);
1404 CFReadStreamOpen(stream
);
1405 plist
= CFPropertyListCreateFromStream(kCFAllocatorDefault
, stream
, 0, kCFPropertyListImmutable
, NULL
, NULL
);
1406 CFReadStreamClose(stream
);
1411 if (plist
!= NULL
) {
1412 Firmware_
= (CFStringRef
) CFRetain(CFDictionaryGetValue((CFDictionaryRef
) plist
, CFSTR("ProductVersion")));
1416 if (void *lockdown
= lockdown_connect()) {
1417 UniqueID_
= lockdown_copy_value(lockdown
, NULL
, kLockdownUniqueDeviceIDKey
);
1418 lockdown_disconnect(lockdown
);