MediaWiki master
HttpRequestFactory.php
Go to the documentation of this file.
1<?php
21
22use GuzzleHttp\Client;
24use InvalidArgumentException;
31use Profiler;
32use Psr\Log\LoggerInterface;
33
40 private $options;
42 private $logger;
44 private $telemetry;
45
49 public const CONSTRUCTOR_OPTIONS = [
56 ];
57
58 public function __construct(
59 ServiceOptions $options,
60 LoggerInterface $logger,
61 Telemetry $telemetry = null
62 ) {
63 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
64 $this->options = $options;
65 $this->logger = $logger;
66 $this->telemetry = $telemetry;
67 }
68
104 public function create( $url, array $options = [], $caller = __METHOD__ ) {
105 if ( !isset( $options['logger'] ) ) {
106 $options['logger'] = $this->logger;
107 }
108 $options['timeout'] = $this->normalizeTimeout(
109 $options['timeout'] ?? null,
110 $options['maxTimeout'] ?? null,
111 $this->options->get( MainConfigNames::HTTPTimeout ),
112 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
113 );
114 $options['connectTimeout'] = $this->normalizeTimeout(
115 $options['connectTimeout'] ?? null,
116 $options['maxConnectTimeout'] ?? null,
117 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
118 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
119 );
120 $client = new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
121 if ( $this->telemetry ) {
122 $client->addTelemetry( $this->telemetry );
123 }
124 return $client;
125 }
126
137 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
138 if ( $parameter === 'default' || $parameter === null ) {
139 if ( !is_numeric( $default ) ) {
140 throw new InvalidArgumentException(
141 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
142 }
143 $value = $default;
144 } else {
145 $value = $parameter;
146 }
147 $max = $maxParameter ?? $maxConfigured;
148 if ( $max && $value > $max ) {
149 return $max;
150 }
151
152 return $value;
153 }
154
160 public function canMakeRequests() {
161 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
162 }
163
175 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
176 $logger = LoggerFactory::getInstance( 'http' );
177 $logger->debug( "$method: $url" );
178
179 $options['method'] = strtoupper( $method );
180
181 $req = $this->create( $url, $options, $caller );
182 $status = $req->execute();
183
184 if ( $status->isOK() ) {
185 return $req->getContent();
186 } else {
187 $errors = $status->getErrorsByType( 'error' );
188 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
189 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
190 return null;
191 }
192 }
193
203 public function get( $url, array $options = [], $caller = __METHOD__ ) {
204 return $this->request( 'GET', $url, $options, $caller );
205 }
206
216 public function post( $url, array $options = [], $caller = __METHOD__ ) {
217 return $this->request( 'POST', $url, $options, $caller );
218 }
219
223 public function getUserAgent() {
224 return 'MediaWiki/' . MW_VERSION;
225 }
226
239 public function createMultiClient( $options = [] ) {
240 $options['reqTimeout'] = $this->normalizeTimeout(
241 $options['reqTimeout'] ?? $options['timeout'] ?? null,
242 $options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
243 $this->options->get( MainConfigNames::HTTPTimeout ),
244 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
245 );
246 $options['connTimeout'] = $this->normalizeTimeout(
247 $options['connTimeout'] ?? $options['connectTimeout'] ?? null,
248 $options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
249 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
250 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
251 );
252 $options += [
253 'maxReqTimeout' => $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF,
254 'maxConnTimeout' =>
255 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF,
256 'userAgent' => $this->getUserAgent(),
257 'logger' => $this->logger,
258 'localProxy' => $this->options->get( MainConfigNames::LocalHTTPProxy ),
259 'localVirtualHosts' => $this->options->get( MainConfigNames::LocalVirtualHosts ),
260 'telemetry' => Telemetry::getInstance(),
261 ];
262 return new MultiHttpClient( $options );
263 }
264
278 public function createGuzzleClient( array $config = [] ): Client {
279 $config['timeout'] = $this->normalizeTimeout(
280 $config['timeout'] ?? null,
281 $config['maxTimeout'] ?? null,
282 $this->options->get( MainConfigNames::HTTPTimeout ),
283 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
284 );
285
286 $config['connect_timeout'] = $this->normalizeTimeout(
287 $config['connect_timeout'] ?? null,
288 $config['maxConnectTimeout'] ?? null,
289 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
290 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
291 );
292
293 if ( !isset( $config['headers']['User-Agent'] ) ) {
294 $config['headers']['User-Agent'] = $this->getUserAgent();
295 }
296 if ( $this->telemetry ) {
297 $config['headers'] = array_merge(
298 $this->telemetry->getRequestHeaders(), $config['headers']
299 );
300 }
301
302 return new Client( $config );
303 }
304}
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:36
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
MWHttpRequest implemented using the Guzzle library.
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Factory creating MWHttpRequest objects.
get( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for ‘request( 'GET’ ), parameters have the same meaning as forrequest()`.
createMultiClient( $options=[])
Get a MultiHttpClient with MediaWiki configured defaults applied.
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for ‘request( 'POST’ ), parameters have the same meaning as forrequest()`.
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
__construct(ServiceOptions $options, LoggerInterface $logger, Telemetry $telemetry=null)
createGuzzleClient(array $config=[])
Get a GuzzleHttp\Client instance.
canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
request( $method, $url, array $options=[], $caller=__METHOD__)
Perform an HTTP request.
Service for handling telemetry data.
Definition Telemetry.php:29
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
const HTTPConnectTimeout
Name constant for the HTTPConnectTimeout setting, for use with Config::get()
const HTTPTimeout
Name constant for the HTTPTimeout setting, for use with Config::get()
const LocalVirtualHosts
Name constant for the LocalVirtualHosts setting, for use with Config::get()
const HTTPMaxConnectTimeout
Name constant for the HTTPMaxConnectTimeout setting, for use with Config::get()
const HTTPMaxTimeout
Name constant for the HTTPMaxTimeout setting, for use with Config::get()
const LocalHTTPProxy
Name constant for the LocalHTTPProxy setting, for use with Config::get()
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Class to handle multiple HTTP requests.
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:37
static instance()
Definition Profiler.php:105