port netrc support from maemon
[ntk/apt.git] / methods / https.cc
index 98dfeef..a86c780 100644 (file)
@@ -14,6 +14,7 @@
 #include <apt-pkg/acquire-method.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/hashes.h>
+#include <apt-pkg/netrc.h>
 
 #include <sys/stat.h>
 #include <sys/time.h>
@@ -61,19 +62,26 @@ void HttpsMethod::SetupProxy()
    URI ServerName = Queue->Uri;
 
    // Determine the proxy setting
-   if (getenv("http_proxy") == 0)
+   string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
+   if (!SpecificProxy.empty())
    {
-      string DefProxy = _config->Find("Acquire::http::Proxy");
-      string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host);
-      if (SpecificProxy.empty() == false)
-      {
-        if (SpecificProxy == "DIRECT")
-           Proxy = "";
-        else
-           Proxy = SpecificProxy;
-      }   
-      else
-        Proxy = DefProxy;
+          if (SpecificProxy == "DIRECT")
+                  Proxy = "";
+          else
+                  Proxy = SpecificProxy;
+   }
+   else
+   {
+          string DefProxy = _config->Find("Acquire::http::Proxy");
+          if (!DefProxy.empty())
+          {
+                  Proxy = DefProxy;
+          }
+          else
+          {
+                  char* result = getenv("http_proxy");
+                  Proxy = result ? result : "";
+          }
    }
    
    // Parse no_proxy, a , separated list of domains
@@ -119,6 +127,8 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    curl_easy_reset(curl);
    SetupProxy();
 
+   maybe_add_auth (Uri, _config->FindFile("Dir::ETc::netrc"));
+
    // callbacks
    curl_easy_setopt(curl, CURLOPT_URL, Itm->Uri.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
@@ -208,6 +218,11 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
 
+   // set redirect options and default to 10 redirects
+   bool AllowRedirect = _config->FindI("Acquire::https::AllowRedirect", true);
+   curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, AllowRedirect);
+   curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
+
    // debug
    if(_config->FindB("Debug::Acquire::https", false))
       curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
@@ -248,7 +263,6 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    // cleanup
    if(success != 0) 
    {
-      unlink(File->Name().c_str());
       _error->Error("%s", curl_errorstr);
       Fail();
       return true;