MediaWiki  1.33.0
GuzzleHttpRequest.php
Go to the documentation of this file.
1 <?php
21 use GuzzleHttp\Client;
22 use GuzzleHttp\Psr7\Request;
23 
40  const SUPPORTS_FILE_POSTS = true;
41 
42  protected $handler = null;
43  protected $sink = null;
44  protected $guzzleOptions = [ 'http_errors' => false ];
45 
53  public function __construct(
54  $url, array $options = [], $caller = __METHOD__, Profiler $profiler = null
55  ) {
56  parent::__construct( $url, $options, $caller, $profiler );
57 
58  if ( isset( $options['handler'] ) ) {
59  $this->handler = $options['handler'];
60  }
61  if ( isset( $options['sink'] ) ) {
62  $this->sink = $options['sink'];
63  }
64  }
65 
86  public function setCallback( $callback ) {
87  $this->sink = null;
88  $this->doSetCallback( $callback );
89  }
90 
101  protected function doSetCallback( $callback ) {
102  if ( !$this->sink ) {
103  parent::doSetCallback( $callback );
104  $this->sink = new MWCallbackStream( $this->callback );
105  }
106  }
107 
113  public function execute() {
114  $this->prepare();
115 
116  if ( !$this->status->isOK() ) {
117  return Status::wrap( $this->status ); // TODO B/C; move this to callers
118  }
119 
120  if ( $this->proxy ) {
121  $this->guzzleOptions['proxy'] = $this->proxy;
122  }
123 
124  $this->guzzleOptions['timeout'] = $this->timeout;
125  $this->guzzleOptions['connect_timeout'] = $this->connectTimeout;
126  $this->guzzleOptions['version'] = '1.1';
127 
128  if ( !$this->followRedirects ) {
129  $this->guzzleOptions['allow_redirects'] = false;
130  } else {
131  $this->guzzleOptions['allow_redirects'] = [
132  'max' => $this->maxRedirects
133  ];
134  }
135 
136  if ( $this->method == 'POST' ) {
138  if ( is_array( $postData ) ) {
139  $this->guzzleOptions['form_params'] = $postData;
140  } else {
141  $this->guzzleOptions['body'] = $postData;
142  }
143 
144  // Suppress 'Expect: 100-continue' header, as some servers
145  // will reject it with a 417 and Curl won't auto retry
146  // with HTTP 1.0 fallback
147  $this->guzzleOptions['expect'] = false;
148  }
149 
150  $this->guzzleOptions['headers'] = $this->reqHeaders;
151 
152  if ( $this->handler ) {
153  $this->guzzleOptions['handler'] = $this->handler;
154  }
155 
156  if ( $this->sink ) {
157  $this->guzzleOptions['sink'] = $this->sink;
158  }
159 
160  if ( $this->caInfo ) {
161  $this->guzzleOptions['verify'] = $this->caInfo;
162  } elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) {
163  $this->guzzleOptions['verify'] = false;
164  }
165 
166  try {
167  $client = new Client( $this->guzzleOptions );
168  $request = new Request( $this->method, $this->url );
169  $response = $client->send( $request );
170  $this->headerList = $response->getHeaders();
171 
172  $this->respVersion = $response->getProtocolVersion();
173  $this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
174  } catch ( GuzzleHttp\Exception\ConnectException $e ) {
175  // ConnectException is thrown for several reasons besides generic "timeout":
176  // Connection refused
177  // couldn't connect to host
178  // connection attempt failed
179  // Could not resolve IPv4 address for host
180  // Could not resolve IPv6 address for host
181  if ( $this->usingCurl() ) {
182  $handlerContext = $e->getHandlerContext();
183  if ( $handlerContext['errno'] == CURLE_OPERATION_TIMEOUTED ) {
184  $this->status->fatal( 'http-timed-out', $this->url );
185  } else {
186  $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
187  }
188  } else {
189  $this->status->fatal( 'http-request-error' );
190  }
191  } catch ( GuzzleHttp\Exception\RequestException $e ) {
192  if ( $this->usingCurl() ) {
193  $handlerContext = $e->getHandlerContext();
194  $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
195  } else {
196  // Non-ideal, but the only way to identify connection timeout vs other conditions
197  $needle = 'Connection timed out';
198  if ( strpos( $e->getMessage(), $needle ) !== false ) {
199  $this->status->fatal( 'http-timed-out', $this->url );
200  } else {
201  $this->status->fatal( 'http-request-error' );
202  }
203  }
204  } catch ( GuzzleHttp\Exception\GuzzleException $e ) {
205  $this->status->fatal( 'http-internal-error' );
206  }
207 
208  if ( $this->profiler ) {
209  $profileSection = $this->profiler->scopedProfileIn(
210  __METHOD__ . '-' . $this->profileName
211  );
212  }
213 
214  if ( $this->profiler ) {
215  $this->profiler->scopedProfileOut( $profileSection );
216  }
217 
218  $this->parseHeader();
219  $this->setStatus();
220 
221  return Status::wrap( $this->status ); // TODO B/C; move this to callers
222  }
223 
224  protected function prepare() {
225  $this->doSetCallback( $this->callback );
226  parent::prepare();
227  }
228 
232  protected function usingCurl() {
233  return ( $this->handler && is_a( $this->handler, 'GuzzleHttp\Handler\CurlHandler' ) ) ||
234  ( !$this->handler && extension_loaded( 'curl' ) );
235  }
236 
241  protected function parseHeader() {
242  // Failure without (valid) headers gets a response status of zero
243  if ( !$this->status->isOK() ) {
244  $this->respStatus = '0 Error';
245  }
246 
247  foreach ( $this->headerList as $name => $values ) {
248  $this->respHeaders[strtolower( $name )] = $values;
249  }
250 
251  $this->parseCookies();
252  }
253 }
prepare
static prepare( $text, $lang)
Backward-compatibility shim for extensions.
Definition: SyntaxHighlight.php:560
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MWHttpRequest\$callback
callable $callback
Definition: MWHttpRequest.php:53
MWHttpRequest\setStatus
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
Definition: MWHttpRequest.php:429
MWHttpRequest\$maxRedirects
$maxRedirects
Definition: MWHttpRequest.php:54
GuzzleHttpRequest\$sink
$sink
Definition: GuzzleHttpRequest.php:43
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MWHttpRequest\$profiler
Profiler $profiler
Definition: MWHttpRequest.php:74
GuzzleHttpRequest\parseHeader
parseHeader()
Guzzle provides headers as an array.
Definition: GuzzleHttpRequest.php:241
MWHttpRequest\$connectTimeout
$connectTimeout
Definition: MWHttpRequest.php:56
MWHttpRequest\$postData
$postData
Definition: MWHttpRequest.php:42
MWHttpRequest\parseCookies
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
Definition: MWHttpRequest.php:561
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
GuzzleHttpRequest
MWHttpRequest implemented using the Guzzle library.
Definition: GuzzleHttpRequest.php:39
GuzzleHttpRequest\prepare
prepare()
Definition: GuzzleHttpRequest.php:224
Profiler
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:33
MWHttpRequest\$timeout
int string $timeout
Definition: MWHttpRequest.php:38
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MWHttpRequest\$reqHeaders
$reqHeaders
Definition: MWHttpRequest.php:49
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
GuzzleHttpRequest\doSetCallback
doSetCallback( $callback)
Worker function for setting callbacks.
Definition: GuzzleHttpRequest.php:101
handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition: hooks.txt:780
GuzzleHttpRequest\$guzzleOptions
$guzzleOptions
Definition: GuzzleHttpRequest.php:44
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2636
GuzzleHttpRequest\$handler
$handler
Definition: GuzzleHttpRequest.php:42
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
GuzzleHttpRequest\execute
execute()
Definition: GuzzleHttpRequest.php:113
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:32
$response
this hook is for auditing only $response
Definition: hooks.txt:780
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
MWHttpRequest\$caInfo
$caInfo
Definition: MWHttpRequest.php:47
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MWCallbackStream
Callback-aware stream.
Definition: MWCallbackStream.php:33
GuzzleHttpRequest\__construct
__construct( $url, array $options=[], $caller=__METHOD__, Profiler $profiler=null)
Definition: GuzzleHttpRequest.php:53
GuzzleHttpRequest\SUPPORTS_FILE_POSTS
const SUPPORTS_FILE_POSTS
Definition: GuzzleHttpRequest.php:40
GuzzleHttpRequest\setCallback
setCallback( $callback)
Set a read callback to accept data read from the HTTP request.
Definition: GuzzleHttpRequest.php:86
MWHttpRequest\$proxy
$proxy
Definition: MWHttpRequest.php:43
GuzzleHttpRequest\usingCurl
usingCurl()
Definition: GuzzleHttpRequest.php:232
MWHttpRequest\$url
$url
Definition: MWHttpRequest.php:50