]> git.saurik.com Git - apt-legacy.git/blobdiff - methods/http.cc.orig
Fixed Dl-Limit (long standing APT bug that they won't acknowledge), specified error...
[apt-legacy.git] / methods / http.cc.orig
index 650bad052a21cc2fae8c783ae20b647f4b49b29e..b720a07c6cdf2f9ebdb065f70a3ce51aa576e648 100644 (file)
 #include <string.h>
 #include <iostream>
 #include <apti18n.h>
+#include <set>
 
 // Internet stuff
 #include <netdb.h>
 #include <arpa/inet.h>
 
+#include <lockdown.h>
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
 #include <SystemConfiguration/SystemConfiguration.h>
@@ -59,7 +61,7 @@ using namespace std;
 
 CFStringRef Firmware_;
 const char *Machine_;
-const char *SerialNumber_;
+CFStringRef UniqueID_;
 
 void CfrsError(const char *name, CFReadStreamRef rs) {
     CFStreamError se = CFReadStreamGetError(rs);
@@ -681,6 +683,51 @@ bool ServerState::HeaderLine(string Line)
 }
                                                                        /*}}}*/
 
+static const CFOptionFlags kNetworkEvents =
+    kCFStreamEventOpenCompleted |
+    kCFStreamEventHasBytesAvailable |
+    kCFStreamEventEndEncountered |
+    kCFStreamEventErrorOccurred |
+0;
+
+static void CFReadStreamCallback(CFReadStreamRef stream, CFStreamEventType event, void *arg) {
+    switch (event) {
+        case kCFStreamEventOpenCompleted:
+        break;
+
+        case kCFStreamEventHasBytesAvailable:
+        case kCFStreamEventEndEncountered:
+            *reinterpret_cast<int *>(arg) = 1;
+            CFRunLoopStop(CFRunLoopGetCurrent());
+        break;
+
+        case kCFStreamEventErrorOccurred:
+            *reinterpret_cast<int *>(arg) = -1;
+            CFRunLoopStop(CFRunLoopGetCurrent());
+        break;
+    }
+}
+
+/* http://lists.apple.com/archives/Macnetworkprog/2006/Apr/msg00014.html */
+int CFReadStreamOpen(CFReadStreamRef stream, double timeout) {
+    CFStreamClientContext context;
+    int value(0);
+
+    memset(&context, 0, sizeof(context));
+    context.info = &value;
+
+    if (CFReadStreamSetClient(stream, kNetworkEvents, CFReadStreamCallback, &context)) {
+        CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
+        if (CFReadStreamOpen(stream))
+            CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, false);
+        else
+            value = -1;
+        CFReadStreamSetClient(stream, kCFStreamEventNone, NULL, NULL);
+    }
+
+    return value;
+}
+
 // HttpMethod::SendReq - Send the HTTP request                         /*{{{*/
 // ---------------------------------------------------------------------
 /* This places the http request in the outbound buffer */
@@ -1094,6 +1141,8 @@ int HttpMethod::Loop()
    signal(SIGINT,SigTerm);
    
    Server = 0;
+
+   std::set<std::string> cached;
    
    int FailCounter = 0;
    while (1)
@@ -1121,8 +1170,24 @@ int HttpMethod::Loop()
       URI uri = std::string(url);
       std::string hs = uri.Host;
 
+      if (cached.find(hs) != cached.end()) {
+         _error->Error("Cached Failure");
+         Fail(true);
+         free(url);
+         FailCounter = 0;
+         continue;
+      }
+
       std::string urs = uri;
 
+      for (;;) {
+         size_t bad = urs.find_first_of("+");
+         if (bad == std::string::npos)
+            break;
+         // XXX: generalize
+         urs = urs.substr(0, bad) + "%2b" + urs.substr(bad + 1);
+      }
+
       CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, urs.c_str(), se);
       CFURLRef ur = CFURLCreateWithString(kCFAllocatorDefault, sr, NULL);
       CFRelease(sr);
@@ -1138,11 +1203,16 @@ int HttpMethod::Loop()
          sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(SBuf.st_mtime).c_str(), se);
          CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Range"), sr);
          CFRelease(sr);
+
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Cache-Control"), CFSTR("no-cache"));
       } else if (Queue->LastModified != 0) {
          sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(Queue->LastModified).c_str(), se);
          CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Modified-Since"), sr);
          CFRelease(sr);
