Bug #29755: Apply patch from Vitaliy Filippov so that MW's HTTP client
authorMark A. Hershberger <mah@users.mediawiki.org>
Thu, 7 Jul 2011 14:56:18 +0000 (14:56 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Thu, 7 Jul 2011 14:56:18 +0000 (14:56 +0000)
respects no_proxy env setting

includes/HttpFunctions.php

index bac062f..c73d9a9 100644 (file)
@@ -273,11 +273,60 @@ class MWHttpRequest {
                        $this->proxy = 'http://localhost:80/';
                } elseif ( $wgHTTPProxy ) {
                        $this->proxy = $wgHTTPProxy ;
-               } elseif ( getenv( "http_proxy" ) ) {
-                       $this->proxy = getenv( "http_proxy" );
+               } elseif ( $this->useProxy( $this->url ) ) {
+                       $this->proxy = $this->useProxy( $this->url );
                }
        }
 
+       /**
+        * Determine HTTP proxy from environment settings respecting
+        * 'http_proxy' and 'no_proxy' environment variables
+        */
+       public static function useProxy( $url ) {
+               if ( $proxy = getenv( "http_proxy" ) ) {
+                       $useproxy = true;
+                       if ( $url && ( $noproxy = preg_split( "#\s*,\s*#is", getenv( "no_proxy" ) ) ) ) {
+                               foreach ( $noproxy as $n ) {
+                                       if ( preg_match('#(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)#s', $n, $m) &&
+                                                preg_match('#^[a-z0-9_]+://(?:[^/]*:[^/]*@)?([^/@]+)(?:/|$|\?)#is', $url, $ip) ) {
+                                               $mask = array(
+                                                       max( 0x100 - ( 1 << max(  8-$m[5], 0 ) ), 0 ),
+                                                       max( 0x100 - ( 1 << max( 16-$m[5], 0 ) ), 0 ),
+                                                       max( 0x100 - ( 1 << max( 24-$m[5], 0 ) ), 0 ),
+                                                       max( 0x100 - ( 1 << max( 32-$m[5], 0 ) ), 0 ),
+                                               );
+                                               $ip = @gethostbyname( $ip[1] );
+                                               if ( preg_match( '#(\d+)\.(\d+)\.(\d+)\.(\d+)#s', $ip, $ipm ) &&
+                                                       ( intval( $ipm[1] ) & $mask[0] ) == intval( $m[1] ) &&
+                                                       ( intval( $ipm[2] ) & $mask[1] ) == intval( $m[2] ) &&
+                                                       ( intval( $ipm[3] ) & $mask[2] ) == intval( $m[3] ) &&
+                                                       ( intval( $ipm[4] ) & $mask[3] ) == intval( $m[4] ) ) {
+                                                       $useproxy = false;
+                                                       break;
+                                               }
+                                       } else {
+                                               $n = preg_replace( '/#.*$/is', '', $n );
+                                               $n = preg_quote( $n );
+                                               $n = str_replace( '\\*', '.*', $n );
+                                               if ( preg_match( '#'.$n.'#is', $url ) ) {
+                                                       $useproxy = false;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
+                       if ( $useproxy ) {
+                               $proxy = preg_replace( '#^http://#is', '', $proxy );
+                               $proxy = preg_replace( '#/*$#is', '', $proxy );
+                       }
+                       else {
+                               $proxy = null;
+                       }
+                       return $proxy;
+               }
+               return null;
+       }
+
        /**
         * Set the refererer header
         */