2013-12-08 23:19:00 +00:00
|
|
|
<?php
|
2014-01-29 22:44:54 +00:00
|
|
|
/**
|
|
|
|
|
* HTTP service client
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-04-28 01:32:44 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
|
2013-12-08 23:19:00 +00:00
|
|
|
/**
|
2018-07-12 22:07:54 +00:00
|
|
|
* Class to handle multiple HTTP requests
|
|
|
|
|
*
|
|
|
|
|
* If curl is available, requests will be made concurrently.
|
|
|
|
|
* Otherwise, they will be made serially.
|
2013-12-08 23:19:00 +00:00
|
|
|
*
|
2014-01-29 22:44:54 +00:00
|
|
|
* HTTP request maps are arrays that use the following format:
|
2013-12-08 23:19:00 +00:00
|
|
|
* - method : GET/HEAD/PUT/POST/DELETE
|
|
|
|
|
* - url : HTTP/HTTPS URL
|
2014-01-09 21:38:55 +00:00
|
|
|
* - query : <query parameter field/value associative array> (uses RFC 3986)
|
2013-12-08 23:19:00 +00:00
|
|
|
* - headers : <header name/value associative array>
|
|
|
|
|
* - body : source to get the HTTP request body from;
|
|
|
|
|
* this can simply be a string (always), a resource for
|
2014-01-09 21:38:55 +00:00
|
|
|
* PUT requests, and a field/value array for POST request;
|
|
|
|
|
* array bodies are encoded as multipart/form-data and strings
|
|
|
|
|
* use application/x-www-form-urlencoded (headers sent automatically)
|
2013-12-08 23:19:00 +00:00
|
|
|
* - stream : resource to stream the HTTP response body to
|
2014-12-24 00:18:40 +00:00
|
|
|
* - proxy : HTTP proxy to use
|
2014-04-20 08:40:06 +00:00
|
|
|
* - flags : map of boolean flags which supports:
|
|
|
|
|
* - relayResponseHeaders : write out header via header()
|
2014-01-29 22:44:54 +00:00
|
|
|
* Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.
|
2013-12-08 23:19:00 +00:00
|
|
|
*
|
2020-05-15 05:19:56 +00:00
|
|
|
* Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get
|
|
|
|
|
* a client object with appropriately configured timeouts.
|
|
|
|
|
*
|
2013-12-08 23:19:00 +00:00
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
2017-04-28 01:32:44 +00:00
|
|
|
class MultiHttpClient implements LoggerAwareInterface {
|
2021-10-28 19:09:31 +00:00
|
|
|
/** Regex for headers likely to contain tokens, etc. that we want to redact from logs */
|
|
|
|
|
private const SENSITIVE_HEADERS = '/(^|-|_)(authorization|auth|password|cookie)($|-|_)/';
|
2021-12-20 12:24:56 +00:00
|
|
|
/**
|
|
|
|
|
* @phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.ObjectTypeHintVar
|
|
|
|
|
* @var resource|object curl_multi_init() handle
|
|
|
|
|
*/
|
2019-09-05 15:30:36 +00:00
|
|
|
protected $cmh;
|
2019-03-06 08:52:34 +00:00
|
|
|
/** @var string|null SSL certificates path */
|
|
|
|
|
protected $caBundlePath;
|
|
|
|
|
/** @var float */
|
2014-01-17 21:32:46 +00:00
|
|
|
protected $connTimeout = 10;
|
2019-03-06 08:52:34 +00:00
|
|
|
/** @var float */
|
2020-05-15 05:19:56 +00:00
|
|
|
protected $maxConnTimeout = INF;
|
|
|
|
|
/** @var float */
|
2020-06-15 05:05:33 +00:00
|
|
|
protected $reqTimeout = 30;
|
2020-05-15 05:19:56 +00:00
|
|
|
/** @var float */
|
|
|
|
|
protected $maxReqTimeout = INF;
|
2019-03-06 08:52:34 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
protected $usePipelining = false;
|
|
|
|
|
/** @var int */
|
|
|
|
|
protected $maxConnsPerHost = 50;
|
2014-12-24 00:18:40 +00:00
|
|
|
/** @var string|null proxy */
|
|
|
|
|
protected $proxy;
|
2021-11-23 18:17:50 +00:00
|
|
|
/** @var string|bool */
|
|
|
|
|
protected $localProxy = false;
|
2021-10-27 23:27:28 +00:00
|
|
|
/** @var string[] */
|
|
|
|
|
protected $localVirtualHosts = [];
|
2015-10-06 22:26:59 +00:00
|
|
|
/** @var string */
|
|
|
|
|
protected $userAgent = 'wikimedia/multi-http-client v1.0';
|
2017-04-28 01:32:44 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
protected $logger;
|
2019-03-06 08:52:34 +00:00
|
|
|
|
|
|
|
|
// In PHP 7 due to https://bugs.php.net/bug.php?id=76480 the request/connect
|
|
|
|
|
// timeouts are periodically polled instead of being accurately respected.
|
|
|
|
|
// The select timeout is set to the minimum timeout multiplied by this factor.
|
2020-05-16 01:32:53 +00:00
|
|
|
private const TIMEOUT_ACCURACY_FACTOR = 0.1;
|
2018-06-15 10:31:02 +00:00
|
|
|
|
2013-12-08 23:19:00 +00:00
|
|
|
/**
|
2020-05-15 05:19:56 +00:00
|
|
|
* Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get
|
|
|
|
|
* a client object with appropriately configured timeouts instead of constructing
|
|
|
|
|
* a MultiHttpClient directly.
|
|
|
|
|
*
|
2013-12-08 23:19:00 +00:00
|
|
|
* @param array $options
|
2021-10-27 23:27:28 +00:00
|
|
|
* - connTimeout : default connection timeout (seconds)
|
|
|
|
|
* - reqTimeout : default request timeout (seconds)
|
|
|
|
|
* - maxConnTimeout : maximum connection timeout (seconds)
|
|
|
|
|
* - maxReqTimeout : maximum request timeout (seconds)
|
|
|
|
|
* - proxy : HTTP proxy to use
|
|
|
|
|
* - localProxy : Reverse proxy to use for domains in localVirtualHosts
|
|
|
|
|
* - localVirtualHosts : Domains that are configured as virtual hosts on the same machine
|
|
|
|
|
* - usePipelining : whether to use HTTP pipelining if possible (for all hosts)
|
|
|
|
|
* - maxConnsPerHost : maximum number of concurrent connections (per host)
|
|
|
|
|
* - userAgent : The User-Agent header value to send
|
|
|
|
|
* - logger : a \Psr\Log\LoggerInterface instance for debug logging
|
|
|
|
|
* - caBundlePath : path to specific Certificate Authority bundle (if any)
|
2014-12-24 13:49:20 +00:00
|
|
|
* @throws Exception
|
2013-12-08 23:19:00 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $options ) {
|
|
|
|
|
if ( isset( $options['caBundlePath'] ) ) {
|
|
|
|
|
$this->caBundlePath = $options['caBundlePath'];
|
|
|
|
|
if ( !file_exists( $this->caBundlePath ) ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
throw new Exception( "Cannot find CA bundle: " . $this->caBundlePath );
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
static $opts = [
|
2020-05-15 05:19:56 +00:00
|
|
|
'connTimeout', 'maxConnTimeout', 'reqTimeout', 'maxReqTimeout',
|
2021-10-27 23:27:28 +00:00
|
|
|
'usePipelining', 'maxConnsPerHost', 'proxy', 'userAgent', 'logger',
|
|
|
|
|
'localProxy', 'localVirtualHosts',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-01-17 21:32:46 +00:00
|
|
|
foreach ( $opts as $key ) {
|
|
|
|
|
if ( isset( $options[$key] ) ) {
|
|
|
|
|
$this->$key = $options[$key];
|
|
|
|
|
}
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
2017-04-28 01:32:44 +00:00
|
|
|
if ( $this->logger === null ) {
|
|
|
|
|
$this->logger = new NullLogger;
|
|
|
|
|
}
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute an HTTP(S) request
|
|
|
|
|
*
|
|
|
|
|
* This method returns a response map of:
|
2018-07-12 22:07:54 +00:00
|
|
|
* - code : HTTP response code or 0 if there was a serious error
|
|
|
|
|
* - reason : HTTP response reason (empty if there was a serious error)
|
2015-06-20 20:42:40 +00:00
|
|
|
* - headers : <header name/value associative array>
|
2019-03-06 08:52:34 +00:00
|
|
|
* - body : HTTP response body or resource (if "stream" was set)
|
|
|
|
|
* - error : Any error string
|
2015-06-20 20:42:40 +00:00
|
|
|
* The map also stores integer-indexed copies of these values. This lets callers do:
|
2015-04-29 04:49:40 +00:00
|
|
|
* @code
|
2019-03-06 08:52:34 +00:00
|
|
|
* list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $http->run( $req );
|
2015-04-29 04:49:40 +00:00
|
|
|
* @endcode
|
2013-12-08 23:19:00 +00:00
|
|
|
* @param array $req HTTP request array
|
2014-01-17 21:32:46 +00:00
|
|
|
* @param array $opts
|
2019-09-05 15:30:36 +00:00
|
|
|
* - connTimeout : connection timeout per request (seconds)
|
|
|
|
|
* - reqTimeout : post-connection timeout per request (seconds)
|
|
|
|
|
* - usePipelining : whether to use HTTP pipelining if possible (for all hosts)
|
|
|
|
|
* - maxConnsPerHost : maximum number of concurrent connections (per host)
|
2021-10-29 10:41:04 +00:00
|
|
|
* - httpVersion : One of 'v1.0', 'v1.1', 'v2' or 'v2.0'. Leave empty to use
|
|
|
|
|
* PHP/curl's default
|
2013-12-08 23:19:00 +00:00
|
|
|
* @return array Response array for request
|
|
|
|
|
*/
|
2016-06-09 19:38:05 +00:00
|
|
|
public function run( array $req, array $opts = [] ) {
|
2016-02-17 19:54:59 +00:00
|
|
|
return $this->runMulti( [ $req ], $opts )[0]['response'];
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-07-12 22:07:54 +00:00
|
|
|
* Execute a set of HTTP(S) requests.
|
|
|
|
|
*
|
|
|
|
|
* If curl is available, requests will be made concurrently.
|
|
|
|
|
* Otherwise, they will be made serially.
|
2013-12-08 23:19:00 +00:00
|
|
|
*
|
|
|
|
|
* The maps are returned by this method with the 'response' field set to a map of:
|
2018-07-12 22:07:54 +00:00
|
|
|
* - code : HTTP response code or 0 if there was a serious error
|
|
|
|
|
* - reason : HTTP response reason (empty if there was a serious error)
|
2014-12-24 13:49:20 +00:00
|
|
|
* - headers : <header name/value associative array>
|
2019-03-06 08:52:34 +00:00
|
|
|
* - body : HTTP response body or resource (if "stream" was set)
|
2018-07-12 22:07:54 +00:00
|
|
|
* - error : Any error string
|
2014-12-24 13:49:20 +00:00
|
|
|
* The map also stores integer-indexed copies of these values. This lets callers do:
|
2015-04-29 04:49:40 +00:00
|
|
|
* @code
|
2014-12-24 13:49:20 +00:00
|
|
|
* list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $req['response'];
|
2015-04-29 04:49:40 +00:00
|
|
|
* @endcode
|
2013-12-08 23:19:00 +00:00
|
|
|
* All headers in the 'headers' field are normalized to use lower case names.
|
2014-01-29 22:44:54 +00:00
|
|
|
* This is true for the request headers and the response headers. Integer-indexed
|
|
|
|
|
* method/URL entries will also be changed to use the corresponding string keys.
|
2013-12-08 23:19:00 +00:00
|
|
|
*
|
2019-08-30 13:09:51 +00:00
|
|
|
* @param array[] $reqs Map of HTTP request arrays
|
2019-09-05 15:30:36 +00:00
|
|
|
* @param array $opts Options
|
2015-04-21 04:22:55 +00:00
|
|
|
* - connTimeout : connection timeout per request (seconds)
|
|
|
|
|
* - reqTimeout : post-connection timeout per request (seconds)
|
2019-09-05 15:30:36 +00:00
|
|
|
* - usePipelining : whether to use HTTP pipelining if possible (for all hosts)
|
2014-01-17 21:32:46 +00:00
|
|
|
* - maxConnsPerHost : maximum number of concurrent connections (per host)
|
2021-10-29 10:41:04 +00:00
|
|
|
* - httpVersion : One of 'v1.0', 'v1.1', 'v2' or 'v2.0'. Leave empty to use
|
|
|
|
|
* PHP/curl's default
|
2019-10-12 12:49:17 +00:00
|
|
|
* @return array[] $reqs With response array populated for each
|
2014-12-24 13:49:20 +00:00
|
|
|
* @throws Exception
|
2013-12-08 23:19:00 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function runMulti( array $reqs, array $opts = [] ) {
|
2018-07-12 22:07:54 +00:00
|
|
|
$this->normalizeRequests( $reqs );
|
2019-09-05 14:29:31 +00:00
|
|
|
$opts += [ 'connTimeout' => $this->connTimeout, 'reqTimeout' => $this->reqTimeout ];
|
|
|
|
|
|
2022-01-28 17:33:43 +00:00
|
|
|
if ( $this->maxConnTimeout && $opts['connTimeout'] > $this->maxConnTimeout ) {
|
2020-05-15 05:19:56 +00:00
|
|
|
$opts['connTimeout'] = $this->maxConnTimeout;
|
|
|
|
|
}
|
2022-01-28 17:33:43 +00:00
|
|
|
if ( $this->maxReqTimeout && $opts['reqTimeout'] > $this->maxReqTimeout ) {
|
2020-05-15 05:19:56 +00:00
|
|
|
$opts['reqTimeout'] = $this->maxReqTimeout;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( $this->isCurlEnabled() ) {
|
2021-10-29 10:41:04 +00:00
|
|
|
switch ( $opts['httpVersion'] ?? null ) {
|
|
|
|
|
case 'v1.0':
|
|
|
|
|
$opts['httpVersion'] = CURL_HTTP_VERSION_1_0;
|
|
|
|
|
break;
|
|
|
|
|
case 'v1.1':
|
|
|
|
|
$opts['httpVersion'] = CURL_HTTP_VERSION_1_1;
|
|
|
|
|
break;
|
|
|
|
|
case 'v2':
|
|
|
|
|
case 'v2.0':
|
|
|
|
|
$opts['httpVersion'] = CURL_HTTP_VERSION_2_0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$opts['httpVersion'] = CURL_HTTP_VERSION_NONE;
|
|
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
return $this->runMultiCurl( $reqs, $opts );
|
|
|
|
|
} else {
|
2021-10-29 10:41:04 +00:00
|
|
|
# TODO: Add handling for httpVersion option
|
2019-03-06 08:52:34 +00:00
|
|
|
return $this->runMultiHttp( $reqs, $opts );
|
|
|
|
|
}
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if the curl extension is available
|
|
|
|
|
*
|
|
|
|
|
* @return bool true if curl is available, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
protected function isCurlEnabled() {
|
2020-04-15 19:34:51 +00:00
|
|
|
// Explicitly test if curl_multi* is blocked, as some users' hosts provide
|
|
|
|
|
// them with a modified curl with the multi-threaded parts removed(!)
|
|
|
|
|
return extension_loaded( 'curl' ) && function_exists( 'curl_multi_init' );
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a set of HTTP(S) requests concurrently
|
|
|
|
|
*
|
|
|
|
|
* @see MultiHttpClient::runMulti()
|
|
|
|
|
*
|
2019-08-30 13:09:51 +00:00
|
|
|
* @param array[] $reqs Map of HTTP request arrays
|
2018-07-12 22:07:54 +00:00
|
|
|
* @param array $opts
|
2019-03-06 08:52:34 +00:00
|
|
|
* - connTimeout : connection timeout per request (seconds)
|
|
|
|
|
* - reqTimeout : post-connection timeout per request (seconds)
|
|
|
|
|
* - usePipelining : whether to use HTTP pipelining if possible
|
|
|
|
|
* - maxConnsPerHost : maximum number of concurrent connections (per host)
|
2021-10-29 10:41:04 +00:00
|
|
|
* - httpVersion: : HTTP version to use
|
2019-08-30 18:17:32 +00:00
|
|
|
* @phan-param array{connTimeout?:int,reqTimeout?:int,usePipelining?:bool,maxConnsPerHost?:int} $opts
|
2018-07-12 22:07:54 +00:00
|
|
|
* @return array $reqs With response array populated for each
|
|
|
|
|
* @throws Exception
|
2019-08-30 18:17:32 +00:00
|
|
|
* @suppress PhanTypeInvalidDimOffset
|
2018-07-12 22:07:54 +00:00
|
|
|
*/
|
2019-09-05 14:29:31 +00:00
|
|
|
private function runMultiCurl( array $reqs, array $opts ) {
|
2019-09-05 15:30:36 +00:00
|
|
|
$chm = $this->getCurlMulti( $opts );
|
2019-03-06 08:52:34 +00:00
|
|
|
|
|
|
|
|
$selectTimeout = $this->getSelectTimeout( $opts );
|
|
|
|
|
|
|
|
|
|
// Add all of the required cURL handles...
|
|
|
|
|
$handles = [];
|
|
|
|
|
foreach ( $reqs as $index => &$req ) {
|
|
|
|
|
$handles[$index] = $this->getCurlHandle( $req, $opts );
|
2019-09-05 15:30:36 +00:00
|
|
|
curl_multi_add_handle( $chm, $handles[$index] );
|
2014-01-17 21:32:46 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
unset( $req ); // don't assign over this by accident
|
2014-01-17 21:32:46 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$infos = [];
|
2019-09-05 15:30:36 +00:00
|
|
|
// Execute the cURL handles concurrently...
|
|
|
|
|
$active = null; // handles still being processed
|
|
|
|
|
do {
|
|
|
|
|
// Do any available work...
|
2019-03-06 08:52:34 +00:00
|
|
|
do {
|
2019-09-05 15:30:36 +00:00
|
|
|
$mrc = curl_multi_exec( $chm, $active );
|
|
|
|
|
$info = curl_multi_info_read( $chm );
|
|
|
|
|
if ( $info !== false ) {
|
2022-01-26 23:21:30 +00:00
|
|
|
// Note: cast to integer even works on PHP 8.0+ despite the
|
|
|
|
|
// handle being an object not a resource, because CurlHandle
|
|
|
|
|
// has a backwards-compatible cast_object handler.
|
2019-09-05 15:30:36 +00:00
|
|
|
$infos[(int)$info['handle']] = $info;
|
2019-03-06 08:52:34 +00:00
|
|
|
}
|
2019-09-05 15:30:36 +00:00
|
|
|
} while ( $mrc == CURLM_CALL_MULTI_PERFORM );
|
|
|
|
|
// Wait (if possible) for available work...
|
|
|
|
|
if ( $active > 0 && $mrc == CURLM_OK && curl_multi_select( $chm, $selectTimeout ) == -1 ) {
|
|
|
|
|
// PHP bug 63411; https://curl.haxx.se/libcurl/c/curl_multi_fdset.html
|
|
|
|
|
usleep( 5000 ); // 5ms
|
|
|
|
|
}
|
|
|
|
|
} while ( $active > 0 && $mrc == CURLM_OK );
|
2015-05-13 09:38:21 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
// Remove all of the added cURL handles and check for errors...
|
|
|
|
|
foreach ( $reqs as $index => &$req ) {
|
|
|
|
|
$ch = $handles[$index];
|
|
|
|
|
curl_multi_remove_handle( $chm, $ch );
|
|
|
|
|
|
|
|
|
|
if ( isset( $infos[(int)$ch] ) ) {
|
|
|
|
|
$info = $infos[(int)$ch];
|
|
|
|
|
$errno = $info['result'];
|
|
|
|
|
if ( $errno !== 0 ) {
|
|
|
|
|
$req['response']['error'] = "(curl error: $errno)";
|
|
|
|
|
if ( function_exists( 'curl_strerror' ) ) {
|
|
|
|
|
$req['response']['error'] .= " " . curl_strerror( $errno );
|
|
|
|
|
}
|
|
|
|
|
$this->logger->warning( "Error fetching URL \"{$req['url']}\": " .
|
|
|
|
|
$req['response']['error'] );
|
2021-03-10 02:20:16 +00:00
|
|
|
} else {
|
|
|
|
|
$this->logger->debug(
|
|
|
|
|
"HTTP complete: {method} {url} code={response_code} size={size} " .
|
|
|
|
|
"total={total_time} connect={connect_time}",
|
|
|
|
|
[
|
|
|
|
|
'method' => $req['method'],
|
|
|
|
|
'url' => $req['url'],
|
|
|
|
|
'response_code' => $req['response']['code'],
|
|
|
|
|
'size' => curl_getinfo( $ch, CURLINFO_SIZE_DOWNLOAD ),
|
|
|
|
|
'total_time' => $this->getCurlTime(
|
|
|
|
|
$ch, CURLINFO_TOTAL_TIME, 'CURLINFO_TOTAL_TIME_T'
|
|
|
|
|
),
|
|
|
|
|
'connect_time' => $this->getCurlTime(
|
|
|
|
|
$ch, CURLINFO_CONNECT_TIME, 'CURLINFO_CONNECT_TIME_T'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
);
|
2019-03-06 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$req['response']['error'] = "(curl error: no status set)";
|
|
|
|
|
}
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
// For convenience with the list() operator
|
|
|
|
|
$req['response'][0] = $req['response']['code'];
|
|
|
|
|
$req['response'][1] = $req['response']['reason'];
|
|
|
|
|
$req['response'][2] = $req['response']['headers'];
|
|
|
|
|
$req['response'][3] = $req['response']['body'];
|
|
|
|
|
$req['response'][4] = $req['response']['error'];
|
|
|
|
|
curl_close( $ch );
|
|
|
|
|
// Close any string wrapper file handles
|
|
|
|
|
if ( isset( $req['_closeHandle'] ) ) {
|
|
|
|
|
fclose( $req['_closeHandle'] );
|
|
|
|
|
unset( $req['_closeHandle'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset( $req ); // don't assign over this by accident
|
2014-01-17 21:32:46 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
return $reqs;
|
|
|
|
|
}
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
/**
|
|
|
|
|
* @param array &$req HTTP request map
|
2021-04-04 19:18:22 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
2019-09-04 08:17:08 +00:00
|
|
|
* @phan-param array{url:string,proxy?:?string,query:mixed,method:string,body:string|resource,headers:string[],stream?:resource,flags:array} $req
|
2019-03-06 08:52:34 +00:00
|
|
|
* @param array $opts
|
2019-09-05 14:29:31 +00:00
|
|
|
* - connTimeout : default connection timeout
|
|
|
|
|
* - reqTimeout : default request timeout
|
2021-10-29 10:41:04 +00:00
|
|
|
* - httpVersion: default HTTP version
|
2021-12-20 12:24:56 +00:00
|
|
|
* @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
|
|
|
|
|
* @return resource|object
|
2019-03-06 08:52:34 +00:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2019-09-05 14:29:31 +00:00
|
|
|
protected function getCurlHandle( array &$req, array $opts ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
|
|
curl_setopt( $ch, CURLOPT_PROXY, $req['proxy'] ?? $this->proxy );
|
2019-09-05 14:29:31 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT_MS, intval( $opts['connTimeout'] * 1e3 ) );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_TIMEOUT_MS, intval( $opts['reqTimeout'] * 1e3 ) );
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_HEADER, 0 );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $this->caBundlePath !== null ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
|
|
|
|
|
}
|
|
|
|
|
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$url = $req['url'];
|
|
|
|
|
$query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
|
|
|
|
|
if ( $query != '' ) {
|
|
|
|
|
$url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
|
|
|
|
|
}
|
|
|
|
|
curl_setopt( $ch, CURLOPT_URL, $url );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req['method'] );
|
2019-09-05 14:29:31 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_NOBODY, ( $req['method'] === 'HEAD' ) );
|
2021-10-29 10:41:04 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_HTTP_VERSION, $opts['httpVersion'] ?? CURL_HTTP_VERSION_NONE );
|
2018-08-21 18:54:43 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( $req['method'] === 'PUT' ) {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_PUT, 1 );
|
2022-01-25 01:53:25 +00:00
|
|
|
// phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( is_resource( $req['body'] ) ) {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_INFILE, $req['body'] );
|
|
|
|
|
if ( isset( $req['headers']['content-length'] ) ) {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_INFILESIZE, $req['headers']['content-length'] );
|
|
|
|
|
} elseif ( isset( $req['headers']['transfer-encoding'] ) &&
|
|
|
|
|
$req['headers']['transfer-encoding'] === 'chunks'
|
|
|
|
|
) {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_UPLOAD, true );
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception( "Missing 'Content-Length' or 'Transfer-Encoding' header." );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $req['body'] !== '' ) {
|
|
|
|
|
$fp = fopen( "php://temp", "wb+" );
|
|
|
|
|
fwrite( $fp, $req['body'], strlen( $req['body'] ) );
|
|
|
|
|
rewind( $fp );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_INFILE, $fp );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req['body'] ) );
|
|
|
|
|
$req['_closeHandle'] = $fp; // remember to close this later
|
|
|
|
|
} else {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_READFUNCTION,
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( $ch, $fd, $length ) {
|
2019-09-01 16:57:51 +00:00
|
|
|
return (string)fread( $fd, $length );
|
2019-03-06 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} elseif ( $req['method'] === 'POST' ) {
|
|
|
|
|
curl_setopt( $ch, CURLOPT_POST, 1 );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_POSTFIELDS, $req['body'] );
|
|
|
|
|
} else {
|
2022-01-25 01:53:25 +00:00
|
|
|
// phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.is_resource
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( is_resource( $req['body'] ) || $req['body'] !== '' ) {
|
|
|
|
|
throw new Exception( "HTTP body specified for a non PUT/POST request." );
|
2018-08-21 18:54:43 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
$req['headers']['content-length'] = 0;
|
2015-10-06 01:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( !isset( $req['headers']['user-agent'] ) ) {
|
|
|
|
|
$req['headers']['user-agent'] = $this->userAgent;
|
|
|
|
|
}
|
2018-08-21 18:54:43 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$headers = [];
|
|
|
|
|
foreach ( $req['headers'] as $name => $value ) {
|
|
|
|
|
if ( strpos( $name, ': ' ) ) {
|
|
|
|
|
throw new Exception( "Headers cannot have ':' in the name." );
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
$headers[] = $name . ': ' . trim( $value );
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( $ch, $header ) use ( &$req ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( !empty( $req['flags']['relayResponseHeaders'] ) && trim( $header ) !== '' ) {
|
|
|
|
|
header( $header );
|
|
|
|
|
}
|
|
|
|
|
$length = strlen( $header );
|
|
|
|
|
$matches = [];
|
2019-12-01 17:59:17 +00:00
|
|
|
if ( preg_match( "/^(HTTP\/(?:1\.[01]|2)) (\d{3}) (.*)/", $header, $matches ) ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
$req['response']['code'] = (int)$matches[2];
|
|
|
|
|
$req['response']['reason'] = trim( $matches[3] );
|
2020-01-17 01:13:01 +00:00
|
|
|
// After a redirect we will receive this again, but we already stored headers
|
|
|
|
|
// that belonged to a redirect response. Start over.
|
|
|
|
|
$req['response']['headers'] = [];
|
2019-03-06 08:52:34 +00:00
|
|
|
return $length;
|
|
|
|
|
}
|
|
|
|
|
if ( strpos( $header, ":" ) === false ) {
|
|
|
|
|
return $length;
|
|
|
|
|
}
|
|
|
|
|
list( $name, $value ) = explode( ":", $header, 2 );
|
|
|
|
|
$name = strtolower( $name );
|
|
|
|
|
$value = trim( $value );
|
|
|
|
|
if ( isset( $req['response']['headers'][$name] ) ) {
|
|
|
|
|
$req['response']['headers'][$name] .= ', ' . $value;
|
|
|
|
|
} else {
|
|
|
|
|
$req['response']['headers'][$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
return $length;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2019-09-05 14:29:31 +00:00
|
|
|
// This works with both file and php://temp handles (unlike CURLOPT_FILE)
|
|
|
|
|
$hasOutputStream = isset( $req['stream'] );
|
|
|
|
|
curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( $ch, $data ) use ( &$req, $hasOutputStream ) {
|
2019-09-05 14:29:31 +00:00
|
|
|
if ( $hasOutputStream ) {
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2019-03-06 08:52:34 +00:00
|
|
|
return fwrite( $req['stream'], $data );
|
2019-09-05 14:29:31 +00:00
|
|
|
} else {
|
2019-09-04 08:17:08 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
|
2019-03-06 08:52:34 +00:00
|
|
|
$req['response']['body'] .= $data;
|
2019-09-05 14:29:31 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
return strlen( $data );
|
|
|
|
|
}
|
2019-09-05 14:29:31 +00:00
|
|
|
}
|
|
|
|
|
);
|
2013-12-08 23:19:00 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
return $ch;
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-12 22:07:54 +00:00
|
|
|
/**
|
2019-09-05 15:30:36 +00:00
|
|
|
* @param array $opts
|
2021-12-20 12:24:56 +00:00
|
|
|
* @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
|
|
|
|
|
* @return resource|object
|
2019-03-06 08:52:34 +00:00
|
|
|
* @throws Exception
|
2018-07-12 22:07:54 +00:00
|
|
|
*/
|
2019-09-05 15:30:36 +00:00
|
|
|
protected function getCurlMulti( array $opts ) {
|
|
|
|
|
if ( !$this->cmh ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
$cmh = curl_multi_init();
|
2019-09-05 15:30:36 +00:00
|
|
|
// Limit the size of the idle connection cache such that consecutive parallel
|
|
|
|
|
// request batches to the same host can avoid having to keep making connections
|
2019-03-06 08:52:34 +00:00
|
|
|
curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (int)$this->maxConnsPerHost );
|
2019-09-05 15:30:36 +00:00
|
|
|
$this->cmh = $cmh;
|
2019-03-06 08:52:34 +00:00
|
|
|
}
|
2019-09-05 15:30:36 +00:00
|
|
|
|
2022-01-25 03:29:37 +00:00
|
|
|
$curlVersion = curl_version()['version'];
|
|
|
|
|
|
2020-12-02 01:05:54 +00:00
|
|
|
// CURLMOPT_MAX_HOST_CONNECTIONS is available since PHP 7.0.7 and cURL 7.30.0
|
2022-01-25 03:29:37 +00:00
|
|
|
if ( version_compare( $curlVersion, '7.30.0', '>=' ) ) {
|
2020-12-02 01:05:54 +00:00
|
|
|
// Limit the number of in-flight requests for any given host
|
|
|
|
|
$maxHostConns = $opts['maxConnsPerHost'] ?? $this->maxConnsPerHost;
|
|
|
|
|
curl_multi_setopt( $this->cmh, CURLMOPT_MAX_HOST_CONNECTIONS, (int)$maxHostConns );
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 03:29:37 +00:00
|
|
|
if ( $opts['usePipelining'] ?? $this->usePipelining ) {
|
|
|
|
|
if ( version_compare( $curlVersion, '7.43', '<' ) ) {
|
|
|
|
|
// The option is a boolean
|
|
|
|
|
$pipelining = 1;
|
|
|
|
|
} elseif ( version_compare( $curlVersion, '7.62', '<' ) ) {
|
|
|
|
|
// The option is a bitfield and HTTP/1.x pipelining is supported
|
|
|
|
|
$pipelining = CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX;
|
|
|
|
|
} else {
|
|
|
|
|
// The option is a bitfield but HTTP/1.x pipelining has been removed
|
|
|
|
|
$pipelining = CURLPIPE_MULTIPLEX;
|
|
|
|
|
}
|
|
|
|
|
// Suppress deprecation, we know already (T264735)
|
|
|
|
|
// phpcs:ignore Generic.PHP.NoSilencedErrors
|
|
|
|
|
@curl_multi_setopt( $this->cmh, CURLMOPT_PIPELINING, $pipelining );
|
|
|
|
|
}
|
2019-09-05 15:30:36 +00:00
|
|
|
|
|
|
|
|
return $this->cmh;
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 02:20:16 +00:00
|
|
|
/**
|
|
|
|
|
* Get a time in seconds, formatted with microsecond resolution, or fall back to second
|
|
|
|
|
* resolution on PHP 7.2
|
|
|
|
|
*
|
2021-12-20 12:24:56 +00:00
|
|
|
* @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
|
|
|
|
|
* @param resource|object $ch
|
2021-03-10 02:20:16 +00:00
|
|
|
* @param int $oldOption
|
|
|
|
|
* @param string $newConstName
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getCurlTime( $ch, $oldOption, $newConstName ): string {
|
2021-06-23 00:47:59 +00:00
|
|
|
if ( defined( $newConstName ) ) {
|
2022-03-22 04:16:55 +00:00
|
|
|
return sprintf( "%.6F", curl_getinfo( $ch, constant( $newConstName ) ) / 1e6 );
|
2021-03-10 02:20:16 +00:00
|
|
|
} else {
|
|
|
|
|
return (string)curl_getinfo( $ch, $oldOption );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 22:07:54 +00:00
|
|
|
/**
|
2019-03-06 08:52:34 +00:00
|
|
|
* Execute a set of HTTP(S) requests sequentially.
|
2018-07-12 22:07:54 +00:00
|
|
|
*
|
2019-03-06 08:52:34 +00:00
|
|
|
* @see MultiHttpClient::runMulti()
|
2021-10-25 01:55:16 +00:00
|
|
|
* @todo Remove dependency on MediaWikiServices: rewrite using Guzzle T202352
|
2019-03-06 08:52:34 +00:00
|
|
|
* @param array $reqs Map of HTTP request arrays
|
2021-04-04 19:18:22 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
2019-10-11 14:06:45 +00:00
|
|
|
* @phan-param array<int,array{url:string,query:array,method:string,body:string,proxy?:?string,headers?:string[]}> $reqs
|
2019-03-06 08:52:34 +00:00
|
|
|
* @param array $opts
|
|
|
|
|
* - connTimeout : connection timeout per request (seconds)
|
|
|
|
|
* - reqTimeout : post-connection timeout per request (seconds)
|
2019-10-11 14:06:45 +00:00
|
|
|
* @phan-param array{connTimeout:int,reqTimeout:int} $opts
|
2019-03-06 08:52:34 +00:00
|
|
|
* @return array $reqs With response array populated for each
|
|
|
|
|
* @throws Exception
|
2018-07-12 22:07:54 +00:00
|
|
|
*/
|
2019-03-06 08:52:34 +00:00
|
|
|
private function runMultiHttp( array $reqs, array $opts = [] ) {
|
|
|
|
|
$httpOptions = [
|
|
|
|
|
'timeout' => $opts['reqTimeout'] ?? $this->reqTimeout,
|
|
|
|
|
'connectTimeout' => $opts['connTimeout'] ?? $this->connTimeout,
|
|
|
|
|
'logger' => $this->logger,
|
|
|
|
|
'caInfo' => $this->caBundlePath,
|
2018-07-12 22:07:54 +00:00
|
|
|
];
|
2019-03-06 08:52:34 +00:00
|
|
|
foreach ( $reqs as &$req ) {
|
|
|
|
|
$reqOptions = $httpOptions + [
|
|
|
|
|
'method' => $req['method'],
|
|
|
|
|
'proxy' => $req['proxy'] ?? $this->proxy,
|
|
|
|
|
'userAgent' => $req['headers']['user-agent'] ?? $this->userAgent,
|
|
|
|
|
'postData' => $req['body'],
|
|
|
|
|
];
|
2018-07-12 22:07:54 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$url = $req['url'];
|
|
|
|
|
$query = http_build_query( $req['query'], '', '&', PHP_QUERY_RFC3986 );
|
|
|
|
|
if ( $query != '' ) {
|
|
|
|
|
$url .= strpos( $req['url'], '?' ) === false ? "?$query" : "&$query";
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$httpRequest = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
|
2020-06-07 12:01:30 +00:00
|
|
|
$url, $reqOptions, __METHOD__ );
|
2021-03-10 02:20:16 +00:00
|
|
|
$httpRequest->setLogger( $this->logger );
|
2019-03-06 08:52:34 +00:00
|
|
|
$sv = $httpRequest->execute()->getStatusValue();
|
2018-07-12 22:07:54 +00:00
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
$respHeaders = array_map(
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( $v ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
return implode( ', ', $v );
|
|
|
|
|
},
|
|
|
|
|
$httpRequest->getResponseHeaders() );
|
|
|
|
|
|
|
|
|
|
$req['response'] = [
|
|
|
|
|
'code' => $httpRequest->getStatus(),
|
|
|
|
|
'reason' => '',
|
|
|
|
|
'headers' => $respHeaders,
|
|
|
|
|
'body' => $httpRequest->getContent(),
|
|
|
|
|
'error' => '',
|
|
|
|
|
];
|
|
|
|
|
|
2019-08-31 20:59:45 +00:00
|
|
|
if ( !$sv->isOK() ) {
|
2019-03-06 08:52:34 +00:00
|
|
|
$svErrors = $sv->getErrors();
|
|
|
|
|
if ( isset( $svErrors[0] ) ) {
|
|
|
|
|
$req['response']['error'] = $svErrors[0]['message'];
|
|
|
|
|
|
|
|
|
|
// param values vary per failure type (ex. unknown host vs unknown page)
|
|
|
|
|
if ( isset( $svErrors[0]['params'][0] ) ) {
|
|
|
|
|
if ( is_numeric( $svErrors[0]['params'][0] ) ) {
|
|
|
|
|
if ( isset( $svErrors[0]['params'][1] ) ) {
|
2020-01-24 20:14:50 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeInvalidDimOffset
|
2019-03-06 08:52:34 +00:00
|
|
|
$req['response']['reason'] = $svErrors[0]['params'][1];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$req['response']['reason'] = $svErrors[0]['params'][0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$req['response'][0] = $req['response']['code'];
|
|
|
|
|
$req['response'][1] = $req['response']['reason'];
|
|
|
|
|
$req['response'][2] = $req['response']['headers'];
|
|
|
|
|
$req['response'][3] = $req['response']['body'];
|
|
|
|
|
$req['response'][4] = $req['response']['error'];
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
|
|
|
|
|
return $reqs;
|
2018-07-12 22:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize request information
|
|
|
|
|
*
|
2019-11-23 22:28:57 +00:00
|
|
|
* @param array[] &$reqs the requests to normalize
|
2018-07-12 22:07:54 +00:00
|
|
|
*/
|
|
|
|
|
private function normalizeRequests( array &$reqs ) {
|
|
|
|
|
foreach ( $reqs as &$req ) {
|
|
|
|
|
$req['response'] = [
|
|
|
|
|
'code' => 0,
|
|
|
|
|
'reason' => '',
|
|
|
|
|
'headers' => [],
|
|
|
|
|
'body' => '',
|
|
|
|
|
'error' => ''
|
|
|
|
|
];
|
|
|
|
|
if ( isset( $req[0] ) ) {
|
|
|
|
|
$req['method'] = $req[0]; // short-form
|
|
|
|
|
unset( $req[0] );
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $req[1] ) ) {
|
|
|
|
|
$req['url'] = $req[1]; // short-form
|
|
|
|
|
unset( $req[1] );
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $req['method'] ) ) {
|
|
|
|
|
throw new Exception( "Request has no 'method' field set." );
|
|
|
|
|
} elseif ( !isset( $req['url'] ) ) {
|
|
|
|
|
throw new Exception( "Request has no 'url' field set." );
|
|
|
|
|
}
|
2021-11-23 18:17:50 +00:00
|
|
|
if ( $this->localProxy !== false && $this->isLocalURL( $req['url'] ) ) {
|
2021-10-27 23:27:28 +00:00
|
|
|
$this->useReverseProxy( $req, $this->localProxy );
|
|
|
|
|
}
|
2018-07-12 22:07:54 +00:00
|
|
|
$req['query'] = $req['query'] ?? [];
|
|
|
|
|
$headers = []; // normalized headers
|
|
|
|
|
if ( isset( $req['headers'] ) ) {
|
|
|
|
|
foreach ( $req['headers'] as $name => $value ) {
|
|
|
|
|
$headers[strtolower( $name )] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$req['headers'] = $headers;
|
|
|
|
|
if ( !isset( $req['body'] ) ) {
|
|
|
|
|
$req['body'] = '';
|
|
|
|
|
$req['headers']['content-length'] = 0;
|
|
|
|
|
}
|
2021-10-28 19:09:31 +00:00
|
|
|
// Redact some headers we know to have tokens before logging them
|
|
|
|
|
$logHeaders = $req['headers'];
|
|
|
|
|
foreach ( $logHeaders as $header => $value ) {
|
|
|
|
|
if ( preg_match( self::SENSITIVE_HEADERS, $header ) === 1 ) {
|
|
|
|
|
$logHeaders[$header] = '[redacted]';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->logger->debug( "HTTP start: {method} {url}",
|
|
|
|
|
[
|
|
|
|
|
'method' => $req['method'],
|
|
|
|
|
'url' => $req['url'],
|
|
|
|
|
'headers' => $logHeaders,
|
|
|
|
|
]
|
|
|
|
|
);
|
2018-07-12 22:07:54 +00:00
|
|
|
$req['flags'] = $req['flags'] ?? [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 23:27:28 +00:00
|
|
|
private function useReverseProxy( array &$req, $proxy ) {
|
|
|
|
|
$parsedProxy = wfParseUrl( $proxy );
|
|
|
|
|
if ( $parsedProxy === false ) {
|
|
|
|
|
throw new Exception( "Invalid reverseProxy configured: $proxy" );
|
|
|
|
|
}
|
|
|
|
|
$parsedUrl = wfParseUrl( $req['url'] );
|
|
|
|
|
if ( $parsedUrl === false ) {
|
|
|
|
|
throw new Exception( "Invalid url specified: ${req['url']}" );
|
|
|
|
|
}
|
|
|
|
|
// Set the current host in the Host header
|
|
|
|
|
$req['headers']['Host'] = $parsedUrl['host'];
|
|
|
|
|
// Replace scheme, host and port in the request
|
|
|
|
|
$parsedUrl['scheme'] = $parsedProxy['scheme'];
|
|
|
|
|
$parsedUrl['host'] = $parsedProxy['host'];
|
|
|
|
|
if ( isset( $parsedProxy['port'] ) ) {
|
|
|
|
|
$parsedUrl['port'] = $parsedProxy['port'];
|
|
|
|
|
} else {
|
|
|
|
|
unset( $parsedUrl['port'] );
|
|
|
|
|
}
|
|
|
|
|
$req['url'] = wfAssembleUrl( $parsedUrl );
|
|
|
|
|
// Explicitly disable use of another proxy by setting to false,
|
|
|
|
|
// since null will fallback to $this->proxy
|
|
|
|
|
$req['proxy'] = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the URL can be served by localhost
|
|
|
|
|
*
|
|
|
|
|
* @note this is mostly a copy of MWHttpRequest::isLocalURL()
|
|
|
|
|
* @param string $url Full url to check
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function isLocalURL( $url ) {
|
|
|
|
|
if ( !$this->localVirtualHosts ) {
|
|
|
|
|
// Shortcut
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract host part
|
|
|
|
|
$matches = [];
|
|
|
|
|
if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
|
|
|
|
|
$host = $matches[1];
|
|
|
|
|
// Split up dotwise
|
|
|
|
|
$domainParts = explode( '.', $host );
|
|
|
|
|
// Check if this domain or any superdomain is listed as a local virtual host
|
|
|
|
|
$domainParts = array_reverse( $domainParts );
|
|
|
|
|
|
|
|
|
|
$domain = '';
|
|
|
|
|
$countParts = count( $domainParts );
|
|
|
|
|
for ( $i = 0; $i < $countParts; $i++ ) {
|
|
|
|
|
$domainPart = $domainParts[$i];
|
|
|
|
|
if ( $i == 0 ) {
|
|
|
|
|
$domain = $domainPart;
|
|
|
|
|
} else {
|
|
|
|
|
$domain = $domainPart . '.' . $domain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( in_array( $domain, $this->localVirtualHosts ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 08:52:34 +00:00
|
|
|
/**
|
|
|
|
|
* Get a suitable select timeout for the given options.
|
|
|
|
|
*
|
|
|
|
|
* @param array $opts
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
private function getSelectTimeout( $opts ) {
|
|
|
|
|
$connTimeout = $opts['connTimeout'] ?? $this->connTimeout;
|
|
|
|
|
$reqTimeout = $opts['reqTimeout'] ?? $this->reqTimeout;
|
|
|
|
|
$timeouts = array_filter( [ $connTimeout, $reqTimeout ] );
|
|
|
|
|
if ( count( $timeouts ) === 0 ) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$selectTimeout = min( $timeouts ) * self::TIMEOUT_ACCURACY_FACTOR;
|
2021-11-19 23:19:42 +00:00
|
|
|
// Minimum 10us
|
2019-03-06 08:52:34 +00:00
|
|
|
if ( $selectTimeout < 10e-6 ) {
|
|
|
|
|
$selectTimeout = 10e-6;
|
|
|
|
|
}
|
|
|
|
|
return $selectTimeout;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 01:32:44 +00:00
|
|
|
/**
|
|
|
|
|
* Register a logger
|
|
|
|
|
*
|
2017-08-11 16:09:41 +00:00
|
|
|
* @param LoggerInterface $logger
|
2017-04-28 01:32:44 +00:00
|
|
|
*/
|
|
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
2019-03-06 08:52:34 +00:00
|
|
|
|
2019-12-05 17:52:55 +00:00
|
|
|
public function __destruct() {
|
2019-09-05 15:30:36 +00:00
|
|
|
if ( $this->cmh ) {
|
|
|
|
|
curl_multi_close( $this->cmh );
|
2019-03-06 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-12-08 23:19:00 +00:00
|
|
|
}
|