]> git.saurik.com Git - apt-legacy.git/blobdiff - methods/http.cc.orig
Added some initial debtags data.
[apt-legacy.git] / methods / http.cc.orig
index d2762c8f99fc137ac6561a92ef66cd9757ea640f..10f1ae0ec8917d61101ceab70cdf6372e89e322b 100644 (file)
@@ -30,6 +30,7 @@
 #include <apt-pkg/error.h>
 #include <apt-pkg/hashes.h>
 
+#include <sys/sysctl.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <utime.h>
@@ -43,6 +44,7 @@
 
 // Internet stuff
 #include <netdb.h>
+#include <arpa/inet.h>
 
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
                                                                        /*}}}*/
 using namespace std;
 
-void CfrsError(CFReadStreamRef rs) {
+CFStringRef Firmware_;
+const char *Machine_;
+const char *SerialNumber_;
+
+void CfrsError(const char *name, CFReadStreamRef rs) {
     CFStreamError se = CFReadStreamGetError(rs);
 
     if (se.domain == kCFStreamErrorDomainCustom) {
@@ -64,7 +70,7 @@ void CfrsError(CFReadStreamRef rs) {
     } else if (se.domain == kCFStreamErrorDomainMacOSStatus) {
         _error->Error("MacOSStatus: %ld", se.error);
     } else if (se.domain == kCFStreamErrorDomainNetDB) {
-        _error->Error("NetDB: %s", gai_strerror(se.error));
+        _error->Error("NetDB: %s %s", name, gai_strerror(se.error));
     } else if (se.domain == kCFStreamErrorDomainMach) {
         _error->Error("Mach: %ld", se.error);
     } else if (se.domain == kCFStreamErrorDomainHTTP) {
@@ -1112,7 +1118,12 @@ int HttpMethod::Loop()
 
       char *url = strdup(Queue->Uri.c_str());
     url:
-      CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, url, se);
+      URI uri = std::string(url);
+      std::string hs = uri.Host;
+
+      std::string urs = uri;
+
+      CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, urs.c_str(), se);
       CFURLRef ur = CFURLCreateWithString(kCFAllocatorDefault, sr, NULL);
       CFRelease(sr);
       CFHTTPMessageRef hm = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), ur, kCFHTTPVersion1_1);
@@ -1133,7 +1144,19 @@ int HttpMethod::Loop()
          CFRelease(sr);
       }
 
+      if (Firmware_ != NULL)
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Firmware"), Firmware_);
+
+      sr = CFStringCreateWithCString(kCFAllocatorDefault, Machine_, se);
+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Machine"), sr);
+      CFRelease(sr);
+
+      sr = CFStringCreateWithCString(kCFAllocatorDefault, SerialNumber_, se);
+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Serial-Number"), sr);
+      CFRelease(sr);
+
       CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98"));
+
       CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm);
       CFRelease(hm);
 
@@ -1144,8 +1167,6 @@ int HttpMethod::Loop()
       //CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
       CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue);
 
-      URI uri = Queue->Uri;
-
       FetchResult Res;
       CFIndex rd;
       UInt32 sc;
@@ -1153,10 +1174,10 @@ int HttpMethod::Loop()
       uint8_t data[10240];
       size_t offset = 0;
 
-      Status("Connecting to %s", uri.Host.c_str());
+      Status("Connecting to %s", hs.c_str());
 
       if (!CFReadStreamOpen(rs)) {
-         CfrsError(rs);
+         CfrsError("Open", rs);
          Fail(true);
          goto done;
       }
@@ -1164,7 +1185,7 @@ int HttpMethod::Loop()
       rd = CFReadStreamRead(rs, data, sizeof(data));
 
       if (rd == -1) {
-         CfrsError(rs);
+         CfrsError(uri.Host.c_str(), rs);
          Fail(true);
          goto done;
       }
@@ -1174,7 +1195,7 @@ int HttpMethod::Loop()
       hm = (CFHTTPMessageRef) CFReadStreamCopyProperty(rs, kCFStreamPropertyHTTPResponseHeader);
       sc = CFHTTPMessageGetResponseStatusCode(hm);
 
-      if (sc == 302) {
+      if (sc == 301 || sc == 302) {
          sr = CFHTTPMessageCopyHeaderFieldValue(hm, CFSTR("Location"));
          if (sr == NULL) {
             Fail();
@@ -1290,7 +1311,7 @@ int HttpMethod::Loop()
         URIStart(Res);
 
          read: if (rd == -1) {
-            CfrsError(rs);
+            CfrsError("rd", rs);
             Fail(true);
          } else if (rd == 0) {
            if (Res.Size == 0)
@@ -1347,6 +1368,39 @@ int main()
    setlocale(LC_ALL, "");
 
    HttpMethod Mth;
+
+    size_t size;
+    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
+    char *machine = new char[size];
+    sysctlbyname("hw.machine", machine, &size, NULL, 0);
+    Machine_ = machine;
+
+    const char *path = "/System/Library/CoreServices/SystemVersion.plist";
+    CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (uint8_t *) path, strlen(path), false);
+
+    CFPropertyListRef plist; {
+        CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
+        CFReadStreamOpen(stream);
+        plist = CFPropertyListCreateFromStream(kCFAllocatorDefault, stream, 0, kCFPropertyListImmutable, NULL, NULL);
+        CFReadStreamClose(stream);
+    }
+
+    CFRelease(url);
+
+    if (plist != NULL) {
+        Firmware_ = (CFStringRef) CFRetain(CFDictionaryGetValue((CFDictionaryRef) plist, CFSTR("ProductVersion")));
+        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);
+        }
    
    return Mth.Loop();
 }