From: Mark A. Hershberger Date: Thu, 7 Jul 2011 14:56:18 +0000 (+0000) Subject: Bug #29755: Apply patch from Vitaliy Filippov so that MW's HTTP client X-Git-Tag: 1.31.0-rc.0~29017 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=1c712a6cc6209f1058937fa810887781bc23e6fa;p=lhc%2Fweb%2Fwiklou.git Bug #29755: Apply patch from Vitaliy Filippov so that MW's HTTP client respects no_proxy env setting --- diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index bac062fd5c..c73d9a94ba 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -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 */