summaryrefslogtreecommitdiff
path: root/includes/HttpFunctions.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/HttpFunctions.php')
-rw-r--r--includes/HttpFunctions.php169
1 files changed, 111 insertions, 58 deletions
diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php
index 8e48da46..fa2fc12b 100644
--- a/includes/HttpFunctions.php
+++ b/includes/HttpFunctions.php
@@ -35,19 +35,20 @@ class Http {
/**
* Perform an HTTP request
*
- * @param $method String: HTTP method. Usually GET/POST
- * @param $url String: full URL to act on. If protocol-relative, will be expanded to an http:// URL
- * @param $options Array: options to pass to MWHttpRequest object.
+ * @param string $method HTTP method. Usually GET/POST
+ * @param string $url full URL to act on. If protocol-relative, will be expanded to an http:// URL
+ * @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;
}
/**
@@ -103,7 +110,7 @@ class Http {
/**
* Check if the URL can be served by localhost
*
- * @param $url String: full url to check
+ * @param string $url full url to check
* @return Boolean
*/
public static function isLocalURL( $url ) {
@@ -209,11 +216,11 @@ class MWHttpRequest {
public $status;
/**
- * @param $url String: url to use. If protocol-relative, will be expanded to an http:// URL
- * @param $options Array: (optional) extra params to pass (see Http::request())
+ * @param string $url url to use. If protocol-relative, will be expanded to an http:// URL
+ * @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,12 +236,17 @@ 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'] );
}
$members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
- "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
+ "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
foreach ( $members as $o ) {
if ( isset( $options[$o] ) ) {
@@ -263,8 +275,9 @@ class MWHttpRequest {
/**
* Generate a new request object
- * @param $url String: url to use
- * @param $options Array: (optional) extra params to pass (see Http::request())
+ * @param string $url url to use
+ * @param array $options (optional) extra params to pass (see Http::request())
+ * @throws MWException
* @return CurlHttpRequest|PhpHttpRequest
* @see MWHttpRequest::__construct
*/
@@ -273,10 +286,10 @@ class MWHttpRequest {
Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
} elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
- ' Http::$httpEngine is set to "curl"' );
+ ' Http::$httpEngine is set to "curl"' );
}
- switch( Http::$httpEngine ) {
+ switch ( Http::$httpEngine ) {
case 'curl':
return new CurlHttpRequest( $url, $options );
case 'php':
@@ -301,7 +314,7 @@ class MWHttpRequest {
/**
* Set the parameters of the request
-
+ *
* @param $args Array
* @todo overload the args param
*/
@@ -317,21 +330,24 @@ class MWHttpRequest {
public function proxySetup() {
global $wgHTTPProxy;
- if ( $this->proxy || !$this->noProxy ) {
+ // If there is an explicit proxy set and proxies are not disabled, then use it
+ if ( $this->proxy && !$this->noProxy ) {
return;
}
+ // Otherwise, fallback to $wgHTTPProxy/http_proxy (when set) if this is not a machine
+ // local URL and proxies are not disabled
if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
$this->proxy = '';
} elseif ( $wgHTTPProxy ) {
- $this->proxy = $wgHTTPProxy ;
+ $this->proxy = $wgHTTPProxy;
} elseif ( getenv( "http_proxy" ) ) {
$this->proxy = getenv( "http_proxy" );
}
}
/**
- * Set the refererer header
+ * Set the referrer header
*/
public function setReferer( $url ) {
$this->setHeader( 'Referer', $url );
@@ -393,6 +409,7 @@ class MWHttpRequest {
* will be aborted.
*
* @param $callback Callback
+ * @throws MWException
*/
public function setCallback( $callback ) {
if ( !is_callable( $callback ) ) {
@@ -422,6 +439,8 @@ class MWHttpRequest {
public function execute() {
global $wgTitle;
+ wfProfileIn( __METHOD__ );
+
$this->content = "";
if ( strtoupper( $this->method ) == "HEAD" ) {
@@ -441,14 +460,18 @@ class MWHttpRequest {
if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
$this->setUserAgent( Http::userAgent() );
}
+
+ wfProfileOut( __METHOD__ );
}
/**
* Parses the headers, including the HTTP status code and any
- * Set-Cookie headers. This function expectes the headers to be
+ * Set-Cookie headers. This function expects the headers to be
* found in an array in the member variable headerList.
*/
protected function parseHeader() {
+ wfProfileIn( __METHOD__ );
+
$lastname = "";
foreach ( $this->headerList as $header ) {
@@ -465,6 +488,8 @@ class MWHttpRequest {
}
$this->parseCookies();
+
+ wfProfileOut( __METHOD__ );
}
/**
@@ -501,7 +526,6 @@ class MWHttpRequest {
return (int)$this->respStatus;
}
-
/**
* Returns true if the last status code was a redirect.
*
@@ -548,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];
}
@@ -579,8 +603,8 @@ class MWHttpRequest {
}
/**
- * Sets a cookie. Used before a request to set up any individual
- * cookies. Used internally after a request to parse the
+ * Sets a cookie. Used before a request to set up any individual
+ * cookies. Used internally after a request to parse the
* Set-Cookie headers.
* @see Cookie::set
* @param $name
@@ -599,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;
}
@@ -609,6 +635,8 @@ class MWHttpRequest {
$this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
}
}
+
+ wfProfileOut( __METHOD__ );
}
/**
@@ -627,18 +655,18 @@ 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);
+ $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' ];
- break; //found correct URI (with host)
+ if ( isset( $url['host'] ) ) {
+ $domain = $url['scheme'] . '://' . $url['host'];
+ break; //found correct URI (with host)
} else {
$foundRelativeURI = true;
}
@@ -646,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];
}
}
@@ -677,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 = "";
@@ -696,14 +719,23 @@ 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;
+
+ // Only supported in curl >= 7.16.2
+ if ( defined( 'CURLOPT_CONNECTTIMEOUT_MS' ) ) {
+ $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" );
@@ -742,6 +774,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." );
}
@@ -756,14 +789,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 );
}
@@ -773,6 +803,8 @@ class CurlHttpRequest extends MWHttpRequest {
$this->parseHeader();
$this->setStatus();
+ wfProfileOut( __METHOD__ );
+
return $this->status;
}
@@ -807,10 +839,12 @@ class PhpHttpRequest extends MWHttpRequest {
}
public function execute() {
+ wfProfileIn( __METHOD__ );
+
parent::execute();
if ( is_array( $this->postData ) ) {
- $this->postData = wfArrayToCGI( $this->postData );
+ $this->postData = wfArrayToCgi( $this->postData );
}
if ( $this->parsedUrl['scheme'] != 'http' &&
@@ -822,7 +856,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";
}
}
@@ -856,7 +890,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;
@@ -899,18 +949,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 );
@@ -926,6 +977,8 @@ class PhpHttpRequest extends MWHttpRequest {
}
fclose( $fh );
+ wfProfileOut( __METHOD__ );
+
return $this->status;
}
}