MediaWiki  1.28.0
MWHttpRequest.php
Go to the documentation of this file.
1 <?php
25 
33 class MWHttpRequest implements LoggerAwareInterface {
34  const SUPPORTS_FILE_POSTS = false;
35 
36  protected $content;
37  protected $timeout = 'default';
38  protected $headersOnly = null;
39  protected $postData = null;
40  protected $proxy = null;
41  protected $noProxy = false;
42  protected $sslVerifyHost = true;
43  protected $sslVerifyCert = true;
44  protected $caInfo = null;
45  protected $method = "GET";
46  protected $reqHeaders = [];
47  protected $url;
48  protected $parsedUrl;
49  protected $callback;
50  protected $maxRedirects = 5;
51  protected $followRedirects = false;
52 
56  protected $cookieJar;
57 
58  protected $headerList = [];
59  protected $respVersion = "0.9";
60  protected $respStatus = "200 Ok";
61  protected $respHeaders = [];
62 
63  public $status;
64 
68  protected $profiler;
69 
73  protected $profileName;
74 
78  protected $logger;
79 
86  protected function __construct(
87  $url, $options = [], $caller = __METHOD__, $profiler = null
88  ) {
89  global $wgHTTPTimeout, $wgHTTPConnectTimeout;
90 
91  $this->url = wfExpandUrl( $url, PROTO_HTTP );
92  $this->parsedUrl = wfParseUrl( $this->url );
93 
94  if ( isset( $options['logger'] ) ) {
95  $this->logger = $options['logger'];
96  } else {
97  $this->logger = new NullLogger();
98  }
99 
100  if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
101  $this->status = Status::newFatal( 'http-invalid-url', $url );
102  } else {
103  $this->status = Status::newGood( 100 ); // continue
104  }
105 
106  if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
107  $this->timeout = $options['timeout'];
108  } else {
109  $this->timeout = $wgHTTPTimeout;
110  }
111  if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
112  $this->connectTimeout = $options['connectTimeout'];
113  } else {
114  $this->connectTimeout = $wgHTTPConnectTimeout;
115  }
116  if ( isset( $options['userAgent'] ) ) {
117  $this->setUserAgent( $options['userAgent'] );
118  }
119 
120  $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
121  "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
122 
123  foreach ( $members as $o ) {
124  if ( isset( $options[$o] ) ) {
125  // ensure that MWHttpRequest::method is always
126  // uppercased. Bug 36137
127  if ( $o == 'method' ) {
128  $options[$o] = strtoupper( $options[$o] );
129  }
130  $this->$o = $options[$o];
131  }
132  }
133 
134  if ( $this->noProxy ) {
135  $this->proxy = ''; // noProxy takes precedence
136  }
137 
138  // Profile based on what's calling us
139  $this->profiler = $profiler;
140  $this->profileName = $caller;
141  }
142 
146  public function setLogger( LoggerInterface $logger ) {
147  $this->logger = $logger;
148  }
149 
155  public static function canMakeRequests() {
156  return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
157  }
158 
168  public static function factory( $url, $options = null, $caller = __METHOD__ ) {
169  if ( !Http::$httpEngine ) {
170  Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
171  } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
172  throw new MWException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
173  ' Http::$httpEngine is set to "curl"' );
174  }
175 
176  if ( !is_array( $options ) ) {
177  $options = [];
178  }
179 
180  if ( !isset( $options['logger'] ) ) {
181  $options['logger'] = LoggerFactory::getInstance( 'http' );
182  }
183 
184  switch ( Http::$httpEngine ) {
185  case 'curl':
186  return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
187  case 'php':
188  if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
189  throw new MWException( __METHOD__ . ': allow_url_fopen ' .
190  'needs to be enabled for pure PHP http requests to ' .
191  'work. If possible, curl should be used instead. See ' .
192  'http://php.net/curl.'
193  );
194  }
195  return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
196  default:
197  throw new MWException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
198  }
199  }
200 
206  public function getContent() {
207  return $this->content;
208  }
209 
216  public function setData( $args ) {
217  $this->postData = $args;
218  }
219 
225  public function proxySetup() {
226  // If there is an explicit proxy set and proxies are not disabled, then use it
227  if ( $this->proxy && !$this->noProxy ) {
228  return;
229  }
230 
231  // Otherwise, fallback to $wgHTTPProxy if this is not a machine
232  // local URL and proxies are not disabled
233  if ( self::isLocalURL( $this->url ) || $this->noProxy ) {
234  $this->proxy = '';
235  } else {
236  $this->proxy = Http::getProxy();
237  }
238  }
239 
246  private static function isLocalURL( $url ) {
247  global $wgCommandLineMode, $wgLocalVirtualHosts;
248 
249  if ( $wgCommandLineMode ) {
250  return false;
251  }
252 
253  // Extract host part
254  $matches = [];
255  if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
256  $host = $matches[1];
257  // Split up dotwise
258  $domainParts = explode( '.', $host );
259  // Check if this domain or any superdomain is listed as a local virtual host
260  $domainParts = array_reverse( $domainParts );
261 
262  $domain = '';
263  $countParts = count( $domainParts );
264  for ( $i = 0; $i < $countParts; $i++ ) {
265  $domainPart = $domainParts[$i];
266  if ( $i == 0 ) {
267  $domain = $domainPart;
268  } else {
269  $domain = $domainPart . '.' . $domain;
270  }
271 
272  if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
273  return true;
274  }
275  }
276  }
277 
278  return false;
279  }
280 
285  public function setUserAgent( $UA ) {
286  $this->setHeader( 'User-Agent', $UA );
287  }
288 
294  public function setHeader( $name, $value ) {
295  // I feel like I should normalize the case here...
296  $this->reqHeaders[$name] = $value;
297  }
298 
303  public function getHeaderList() {
304  $list = [];
305 
306  if ( $this->cookieJar ) {
307  $this->reqHeaders['Cookie'] =
308  $this->cookieJar->serializeToHttpRequest(
309  $this->parsedUrl['path'],
310  $this->parsedUrl['host']
311  );
312  }
313 
314  foreach ( $this->reqHeaders as $name => $value ) {
315  $list[] = "$name: $value";
316  }
317 
318  return $list;
319  }
320 
339  public function setCallback( $callback ) {
340  if ( !is_callable( $callback ) ) {
341  throw new MWException( 'Invalid MwHttpRequest callback' );
342  }
343  $this->callback = $callback;
344  }
345 
354  public function read( $fh, $content ) {
355  $this->content .= $content;
356  return strlen( $content );
357  }
358 
364  public function execute() {
365 
366  $this->content = "";
367 
368  if ( strtoupper( $this->method ) == "HEAD" ) {
369  $this->headersOnly = true;
370  }
371 
372  $this->proxySetup(); // set up any proxy as needed
373 
374  if ( !$this->callback ) {
375  $this->setCallback( [ $this, 'read' ] );
376  }
377 
378  if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
379  $this->setUserAgent( Http::userAgent() );
380  }
381 
382  }
383 
389  protected function parseHeader() {
390 
391  $lastname = "";
392 
393  foreach ( $this->headerList as $header ) {
394  if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
395  $this->respVersion = $match[1];
396  $this->respStatus = $match[2];
397  } elseif ( preg_match( "#^[ \t]#", $header ) ) {
398  $last = count( $this->respHeaders[$lastname] ) - 1;
399  $this->respHeaders[$lastname][$last] .= "\r\n$header";
400  } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
401  $this->respHeaders[strtolower( $match[1] )][] = $match[2];
402  $lastname = strtolower( $match[1] );
403  }
404  }
405 
406  $this->parseCookies();
407 
408  }
409 
418  protected function setStatus() {
419  if ( !$this->respHeaders ) {
420  $this->parseHeader();
421  }
422 
423  if ( (int)$this->respStatus > 399 ) {
424  list( $code, $message ) = explode( " ", $this->respStatus, 2 );
425  $this->status->fatal( "http-bad-status", $code, $message );
426  }
427  }
428 
436  public function getStatus() {
437  if ( !$this->respHeaders ) {
438  $this->parseHeader();
439  }
440 
441  return (int)$this->respStatus;
442  }
443 
449  public function isRedirect() {
450  if ( !$this->respHeaders ) {
451  $this->parseHeader();
452  }
453 
454  $status = (int)$this->respStatus;
455 
456  if ( $status >= 300 && $status <= 303 ) {
457  return true;
458  }
459 
460  return false;
461  }
462 
471  public function getResponseHeaders() {
472  if ( !$this->respHeaders ) {
473  $this->parseHeader();
474  }
475 
476  return $this->respHeaders;
477  }
478 
485  public function getResponseHeader( $header ) {
486  if ( !$this->respHeaders ) {
487  $this->parseHeader();
488  }
489 
490  if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
491  $v = $this->respHeaders[strtolower( $header )];
492  return $v[count( $v ) - 1];
493  }
494 
495  return null;
496  }
497 
503  public function setCookieJar( $jar ) {
504  $this->cookieJar = $jar;
505  }
506 
512  public function getCookieJar() {
513  if ( !$this->respHeaders ) {
514  $this->parseHeader();
515  }
516 
517  return $this->cookieJar;
518  }
519 
529  public function setCookie( $name, $value = null, $attr = null ) {
530  if ( !$this->cookieJar ) {
531  $this->cookieJar = new CookieJar;
532  }
533 
534  $this->cookieJar->setCookie( $name, $value, $attr );
535  }
536 
540  protected function parseCookies() {
541 
542  if ( !$this->cookieJar ) {
543  $this->cookieJar = new CookieJar;
544  }
545 
546  if ( isset( $this->respHeaders['set-cookie'] ) ) {
547  $url = parse_url( $this->getFinalUrl() );
548  foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
549  $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
550  }
551  }
552 
553  }
554 
571  public function getFinalUrl() {
572  $headers = $this->getResponseHeaders();
573 
574  // return full url (fix for incorrect but handled relative location)
575  if ( isset( $headers['location'] ) ) {
576  $locations = $headers['location'];
577  $domain = '';
578  $foundRelativeURI = false;
579  $countLocations = count( $locations );
580 
581  for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
582  $url = parse_url( $locations[$i] );
583 
584  if ( isset( $url['host'] ) ) {
585  $domain = $url['scheme'] . '://' . $url['host'];
586  break; // found correct URI (with host)
587  } else {
588  $foundRelativeURI = true;
589  }
590  }
591 
592  if ( $foundRelativeURI ) {
593  if ( $domain ) {
594  return $domain . $locations[$countLocations - 1];
595  } else {
596  $url = parse_url( $this->url );
597  if ( isset( $url['host'] ) ) {
598  return $url['scheme'] . '://' . $url['host'] .
599  $locations[$countLocations - 1];
600  }
601  }
602  } else {
603  return $locations[$countLocations - 1];
604  }
605  }
606 
607  return $this->url;
608  }
609 
615  public function canFollowRedirects() {
616  return true;
617  }
618 }
CookieJar $cookieJar
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
proxySetup()
Take care of setting up the proxy (do nothing if "noProxy" is set)
setCookie($name, $value=null, $attr=null)
Sets a cookie.
static getProxy()
Gets the relevant proxy from $wgHTTPProxy.
Definition: Http.php:154
execute()
Take care of whatever is necessary to perform the URI request.
per default it will return the text for text based content
canFollowRedirects()
Returns true if the backend can follow redirects.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
setHeader($name, $value)
Set an arbitrary header.
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
static instance()
Singleton.
Definition: Profiler.php:61
setCookieJar($jar)
Tells the MWHttpRequest object to use this pre-loaded CookieJar.
isRedirect()
Returns true if the last status code was a redirect.
$value
static isLocalURL($url)
Check if the URL can be served by localhost.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfExpandUrl($url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
static canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen() ...
MWHttpRequest implemented using internal curl compiled into PHP.
static userAgent()
A standard user-agent we can use for external requests.
Definition: Http.php:125
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
if($line===false) $args
Definition: cdb.php:64
$last
global $wgCommandLineMode
Definition: Setup.php:495
getCookieJar()
Returns the cookie jar in use.
getHeaderList()
Get an array of the headers.
static $httpEngine
Definition: Http.php:28
getFinalUrl()
Returns the final URL after all redirections.
Profiler $profiler
wfIniGetBool($setting)
Safety wrapper around ini_get() for boolean settings.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
setLogger(LoggerInterface $logger)
__construct($url, $options=[], $caller=__METHOD__, $profiler=null)
const SUPPORTS_FILE_POSTS
read($fh, $content)
A generic callback to read the body of the response from a remote server.
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
$header
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
setCallback($callback)
Set a read callback to accept data read from the HTTP request.
getResponseHeaders()
Returns an associative array of response headers after the request has been executed.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:802
const PROTO_HTTP
Definition: Defines.php:223
getStatus()
Get the integer value of the HTTP status code (e.g.
string $profileName
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
setUserAgent($UA)
Set the user agent.
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method.MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances.The"Spi"in MediaWiki\Logger\Spi stands for"service provider interface".An SPI is an API intended to be implemented or extended by a third party.This software design pattern is intended to enable framework extension and replaceable components.It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki.The service provider interface allows the backend logging library to be implemented in multiple ways.The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime.This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance.Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
static factory($url, $options=null, $caller=__METHOD__)
Generate a new request object.
getContent()
Get the body, or content, of the response to the request.
wfParseUrl($url)
parse_url() work-alike, but non-broken.
setData($args)
Set the parameters of the request.
setCookie($name, $value, $attr)
Set a cookie in the cookie jar.
Definition: CookieJar.php:32
static isValidURI($uri)
Checks that the given URI is a valid one.
Definition: Http.php:142
getResponseHeader($header)
Returns the value of the given response header.
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300