1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Http;
use GuzzleHttp\Client;
use GuzzleHttpRequest;
use InvalidArgumentException;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Status\Status;
use MWHttpRequest;
use Profiler;
use Psr\Log\LoggerInterface;
use Wikimedia\Http\MultiHttpClient;
/**
* Factory creating MWHttpRequest objects.
*/
class HttpRequestFactory {
/** @var ServiceOptions */
private $options;
/** @var LoggerInterface */
private $logger;
/** @var Telemetry|null */
private $telemetry;
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::HTTPTimeout,
MainConfigNames::HTTPConnectTimeout,
MainConfigNames::HTTPMaxTimeout,
MainConfigNames::HTTPMaxConnectTimeout,
MainConfigNames::LocalVirtualHosts,
MainConfigNames::LocalHTTPProxy,
];
public function __construct(
ServiceOptions $options,
LoggerInterface $logger,
?Telemetry $telemetry = null
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->logger = $logger;
$this->telemetry = $telemetry;
}
/**
* Generate a new MWHttpRequest object
* @param string $url Url to use
* @param array $options Possible keys for the array:
* - timeout Timeout length in seconds or 'default'
* - connectTimeout Timeout for connection, in seconds (curl only) or 'default'
* - maxTimeout Override for the configured maximum timeout. This should not be
* used in production code.
* - maxConnectTimeout Override for the configured maximum connect timeout. This should
* not be used in production code.
* - postData An array of key-value pairs or a url-encoded form data
* - proxy The proxy to use.
* Otherwise it will use $wgHTTPProxy or $wgLocalHTTPProxy (if set)
* Otherwise it will use the environment variable "http_proxy" (if set)
* - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
* - sslVerifyHost Verify hostname against certificate
* - sslVerifyCert Verify SSL certificate
* - caInfo Provide CA information
* - maxRedirects Maximum number of redirects to follow (defaults to 5)
* - followRedirects Whether to follow redirects (defaults to false).
* Note: this should only be used when the target URL is trusted,
* to avoid attacks on intranet services accessible by HTTP.
* - userAgent A user agent, if you want to override the default
* "MediaWiki/{MW_VERSION}".
* - logger A \Psr\Logger\LoggerInterface instance for debug logging
* - username Username for HTTP Basic Authentication
* - password Password for HTTP Basic Authentication
* - originalRequest Information about the original request (as a WebRequest object or
* an associative array with 'ip' and 'userAgent').
* @phpcs:ignore Generic.Files.LineLength
* @phan-param array{timeout?:int|string,connectTimeout?:int|string,postData?:string|array,proxy?:?string,noProxy?:bool,sslVerifyHost?:bool,sslVerifyCert?:bool,caInfo?:?string,maxRedirects?:int,followRedirects?:bool,userAgent?:string,method?:string,logger?:\Psr\Log\LoggerInterface,username?:string,password?:string,originalRequest?:\MediaWiki\Request\WebRequest|array{ip:string,userAgent:string}} $options
* @param string $caller The method making this request, for profiling @phan-mandatory-param
* @return MWHttpRequest
* @see MWHttpRequest::__construct
*/
public function create( $url, array $options = [], $caller = __METHOD__ ) {
if ( !isset( $options['logger'] ) ) {
$options['logger'] = $this->logger;
}
$options['timeout'] = $this->normalizeTimeout(
$options['timeout'] ?? null,
$options['maxTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPTimeout ),
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
);
$options['connectTimeout'] = $this->normalizeTimeout(
$options['connectTimeout'] ?? null,
$options['maxConnectTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPConnectTimeout ),
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
);
$client = new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
if ( $this->telemetry ) {
$client->addTelemetry( $this->telemetry );
}
return $client;
}
/**
* Given a passed parameter value, a default and a maximum, figure out the
* correct timeout to pass to the backend.
*
* @param int|float|string|null $parameter The timeout in seconds, or "default" or null
* @param int|float|null $maxParameter The maximum timeout specified by the caller
* @param int|float $default The configured default timeout
* @param int|float $maxConfigured The configured maximum timeout
* @return int|float
*/
private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
if ( $parameter === 'default' || $parameter === null ) {
if ( !is_numeric( $default ) ) {
throw new InvalidArgumentException(
'$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
}
$value = $default;
} else {
$value = $parameter;
}
$max = $maxParameter ?? $maxConfigured;
if ( $max && $value > $max ) {
return $max;
}
return $value;
}
/**
* Simple function to test if we can make any sort of requests at all, using
* cURL or fopen()
* @return bool
*/
public function canMakeRequests() {
return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
}
/**
* Perform an HTTP request
*
* @since 1.34
* @param string $method HTTP method. Usually GET/POST
* @param string $url Full URL to act on. If protocol-relative, will be expanded to an http://
* URL
* @param array $options See HttpRequestFactory::create
* @param string $caller The method making this request, for profiling @phan-mandatory-param
* @return string|null null on failure or a string on success
*/
public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
$logger = LoggerFactory::getInstance( 'http' );
$logger->debug( "$method: $url" );
$options['method'] = strtoupper( $method );
$req = $this->create( $url, $options, $caller );
$status = $req->execute();
if ( $status->isOK() ) {
return $req->getContent();
} else {
$errors = array_map( fn ( $msg ) => $msg->getKey(), $status->getMessages( 'error' ) );
$logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
[ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
return null;
}
}
/**
* Simple wrapper for `request( 'GET' )`, parameters have the same meaning as for `request()`
*
* @since 1.34
* @param string $url
* @param array $options
* @param string $caller @phan-mandatory-param
* @return string|null
*/
public function get( $url, array $options = [], $caller = __METHOD__ ) {
return $this->request( 'GET', $url, $options, $caller );
}
/**
* Simple wrapper for `request( 'POST' )`, parameters have the same meaning as for `request()`
*
* @since 1.34
* @param string $url
* @param array $options
* @param string $caller @phan-mandatory-param
* @return string|null
*/
public function post( $url, array $options = [], $caller = __METHOD__ ) {
return $this->request( 'POST', $url, $options, $caller );
}
/**
* @return string
*/
public function getUserAgent() {
return 'MediaWiki/' . MW_VERSION;
}
/**
* Get a MultiHttpClient with MediaWiki configured defaults applied.
*
* Unlike create(), by default, no proxy will be used. To use a proxy,
* specify the 'proxy' option.
*
* @param array $options Options as documented in MultiHttpClient::__construct(),
* except that for consistency with create(), 'timeout' is accepted as an
* alias for 'reqTimeout', and 'connectTimeout' is accepted as an alias for
* 'connTimeout'.
* @return MultiHttpClient
*/
public function createMultiClient( $options = [] ) {
$options['reqTimeout'] = $this->normalizeTimeout(
$options['reqTimeout'] ?? $options['timeout'] ?? null,
$options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPTimeout ),
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
);
$options['connTimeout'] = $this->normalizeTimeout(
$options['connTimeout'] ?? $options['connectTimeout'] ?? null,
$options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPConnectTimeout ),
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
);
$options += [
'maxReqTimeout' => $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF,
'maxConnTimeout' =>
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF,
'userAgent' => $this->getUserAgent(),
'logger' => $this->logger,
'localProxy' => $this->options->get( MainConfigNames::LocalHTTPProxy ),
'localVirtualHosts' => $this->options->get( MainConfigNames::LocalVirtualHosts ),
'telemetry' => Telemetry::getInstance(),
];
return new MultiHttpClient( $options );
}
/**
* Get a GuzzleHttp\Client instance.
*
* @since 1.36
* @param array $config Client configuration settings.
* @return Client
*
* @see \GuzzleHttp\RequestOptions for a list of available request options.
* @see Client::__construct() for additional options.
* Additional options that should not be used in production code:
* - maxTimeout Override for the configured maximum timeout.
* - maxConnectTimeout Override for the configured maximum connect timeout.
*/
public function createGuzzleClient( array $config = [] ): Client {
$config['timeout'] = $this->normalizeTimeout(
$config['timeout'] ?? null,
$config['maxTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPTimeout ),
$this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
);
$config['connect_timeout'] = $this->normalizeTimeout(
$config['connect_timeout'] ?? null,
$config['maxConnectTimeout'] ?? null,
$this->options->get( MainConfigNames::HTTPConnectTimeout ),
$this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
);
if ( !isset( $config['headers']['User-Agent'] ) ) {
$config['headers']['User-Agent'] = $this->getUserAgent();
}
if ( $this->telemetry ) {
$config['headers'] = array_merge(
$this->telemetry->getRequestHeaders(), $config['headers']
);
}
return new Client( $config );
}
}
|