| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use GuzzleHttp\Client; |
| 22 |
use GuzzleHttp\Handler\CurlHandler; |
| 23 |
use GuzzleHttp\HandlerStack; |
| 24 |
use GuzzleHttp\MessageFormatter; |
| 25 |
use GuzzleHttp\Middleware; |
| 26 |
use GuzzleHttp\Psr7\Request; |
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use Psr\Http\Message\RequestInterface; |
| 29 |
use Psr\Http\Message\StreamInterface; |
| 30 |
use Psr\Log\NullLogger; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class GuzzleHttpRequest extends MWHttpRequest { |
| 47 |
public const SUPPORTS_FILE_POSTS = true; |
| 48 |
|
| 49 |
|
| 50 |
protected $handler = null; |
| 51 |
|
| 52 |
protected $sink = null; |
| 53 |
|
| 54 |
protected $guzzleOptions = [ 'http_errors' => false ]; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function __construct( |
| 66 |
$url, array $options = [], $caller = __METHOD__, ?Profiler $profiler = null |
| 67 |
) { |
| 68 |
parent::__construct( $url, $options, $caller, $profiler ); |
| 69 |
|
| 70 |
if ( isset( $options['handler'] ) ) { |
| 71 |
$this->handler = $options['handler']; |
| 72 |
} |
| 73 |
if ( isset( $options['sink'] ) ) { |
| 74 |
$this->sink = $options['sink']; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function setCallback( $callback ) { |
| 98 |
$this->sink = null; |
| 99 |
$this->doSetCallback( $callback ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
protected function doSetCallback( $callback ) { |
| 112 |
if ( !$this->sink ) { |
| 113 |
parent::doSetCallback( $callback ); |
| 114 |
$this->sink = new MWCallbackStream( $this->callback ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function execute() { |
| 124 |
$this->prepare(); |
| 125 |
|
| 126 |
if ( !$this->status->isOK() ) { |
| 127 |
return Status::wrap( $this->status ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( $this->proxy ) { |
| 131 |
$this->guzzleOptions['proxy'] = $this->proxy; |
| 132 |
} |
| 133 |
|
| 134 |
$this->guzzleOptions['timeout'] = $this->timeout; |
| 135 |
$this->guzzleOptions['connect_timeout'] = $this->connectTimeout; |
| 136 |
$this->guzzleOptions['version'] = '1.1'; |
| 137 |
|
| 138 |
if ( !$this->followRedirects ) { |
| 139 |
$this->guzzleOptions['allow_redirects'] = false; |
| 140 |
} else { |
| 141 |
$this->guzzleOptions['allow_redirects'] = [ |
| 142 |
'max' => $this->maxRedirects |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
if ( $this->method == 'POST' ) { |
| 147 |
$postData = $this->postData; |
| 148 |
if ( is_array( $postData ) ) { |
| 149 |
$this->guzzleOptions['form_params'] = $postData; |
| 150 |
} else { |
| 151 |
$this->guzzleOptions['body'] = $postData; |
| 152 |
|
| 153 |
if ( !isset( $this->reqHeaders['Content-Type'] ) ) { |
| 154 |
$this->reqHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
$this->guzzleOptions['expect'] = false; |
| 162 |
} |
| 163 |
|
| 164 |
$stack = HandlerStack::create( $this->handler ); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
$stack->remove( 'cookies' ); |
| 172 |
$mwCookieJar = $this->getCookieJar(); |
| 173 |
$stack->push( Middleware::mapRequest( |
| 174 |
static function ( RequestInterface $request ) use ( $mwCookieJar ) { |
| 175 |
$uri = $request->getUri(); |
| 176 |
$cookieHeader = $mwCookieJar->serializeToHttpRequest( |
| 177 |
$uri->getPath() ?: '/', |
| 178 |
$uri->getHost() |
| 179 |
); |
| 180 |
if ( !$cookieHeader ) { |
| 181 |
return $request; |
| 182 |
} |
| 183 |
|
| 184 |
return $request->withHeader( 'Cookie', $cookieHeader ); |
| 185 |
} |
| 186 |
), 'cookies' ); |
| 187 |
|
| 188 |
if ( !$this->logger instanceof NullLogger ) { |
| 189 |
$stack->push( Middleware::log( $this->logger, new MessageFormatter( |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
'{method} {uri} HTTP/{version} - {code} {error}' |
| 194 |
) ), 'logger' ); |
| 195 |
} |
| 196 |
|
| 197 |
$this->guzzleOptions['handler'] = $stack; |
| 198 |
|
| 199 |
if ( $this->sink ) { |
| 200 |
$this->guzzleOptions['sink'] = $this->sink; |
| 201 |
} |
| 202 |
|
| 203 |
if ( $this->caInfo ) { |
| 204 |
$this->guzzleOptions['verify'] = $this->caInfo; |
| 205 |
} elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) { |
| 206 |
$this->guzzleOptions['verify'] = false; |
| 207 |
} |
| 208 |
|
| 209 |
$client = new Client( $this->guzzleOptions ); |
| 210 |
$request = new Request( $this->method, $this->url ); |
| 211 |
foreach ( $this->reqHeaders as $name => $value ) { |
| 212 |
$request = $request->withHeader( $name, $value ); |
| 213 |
} |
| 214 |
|
| 215 |
try { |
| 216 |
$response = $client->send( $request ); |
| 217 |
$this->headerList = $response->getHeaders(); |
| 218 |
|
| 219 |
$this->respVersion = $response->getProtocolVersion(); |
| 220 |
$this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase(); |
| 221 |
} catch ( GuzzleHttp\Exception\ConnectException $e ) { |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if ( $this->usingCurl() ) { |
| 229 |
$handlerContext = $e->getHandlerContext(); |
| 230 |
if ( $handlerContext['errno'] == CURLE_OPERATION_TIMEOUTED ) { |
| 231 |
$this->status->fatal( 'http-timed-out', $this->url ); |
| 232 |
} else { |
| 233 |
$this->status->fatal( 'http-curl-error', $handlerContext['error'] ); |
| 234 |
} |
| 235 |
} else { |
| 236 |
$this->status->fatal( 'http-request-error' ); |
| 237 |
} |
| 238 |
} catch ( GuzzleHttp\Exception\RequestException $e ) { |
| 239 |
if ( $this->usingCurl() ) { |
| 240 |
$handlerContext = $e->getHandlerContext(); |
| 241 |
$this->status->fatal( 'http-curl-error', $handlerContext['error'] ); |
| 242 |
} else { |
| 243 |
|
| 244 |
$needle = 'Connection timed out'; |
| 245 |
if ( strpos( $e->getMessage(), $needle ) !== false ) { |
| 246 |
$this->status->fatal( 'http-timed-out', $this->url ); |
| 247 |
} else { |
| 248 |
$this->status->fatal( 'http-request-error' ); |
| 249 |
} |
| 250 |
} |
| 251 |
} catch ( GuzzleHttp\Exception\GuzzleException $e ) { |
| 252 |
$this->status->fatal( 'http-internal-error' ); |
| 253 |
} |
| 254 |
|
| 255 |
if ( $this->profiler ) { |
| 256 |
$profileSection = $this->profiler->scopedProfileIn( |
| 257 |
__METHOD__ . '-' . $this->profileName |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
if ( $this->profiler ) { |
| 262 |
$this->profiler->scopedProfileOut( $profileSection ); |
| 263 |
} |
| 264 |
|
| 265 |
$this->parseHeader(); |
| 266 |
$this->setStatus(); |
| 267 |
|
| 268 |
return Status::wrap( $this->status ); |
| 269 |
} |
| 270 |
|
| 271 |
protected function prepare() { |
| 272 |
$this->doSetCallback( $this->callback ); |
| 273 |
parent::prepare(); |
| 274 |
} |
| 275 |
|
| 276 |
protected function usingCurl(): bool { |
| 277 |
return $this->handler instanceof CurlHandler || |
| 278 |
( !$this->handler && extension_loaded( 'curl' ) ); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
protected function parseHeader() { |
| 286 |
|
| 287 |
if ( !$this->status->isOK() ) { |
| 288 |
$this->respStatus = '0 Error'; |
| 289 |
} |
| 290 |
|
| 291 |
foreach ( $this->headerList as $name => $values ) { |
| 292 |
$this->respHeaders[strtolower( $name )] = $values; |
| 293 |
} |
| 294 |
|
| 295 |
$this->parseCookies(); |
| 296 |
} |
| 297 |
} |