21 use GuzzleHttp\Client;
22 use GuzzleHttp\HandlerStack;
23 use GuzzleHttp\Middleware;
24 use GuzzleHttp\Psr7\Request;
25 use Psr\Http\Message\RequestInterface;
63 if ( isset( $options[
'handler'] ) ) {
64 $this->handler = $options[
'handler'];
66 if ( isset( $options[
'sink'] ) ) {
67 $this->sink = $options[
'sink'];
107 if ( !$this->sink ) {
121 if ( !$this->status->isOK() ) {
125 if ( $this->proxy ) {
131 $this->guzzleOptions[
'version'] =
'1.1';
133 if ( !$this->followRedirects ) {
134 $this->guzzleOptions[
'allow_redirects'] =
false;
136 $this->guzzleOptions[
'allow_redirects'] = [
141 if ( $this->method ==
'POST' ) {
144 $this->guzzleOptions[
'form_params'] =
$postData;
146 $this->guzzleOptions[
'body'] =
$postData;
148 if ( !isset( $this->reqHeaders[
'Content-Type'] ) ) {
149 $this->reqHeaders[
'Content-Type'] =
'application/x-www-form-urlencoded';
156 $this->guzzleOptions[
'expect'] =
false;
163 $stack = HandlerStack::create( $this->handler );
166 $stack->remove(
'cookies' );
169 $stack->push( Middleware::mapRequest(
170 static function ( RequestInterface $request ) use ( $mwCookieJar ) {
171 $uri = $request->getUri();
172 $cookieHeader = $mwCookieJar->serializeToHttpRequest(
173 $uri->getPath() ?:
'/',
176 if ( !$cookieHeader ) {
180 return $request->withHeader(
'Cookie', $cookieHeader );
184 $this->guzzleOptions[
'handler'] = $stack;
190 if ( $this->caInfo ) {
192 } elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) {
193 $this->guzzleOptions[
'verify'] =
false;
196 $client =
new Client( $this->guzzleOptions );
197 $request =
new Request( $this->method, $this->url );
198 foreach ( $this->reqHeaders as $name => $value ) {
199 $request = $request->withHeader( $name, $value );
203 $response = $client->send( $request );
204 $this->headerList = $response->getHeaders();
206 $this->respVersion = $response->getProtocolVersion();
207 $this->respStatus = $response->getStatusCode() .
' ' . $response->getReasonPhrase();
208 }
catch ( GuzzleHttp\Exception\ConnectException $e ) {
216 $handlerContext = $e->getHandlerContext();
217 if ( $handlerContext[
'errno'] == CURLE_OPERATION_TIMEOUTED ) {
218 $this->status->fatal(
'http-timed-out', $this->url );
220 $this->status->fatal(
'http-curl-error', $handlerContext[
'error'] );
223 $this->status->fatal(
'http-request-error' );
225 }
catch ( GuzzleHttp\Exception\RequestException $e ) {
227 $handlerContext = $e->getHandlerContext();
228 $this->status->fatal(
'http-curl-error', $handlerContext[
'error'] );
231 $needle =
'Connection timed out';
232 if ( strpos( $e->getMessage(), $needle ) !==
false ) {
233 $this->status->fatal(
'http-timed-out', $this->url );
235 $this->status->fatal(
'http-request-error' );
238 }
catch ( GuzzleHttp\Exception\GuzzleException $e ) {
239 $this->status->fatal(
'http-internal-error' );
242 if ( $this->profiler ) {
243 $profileSection = $this->profiler->scopedProfileIn(
244 __METHOD__ .
'-' . $this->profileName
248 if ( $this->profiler ) {
249 $this->profiler->scopedProfileOut( $profileSection );
267 return ( $this->handler && is_a( $this->handler,
'GuzzleHttp\Handler\CurlHandler' ) ) ||
268 ( !$this->handler && extension_loaded(
'curl' ) );
277 if ( !$this->status->isOK() ) {
278 $this->respStatus =
'0 Error';
281 foreach ( $this->headerList as $name => $values ) {
282 $this->respHeaders[strtolower( $name )] = $values;
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
MWHttpRequest implemented using the Guzzle library.
parseHeader()
Guzzle provides headers as an array.
setCallback( $callback)
Set a read callback to accept data read from the HTTP request.
doSetCallback( $callback)
Worker function for setting callbacks.
__construct( $url, array $options=[], $caller=__METHOD__, Profiler $profiler=null)
const SUPPORTS_FILE_POSTS
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
getCookieJar()
Returns the cookie jar in use.
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
Profiler base class that defines the interface and some shared functionality.
static wrap( $sv)
Succinct helper method to wrap a StatusValue.