]> git.saurik.com Git - apt-legacy.git/blobdiff - methods/http.cc
Drastically improved APT HTTP error messages and actually set the proxy server config...
[apt-legacy.git] / methods / http.cc
index 012d4506b021b0f9fd030426165cd2cb0de4ec8f..4cea76aca3a25afb75960e974af5581cb2529bea 100644 (file)
@@ -50,6 +50,7 @@ extern "C" {
 
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
+#include <SystemConfiguration/SystemConfiguration.h>
 
 #include "connect.h"
 #include "rfc2553emu.h"
@@ -58,6 +59,47 @@ extern "C" {
                                                                        /*}}}*/
 using namespace std;
 
+void CfrsError(CFReadStreamRef rs) {
+    CFStreamError se = CFReadStreamGetError(rs);
+
+    if (se.domain == kCFStreamErrorDomainCustom) {
+    } else if (se.domain == kCFStreamErrorDomainPOSIX) {
+        _error->Error("POSIX: %s", strerror(se.error));
+    } else if (se.domain == kCFStreamErrorDomainMacOSStatus) {
+        _error->Error("MacOSStatus: %ld", se.error);
+    } else if (se.domain == kCFStreamErrorDomainNetDB) {
+        _error->Error("NetDB: %s", gai_strerror(se.error));
+    } else if (se.domain == kCFStreamErrorDomainMach) {
+        _error->Error("Mach: %ld", se.error);
+    } else if (se.domain == kCFStreamErrorDomainHTTP) {
+        switch (se.error) {
+            case kCFStreamErrorHTTPParseFailure:
+                _error->Error("Parse failure");
+            break;
+
+            case kCFStreamErrorHTTPRedirectionLoop:
+                _error->Error("Redirection loop");
+            break;
+
+            case kCFStreamErrorHTTPBadURL:
+                _error->Error("Bad URL");
+            break;
+
+            default:
+                _error->Error("Unknown HTTP error: %ld", se.error);
+            break;
+        }
+    } else if (se.domain == kCFStreamErrorDomainSOCKS) {
+        _error->Error("SOCKS: %ld", se.error);
+    } else if (se.domain == kCFStreamErrorDomainSystemConfiguration) {
+        _error->Error("SystemConfiguration: %ld", se.error);
+    } else if (se.domain == kCFStreamErrorDomainSSL) {
+        _error->Error("SSL: %ld", se.error);
+    } else {
+        _error->Error("Domain #%d: %ld", se.domain, se.error);
+    }
+}
+
 string HttpMethod::FailFile;
 int HttpMethod::FailFd = -1;
 time_t HttpMethod::FailTime = 0;
@@ -1097,6 +1139,10 @@ int HttpMethod::Loop()
       CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm);
       CFRelease(hm);
 
+      CFDictionaryRef dr = SCDynamicStoreCopyProxies(NULL);
+      CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPProxy, dr);
+      CFRelease(dr);
+
       CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
       CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue);
 
@@ -1110,7 +1156,7 @@ int HttpMethod::Loop()
       Status("Connecting to %s", uri.Host.c_str());
 
       if (!CFReadStreamOpen(rs)) {
-         _error->Error("Unable to open stream");
+         CfrsError(rs);
          Fail(true);
          goto done;
       }
@@ -1118,7 +1164,7 @@ int HttpMethod::Loop()
       CFIndex rd = CFReadStreamRead(rs, data, sizeof(data));
 
       if (rd == -1) {
-         _error->Error("Stream read failure");
+         CfrsError(rs);
          Fail(true);
          goto done;
       }
@@ -1135,7 +1181,7 @@ int HttpMethod::Loop()
 
          if (!CFStringGetCString(sr, cr, ln, se)) {
             Fail();
-            goto done;
+            goto done_;
          }
 
          CFRelease(sr);
@@ -1143,13 +1189,13 @@ int HttpMethod::Loop()
          if (sscanf(cr, "bytes %lu-%*u/%lu", &offset, &Res.Size) != 2) {
            _error->Error(_("The HTTP server sent an invalid Content-Range header"));
             Fail();
-            goto done;
+            goto done_;
          }
 
          if (offset > Res.Size) {
            _error->Error(_("This HTTP server has broken range support"));
             Fail();
-            goto done;
+            goto done_;
          }
       } else {
          sr = CFHTTPMessageCopyHeaderFieldValue(hm, CFSTR("Content-Length"));
@@ -1168,7 +1214,7 @@ int HttpMethod::Loop()
 
          if (!CFStringGetCString(sr, cr, ln, se)) {
             Fail();
-            goto done;
+            goto done_;
          }
 
          CFRelease(sr);
@@ -1176,7 +1222,7 @@ int HttpMethod::Loop()
          if (!StrToTime(cr, Res.LastModified)) {
            _error->Error(_("Unknown date format"));
             Fail();
-            goto done;
+            goto done_;
          }
       }
 
@@ -1224,7 +1270,7 @@ int HttpMethod::Loop()
         URIStart(Res);
 
          read: if (rd == -1) {
-            _error->Error("Stream read failure");
+            CfrsError(rs);
             Fail(true);
          } else if (rd == 0) {
            if (Res.Size == 0)
@@ -1261,7 +1307,11 @@ int HttpMethod::Loop()
          }
       }
 
+     goto done;
+    done_:
+      CFRelease(hm);
     done:
+      CFReadStreamClose(rs);
       CFRelease(rs);
 
       FailCounter = 0;