MediaWiki REL1_35
HttpRequestFactory.php
Go to the documentation of this file.
1<?php
21
24use Http;
30use Profiler;
31use Psr\Log\LoggerInterface;
32use RuntimeException;
33use Status;
34
40 private $options;
42 private $logger;
43
44 public const CONSTRUCTOR_OPTIONS = [
45 'HTTPTimeout',
46 'HTTPConnectTimeout',
47 'HTTPMaxTimeout',
48 'HTTPMaxConnectTimeout',
49 ];
50
51 public function __construct( ServiceOptions $options, LoggerInterface $logger ) {
52 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
53 $this->options = $options;
54 $this->logger = $logger;
55 }
56
94 public function create( $url, array $options = [], $caller = __METHOD__ ) {
95 if ( !Http::$httpEngine ) {
96 Http::$httpEngine = 'guzzle';
97 }
98
99 if ( !isset( $options['logger'] ) ) {
100 $options['logger'] = $this->logger;
101 }
102 $options['timeout'] = $this->normalizeTimeout(
103 $options['timeout'] ?? null,
104 $options['maxTimeout'] ?? null,
105 $this->options->get( 'HTTPTimeout' ),
106 $this->options->get( 'HTTPMaxTimeout' )
107 );
108 $options['connectTimeout'] = $this->normalizeTimeout(
109 $options['connectTimeout'] ?? null,
110 $options['maxConnectTimeout'] ?? null,
111 $this->options->get( 'HTTPConnectTimeout' ),
112 $this->options->get( 'HTTPMaxConnectTimeout' )
113 );
114
115 switch ( Http::$httpEngine ) {
116 case 'guzzle':
117 return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
118 case 'curl':
119 return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
120 case 'php':
121 return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
122 default:
123 throw new RuntimeException( __METHOD__ . ': The requested engine is not valid.' );
124 }
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 if ( $maxParameter !== null ) {
148 $max = $maxParameter;
149 } else {
150 $max = $maxConfigured;
151 }
152 if ( $max && $value > $max ) {
153 return $max;
154 } else {
155 return $value;
156 }
157 }
158
164 public function canMakeRequests() {
165 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
166 }
167
179 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
180 $logger = LoggerFactory::getInstance( 'http' );
181 $logger->debug( "$method: $url" );
182
183 $options['method'] = strtoupper( $method );
184
185 $req = $this->create( $url, $options, $caller );
186 $status = $req->execute();
187
188 if ( $status->isOK() ) {
189 return $req->getContent();
190 } else {
191 $errors = $status->getErrorsByType( 'error' );
192 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
193 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
194 return null;
195 }
196 }
197
207 public function get( $url, array $options = [], $caller = __METHOD__ ) {
208 return $this->request( 'GET', $url, $options, $caller );
209 }
210
220 public function post( $url, array $options = [], $caller = __METHOD__ ) {
221 return $this->request( 'POST', $url, $options, $caller );
222 }
223
227 public function getUserAgent() {
228 return 'MediaWiki/' . MW_VERSION;
229 }
230
243 public function createMultiClient( $options = [] ) {
244 $options['reqTimeout'] = $this->normalizeTimeout(
245 $options['reqTimeout'] ?? $options['timeout'] ?? null,
246 $options['maxReqTimeout'] ?? $options['maxTimeout'] ?? null,
247 $this->options->get( 'HTTPTimeout' ),
248 $this->options->get( 'HTTPMaxTimeout' )
249 );
250 $options['connTimeout'] = $this->normalizeTimeout(
251 $options['connTimeout'] ?? $options['connectTimeout'] ?? null,
252 $options['maxConnTimeout'] ?? $options['maxConnectTimeout'] ?? null,
253 $this->options->get( 'HTTPConnectTimeout' ),
254 $this->options->get( 'HTTPMaxConnectTimeout' )
255 );
256 $options += [
257 'maxReqTimeout' => $this->options->get( 'HTTPMaxTimeout' ),
258 'maxConnTimeout' => $this->options->get( 'HTTPMaxConnectTimeout' ),
259 'userAgent' => $this->getUserAgent(),
260 'logger' => $this->logger
261 ];
262 return new MultiHttpClient( $options );
263 }
264}
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:40
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
MWHttpRequest implemented using internal curl compiled into PHP.
MWHttpRequest implemented using the Guzzle library.
Various HTTP related functions.
Definition Http.php:28
static $httpEngine
Definition Http.php:30
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.
normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured)
Given a passed parameter value, a default and a maximum, figure out the correct timeout to pass to th...
__construct(ServiceOptions $options, LoggerInterface $logger)
createMultiClient( $options=[])
Get a MultiHttpClient with MediaWiki configured defaults applied.
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for request( 'POST' ), parameters have same meaning as for request()
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
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.
PSR-3 logger instance factory.
Class to handle multiple HTTP requests.
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:33
static instance()
Singleton.
Definition Profiler.php:63
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44