MediaWiki 1.42.0
HttpRequestFactory.php
Go to the documentation of this file.
1<?php
21
22use GuzzleHttp\Client;
24use InvalidArgumentException;
31use Profiler;
32use Psr\Log\LoggerInterface;
33
39 private $options;
41 private $logger;
43 private $telemetry;
44
48 public const CONSTRUCTOR_OPTIONS = [
55 ];
56
57 public function __construct(
58 ServiceOptions $options,
59 LoggerInterface $logger,
60 Telemetry $telemetry = null
61 ) {
62 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
63 $this->options = $options;
64 $this->logger = $logger;
65 $this->telemetry = $telemetry;
66 }
67
103 public function create( $url, array $options = [], $caller = __METHOD__ ) {
104 if ( !isset( $options['logger'] ) ) {
105 $options['logger'] = $this->logger;
106 }
107 $options['timeout'] = $this->normalizeTimeout(
108 $options['timeout'] ?? null,
109 $options['maxTimeout'] ?? null,
110 $this->options->get( MainConfigNames::HTTPTimeout ),
111 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
112 );
113 $options['connectTimeout'] = $this->normalizeTimeout(
114 $options['connectTimeout'] ?? null,
115 $options['maxConnectTimeout'] ?? null,
116 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
117 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
118 );
119 $client = new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
120 if ( $this->telemetry ) {
121 $client->addTelemetry( $this->telemetry );
122 }
123 return $client;
124 }
125
136 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
137 if ( $parameter === 'default' || $parameter === null ) {
138 if ( !is_numeric( $default ) ) {
139 throw new InvalidArgumentException(
140 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
141 }
142 $value = $default;
143 } else {
144 $value = $parameter;
145 }
146 $max = $maxParameter ?? $maxConfigured;
147 if ( $max && $value > $max ) {
148 return $max;
149 }
150
151 return $value;
152 }
153
159 public function canMakeRequests() {
160 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
161 }
162
174 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
175 $logger = LoggerFactory::getInstance( 'http' );
176 $logger->debug( "$method: $url" );
177
178 $options['method'] = strtoupper( $method );
179
180 $req = $this->create( $url, $options, $caller );
181 $status = $req->execute();
182
183 if ( $status->isOK() ) {
184 return $req->getContent();
185 } else {
186 $errors = $status->getErrorsByType( 'error' );
187 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
188 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
189 return null;
190 }
191 }
192
202 public function get( $url, array $options = [], $caller = __METHOD__ ) {
203 return $this->request( 'GET', $url, $options, $caller );
204 }
205
215 public function post( $url, array $options = [], $caller = __METHOD__ ) {
216 return $this->request( 'POST', $url, $options, $caller );
217 }
218
222 public function getUserAgent() {
223 return 'MediaWiki/' . MW_VERSION;
224 }
225
238 public function createMultiClient( $options = [] ) {
239 $options['reqTimeout'] = $this->normalizeTimeout(
240 $options['reqTimeout'] ?? $options['timeout'] ?? null,
241 $options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
242 $this->options->get( MainConfigNames::HTTPTimeout ),
243 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
244 );
245 $options['connTimeout'] = $this->normalizeTimeout(
246 $options['connTimeout'] ?? $options['connectTimeout'] ?? null,
247 $options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
248 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
249 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
250 );
251 $options += [
252 'maxReqTimeout' => $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF,
253 'maxConnTimeout' =>
254 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF,
255 'userAgent' => $this->getUserAgent(),
256 'logger' => $this->logger,
257 'localProxy' => $this->options->get( MainConfigNames::LocalHTTPProxy ),
258 'localVirtualHosts' => $this->options->get( MainConfigNames::LocalVirtualHosts ),
259 'telemetry' => Telemetry::getInstance(),
260 ];
261 return new MultiHttpClient( $options );
262 }
263
277 public function createGuzzleClient( array $config = [] ): Client {
278 $config['timeout'] = $this->normalizeTimeout(
279 $config['timeout'] ?? null,
280 $config['maxTimeout'] ?? null,
281 $this->options->get( MainConfigNames::HTTPTimeout ),
282 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
283 );
284
285 $config['connect_timeout'] = $this->normalizeTimeout(
286 $config['connect_timeout'] ?? null,
287 $config['maxConnectTimeout'] ?? null,
288 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
289 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
290 );
291
292 if ( !isset( $config['headers']['User-Agent'] ) ) {
293 $config['headers']['User-Agent'] = $this->getUserAgent();
294 }
295 if ( $this->telemetry ) {
296 $config['headers'] = array_merge(
297 $this->telemetry->getRequestHeaders(), $config['headers']
298 );
299 }
300
301 return new Client( $config );
302 }
303}
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