1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?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
*/
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use MediaWiki\Status\Status;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\NullLogger;
/**
* MWHttpRequest implemented using the Guzzle library
*
* @note a new 'sink' option is available as an alternative to callbacks.
* See: http://docs.guzzlephp.org/en/stable/request-options.html#sink)
* The 'callback' option remains available as well. If both 'sink' and 'callback' are
* specified, 'sink' is used.
* @note Callers may set a custom handler via the 'handler' option.
* If this is not set, Guzzle will use curl (if available) or PHP streams (otherwise)
* @note Setting either sslVerifyHost or sslVerifyCert will enable both.
* Guzzle does not allow them to be set separately.
*
* @since 1.33
*/
class GuzzleHttpRequest extends MWHttpRequest {
public const SUPPORTS_FILE_POSTS = true;
/** @var callable|null */
protected $handler = null;
/** @var StreamInterface|null */
protected $sink = null;
/** @var array */
protected $guzzleOptions = [ 'http_errors' => false ];
/**
* @internal Use HttpRequestFactory
*
* @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 HttpRequestFactory::create())
* @param string $caller The method making this request, for profiling @phan-mandatory-param
* @param Profiler|null $profiler An instance of the profiler for profiling, or null
* @throws Exception
*/
public function __construct(
$url, array $options = [], $caller = __METHOD__, ?Profiler $profiler = null
) {
parent::__construct( $url, $options, $caller, $profiler );
if ( isset( $options['handler'] ) ) {
$this->handler = $options['handler'];
}
if ( isset( $options['sink'] ) ) {
$this->sink = $options['sink'];
}
}
/**
* Set a read callback to accept data read from the HTTP request.
* By default, data is appended to an internal buffer which can be
* retrieved through $req->getContent().
*
* To handle data as it comes in -- especially for large files that
* would not fit in memory -- you can instead set your own callback,
* in the form function($resource, $buffer) where the first parameter
* is the low-level resource being read (implementation specific),
* and the second parameter is the data buffer.
*
* You MUST return the number of bytes handled in the buffer; if fewer
* bytes are reported handled than were passed to you, the HTTP fetch
* will be aborted.
*
* This function overrides any 'sink' or 'callback' constructor option.
*
* @param callable|null $callback
*/
public function setCallback( $callback ) {
$this->sink = null;
$this->doSetCallback( $callback );
}
/**
* Worker function for setting callbacks. Calls can originate both internally and externally
* via setCallback). Defaults to the internal read callback if $callback is null.
*
* If a sink is already specified, this does nothing. This causes the 'sink' constructor
* option to override the 'callback' constructor option.
*
* @param callable|null $callback
*/
protected function doSetCallback( $callback ) {
if ( !$this->sink ) {
parent::doSetCallback( $callback );
$this->sink = new MWCallbackStream( $this->callback );
}
}
/**
* @see MWHttpRequest::execute
*
* @return Status
*/
public function execute() {
$this->prepare();
if ( !$this->status->isOK() ) {
return Status::wrap( $this->status ); // TODO B/C; move this to callers
}
if ( $this->proxy ) {
$this->guzzleOptions['proxy'] = $this->proxy;
}
$this->guzzleOptions['timeout'] = $this->timeout;
$this->guzzleOptions['connect_timeout'] = $this->connectTimeout;
$this->guzzleOptions['version'] = '1.1';
if ( !$this->followRedirects ) {
$this->guzzleOptions['allow_redirects'] = false;
} else {
$this->guzzleOptions['allow_redirects'] = [
'max' => $this->maxRedirects
];
}
if ( $this->method == 'POST' ) {
$postData = $this->postData;
if ( is_array( $postData ) ) {
$this->guzzleOptions['form_params'] = $postData;
} else {
$this->guzzleOptions['body'] = $postData;
// mimic CURLOPT_POST option
if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
$this->reqHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
}
}
// Suppress 'Expect: 100-continue' header, as some servers
// will reject it with a 417 and Curl won't auto retry
// with HTTP 1.0 fallback
$this->guzzleOptions['expect'] = false;
}
$stack = HandlerStack::create( $this->handler );
// Create Middleware to use cookies from $this->getCookieJar(),
// which is in MediaWiki CookieJar format, not in Guzzle-specific CookieJar format.
// Note: received cookies (from HTTP response) don't need to be handled here,
// they will be added back into the CookieJar by MWHttpRequest::parseCookies().
// @phan-suppress-next-line PhanUndeclaredFunctionInCallable
$stack->remove( 'cookies' );
$mwCookieJar = $this->getCookieJar();
$stack->push( Middleware::mapRequest(
static function ( RequestInterface $request ) use ( $mwCookieJar ) {
$uri = $request->getUri();
$cookieHeader = $mwCookieJar->serializeToHttpRequest(
$uri->getPath() ?: '/',
$uri->getHost()
);
if ( !$cookieHeader ) {
return $request;
}
return $request->withHeader( 'Cookie', $cookieHeader );
}
), 'cookies' );
if ( !$this->logger instanceof NullLogger ) {
$stack->push( Middleware::log( $this->logger, new MessageFormatter(
// TODO {error} will be 'NULL' on success which is unfortunate, but
// doesn't seem fixable without a custom formatter. Same for using
// PSR-3 variable replacement instead of raw strings.
'{method} {uri} HTTP/{version} - {code} {error}'
) ), 'logger' );
}
$this->guzzleOptions['handler'] = $stack;
if ( $this->sink ) {
$this->guzzleOptions['sink'] = $this->sink;
}
if ( $this->caInfo ) {
$this->guzzleOptions['verify'] = $this->caInfo;
} elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) {
$this->guzzleOptions['verify'] = false;
}
$client = new Client( $this->guzzleOptions );
$request = new Request( $this->method, $this->url );
foreach ( $this->reqHeaders as $name => $value ) {
$request = $request->withHeader( $name, $value );
}
try {
$response = $client->send( $request );
$this->headerList = $response->getHeaders();
$this->respVersion = $response->getProtocolVersion();
$this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
} catch ( GuzzleHttp\Exception\ConnectException $e ) {
// ConnectException is thrown for several reasons besides generic "timeout":
// Connection refused
// couldn't connect to host
// connection attempt failed
// Could not resolve IPv4 address for host
// Could not resolve IPv6 address for host
if ( $this->usingCurl() ) {
$handlerContext = $e->getHandlerContext();
if ( $handlerContext['errno'] == CURLE_OPERATION_TIMEOUTED ) {
$this->status->fatal( 'http-timed-out', $this->url );
} else {
$this->status->fatal( 'http-curl-error', $handlerContext['error'] );
}
} else {
$this->status->fatal( 'http-request-error' );
}
} catch ( GuzzleHttp\Exception\RequestException $e ) {
if ( $this->usingCurl() ) {
$handlerContext = $e->getHandlerContext();
$this->status->fatal( 'http-curl-error', $handlerContext['error'] );
} else {
// Non-ideal, but the only way to identify connection timeout vs other conditions
$needle = 'Connection timed out';
if ( strpos( $e->getMessage(), $needle ) !== false ) {
$this->status->fatal( 'http-timed-out', $this->url );
} else {
$this->status->fatal( 'http-request-error' );
}
}
} catch ( GuzzleHttp\Exception\GuzzleException $e ) {
$this->status->fatal( 'http-internal-error' );
}
if ( $this->profiler ) {
$profileSection = $this->profiler->scopedProfileIn(
__METHOD__ . '-' . $this->profileName
);
}
if ( $this->profiler ) {
$this->profiler->scopedProfileOut( $profileSection );
}
$this->parseHeader();
$this->setStatus();
return Status::wrap( $this->status ); // TODO B/C; move this to callers
}
protected function prepare() {
$this->doSetCallback( $this->callback );
parent::prepare();
}
protected function usingCurl(): bool {
return $this->handler instanceof CurlHandler ||
( !$this->handler && extension_loaded( 'curl' ) );
}
/**
* Guzzle provides headers as an array. Reprocess to match our expectations. Guzzle will
* have already parsed and removed the status line (in EasyHandle::createResponse).
*/
protected function parseHeader() {
// Failure without (valid) headers gets a response status of zero
if ( !$this->status->isOK() ) {
$this->respStatus = '0 Error';
}
foreach ( $this->headerList as $name => $values ) {
$this->respHeaders[strtolower( $name )] = $values;
}
$this->parseCookies();
}
}
|