MediaWiki master
HttpRequestFactory.php
Go to the documentation of this file.
1<?php
6namespace MediaWiki\Http;
7
8use GuzzleHttp\Client;
9use InvalidArgumentException;
15use Psr\Log\LoggerInterface;
18
26 public const CONSTRUCTOR_OPTIONS = [
33 ];
34
35 public function __construct(
36 private readonly ServiceOptions $options,
37 private readonly LoggerInterface $logger,
38 private readonly ?TelemetryHeadersInterface $telemetry = null,
39 ) {
40 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
41 }
42
77 public function create( $url, array $options = [], $caller = __METHOD__ ) {
78 if ( !isset( $options['logger'] ) ) {
79 $options['logger'] = $this->logger;
80 }
81 $options['timeout'] = $this->normalizeTimeout(
82 $options['timeout'] ?? null,
83 $options['maxTimeout'] ?? null,
84 $this->options->get( MainConfigNames::HTTPTimeout ),
85 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
86 );
87 $options['connectTimeout'] = $this->normalizeTimeout(
88 $options['connectTimeout'] ?? null,
89 $options['maxConnectTimeout'] ?? null,
90 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
91 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
92 );
93 $client = new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
94 if ( $this->telemetry ) {
95 $client->addTelemetry( $this->telemetry );
96 }
97 return $client;
98 }
99
110 private function normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured ) {
111 if ( $parameter === 'default' || $parameter === null ) {
112 if ( !is_numeric( $default ) ) {
113 throw new InvalidArgumentException(
114 '$wgHTTPTimeout and $wgHTTPConnectTimeout must be set to a number' );
115 }
116 $value = $default;
117 } else {
118 $value = $parameter;
119 }
120 $max = $maxParameter ?? $maxConfigured;
121 if ( $max && $value > $max ) {
122 return $max;
123 }
124
125 return $value;
126 }
127
133 public function canMakeRequests() {
134 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
135 }
136
148 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
149 $logger = LoggerFactory::getInstance( 'http' );
150 $logger->debug( "$method: $url" );
151
152 $options['method'] = strtoupper( $method );
153
154 $req = $this->create( $url, $options, $caller );
155 $status = $req->execute();
156
157 if ( $status->isOK() ) {
158 return $req->getContent();
159 } else {
160 $errors = array_map( static fn ( $msg ) => $msg->getKey(), $status->getMessages( 'error' ) );
161 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
162 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
163 return null;
164 }
165 }
166
176 public function get( $url, array $options = [], $caller = __METHOD__ ) {
177 return $this->request( 'GET', $url, $options, $caller );
178 }
179
189 public function post( $url, array $options = [], $caller = __METHOD__ ) {
190 return $this->request( 'POST', $url, $options, $caller );
191 }
192
196 public function getUserAgent() {
197 return 'MediaWiki/' . MW_VERSION;
198 }
199
212 public function createMultiClient( $options = [] ) {
213 $options['reqTimeout'] = $this->normalizeTimeout(
214 $options['reqTimeout'] ?? $options['timeout'] ?? null,
215 $options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
216 $this->options->get( MainConfigNames::HTTPTimeout ),
217 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
218 );
219 $options['connTimeout'] = $this->normalizeTimeout(
220 $options['connTimeout'] ?? $options['connectTimeout'] ?? null,
221 $options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
222 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
223 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
224 );
225 $options += [
226 'maxReqTimeout' => $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF,
227 'maxConnTimeout' =>
228 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF,
229 'userAgent' => $this->getUserAgent(),
230 'logger' => $this->logger,
231 'localProxy' => $this->options->get( MainConfigNames::LocalHTTPProxy ),
232 'localVirtualHosts' => $this->options->get( MainConfigNames::LocalVirtualHosts ),
233 'telemetry' => Telemetry::getInstance(),
234 ];
235 return new MultiHttpClient( $options );
236 }
237
251 public function createGuzzleClient( array $config = [] ): Client {
252 $config['timeout'] = $this->normalizeTimeout(
253 $config['timeout'] ?? null,
254 $config['maxTimeout'] ?? null,
255 $this->options->get( MainConfigNames::HTTPTimeout ),
256 $this->options->get( MainConfigNames::HTTPMaxTimeout ) ?: INF
257 );
258
259 $config['connect_timeout'] = $this->normalizeTimeout(
260 $config['connect_timeout'] ?? null,
261 $config['maxConnectTimeout'] ?? null,
262 $this->options->get( MainConfigNames::HTTPConnectTimeout ),
263 $this->options->get( MainConfigNames::HTTPMaxConnectTimeout ) ?: INF
264 );
265
266 if ( !isset( $config['headers']['User-Agent'] ) ) {
267 $config['headers']['User-Agent'] = $this->getUserAgent();
268 }
269 if ( $this->telemetry ) {
270 $config['headers'] = array_merge(
271 $this->telemetry->getRequestHeaders(), $config['headers']
272 );
273 }
274
275 return new Client( $config );
276 }
277}
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:23
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
A class for passing options to services.
MWHttpRequest implemented using the Guzzle library.
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.
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.
__construct(private readonly ServiceOptions $options, private readonly LoggerInterface $logger, private readonly ?TelemetryHeadersInterface $telemetry=null,)
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()
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:26
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Class to handle multiple HTTP requests.
Provide Request Telemetry information.