MediaWiki  master
HttpRequestFactory.php
Go to the documentation of this file.
1 <?php
20 namespace MediaWiki\Http;
21 
22 use CurlHttpRequest;
24 use Http;
27 use MultiHttpClient;
28 use MWHttpRequest;
29 use PhpHttpRequest;
30 use Profiler;
31 use Psr\Log\LoggerInterface;
32 use RuntimeException;
33 use 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__ ) {
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 }
MultiHttpClient
Class to handle multiple HTTP requests.
Definition: MultiHttpClient.php:55
Http\$httpEngine
static $httpEngine
Definition: Http.php:30
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
MediaWiki\Http\HttpRequestFactory\canMakeRequests
canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
Definition: HttpRequestFactory.php:164
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
MediaWiki\Http\HttpRequestFactory
Factory creating MWHttpRequest objects.
Definition: HttpRequestFactory.php:38
MediaWiki\Http\HttpRequestFactory\createMultiClient
createMultiClient( $options=[])
Get a MultiHttpClient with MediaWiki configured defaults applied.
Definition: HttpRequestFactory.php:243
MediaWiki\Http
Definition: HttpRequestFactory.php:20
MW_VERSION
const MW_VERSION
The running version of MediaWiki.
Definition: Defines.php:39
CurlHttpRequest
MWHttpRequest implemented using internal curl compiled into PHP.
Definition: CurlHttpRequest.php:24
MediaWiki\Http\HttpRequestFactory\$logger
LoggerInterface $logger
Definition: HttpRequestFactory.php:42
MediaWiki\Http\HttpRequestFactory\__construct
__construct(ServiceOptions $options, LoggerInterface $logger)
Definition: HttpRequestFactory.php:51
MediaWiki\Http\HttpRequestFactory\getUserAgent
getUserAgent()
Definition: HttpRequestFactory.php:227
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:25
MediaWiki\Http\HttpRequestFactory\request
request( $method, $url, array $options=[], $caller=__METHOD__)
Perform an HTTP request.
Definition: HttpRequestFactory.php:179
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
GuzzleHttpRequest
MWHttpRequest implemented using the Guzzle library.
Definition: GuzzleHttpRequest.php:42
Profiler
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:33
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:33
wfIniGetBool
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
Definition: GlobalFunctions.php:1987
MediaWiki\Http\HttpRequestFactory\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: HttpRequestFactory.php:44
MediaWiki\Http\HttpRequestFactory\post
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for request( 'POST' ), parameters have same meaning as for request()
Definition: HttpRequestFactory.php:220
MediaWiki\Http\HttpRequestFactory\normalizeTimeout
normalizeTimeout( $parameter, $maxParameter, $default, $maxConfigured)
Given a passed parameter value, a default and a maximum, figure out the correct timeout to pass to th...
Definition: HttpRequestFactory.php:137
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:84
PhpHttpRequest
Definition: PhpHttpRequest.php:21
MediaWiki\Http\HttpRequestFactory\$options
ServiceOptions $options
Definition: HttpRequestFactory.php:40
MediaWiki\Http\HttpRequestFactory\create
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
Definition: HttpRequestFactory.php:94
Http
Various HTTP related functions.
Definition: Http.php:28
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:62