瀏覽代碼

Reformat some code and avoid adding proxy auth if no_proxy matched the url, refs #2017

Jordi Boggiano 12 年之前
父節點
當前提交
13c7be2d7e
共有 2 個文件被更改,包括 37 次插入43 次删除
  1. 1 5
      src/Composer/Util/NoProxyPattern.php
  2. 36 38
      src/Composer/Util/StreamContextFactory.php

+ 1 - 5
src/Composer/Util/NoProxyPattern.php

@@ -135,10 +135,6 @@ class NoProxyPattern
 
 
         // If the ip is within the range, including highest/lowest values,
         // If the ip is within the range, including highest/lowest values,
         // then it's witin the CIDR range
         // then it's witin the CIDR range
-        if ($check >= $low && $check <= $high) {
-            return true;
-        } else {
-            return false;
-        }
+        return $check >= $low && $check <= $high;
     }
     }
 }
 }

+ 36 - 38
src/Composer/Util/StreamContextFactory.php

@@ -63,51 +63,49 @@ final class StreamContextFactory
             }
             }
 
 
             $options['http']['proxy'] = $proxyURL;
             $options['http']['proxy'] = $proxyURL;
-            
+
             // Handle no_proxy directive
             // Handle no_proxy directive
-            if (!empty($_SERVER['no_proxy'])) {
-                $host = parse_url($url, PHP_URL_HOST);
-                
-                if (!empty($host)) {
-                    $pattern = new NoProxyPattern($_SERVER['no_proxy']);
-                    
-                    if ($pattern->test($url)) {
-                        $options['http']['proxy'] = '';
-                    }
+            if (!empty($_SERVER['no_proxy']) && parse_url($url, PHP_URL_HOST)) {
+                $pattern = new NoProxyPattern($_SERVER['no_proxy']);
+                if ($pattern->test($url)) {
+                    unset($options['http']['proxy']);
                 }
                 }
             }
             }
 
 
-            // enabled request_fulluri unless it is explicitly disabled
-            switch (parse_url($url, PHP_URL_SCHEME)) {
-                case 'http': // default request_fulluri to true
-                    $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI');
-                    if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
-                        $options['http']['request_fulluri'] = true;
-                    }
-                    break;
-                case 'https': // default request_fulluri to true
-                    $reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI');
-                    if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
-                        $options['http']['request_fulluri'] = true;
-                    }
-                    break;
-            }
-
-            if (isset($proxy['user'])) {
-                $auth = $proxy['user'];
-                if (isset($proxy['pass'])) {
-                    $auth .= ':' . $proxy['pass'];
+            // add request_fulluri and authentication if we still have a proxy to connect to
+            if (isset($options['http']['proxy'])) {
+                // enabled request_fulluri unless it is explicitly disabled
+                switch (parse_url($url, PHP_URL_SCHEME)) {
+                    case 'http': // default request_fulluri to true
+                        $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI');
+                        if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
+                            $options['http']['request_fulluri'] = true;
+                        }
+                        break;
+                    case 'https': // default request_fulluri to true
+                        $reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI');
+                        if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
+                            $options['http']['request_fulluri'] = true;
+                        }
+                        break;
                 }
                 }
-                $auth = base64_encode($auth);
 
 
-                // Preserve headers if already set in default options
-                if (isset($defaultOptions['http']['header'])) {
-                    if (is_string($defaultOptions['http']['header'])) {
-                        $defaultOptions['http']['header'] = array($defaultOptions['http']['header']);
+                if (isset($proxy['user'])) {
+                    $auth = $proxy['user'];
+                    if (isset($proxy['pass'])) {
+                        $auth .= ':' . $proxy['pass'];
+                    }
+                    $auth = base64_encode($auth);
+
+                    // Preserve headers if already set in default options
+                    if (isset($defaultOptions['http']['header'])) {
+                        if (is_string($defaultOptions['http']['header'])) {
+                            $defaultOptions['http']['header'] = array($defaultOptions['http']['header']);
+                        }
+                        $defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}";
+                    } else {
+                        $options['http']['header'] = array("Proxy-Authorization: Basic {$auth}");
                     }
                     }
-                    $defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}";
-                } else {
-                    $options['http']['header'] = array("Proxy-Authorization: Basic {$auth}");
                 }
                 }
             }
             }
         }
         }