]> git.saurik.com Git - apt-legacy.git/blobdiff - methods/http.cc.orig
Added no-cache cache control to APT.
[apt-legacy.git] / methods / http.cc.orig
index 27454b97b8cd825a3d3608e6278d7c562833de7e..2e69a79298abab3039534d7d44ae44ca1ae68870 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>
@@ -45,6 +46,7 @@
 #include <netdb.h>
 #include <arpa/inet.h>
 
+#include <lockdown.h>
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
 #include <SystemConfiguration/SystemConfiguration.h>
                                                                        /*}}}*/
 using namespace std;
 
+CFStringRef Firmware_;
+const char *Machine_;
+CFStringRef UniqueID_;
+
 void CfrsError(const char *name, CFReadStreamRef rs) {
     CFStreamError se = CFReadStreamGetError(rs);
 
@@ -1113,20 +1119,19 @@ int HttpMethod::Loop()
 
       char *url = strdup(Queue->Uri.c_str());
     url:
-      URI uri = Queue->Uri;
+      URI uri = std::string(url);
       std::string hs = uri.Host;
 
-      struct hostent *he = gethostbyname(hs.c_str());
-      if (he == NULL || he->h_addr_list[0] == NULL) {
-         _error->Error(hstrerror(h_errno));
-         Fail(true);
-         free(url);
-      }
-
-      uri.Host = inet_ntoa(* (struct in_addr *) he->h_addr_list[0]);
-
       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);
@@ -1142,18 +1147,29 @@ 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(SBuf.st_mtime).c_str(), se);
+         sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(Queue->LastModified).c_str(), se);
          CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Modified-Since"), sr);
          CFRelease(sr);
-      }
 
-      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98"));
+         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_);
 
-      sr = CFStringCreateWithCString(kCFAllocatorDefault, hs.c_str(), se);
-      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Host"), sr);
+      sr = CFStringCreateWithCString(kCFAllocatorDefault, Machine_, se);
+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Machine"), sr);
       CFRelease(sr);
 
+      if (UniqueID_ != NULL)
+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("X-Unique-ID"), UniqueID_);
+
+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.484"));
+
       CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm);
       CFRelease(hm);
 
@@ -1192,7 +1208,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();
@@ -1365,6 +1381,34 @@ 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 (void *lockdown = lockdown_connect()) {
+        UniqueID_ = lockdown_copy_value(lockdown, NULL, kLockdownUniqueDeviceIDKey);
+        lockdown_disconnect(lockdown);
+    }
    
    return Mth.Loop();
 }