libproxy implementation for ProxyInfo. Right now it is used if libproxy is present.
[platform/upstream/libzypp.git] / zypp / media / MediaCurl.cc
index f7cce4e..0699e59 100644 (file)
@@ -295,7 +295,7 @@ void fillSettingsFromUrl( const Url &url, TransferSettings &s )
             }
             s.setProxy(proxy);
             s.setProxyEnabled(true);
-        }        
+        }
     }
 
     // HTTP authentication type
@@ -313,6 +313,11 @@ void fillSettingsFromUrl( const Url &url, TransferSettings &s )
        }
         s.setAuthType(use_auth);
     }
+
+    // workarounds
+    std::string head_requests( url.getQueryParam("head_requests"));
+    if( !head_requests.empty() && head_requests == "no")
+        s.setHeadRequestsAllowed(false);
 }
 
 /**
@@ -321,10 +326,14 @@ void fillSettingsFromUrl( const Url &url, TransferSettings &s )
  */
 void fillSettingsSystemProxy( const Url&url, TransferSettings &s )
 {
+#ifdef _WITH_LIBPROXY_SUPPORT_
+    ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoLibproxy()));
+#else
     ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoSysconfig("proxy")));
+#endif
     s.setProxyEnabled( proxy_info.useProxyFor( url ) );
     if ( s.proxyEnabled() )
-      s.setProxy(proxy_info.proxy(url.getScheme()));
+      s.setProxy(proxy_info.proxy(url));
 }
 
 Pathname MediaCurl::_cookieFile = "/var/lib/YaST2/cookies";
@@ -436,6 +445,34 @@ MediaCurl::MediaCurl( const Url &      url_r,
   }
 }
 
+Url MediaCurl::clearQueryString(const Url &url) const
+{
+  Url curlUrl (url);
+  curlUrl.setUsername( "" );
+  curlUrl.setPassword( "" );
+  curlUrl.setPathParams( "" );
+  curlUrl.setFragment( "" );
+  curlUrl.delQueryParam("cookies");
+  curlUrl.delQueryParam("proxy");
+  curlUrl.delQueryParam("proxyport");
+  curlUrl.delQueryParam("proxyuser");
+  curlUrl.delQueryParam("proxypass");
+  curlUrl.delQueryParam("ssl_capath");
+  curlUrl.delQueryParam("ssl_verify");
+  curlUrl.delQueryParam("timeout");
+  curlUrl.delQueryParam("auth");
+  curlUrl.delQueryParam("username");
+  curlUrl.delQueryParam("password");
+  curlUrl.delQueryParam("mediahandler");
+  return curlUrl;
+}
+
+TransferSettings & MediaCurl::settings()
+{
+    return _settings;
+}
+
+
 void MediaCurl::setCookieFile( const Pathname &fileName )
 {
   _cookieFile = fileName;
@@ -454,17 +491,17 @@ void MediaCurl::checkProtocol(const Url &url) const
     std::string        scheme( url.getScheme());
     bool               found = false;
     for(proto=curl_info->protocols; !found && *proto; ++proto)
-    {    
+    {
       if( scheme == std::string((const char *)*proto))
         found = true;
-    }    
+    }
     if( !found)
-    {    
+    {
       std::string msg("Unsupported protocol '");
       msg += scheme;
-      msg += "'"; 
+      msg += "'";
       ZYPP_THROW(MediaBadUrlException(_url, msg));
-    }    
+    }
   }
 }
 
@@ -490,13 +527,14 @@ void MediaCurl::setupEasy()
   SET_OPTION(CURLOPT_FAILONERROR, 1L);
   SET_OPTION(CURLOPT_NOSIGNAL, 1L);
 
-  // reset settings in case we are re-attaching
-  _settings.reset();
+  // create non persistant settings
+  // so that we don't add headers twice
+  TransferSettings vol_settings(_settings);
 
   // add custom headers
-  _settings.addHeader(anonymousIdHeader());
-  _settings.addHeader(distributionFlavorHeader());
-  _settings.addHeader("Pragma:");
+  vol_settings.addHeader(anonymousIdHeader());
+  vol_settings.addHeader(distributionFlavorHeader());
+  vol_settings.addHeader("Pragma:");
 
   _settings.setTimeout(TRANSFER_TIMEOUT);
   _settings.setConnectTimeout(CONNECT_TIMEOUT);
@@ -609,7 +647,7 @@ void MediaCurl::setupEasy()
   {
       SET_OPTION(CURLOPT_NOPROXY, "*");
   }
-  
+
   /** Speed limits */
   if ( _settings.minDownloadSpeed() != 0 )
   {
@@ -637,10 +675,11 @@ void MediaCurl::setupEasy()
   SET_OPTION(CURLOPT_PROXY_TRANSFER_MODE, 1L );
 
   // append settings custom headers to curl
-  for ( TransferSettings::Headers::const_iterator it = _settings.headersBegin();
-        it != _settings.headersEnd();
+  for ( TransferSettings::Headers::const_iterator it = vol_settings.headersBegin();
+        it != vol_settings.headersEnd();
         ++it )
   {
+      MIL << "HEADER " << *it << std::endl;
 
       _customHeaders = curl_slist_append(_customHeaders, it->c_str());
       if ( !_customHeaders )
@@ -976,14 +1015,7 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
     // (some proxies dislike them in the URL).
     // Curl seems to need the just scheme, hostname and a path;
     // the rest was already passed as curl options (in attachTo).
-  Url curlUrl( url );
-
-  // Use asString + url::ViewOptions instead?
-  curlUrl.setUsername( "" );
-  curlUrl.setPassword( "" );
-  curlUrl.setPathParams( "" );
-  curlUrl.setQueryString( "" );
-  curlUrl.setFragment( "" );
+  Url curlUrl( clearQueryString(url) );
 
   //
     // See also Bug #154197 and ftp url definition in RFC 1738:
@@ -1005,7 +1037,8 @@ bool MediaCurl::doGetDoesFileExist( const Pathname & filename ) const
   // works for ftp as well, because retrieving only headers
   // ftp will return always OK code ?
   // See http://curl.haxx.se/docs/knownbugs.html #58
-  if (  _url.getScheme() == "http" ||  _url.getScheme() == "https" )
+  if (  (_url.getScheme() == "http" ||  _url.getScheme() == "https") &&
+        _settings.headRequestsAllowed() )
     ret = curl_easy_setopt( _curl, CURLOPT_NOBODY, 1L );
   else
     ret = curl_easy_setopt( _curl, CURLOPT_RANGE, "0-1" );
@@ -1287,14 +1320,7 @@ void MediaCurl::doGetFileCopyFile( const Pathname & filename , const Pathname &
     // (some proxies dislike them in the URL).
     // Curl seems to need the just scheme, hostname and a path;
     // the rest was already passed as curl options (in attachTo).
-    Url curlUrl( url );
-
-    // Use asString + url::ViewOptions instead?
-    curlUrl.setUsername( "" );
-    curlUrl.setPassword( "" );
-    curlUrl.setPathParams( "" );
-    curlUrl.setQueryString( "" );
-    curlUrl.setFragment( "" );
+    Url curlUrl( clearQueryString(url) );
 
     //
     // See also Bug #154197 and ftp url definition in RFC 1738: