Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / http / GuzzleHttpRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use GuzzleHttp\Client;
22 use GuzzleHttp\Psr7\Request;
23
24 /**
25 * MWHttpRequest implemented using the Guzzle library
26 *
27 * Differences from the CurlHttpRequest implementation:
28 * 1) a new 'sink' option is available as an alternative to callbacks. See:
29 * http://docs.guzzlephp.org/en/stable/request-options.html#sink)
30 * The 'callback' option remains available as well. If both 'sink' and 'callback' are
31 * specified, 'sink' is used.
32 * 2) callers may set a custom handler via the 'handler' option.
33 * If this is not set, Guzzle will use curl (if available) or PHP streams (otherwise)
34 * 3) setting either sslVerifyHost or sslVerifyCert will enable both. Guzzle does not allow
35 * them to be set separately.
36 *
37 * @since 1.33
38 */
39 class GuzzleHttpRequest extends MWHttpRequest {
40 const SUPPORTS_FILE_POSTS = true;
41
42 protected $handler = null;
43 protected $sink = null;
44 protected $guzzleOptions = [ 'http_errors' => false ];
45
46 /**
47 * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
48 * @param array $options (optional) extra params to pass (see Http::request())
49 * @param string $caller The method making this request, for profiling
50 * @param Profiler|null $profiler An instance of the profiler for profiling, or null
51 * @throws Exception
52 */
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
66 /**
67 * Set a read callback to accept data read from the HTTP request.
68 * By default, data is appended to an internal buffer which can be
69 * retrieved through $req->getContent().
70 *
71 * To handle data as it comes in -- especially for large files that
72 * would not fit in memory -- you can instead set your own callback,
73 * in the form function($resource, $buffer) where the first parameter
74 * is the low-level resource being read (implementation specific),
75 * and the second parameter is the data buffer.
76 *
77 * You MUST return the number of bytes handled in the buffer; if fewer
78 * bytes are reported handled than were passed to you, the HTTP fetch
79 * will be aborted.
80 *
81 * This function overrides any 'sink' or 'callback' constructor option.
82 *
83 * @param callable|null $callback
84 * @throws InvalidArgumentException
85 */
86 public function setCallback( $callback ) {
87 $this->sink = null;
88 $this->doSetCallback( $callback );
89 }
90
91 /**
92 * Worker function for setting callbacks. Calls can originate both internally and externally
93 * via setCallback). Defaults to the internal read callback if $callback is null.
94 *
95 * If a sink is already specified, this does nothing. This causes the 'sink' constructor
96 * option to override the 'callback' constructor option.
97 *
98 * @param callable|null $callback
99 * @throws InvalidArgumentException
100 */
101 protected function doSetCallback( $callback ) {
102 if ( !$this->sink ) {
103 parent::doSetCallback( $callback );
104 $this->sink = new MWCallbackStream( $this->callback );
105 }
106 }
107
108 /**
109 * @see MWHttpRequest::execute
110 *
111 * @return Status
112 */
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 }
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
229 /**
230 * @return bool
231 */
232 protected function usingCurl() {
233 return ( $this->handler && is_a( $this->handler, 'GuzzleHttp\Handler\CurlHandler' ) ) ||
234 ( !$this->handler && extension_loaded( 'curl' ) );
235 }
236
237 /**
238 * Guzzle provides headers as an array. Reprocess to match our expectations. Guzzle will
239 * have already parsed and removed the status line (in EasyHandle::createResponse).
240 */
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 }