pick up Translation-* even if only compressed available
[ntk/apt.git] / methods / https.cc
index fc649d6..84ce2d6 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/acquire-method.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/hashes.h>
 #include <apt-pkg/netrc.h>
+#include <apt-pkg/configuration.h>
 
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <errno.h>
 #include <string.h>
 #include <iostream>
-#include <apti18n.h>
 #include <sstream>
 
 #include "config.h"
 #include "https.h"
-
+#include <apti18n.h>
                                                                        /*}}}*/
 using namespace std;
 
@@ -51,7 +53,7 @@ HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow,
 {
    HttpsMethod *me = (HttpsMethod *)clientp;
    if(dltotal > 0 && me->Res.Size == 0) {
-      me->Res.Size = (unsigned long)dltotal;
+      me->Res.Size = (unsigned long long)dltotal;
       me->URIStart(me->Res);
    }
    return 0;
@@ -61,6 +63,12 @@ void HttpsMethod::SetupProxy()                                       /*{{{*/
 {
    URI ServerName = Queue->Uri;
 
+   // Curl should never read proxy settings from the environment, as
+   // we determine which proxy to use.  Do this for consistency among
+   // methods and prevent an environment variable overriding a
+   // no-proxy ("DIRECT") setting in apt.conf.
+   curl_easy_setopt(curl, CURLOPT_PROXY, "");
+
    // Determine the proxy setting - try https first, fallback to http and use env at last
    string UseProxy = _config->Find("Acquire::https::Proxy::" + ServerName.Host,
                                   _config->Find("Acquire::http::Proxy::" + ServerName.Host).c_str());
@@ -79,7 +87,14 @@ void HttpsMethod::SetupProxy()                                       /*{{{*/
       if (getenv("no_proxy") != 0 && CheckDomainList(ServerName.Host,getenv("no_proxy")) == true)
         return;
    } else {
-      const char* result = getenv("http_proxy");
+      const char* result = getenv("https_proxy");
+      // FIXME: Fall back to http_proxy is to remain compatible with
+      // existing setups and behaviour of apt.conf.  This should be
+      // deprecated in the future (including apt.conf).  Most other
+      // programs do not fall back to http proxy settings and neither
+      // should Apt.
+      if (result == NULL)
+         result = getenv("http_proxy");
       UseProxy = result == NULL ? "" : result;
    }
 
@@ -90,6 +105,11 @@ void HttpsMethod::SetupProxy()                                      /*{{{*/
       if (Proxy.Port != 1)
         curl_easy_setopt(curl, CURLOPT_PROXYPORT, Proxy.Port);
       curl_easy_setopt(curl, CURLOPT_PROXY, Proxy.Host.c_str());
+      if (Proxy.User.empty() == false || Proxy.Password.empty() == false)
+      {
+         curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, Proxy.User.c_str());
+         curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, Proxy.Password.c_str());
+      }
    }
 }                                                                      /*}}}*/
 // HttpsMethod::Fetch - Fetch an item                                  /*{{{*/
@@ -98,7 +118,6 @@ void HttpsMethod::SetupProxy()                                       /*{{{*/
    depth. */
 bool HttpsMethod::Fetch(FetchItem *Itm)
 {
-   stringstream ss;
    struct stat SBuf;
    struct curl_slist *headers=NULL;  
    char curl_errorstr[CURL_ERROR_SIZE];
@@ -123,7 +142,6 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
-   curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
    curl_easy_setopt(curl, CURLOPT_FILETIME, true);
 
    // SSL parameters are set by default to the common (non mirror-specific) value
@@ -197,6 +215,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
       if (_config->FindB("Acquire::https::No-Store",
                _config->FindB("Acquire::http::No-Store",false)) == true)
         headers = curl_slist_append(headers,"Cache-Control: no-store");
+      stringstream ss;
       ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age",
                _config->FindI("Acquire::http::Max-Age",0)));
       headers = curl_slist_append(headers, ss.str().c_str());
@@ -217,7 +236,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    curl_easy_setopt(curl, CURLOPT_USERAGENT,
        _config->Find("Acquire::https::User-Agent",
                _config->Find("Acquire::http::User-Agent",
-                       "Debian APT-CURL/1.0 ("VERSION")").c_str()).c_str());
+                       "Debian APT-CURL/1.0 (" PACKAGE_VERSION ")").c_str()).c_str());
 
    // set timeout
    int const timeout = _config->FindI("Acquire::https::Timeout",
@@ -238,17 +257,31 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
       curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
 
    // error handling
+   curl_errorstr[0] = '\0';
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 
+   // If we ask for uncompressed files servers might respond with content-
+   // negotation which lets us end up with compressed files we do not support,
+   // see 657029, 657560 and co, so if we have no extension on the request
+   // ask for text only. As a sidenote: If there is nothing to negotate servers
+   // seem to be nice and ignore it.
+   if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true)
+   {
+      size_t const filepos = Itm->Uri.find_last_of('/');
+      string const file = Itm->Uri.substr(filepos + 1);
+      if (flExtension(file) == file)
+        headers = curl_slist_append(headers, "Accept: text/*");
+   }
+
    // if we have the file send an if-range query with a range header
    if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
    {
       char Buf[1000];
-      sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",
-             (long)SBuf.st_size - 1,
-             TimeRFC1123(SBuf.st_mtime).c_str());
+      sprintf(Buf, "Range: bytes=%li-", (long) SBuf.st_size - 1);
       headers = curl_slist_append(headers, Buf);
-   } 
+      sprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str());
+      headers = curl_slist_append(headers, Buf);
+   }
    else if(Itm->LastModified > 0)
    {
       curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
@@ -270,14 +303,22 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    long curl_servdate;
    curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate);
 
+   // If the server returns 200 OK but the If-Modified-Since condition is not
+   // met, CURLINFO_CONDITION_UNMET will be set to 1
+   long curl_condition_unmet = 0;
+   curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &curl_condition_unmet);
+
+   File->Close();
+
    // cleanup
-   if(success != 0
+   if(success != 0 || (curl_responsecode != 200 && curl_responsecode != 304))
    {
       _error->Error("%s", curl_errorstr);
+      // unlink, no need keep 401/404 page content in partial/
+      unlink(File->Name().c_str());
       Fail();
       return true;
    }
-   File->Close();
 
    // Timestamp
    struct utimbuf UBuf;
@@ -294,7 +335,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
       Res.Filename = File->Name();
       Res.LastModified = Buf.st_mtime;
       Res.IMSHit = false;
-      if (curl_responsecode == 304)
+      if (curl_responsecode == 304 || curl_condition_unmet)
       {
         unlink(File->Name().c_str());
         Res.IMSHit = true;
@@ -309,7 +350,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm)
    // take hashes
    Hashes Hash;
    FileFd Fd(Res.Filename, FileFd::ReadOnly);
-   Hash.AddFD(Fd.Fd(), Fd.Size());
+   Hash.AddFD(Fd);
    Res.TakeHashes(Hash);
    
    // keep apt updated