-      }
+
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Cache-Control"), CFSTR("no-cache"));
+      } else
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Cache-Control"), CFSTR("max-age=0"));
 
       if (Firmware_ != NULL)
          CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Firmware"), Firmware_);
@@ -1151,15 +1221,28 @@ int HttpMethod::Loop()
       CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Machine"), sr);
       CFRelease(sr);
 
-      sr = CFStringCreateWithCString(kCFAllocatorDefault, SerialNumber_, se);
-      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Serial-Number"), sr);
-      CFRelease(sr);
+      if (UniqueID_ != NULL)
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Unique-ID"), UniqueID_);
 
-      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98"));
+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.534"));
 
       CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm);
       CFRelease(hm);
 
+#define _kCFStreamPropertyReadTimeout CFSTR("_kCFStreamPropertyReadTimeout")
+#define _kCFStreamPropertyWriteTimeout CFSTR("_kCFStreamPropertyWriteTimeout")
+#define _kCFStreamPropertySocketImmediateBufferTimeOut CFSTR("_kCFStreamPropertySocketImmediateBufferTimeOut")
+
+      /*SInt32 to(TimeOut);
+      CFNumberRef nm(CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &to));*/
+      double to(TimeOut);
+      CFNumberRef nm(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &to));
+
+      CFReadStreamSetProperty(rs, _kCFStreamPropertyReadTimeout, nm);
+      CFReadStreamSetProperty(rs, _kCFStreamPropertyWriteTimeout, nm);
+      CFReadStreamSetProperty(rs, _kCFStreamPropertySocketImmediateBufferTimeOut, nm);
+      CFRelease(nm);
+
       CFDictionaryRef dr = SCDynamicStoreCopyProxies(NULL);
       CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPProxy, dr);
       CFRelease(dr);
@@ -1176,9 +1259,22 @@ int HttpMethod::Loop()
 
       Status("Connecting to %s", hs.c_str());
 
-      if (!CFReadStreamOpen(rs)) {
-         CfrsError("Open", rs);
-         Fail(true);
+      switch (CFReadStreamOpen(rs, to)) {
+         case -1:
+            CfrsError("Open", rs);
+         goto fail;
+
+         case 0:
+            _error->Error("Host Unreachable");
+            cached.insert(hs);
+         goto fail;
+
+         case 1:
+            /* success */
+         break;
+
+         fail:
+            Fail(true);
          goto done;
       }
 
@@ -1186,6 +1282,7 @@ int HttpMethod::Loop()
 
       if (rd == -1) {
          CfrsError(uri.Host.c_str(), rs);
+         cached.insert(hs);
          Fail(true);
          goto done;
       }
@@ -1267,6 +1364,25 @@ int HttpMethod::Loop()
          }
       }
 
+      if (sc < 200 || sc >= 300 && sc != 304) {
+         sr = CFHTTPMessageCopyResponseStatusLine(hm);
+
+         size_t ln = CFStringGetLength(sr) + 1;
+         char cr[ln];
+
+         if (!CFStringGetCString(sr, cr, ln, se)) {
+            Fail();
+            goto done;
+         }
+
+         CFRelease(sr);
+
+         _error->Error("%s", cr);
+
+         Fail();
+         goto done_;
+      }
+
       CFRelease(hm);
 
       if (sc == 304) {
@@ -1274,9 +1390,7 @@ int HttpMethod::Loop()
          Res.IMSHit = true;
          Res.LastModified = Queue->LastModified;
         URIDone(Res);
-      } else if (sc < 200 || sc >= 300)
-        Fail();
-      else {
+      } else {
          Hashes hash;
 
          File = new FileFd(Queue->DestFile, FileFd::WriteAny);
@@ -1392,15 +1506,10 @@ int main()
         CFRelease(plist);
     }
 
-    if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice"))
-        if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) {
-            if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) {
-                SerialNumber_ = strdup(CFStringGetCStringPtr((CFStringRef) serial, CFStringGetSystemEncoding()));
-                CFRelease(serial);
-            }
-
-            IOObjectRelease(service);
-        }
+    if (void *lockdown = lockdown_connect()) {
+        UniqueID_ = lockdown_copy_value(lockdown, NULL, kLockdownUniqueDeviceIDKey);
+        lockdown_disconnect(lockdown);
+    }
    
    return Mth.Loop();
 }