diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2013-12-08 09:55:49 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2013-12-08 09:55:49 +0100 |
commit | 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e (patch) | |
tree | af68743f2f4a47d13f2b0eb05f5c4aaf86d8ea37 /includes/HttpFunctions.php | |
parent | af4da56f1ad4d3ef7b06557bae365da2ea27a897 (diff) |
Update to MediaWiki 1.22.0
Diffstat (limited to 'includes/HttpFunctions.php')
-rw-r--r-- | includes/HttpFunctions.php | 118 |
1 files changed, 81 insertions, 37 deletions
diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index dc65c67e..78c2ac7a 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -40,14 +40,15 @@ class Http { * @param array $options options to pass to MWHttpRequest object. * Possible keys for the array: * - timeout Timeout length in seconds + * - connectTimeout Timeout for connection, in seconds (curl only) * - postData An array of key-value pairs or a url-encoded form data * - proxy The proxy to use. * Otherwise it will use $wgHTTPProxy (if set) * Otherwise it will use the environment variable "http_proxy" (if set) * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s). - * - sslVerifyHost (curl only) Verify hostname against certificate - * - sslVerifyCert (curl only) Verify SSL certificate - * - caInfo (curl only) Provide CA information + * - sslVerifyHost Verify hostname against certificate + * - sslVerifyCert Verify SSL certificate + * - caInfo Provide CA information * - maxRedirects Maximum number of redirects to follow (defaults to 5) * - followRedirects Whether to follow redirects (defaults to false). * Note: this should only be used when the target URL is trusted, @@ -58,20 +59,26 @@ class Http { */ public static function request( $method, $url, $options = array() ) { wfDebug( "HTTP: $method: $url\n" ); + wfProfileIn( __METHOD__ . "-$method" ); + $options['method'] = strtoupper( $method ); if ( !isset( $options['timeout'] ) ) { $options['timeout'] = 'default'; } + if ( !isset( $options['connectTimeout'] ) ) { + $options['connectTimeout'] = 'default'; + } $req = MWHttpRequest::factory( $url, $options ); $status = $req->execute(); + $content = false; if ( $status->isOK() ) { - return $req->getContent(); - } else { - return false; + $content = $req->getContent(); } + wfProfileOut( __METHOD__ . "-$method" ); + return $content; } /** @@ -213,7 +220,7 @@ class MWHttpRequest { * @param array $options (optional) extra params to pass (see Http::request()) */ protected function __construct( $url, $options = array() ) { - global $wgHTTPTimeout; + global $wgHTTPTimeout, $wgHTTPConnectTimeout; $this->url = wfExpandUrl( $url, PROTO_HTTP ); $this->parsedUrl = wfParseUrl( $this->url ); @@ -229,7 +236,12 @@ class MWHttpRequest { } else { $this->timeout = $wgHTTPTimeout; } - if( isset( $options['userAgent'] ) ) { + if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) { + $this->connectTimeout = $options['connectTimeout']; + } else { + $this->connectTimeout = $wgHTTPConnectTimeout; + } + if ( isset( $options['userAgent'] ) ) { $this->setUserAgent( $options['userAgent'] ); } @@ -277,7 +289,7 @@ class MWHttpRequest { ' Http::$httpEngine is set to "curl"' ); } - switch( Http::$httpEngine ) { + switch ( Http::$httpEngine ) { case 'curl': return new CurlHttpRequest( $url, $options ); case 'php': @@ -302,7 +314,7 @@ class MWHttpRequest { /** * Set the parameters of the request - + * * @param $args Array * @todo overload the args param */ @@ -427,6 +439,8 @@ class MWHttpRequest { public function execute() { global $wgTitle; + wfProfileIn( __METHOD__ ); + $this->content = ""; if ( strtoupper( $this->method ) == "HEAD" ) { @@ -446,6 +460,8 @@ class MWHttpRequest { if ( !isset( $this->reqHeaders['User-Agent'] ) ) { $this->setUserAgent( Http::userAgent() ); } + + wfProfileOut( __METHOD__ ); } /** @@ -454,6 +470,8 @@ class MWHttpRequest { * found in an array in the member variable headerList. */ protected function parseHeader() { + wfProfileIn( __METHOD__ ); + $lastname = ""; foreach ( $this->headerList as $header ) { @@ -470,6 +488,8 @@ class MWHttpRequest { } $this->parseCookies(); + + wfProfileOut( __METHOD__ ); } /** @@ -552,8 +572,8 @@ class MWHttpRequest { $this->parseHeader(); } - if ( isset( $this->respHeaders[strtolower ( $header ) ] ) ) { - $v = $this->respHeaders[strtolower ( $header ) ]; + if ( isset( $this->respHeaders[strtolower( $header )] ) ) { + $v = $this->respHeaders[strtolower( $header )]; return $v[count( $v ) - 1]; } @@ -603,6 +623,8 @@ class MWHttpRequest { * Parse the cookies in the response headers and store them in the cookie jar. */ protected function parseCookies() { + wfProfileIn( __METHOD__ ); + if ( !$this->cookieJar ) { $this->cookieJar = new CookieJar; } @@ -613,6 +635,8 @@ class MWHttpRequest { $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] ); } } + + wfProfileOut( __METHOD__ ); } /** @@ -631,17 +655,17 @@ class MWHttpRequest { $headers = $this->getResponseHeaders(); //return full url (fix for incorrect but handled relative location) - if ( isset( $headers[ 'location' ] ) ) { - $locations = $headers[ 'location' ]; + if ( isset( $headers['location'] ) ) { + $locations = $headers['location']; $domain = ''; $foundRelativeURI = false; $countLocations = count( $locations ); for ( $i = $countLocations - 1; $i >= 0; $i-- ) { - $url = parse_url( $locations[ $i ] ); + $url = parse_url( $locations[$i] ); if ( isset( $url['host'] ) ) { - $domain = $url[ 'scheme' ] . '://' . $url[ 'host' ]; + $domain = $url['scheme'] . '://' . $url['host']; break; //found correct URI (with host) } else { $foundRelativeURI = true; @@ -650,15 +674,15 @@ class MWHttpRequest { if ( $foundRelativeURI ) { if ( $domain ) { - return $domain . $locations[ $countLocations - 1 ]; + return $domain . $locations[$countLocations - 1]; } else { $url = parse_url( $this->url ); - if ( isset($url[ 'host' ]) ) { - return $url[ 'scheme' ] . '://' . $url[ 'host' ] . $locations[ $countLocations - 1 ]; + if ( isset( $url['host'] ) ) { + return $url['scheme'] . '://' . $url['host'] . $locations[$countLocations - 1]; } } } else { - return $locations[ $countLocations - 1 ]; + return $locations[$countLocations - 1]; } } @@ -681,11 +705,6 @@ class MWHttpRequest { class CurlHttpRequest extends MWHttpRequest { const SUPPORTS_FILE_POSTS = true; - static $curlMessageMap = array( - 6 => 'http-host-unreachable', - 28 => 'http-timed-out' - ); - protected $curlOptions = array(); protected $headerText = ""; @@ -700,14 +719,18 @@ class CurlHttpRequest extends MWHttpRequest { } public function execute() { + wfProfileIn( __METHOD__ ); + parent::execute(); if ( !$this->status->isOK() ) { + wfProfileOut( __METHOD__ ); return $this->status; } $this->curlOptions[CURLOPT_PROXY] = $this->proxy; $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout; + $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000; $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback; $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" ); @@ -746,6 +769,7 @@ class CurlHttpRequest extends MWHttpRequest { $curlHandle = curl_init( $this->url ); if ( !curl_setopt_array( $curlHandle, $this->curlOptions ) ) { + wfProfileOut( __METHOD__ ); throw new MWException( "Error setting curl options." ); } @@ -760,14 +784,11 @@ class CurlHttpRequest extends MWHttpRequest { wfRestoreWarnings(); } - if ( false === curl_exec( $curlHandle ) ) { - $code = curl_error( $curlHandle ); - - if ( isset( self::$curlMessageMap[$code] ) ) { - $this->status->fatal( self::$curlMessageMap[$code] ); - } else { - $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) ); - } + $curlRes = curl_exec( $curlHandle ); + if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) { + $this->status->fatal( 'http-timed-out', $this->url ); + } elseif ( $curlRes === false ) { + $this->status->fatal( 'http-curl-error', curl_error( $curlHandle ) ); } else { $this->headerList = explode( "\r\n", $this->headerText ); } @@ -777,6 +798,8 @@ class CurlHttpRequest extends MWHttpRequest { $this->parseHeader(); $this->setStatus(); + wfProfileOut( __METHOD__ ); + return $this->status; } @@ -811,6 +834,8 @@ class PhpHttpRequest extends MWHttpRequest { } public function execute() { + wfProfileIn( __METHOD__ ); + parent::execute(); if ( is_array( $this->postData ) ) { @@ -826,7 +851,7 @@ class PhpHttpRequest extends MWHttpRequest { if ( $this->method == 'POST' ) { // Required for HTTP 1.0 POSTs $this->reqHeaders['Content-Length'] = strlen( $this->postData ); - if( !isset( $this->reqHeaders['Content-Type'] ) ) { + if ( !isset( $this->reqHeaders['Content-Type'] ) ) { $this->reqHeaders['Content-Type'] = "application/x-www-form-urlencoded"; } } @@ -860,7 +885,23 @@ class PhpHttpRequest extends MWHttpRequest { $options['timeout'] = $this->timeout; - $context = stream_context_create( array( 'http' => $options ) ); + if ( $this->sslVerifyHost ) { + $options['CN_match'] = $this->parsedUrl['host']; + } + if ( $this->sslVerifyCert ) { + $options['verify_peer'] = true; + } + + if ( is_dir( $this->caInfo ) ) { + $options['capath'] = $this->caInfo; + } elseif ( is_file( $this->caInfo ) ) { + $options['cafile'] = $this->caInfo; + } elseif ( $this->caInfo ) { + throw new MWException( "Invalid CA info passed: {$this->caInfo}" ); + } + + $scheme = $this->parsedUrl['scheme']; + $context = stream_context_create( array( "$scheme" => $options ) ); $this->headerList = array(); $reqCount = 0; @@ -903,18 +944,19 @@ class PhpHttpRequest extends MWHttpRequest { if ( $fh === false ) { $this->status->fatal( 'http-request-error' ); + wfProfileOut( __METHOD__ ); return $this->status; } if ( $result['timed_out'] ) { $this->status->fatal( 'http-timed-out', $this->url ); + wfProfileOut( __METHOD__ ); return $this->status; } // If everything went OK, or we received some error code // get the response body content. - if ( $this->status->isOK() - || (int)$this->respStatus >= 300) { + if ( $this->status->isOK() || (int)$this->respStatus >= 300 ) { while ( !feof( $fh ) ) { $buf = fread( $fh, 8192 ); @@ -930,6 +972,8 @@ class PhpHttpRequest extends MWHttpRequest { } fclose( $fh ); + wfProfileOut( __METHOD__ ); + return $this->status; } } |