MediaWiki REL1_33
GuzzleHttpRequest.php
Go to the documentation of this file.
1<?php
21use GuzzleHttp\Client;
22use 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' ) {
137 $postData = $this->postData;
138 if ( is_array( $postData ) ) {
139 $this->guzzleOptions['form_params'] = $postData;
140 } else {
141 $this->guzzleOptions['body'] = $postData;
142 // mimic CURLOPT_POST option
143 if ( !isset( $this->reqHeaders['Content-Type'] ) ) {
144 $this->reqHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
145 }
146 }
147
148 // Suppress 'Expect: 100-continue' header, as some servers
149 // will reject it with a 417 and Curl won't auto retry
150 // with HTTP 1.0 fallback
151 $this->guzzleOptions['expect'] = false;
152 }
153
154 $this->guzzleOptions['headers'] = $this->reqHeaders;
155
156 if ( $this->handler ) {
157 $this->guzzleOptions['handler'] = $this->handler;
158 }
159
160 if ( $this->sink ) {
161 $this->guzzleOptions['sink'] = $this->sink;
162 }
163
164 if ( $this->caInfo ) {
165 $this->guzzleOptions['verify'] = $this->caInfo;
166 } elseif ( !$this->sslVerifyHost && !$this->sslVerifyCert ) {
167 $this->guzzleOptions['verify'] = false;
168 }
169
170 try {
171 $client = new Client( $this->guzzleOptions );
172 $request = new Request( $this->method, $this->url );
173 $response = $client->send( $request );
174 $this->headerList = $response->getHeaders();
175
176 $this->respVersion = $response->getProtocolVersion();
177 $this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
178 } catch ( GuzzleHttp\Exception\ConnectException $e ) {
179 // ConnectException is thrown for several reasons besides generic "timeout":
180 // Connection refused
181 // couldn't connect to host
182 // connection attempt failed
183 // Could not resolve IPv4 address for host
184 // Could not resolve IPv6 address for host
185 if ( $this->usingCurl() ) {
186 $handlerContext = $e->getHandlerContext();
187 if ( $handlerContext['errno'] == CURLE_OPERATION_TIMEOUTED ) {
188 $this->status->fatal( 'http-timed-out', $this->url );
189 } else {
190 $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
191 }
192 } else {
193 $this->status->fatal( 'http-request-error' );
194 }
195 } catch ( GuzzleHttp\Exception\RequestException $e ) {
196 if ( $this->usingCurl() ) {
197 $handlerContext = $e->getHandlerContext();
198 $this->status->fatal( 'http-curl-error', $handlerContext['error'] );
199 } else {
200 // Non-ideal, but the only way to identify connection timeout vs other conditions
201 $needle = 'Connection timed out';
202 if ( strpos( $e->getMessage(), $needle ) !== false ) {
203 $this->status->fatal( 'http-timed-out', $this->url );
204 } else {
205 $this->status->fatal( 'http-request-error' );
206 }
207 }
208 } catch ( GuzzleHttp\Exception\GuzzleException $e ) {
209 $this->status->fatal( 'http-internal-error' );
210 }
211
212 if ( $this->profiler ) {
213 $profileSection = $this->profiler->scopedProfileIn(
214 __METHOD__ . '-' . $this->profileName
215 );
216 }
217
218 if ( $this->profiler ) {
219 $this->profiler->scopedProfileOut( $profileSection );
220 }
221
222 $this->parseHeader();
223 $this->setStatus();
224
225 return Status::wrap( $this->status ); // TODO B/C; move this to callers
226 }
227
228 protected function prepare() {
229 $this->doSetCallback( $this->callback );
230 parent::prepare();
231 }
232
236 protected function usingCurl() {
237 return ( $this->handler && is_a( $this->handler, 'GuzzleHttp\Handler\CurlHandler' ) ) ||
238 ( !$this->handler && extension_loaded( 'curl' ) );
239 }
240
245 protected function parseHeader() {
246 // Failure without (valid) headers gets a response status of zero
247 if ( !$this->status->isOK() ) {
248 $this->respStatus = '0 Error';
249 }
250
251 foreach ( $this->headerList as $name => $values ) {
252 $this->respHeaders[strtolower( $name )] = $values;
253 }
254
255 $this->parseCookies();
256 }
257}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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)
Callback-aware stream.
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
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.
callable $callback
Profiler $profiler
Profiler base class that defines the interface and some trivial functionality.
Definition Profiler.php:33
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
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:921
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:2843
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 $handler
Definition hooks.txt:894
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:1999
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
this hook is for auditing only $response
Definition hooks.txt:780
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
returning false will NOT prevent logging $e
Definition hooks.txt:2175
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:37
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))