summaryrefslogtreecommitdiff
path: root/includes/HttpFunctions.php
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-06-04 07:31:04 +0200
committerPierre Schmitz <pierre@archlinux.de>2015-06-04 07:58:39 +0200
commitf6d65e533c62f6deb21342d4901ece24497b433e (patch)
treef28adf0362d14bcd448f7b65a7aaf38650f923aa /includes/HttpFunctions.php
parentc27b2e832fe25651ef2410fae85b41072aae7519 (diff)
Update to MediaWiki 1.25.1
Diffstat (limited to 'includes/HttpFunctions.php')
-rw-r--r--includes/HttpFunctions.php98
1 files changed, 65 insertions, 33 deletions
diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php
index 83021245..8e05f597 100644
--- a/includes/HttpFunctions.php
+++ b/includes/HttpFunctions.php
@@ -55,11 +55,11 @@ class Http {
* to avoid attacks on intranet services accessible by HTTP.
* - userAgent A user agent, if you want to override the default
* MediaWiki/$wgVersion
+ * @param string $caller The method making this request, for profiling
* @return string|bool (bool)false on failure or a string on success
*/
- public static function request( $method, $url, $options = array() ) {
+ public static function request( $method, $url, $options = array(), $caller = __METHOD__ ) {
wfDebug( "HTTP: $method: $url\n" );
- wfProfileIn( __METHOD__ . "-$method" );
$options['method'] = strtoupper( $method );
@@ -70,29 +70,39 @@ class Http {
$options['connectTimeout'] = 'default';
}
- $req = MWHttpRequest::factory( $url, $options );
+ $req = MWHttpRequest::factory( $url, $options, $caller );
$status = $req->execute();
$content = false;
if ( $status->isOK() ) {
$content = $req->getContent();
}
- wfProfileOut( __METHOD__ . "-$method" );
return $content;
}
/**
* Simple wrapper for Http::request( 'GET' )
* @see Http::request()
+ * @since 1.25 Second parameter $timeout removed. Second parameter
+ * is now $options which can be given a 'timeout'
*
* @param string $url
- * @param string $timeout
* @param array $options
+ * @param string $caller The method making this request, for profiling
* @return string
*/
- public static function get( $url, $timeout = 'default', $options = array() ) {
- $options['timeout'] = $timeout;
- return Http::request( 'GET', $url, $options );
+ public static function get( $url, $options = array(), $caller = __METHOD__ ) {
+ $args = func_get_args();
+ if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
+ // Second was used to be the timeout
+ // And third parameter used to be $options
+ wfWarn( "Second parameter should not be a timeout.", 2 );
+ $options = isset( $args[2] ) && is_array( $args[2] ) ?
+ $args[2] : array();
+ $options['timeout'] = $args[1];
+ $caller = __METHOD__;
+ }
+ return Http::request( 'GET', $url, $options, $caller );
}
/**
@@ -101,10 +111,11 @@ class Http {
*
* @param string $url
* @param array $options
+ * @param string $caller The method making this request, for profiling
* @return string
*/
- public static function post( $url, $options = array() ) {
- return Http::request( 'POST', $url, $options );
+ public static function post( $url, $options = array(), $caller = __METHOD__ ) {
+ return Http::request( 'POST', $url, $options, $caller );
}
/**
@@ -114,7 +125,7 @@ class Http {
* @return bool
*/
public static function isLocalURL( $url ) {
- global $wgCommandLineMode, $wgConf;
+ global $wgCommandLineMode, $wgLocalVirtualHosts, $wgConf;
if ( $wgCommandLineMode ) {
return false;
@@ -126,7 +137,7 @@ class Http {
$host = $matches[1];
// Split up dotwise
$domainParts = explode( '.', $host );
- // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
+ // Check if this domain or any superdomain is listed as a local virtual host
$domainParts = array_reverse( $domainParts );
$domain = '';
@@ -139,7 +150,9 @@ class Http {
$domain = $domainPart . '.' . $domain;
}
- if ( $wgConf->isLocalVHost( $domain ) ) {
+ if ( in_array( $domain, $wgLocalVirtualHosts )
+ || $wgConf->isLocalVHost( $domain )
+ ) {
return true;
}
}
@@ -217,10 +230,22 @@ class MWHttpRequest {
public $status;
/**
+ * @var Profiler
+ */
+ protected $profiler;
+
+ /**
+ * @var string
+ */
+ protected $profileName;
+
+ /**
* @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())
+ * @param string $caller The method making this request, for profiling
+ * @param Profiler $profiler An instance of the profiler for profiling, or null
*/
- protected function __construct( $url, $options = array() ) {
+ protected function __construct( $url, $options = array(), $caller = __METHOD__, $profiler = null ) {
global $wgHTTPTimeout, $wgHTTPConnectTimeout;
$this->url = wfExpandUrl( $url, PROTO_HTTP );
@@ -263,6 +288,10 @@ class MWHttpRequest {
if ( $this->noProxy ) {
$this->proxy = ''; // noProxy takes precedence
}
+
+ // Profile based on what's calling us
+ $this->profiler = $profiler;
+ $this->profileName = $caller;
}
/**
@@ -278,11 +307,12 @@ class MWHttpRequest {
* Generate a new request object
* @param string $url Url to use
* @param array $options (optional) extra params to pass (see Http::request())
+ * @param string $caller The method making this request, for profiling
* @throws MWException
* @return CurlHttpRequest|PhpHttpRequest
* @see MWHttpRequest::__construct
*/
- public static function factory( $url, $options = null ) {
+ public static function factory( $url, $options = null, $caller = __METHOD__ ) {
if ( !Http::$httpEngine ) {
Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
} elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
@@ -292,7 +322,7 @@ class MWHttpRequest {
switch ( Http::$httpEngine ) {
case 'curl':
- return new CurlHttpRequest( $url, $options );
+ return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
case 'php':
if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
throw new MWException( __METHOD__ . ': allow_url_fopen ' .
@@ -301,7 +331,7 @@ class MWHttpRequest {
'http://php.net/curl.'
);
}
- return new PhpHttpRequest( $url, $options );
+ return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
default:
throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
}
@@ -434,7 +464,6 @@ class MWHttpRequest {
* @return Status
*/
public function execute() {
- wfProfileIn( __METHOD__ );
$this->content = "";
@@ -452,7 +481,6 @@ class MWHttpRequest {
$this->setUserAgent( Http::userAgent() );
}
- wfProfileOut( __METHOD__ );
}
/**
@@ -461,7 +489,6 @@ class MWHttpRequest {
* found in an array in the member variable headerList.
*/
protected function parseHeader() {
- wfProfileIn( __METHOD__ );
$lastname = "";
@@ -480,7 +507,6 @@ class MWHttpRequest {
$this->parseCookies();
- wfProfileOut( __METHOD__ );
}
/**
@@ -614,7 +640,6 @@ 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;
@@ -627,7 +652,6 @@ class MWHttpRequest {
}
}
- wfProfileOut( __METHOD__ );
}
/**
@@ -715,12 +739,10 @@ class CurlHttpRequest extends MWHttpRequest {
}
public function execute() {
- wfProfileIn( __METHOD__ );
parent::execute();
if ( !$this->status->isOK() ) {
- wfProfileOut( __METHOD__ );
return $this->status;
}
@@ -766,7 +788,6 @@ 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." );
}
@@ -781,6 +802,12 @@ class CurlHttpRequest extends MWHttpRequest {
wfRestoreWarnings();
}
+ if ( $this->profiler ) {
+ $profileSection = $this->profiler->scopedProfileIn(
+ __METHOD__ . '-' . $this->profileName
+ );
+ }
+
$curlRes = curl_exec( $curlHandle );
if ( curl_errno( $curlHandle ) == CURLE_OPERATION_TIMEOUTED ) {
$this->status->fatal( 'http-timed-out', $this->url );
@@ -792,11 +819,13 @@ class CurlHttpRequest extends MWHttpRequest {
curl_close( $curlHandle );
+ if ( $this->profiler ) {
+ $this->profiler->scopedProfileOut( $profileSection );
+ }
+
$this->parseHeader();
$this->setStatus();
- wfProfileOut( __METHOD__ );
-
return $this->status;
}
@@ -832,7 +861,6 @@ class PhpHttpRequest extends MWHttpRequest {
}
public function execute() {
- wfProfileIn( __METHOD__ );
parent::execute();
@@ -903,6 +931,11 @@ class PhpHttpRequest extends MWHttpRequest {
$result = array();
+ if ( $this->profiler ) {
+ $profileSection = $this->profiler->scopedProfileIn(
+ __METHOD__ . '-' . $this->profileName
+ );
+ }
do {
$reqCount++;
wfSuppressWarnings();
@@ -933,18 +966,19 @@ class PhpHttpRequest extends MWHttpRequest {
break;
}
} while ( true );
+ if ( $this->profiler ) {
+ $this->profiler->scopedProfileOut( $profileSection );
+ }
$this->setStatus();
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;
}
@@ -966,8 +1000,6 @@ class PhpHttpRequest extends MWHttpRequest {
}
fclose( $fh );
- wfProfileOut( __METHOD__ );
-
return $this->status;
}
}