2018-01-16 23:11:08 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
namespace MediaWiki\Http;
|
|
|
|
|
|
|
|
|
|
use CurlHttpRequest;
|
2019-04-15 13:23:02 +00:00
|
|
|
use GuzzleHttpRequest;
|
2018-01-16 23:11:08 +00:00
|
|
|
use Http;
|
2020-05-15 05:19:56 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2018-01-16 23:11:08 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2020-05-15 05:19:56 +00:00
|
|
|
use MultiHttpClient;
|
2018-01-16 23:11:08 +00:00
|
|
|
use MWHttpRequest;
|
|
|
|
|
use PhpHttpRequest;
|
|
|
|
|
use Profiler;
|
2020-05-15 05:19:56 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-04-15 13:23:02 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
use Status;
|
2018-01-16 23:11:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Factory creating MWHttpRequest objects.
|
|
|
|
|
*/
|
|
|
|
|
class HttpRequestFactory {
|
2020-05-15 05:19:56 +00:00
|
|
|
/** @var ServiceOptions */
|
|
|
|
|
private $options;
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
'HTTPTimeout',
|
|
|
|
|
'HTTPConnectTimeout',
|
|
|
|
|
'HTTPMaxTimeout',
|
|
|
|
|
'HTTPMaxConnectTimeout',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function __construct( ServiceOptions $options, LoggerInterface $logger ) {
|
|
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->options = $options;
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 23:11:08 +00:00
|
|
|
/**
|
|
|
|
|
* Generate a new MWHttpRequest object
|
|
|
|
|
* @param string $url Url to use
|
2019-04-15 13:23:02 +00:00
|
|
|
* @param array $options Possible keys for the array:
|
2019-09-22 19:29:12 +00:00
|
|
|
* - timeout Timeout length in seconds or 'default'
|
|
|
|
|
* - connectTimeout Timeout for connection, in seconds (curl only) or 'default'
|
2020-05-15 05:19:56 +00:00
|
|
|
* - maxTimeout Override for the configured maximum timeout. This should not be
|
|
|
|
|
* used in production code.
|
|
|
|
|
* - maxConnectTimeout Override for the configured maximum connect timeout. This should
|
|
|
|
|
* not be used in production code.
|
2019-04-15 13:23:02 +00:00
|
|
|
* - 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 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,
|
|
|
|
|
* to avoid attacks on intranet services accessible by HTTP.
|
|
|
|
|
* - userAgent A user agent, if you want to override the default
|
2020-02-25 01:33:18 +00:00
|
|
|
* "MediaWiki/{MW_VERSION}".
|
2019-04-15 13:23:02 +00:00
|
|
|
* - logger A \Psr\Logger\LoggerInterface instance for debug logging
|
|
|
|
|
* - username Username for HTTP Basic Authentication
|
|
|
|
|
* - password Password for HTTP Basic Authentication
|
|
|
|
|
* - originalRequest Information about the original request (as a WebRequest object or
|
|
|
|
|
* an associative array with 'ip' and 'userAgent').
|
2019-08-30 18:17:32 +00:00
|
|
|
* @codingStandardsIgnoreStart
|
2019-10-11 14:06:45 +00:00
|
|
|
* @phan-param array{timeout?:int|string,connectTimeout?:int|string,postData?:string|array,proxy?:?string,noProxy?:bool,sslVerifyHost?:bool,sslVerifyCert?:bool,caInfo?:?string,maxRedirects?:int,followRedirects?:bool,userAgent?:string,method?:string,logger?:\Psr\Log\LoggerInterface,username?:string,password?:string,originalRequest?:\WebRequest|array{ip:string,userAgent:string}} $options
|
2019-08-30 18:17:32 +00:00
|
|
|
* @codingStandardsIgnoreEnd
|
2018-01-16 23:11:08 +00:00
|
|
|
* @param string $caller The method making this request, for profiling
|
2019-04-15 13:23:02 +00:00
|
|
|
* @throws RuntimeException
|
2018-01-16 23:11:08 +00:00
|
|
|
* @return MWHttpRequest
|
|
|
|
|
* @see MWHttpRequest::__construct
|
|
|
|
|
*/
|
|
|
|
|
public function create( $url, array $options = [], $caller = __METHOD__ ) {
|
|
|
|
|
if ( !Http::$httpEngine ) {
|
2019-02-06 16:29:52 +00:00
|
|
|
Http::$httpEngine = 'guzzle';
|
2018-01-16 23:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $options['logger'] ) ) {
|
2020-05-15 05:19:56 +00:00
|
|
|
$options['logger'] = $this->logger;
|
2018-01-16 23:11:08 +00:00
|
|
|
}
|
2020-05-15 05:19:56 +00:00
|
|
|
$options['timeout'] = $this->normalizeTimeout(
|
|
|
|
|
$options['timeout'] ?? null,
|
|
|
|
|
$options['maxTimeout'] ?? null,
|
|
|
|
|
$this->options->get( 'HTTPTimeout' ),
|
|
|
|
|
$this->options->get( 'HTTPMaxTimeout' )
|
|
|
|
|
);
|
|
|
|
|
$options['connectTimeout'] = $this->normalizeTimeout(
|
|
|
|
|
$options['connectTimeout'] ?? null,
|
|
|
|
|
$options['maxConnectTimeout'] ?? null,
|
|
|
|
|
$this->options->get( 'HTTPConnectTimeout' ),
|
|
|
|
|
$this->options->get( 'HTTPMaxConnectTimeout' )
|
|
|
|
|
);
|
2018-01-16 23:11:08 +00:00
|
|
|
|
|
|
|
|
switch ( Http::$httpEngine ) {
|
2018-08-17 14:53:59 +00:00
|
|
|
case 'guzzle':
|
|
|
|
|
return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
|
2018-01-16 23:11:08 +00:00
|
|
|
case 'curl':
|
|
|
|
|
return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
|
|
|
|
|
case 'php':
|
|
|
|
|
return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
|
|
|
|
|
default:
|
2019-04-15 13:23:02 +00:00
|
|
|
throw new RuntimeException( __METHOD__ . ': The requested engine is not valid.' );
|
2018-01-16 23:11:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 05:19:56 +00:00
|
|
|
/**
|
|
|
|
|
* Given a passed parameter value, a default and a maximum, figure out the
|
|
|
|
|
* correct timeout to pass to the backend.
|
|
|
|
|
*
|
|
|
|
|
* @param int|float|string|null $parameter The timeout in seconds, or "default" or null
|
|
|
|
|
* @param int|float|null $maxParameter The maximum timeout specified by the caller
|
|
|
|
|
* @param int|float $default The configured default timeout
|
|
|
|
|
* @param int|float $maxConfigured The configured maximum timeout
|
|
|
|
|
* @return int|float
|
|
|
|
|
*/
|
|
|
|
|
private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
|
|
|
|
|
if ( $parameter === 'default' || $parameter === null ) {
|
|
|
|
|
if ( !is_numeric( $default ) ) {
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
|
'$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
|
|
|
|
|
}
|
|
|
|
|
$value = $default;
|
|
|
|
|
} else {
|
|
|
|
|
$value = $parameter;
|
|
|
|
|
}
|
|
|
|
|
if ( $maxParameter !== null ) {
|
|
|
|
|
$max = $maxParameter;
|
|
|
|
|
} else {
|
|
|
|
|
$max = $maxConfigured;
|
|
|
|
|
}
|
|
|
|
|
if ( $max && $value > $max ) {
|
|
|
|
|
return $max;
|
|
|
|
|
} else {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 23:11:08 +00:00
|
|
|
/**
|
|
|
|
|
* Simple function to test if we can make any sort of requests at all, using
|
|
|
|
|
* cURL or fopen()
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canMakeRequests() {
|
|
|
|
|
return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 13:23:02 +00:00
|
|
|
/**
|
|
|
|
|
* Perform an HTTP request
|
|
|
|
|
*
|
|
|
|
|
* @since 1.34
|
|
|
|
|
* @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 See HttpRequestFactory::create
|
|
|
|
|
* @param string $caller The method making this request, for profiling
|
|
|
|
|
* @return string|null null on failure or a string on success
|
|
|
|
|
*/
|
|
|
|
|
public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
|
|
|
|
|
$logger = LoggerFactory::getInstance( 'http' );
|
|
|
|
|
$logger->debug( "$method: $url" );
|
|
|
|
|
|
|
|
|
|
$options['method'] = strtoupper( $method );
|
|
|
|
|
|
|
|
|
|
$req = $this->create( $url, $options, $caller );
|
|
|
|
|
$status = $req->execute();
|
|
|
|
|
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
return $req->getContent();
|
|
|
|
|
} else {
|
|
|
|
|
$errors = $status->getErrorsByType( 'error' );
|
|
|
|
|
$logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
|
|
|
|
|
[ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple wrapper for request( 'GET' ), parameters have same meaning as for request()
|
|
|
|
|
*
|
|
|
|
|
* @since 1.34
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param array $options
|
|
|
|
|
* @param string $caller
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function get( $url, array $options = [], $caller = __METHOD__ ) {
|
2019-05-13 09:31:52 +00:00
|
|
|
return $this->request( 'GET', $url, $options, $caller );
|
2019-04-15 13:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple wrapper for request( 'POST' ), parameters have same meaning as for request()
|
|
|
|
|
*
|
|
|
|
|
* @since 1.34
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param array $options
|
|
|
|
|
* @param string $caller
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function post( $url, array $options = [], $caller = __METHOD__ ) {
|
2019-05-13 09:31:52 +00:00
|
|
|
return $this->request( 'POST', $url, $options, $caller );
|
2019-04-15 13:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getUserAgent() {
|
2020-02-25 01:33:18 +00:00
|
|
|
return 'MediaWiki/' . MW_VERSION;
|
2019-04-15 13:23:02 +00:00
|
|
|
}
|
2020-05-15 05:19:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a MultiHttpClient with MediaWiki configured defaults applied.
|
|
|
|
|
*
|
|
|
|
|
* Unlike create(), by default, no proxy will be used. To use a proxy,
|
|
|
|
|
* specify the 'proxy' option.
|
|
|
|
|
*
|
|
|
|
|
* @param array $options Options as documented in MultiHttpClient::__construct(),
|
|
|
|
|
* except that for consistency with create(), 'timeout' is accepted as an
|
|
|
|
|
* alias for 'reqTimeout', and 'connectTimeout' is accepted as an alias for
|
|
|
|
|
* 'connTimeout'.
|
|
|
|
|
* @return MultiHttpClient
|
|
|
|
|
*/
|
|
|
|
|
public function createMultiClient( $options = [] ) {
|
|
|
|
|
$options['reqTimeout'] = $this->normalizeTimeout(
|
|
|
|
|
$options['reqTimeout'] ?? $options['timeout'] ?? null,
|
|
|
|
|
$options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
|
|
|
|
|
$this->options->get( 'HTTPTimeout' ),
|
|
|
|
|
$this->options->get( 'HTTPMaxTimeout' )
|
|
|
|
|
);
|
|
|
|
|
$options['connTimeout'] = $this->normalizeTimeout(
|
|
|
|
|
$options['connTimeout'] ?? $options['connectTimeout'] ?? null,
|
|
|
|
|
$options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
|
|
|
|
|
$this->options->get( 'HTTPConnectTimeout' ),
|
|
|
|
|
$this->options->get( 'HTTPMaxConnectTimeout' )
|
|
|
|
|
);
|
|
|
|
|
$options += [
|
|
|
|
|
'maxReqTimeout' => $this->options->get( 'HTTPMaxTimeout' ),
|
|
|
|
|
'maxConnTimeout' => $this->options->get( 'HTTPMaxConnectTimeout' ),
|
|
|
|
|
'userAgent' => $this->getUserAgent(),
|
|
|
|
|
'logger' => $this->logger
|
|
|
|
|
];
|
|
|
|
|
return new MultiHttpClient( $options );
|
|
|
|
|
}
|
2018-01-16 23:11:08 +00:00
|
|
|
}
|