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.
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.
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
21 use GuzzleHttp\Client
;
22 use GuzzleHttp\Psr7\Request
;
25 * MWHttpRequest implemented using the Guzzle library
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.
39 class GuzzleHttpRequest
extends MWHttpRequest
{
40 const SUPPORTS_FILE_POSTS
= true;
42 protected $handler = null;
43 protected $sink = null;
44 protected $guzzleOptions = [ 'http_errors' => false ];
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 HttpRequestFactory::create())
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
53 public function __construct(
54 $url, array $options = [], $caller = __METHOD__
, Profiler
$profiler = null
56 parent
::__construct( $url, $options, $caller, $profiler );
58 if ( isset( $options['handler'] ) ) {
59 $this->handler
= $options['handler'];
61 if ( isset( $options['sink'] ) ) {
62 $this->sink
= $options['sink'];
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().
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.
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
81 * This function overrides any 'sink' or 'callback' constructor option.
83 * @param callable|null $callback
84 * @throws InvalidArgumentException
86 public function setCallback( $callback ) {
88 $this->doSetCallback( $callback );
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.
95 * If a sink is already specified, this does nothing. This causes the 'sink' constructor
96 * option to override the 'callback' constructor option.
98 * @param callable|null $callback
99 * @throws InvalidArgumentException
101 protected function doSetCallback( $callback ) {
102 if ( !$this->sink
) {
103 parent
::doSetCallback( $callback );
104 $this->sink
= new MWCallbackStream( $this->callback
);
109 * @see MWHttpRequest::execute
113 public function execute() {
116 if ( !$this->status
->isOK() ) {
117 return Status
::wrap( $this->status
); // TODO B/C; move this to callers
120 if ( $this->proxy
) {
121 $this->guzzleOptions
['proxy'] = $this->proxy
;
124 $this->guzzleOptions
['timeout'] = $this->timeout
;
125 $this->guzzleOptions
['connect_timeout'] = $this->connectTimeout
;
126 $this->guzzleOptions
['version'] = '1.1';
128 if ( !$this->followRedirects
) {
129 $this->guzzleOptions
['allow_redirects'] = false;
131 $this->guzzleOptions
['allow_redirects'] = [
132 'max' => $this->maxRedirects
136 if ( $this->method
== 'POST' ) {
137 $postData = $this->postData
;
138 if ( is_array( $postData ) ) {
139 $this->guzzleOptions
['form_params'] = $postData;
141 $this->guzzleOptions
['body'] = $postData;
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;
150 $this->guzzleOptions
['headers'] = $this->reqHeaders
;
152 if ( $this->handler
) {
153 $this->guzzleOptions
['handler'] = $this->handler
;
157 $this->guzzleOptions
['sink'] = $this->sink
;
160 if ( $this->caInfo
) {
161 $this->guzzleOptions
['verify'] = $this->caInfo
;
162 } elseif ( !$this->sslVerifyHost
&& !$this->sslVerifyCert
) {
163 $this->guzzleOptions
['verify'] = false;
167 $client = new Client( $this->guzzleOptions
);
168 $request = new Request( $this->method
, $this->url
);
169 $response = $client->send( $request );
170 $this->headerList
= $response->getHeaders();
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
);
186 $this->status
->fatal( 'http-curl-error', $handlerContext['error'] );
189 $this->status
->fatal( 'http-request-error' );
191 } catch ( GuzzleHttp\Exception\RequestException
$e ) {
192 if ( $this->usingCurl() ) {
193 $handlerContext = $e->getHandlerContext();
194 $this->status
->fatal( 'http-curl-error', $handlerContext['error'] );
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
);
201 $this->status
->fatal( 'http-request-error' );
204 } catch ( GuzzleHttp\Exception\GuzzleException
$e ) {
205 $this->status
->fatal( 'http-internal-error' );
208 if ( $this->profiler
) {
209 $profileSection = $this->profiler
->scopedProfileIn(
210 __METHOD__
. '-' . $this->profileName
214 if ( $this->profiler
) {
215 $this->profiler
->scopedProfileOut( $profileSection );
218 $this->parseHeader();
221 return Status
::wrap( $this->status
); // TODO B/C; move this to callers
224 protected function prepare() {
225 $this->doSetCallback( $this->callback
);
232 protected function usingCurl() {
233 return ( $this->handler
&& is_a( $this->handler
, 'GuzzleHttp\Handler\CurlHandler' ) ) ||
234 ( !$this->handler
&& extension_loaded( 'curl' ) );
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).
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';
247 foreach ( $this->headerList
as $name => $values ) {
248 $this->respHeaders
[strtolower( $name )] = $values;
251 $this->parseCookies();