MediaWiki  master
MWHttpRequest.php
Go to the documentation of this file.
1 <?php
22 use Psr\Log\LoggerAwareInterface;
23 use Psr\Log\LoggerInterface;
24 use Psr\Log\NullLogger;
25 
33 abstract class MWHttpRequest implements LoggerAwareInterface {
34  public const SUPPORTS_FILE_POSTS = false;
35 
39  protected $timeout = 'default';
40 
41  protected $content;
42  protected $headersOnly = null;
43  protected $postData = null;
44  protected $proxy = null;
45  protected $noProxy = false;
46  protected $sslVerifyHost = true;
47  protected $sslVerifyCert = true;
48  protected $caInfo = null;
49  protected $method = "GET";
51  protected $reqHeaders = [];
52  protected $url;
53  protected $parsedUrl;
55  protected $callback;
56  protected $maxRedirects = 5;
57  protected $followRedirects = false;
58  protected $connectTimeout;
59 
63  protected $cookieJar;
64 
65  protected $headerList = [];
66  protected $respVersion = "0.9";
67  protected $respStatus = "200 Ok";
69  protected $respHeaders = [];
70 
72  protected $status;
73 
77  protected $profiler;
78 
82  protected $profileName;
83 
87  protected $logger;
88 
98  public function __construct(
99  $url, array $options = [], $caller = __METHOD__, Profiler $profiler = null
100  ) {
101  $this->url = wfExpandUrl( $url, PROTO_HTTP );
102  $this->parsedUrl = wfParseUrl( $this->url );
103 
104  $this->logger = $options['logger'] ?? new NullLogger();
105 
106  if ( !$this->parsedUrl || !self::isValidURI( $this->url ) ) {
107  $this->status = StatusValue::newFatal( 'http-invalid-url', $url );
108  } else {
109  $this->status = StatusValue::newGood( 100 ); // continue
110  }
111 
112  if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
113  $this->timeout = $options['timeout'];
114  } else {
115  // The timeout should always be set by HttpRequestFactory, so this
116  // should only happen if the class was directly constructed
117  wfDeprecated( __METHOD__ . ' without the timeout option', '1.35' );
118  global $wgHTTPTimeout;
119  $this->timeout = $wgHTTPTimeout;
120  }
121  if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
122  $this->connectTimeout = $options['connectTimeout'];
123  } else {
124  // The timeout should always be set by HttpRequestFactory, so this
125  // should only happen if the class was directly constructed
126  wfDeprecated( __METHOD__ . ' without the connectTimeout option', '1.35' );
127  global $wgHTTPConnectTimeout;
128  $this->connectTimeout = $wgHTTPConnectTimeout;
129  }
130  if ( isset( $options['userAgent'] ) ) {
131  $this->setUserAgent( $options['userAgent'] );
132  }
133  if ( isset( $options['username'] ) && isset( $options['password'] ) ) {
134  $this->setHeader(
135  'Authorization',
136  'Basic ' . base64_encode( $options['username'] . ':' . $options['password'] )
137  );
138  }
139  if ( isset( $options['originalRequest'] ) ) {
140  $this->setOriginalRequest( $options['originalRequest'] );
141  }
142 
143  $this->setHeader( 'X-Request-Id', WebRequest::getRequestId() );
144 
145  $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
146  "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
147 
148  foreach ( $members as $o ) {
149  if ( isset( $options[$o] ) ) {
150  // ensure that MWHttpRequest::method is always
151  // uppercased. T38137
152  if ( $o == 'method' ) {
153  $options[$o] = strtoupper( $options[$o] );
154  }
155  $this->$o = $options[$o];
156  }
157  }
158 
159  if ( $this->noProxy ) {
160  $this->proxy = ''; // noProxy takes precedence
161  }
162 
163  // Profile based on what's calling us
164  $this->profiler = $profiler;
165  $this->profileName = $caller;
166  }
167 
171  public function setLogger( LoggerInterface $logger ) {
172  $this->logger = $logger;
173  }
174 
180  public static function canMakeRequests() {
181  return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
182  }
183 
194  public static function factory( $url, array $options = null, $caller = __METHOD__ ) {
195  if ( $options === null ) {
196  $options = [];
197  }
198  return MediaWikiServices::getInstance()->getHttpRequestFactory()
199  ->create( $url, $options, $caller );
200  }
201 
207  public function getContent() {
208  return $this->content;
209  }
210 
217  public function setData( array $args ) {
218  $this->postData = $args;
219  }
220 
226  protected function proxySetup() {
228  // If proxies are disabled, clear any other proxy
229  if ( $this->noProxy ) {
230  $this->proxy = '';
231  return;
232  }
233 
234  // If there is an explicit proxy already set, use it
235  if ( $this->proxy ) {
236  return;
237  }
238 
239  // Otherwise, fallback to $wgLocalHTTPProxy for local URLs
240  // or $wgHTTPProxy for everything else
241  if ( self::isLocalURL( $this->url ) ) {
242  $this->proxy = (string)$wgLocalHTTPProxy;
243  } else {
244  $this->proxy = (string)$wgHTTPProxy;
245  }
246  }
247 
254  private static function isLocalURL( $url ) {
256 
257  if ( $wgCommandLineMode ) {
258  return false;
259  }
260 
261  // Extract host part
262  $matches = [];
263  if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
264  $host = $matches[1];
265  // Split up dotwise
266  $domainParts = explode( '.', $host );
267  // Check if this domain or any superdomain is listed as a local virtual host
268  $domainParts = array_reverse( $domainParts );
269 
270  $domain = '';
271  $countParts = count( $domainParts );
272  for ( $i = 0; $i < $countParts; $i++ ) {
273  $domainPart = $domainParts[$i];
274  if ( $i == 0 ) {
275  $domain = $domainPart;
276  } else {
277  $domain = $domainPart . '.' . $domain;
278  }
279 
280  if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
281  return true;
282  }
283  }
284  }
285 
286  return false;
287  }
288 
292  public function setUserAgent( $UA ) {
293  $this->setHeader( 'User-Agent', $UA );
294  }
295 
301  public function setHeader( $name, $value ) {
302  // I feel like I should normalize the case here...
303  $this->reqHeaders[$name] = $value;
304  }
305 
310  protected function getHeaderList() {
311  $list = [];
312 
313  if ( $this->cookieJar ) {
314  $this->reqHeaders['Cookie'] =
315  $this->cookieJar->serializeToHttpRequest(
316  $this->parsedUrl['path'],
317  $this->parsedUrl['host']
318  );
319  }
320 
321  foreach ( $this->reqHeaders as $name => $value ) {
322  $list[] = "$name: $value";
323  }
324 
325  return $list;
326  }
327 
346  public function setCallback( $callback ) {
347  $this->doSetCallback( $callback );
348  }
349 
357  protected function doSetCallback( $callback ) {
358  if ( $callback === null ) {
359  $callback = [ $this, 'read' ];
360  } elseif ( !is_callable( $callback ) ) {
361  $this->status->fatal( 'http-internal-error' );
362  throw new InvalidArgumentException( __METHOD__ . ': invalid callback' );
363  }
364  $this->callback = $callback;
365  }
366 
376  public function read( $fh, $content ) {
377  $this->content .= $content;
378  return strlen( $content );
379  }
380 
387  public function execute() {
388  throw new LogicException( 'children must override this' );
389  }
390 
391  protected function prepare() {
392  $this->content = "";
393 
394  if ( strtoupper( $this->method ) == "HEAD" ) {
395  $this->headersOnly = true;
396  }
397 
398  $this->proxySetup(); // set up any proxy as needed
399 
400  if ( !$this->callback ) {
401  $this->doSetCallback( null );
402  }
403 
404  if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
405  $http = MediaWikiServices::getInstance()->getHttpRequestFactory();
406  $this->setUserAgent( $http->getUserAgent() );
407  }
408  }
409 
415  protected function parseHeader() {
416  $lastname = "";
417 
418  // Failure without (valid) headers gets a response status of zero
419  if ( !$this->status->isOK() ) {
420  $this->respStatus = '0 Error';
421  }
422 
423  foreach ( $this->headerList as $header ) {
424  if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
425  $this->respVersion = $match[1];
426  $this->respStatus = $match[2];
427  } elseif ( preg_match( "#^[ \t]#", $header ) ) {
428  $last = count( $this->respHeaders[$lastname] ) - 1;
429  $this->respHeaders[$lastname][$last] .= "\r\n$header";
430  } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
431  $this->respHeaders[strtolower( $match[1] )][] = $match[2];
432  $lastname = strtolower( $match[1] );
433  }
434  }
435 
436  $this->parseCookies();
437  }
438 
446  protected function setStatus() {
447  if ( !$this->respHeaders ) {
448  $this->parseHeader();
449  }
450 
451  if ( (int)$this->respStatus > 0 && (int)$this->respStatus < 400 ) {
452  $this->status->setResult( true, (int)$this->respStatus );
453  } else {
454  list( $code, $message ) = explode( " ", $this->respStatus, 2 );
455  $this->status->setResult( false, (int)$this->respStatus );
456  $this->status->fatal( "http-bad-status", $code, $message );
457  }
458  }
459 
467  public function getStatus() {
468  if ( !$this->respHeaders ) {
469  $this->parseHeader();
470  }
471 
472  return (int)$this->respStatus;
473  }
474 
480  public function isRedirect() {
481  if ( !$this->respHeaders ) {
482  $this->parseHeader();
483  }
484 
485  $status = (int)$this->respStatus;
486 
487  if ( $status >= 300 && $status <= 303 ) {
488  return true;
489  }
490 
491  return false;
492  }
493 
503  public function getResponseHeaders() {
504  if ( !$this->respHeaders ) {
505  $this->parseHeader();
506  }
507 
508  return $this->respHeaders;
509  }
510 
517  public function getResponseHeader( $header ) {
518  if ( !$this->respHeaders ) {
519  $this->parseHeader();
520  }
521 
522  if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
523  $v = $this->respHeaders[strtolower( $header )];
524  return $v[count( $v ) - 1];
525  }
526 
527  return null;
528  }
529 
537  public function setCookieJar( CookieJar $jar ) {
538  $this->cookieJar = $jar;
539  }
540 
546  public function getCookieJar() {
547  if ( !$this->respHeaders ) {
548  $this->parseHeader();
549  }
550 
551  return $this->cookieJar;
552  }
553 
563  public function setCookie( $name, $value, array $attr = [] ) {
564  if ( !$this->cookieJar ) {
565  $this->cookieJar = new CookieJar;
566  }
567 
568  if ( $this->parsedUrl && !isset( $attr['domain'] ) ) {
569  $attr['domain'] = $this->parsedUrl['host'];
570  }
571 
572  $this->cookieJar->setCookie( $name, $value, $attr );
573  }
574 
578  protected function parseCookies() {
579  if ( !$this->cookieJar ) {
580  $this->cookieJar = new CookieJar;
581  }
582 
583  if ( isset( $this->respHeaders['set-cookie'] ) ) {
584  $url = parse_url( $this->getFinalUrl() );
585  foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
586  $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
587  }
588  }
589  }
590 
607  public function getFinalUrl() {
608  $headers = $this->getResponseHeaders();
609 
610  // return full url (fix for incorrect but handled relative location)
611  if ( isset( $headers['location'] ) ) {
612  $locations = $headers['location'];
613  $domain = '';
614  $foundRelativeURI = false;
615  $countLocations = count( $locations );
616 
617  for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
618  $url = parse_url( $locations[$i] );
619 
620  if ( isset( $url['host'] ) ) {
621  $domain = $url['scheme'] . '://' . $url['host'];
622  break; // found correct URI (with host)
623  } else {
624  $foundRelativeURI = true;
625  }
626  }
627 
628  if ( !$foundRelativeURI ) {
629  return $locations[$countLocations - 1];
630  }
631  if ( $domain ) {
632  return $domain . $locations[$countLocations - 1];
633  }
634  $url = parse_url( $this->url );
635  if ( isset( $url['host'] ) ) {
636  return $url['scheme'] . '://' . $url['host'] .
637  $locations[$countLocations - 1];
638  }
639  }
640 
641  return $this->url;
642  }
643 
649  public function canFollowRedirects() {
650  return true;
651  }
652 
665  public function setOriginalRequest( $originalRequest ) {
666  if ( $originalRequest instanceof WebRequest ) {
667  $originalRequest = [
668  'ip' => $originalRequest->getIP(),
669  'userAgent' => $originalRequest->getHeader( 'User-Agent' ),
670  ];
671  } elseif (
672  !is_array( $originalRequest )
673  || array_diff( [ 'ip', 'userAgent' ], array_keys( $originalRequest ) )
674  ) {
675  throw new InvalidArgumentException( __METHOD__ . ': $originalRequest must be a '
676  . "WebRequest or an array with 'ip' and 'userAgent' keys" );
677  }
678 
679  $this->reqHeaders['X-Forwarded-For'] = $originalRequest['ip'];
680  $this->reqHeaders['X-Original-User-Agent'] = $originalRequest['userAgent'];
681  }
682 
699  public static function isValidURI( $uri ) {
700  return (bool)preg_match(
701  '/^https?:\/\/[^\/\s]\S*$/D',
702  $uri
703  );
704  }
705 }
MWHttpRequest\$headerList
$headerList
Definition: MWHttpRequest.php:65
$wgLocalVirtualHosts
$wgLocalVirtualHosts
Local virtual hosts.
Definition: DefaultSettings.php:9654
MWHttpRequest\$headersOnly
$headersOnly
Definition: MWHttpRequest.php:42
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
MWHttpRequest\$logger
LoggerInterface $logger
Definition: MWHttpRequest.php:87
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
MWHttpRequest\$callback
callable $callback
Definition: MWHttpRequest.php:55
MWHttpRequest\setStatus
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
Definition: MWHttpRequest.php:446
MWHttpRequest\$respVersion
$respVersion
Definition: MWHttpRequest.php:66
MWHttpRequest\__construct
__construct( $url, array $options=[], $caller=__METHOD__, Profiler $profiler=null)
Definition: MWHttpRequest.php:98
MWHttpRequest\doSetCallback
doSetCallback( $callback)
Worker function for setting callbacks.
Definition: MWHttpRequest.php:357
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
MWHttpRequest\proxySetup
proxySetup()
Take care of setting up the proxy (do nothing if "noProxy" is set)
Definition: MWHttpRequest.php:226
MWHttpRequest\$maxRedirects
$maxRedirects
Definition: MWHttpRequest.php:56
MWHttpRequest\$sslVerifyCert
$sslVerifyCert
Definition: MWHttpRequest.php:47
MWHttpRequest\$followRedirects
$followRedirects
Definition: MWHttpRequest.php:57
MWHttpRequest\$content
$content
Definition: MWHttpRequest.php:41
MWHttpRequest\$status
StatusValue $status
Definition: MWHttpRequest.php:72
MWHttpRequest\$profiler
Profiler $profiler
Definition: MWHttpRequest.php:77
$wgHTTPConnectTimeout
float int $wgHTTPConnectTimeout
Timeout for connections done internally (in seconds).
Definition: DefaultSettings.php:9605
MWHttpRequest\getStatus
getStatus()
Get the integer value of the HTTP status code (e.g.
Definition: MWHttpRequest.php:467
MWHttpRequest\$noProxy
$noProxy
Definition: MWHttpRequest.php:45
MWHttpRequest\setCookieJar
setCookieJar(CookieJar $jar)
Tells the MWHttpRequest object to use this pre-loaded CookieJar.
Definition: MWHttpRequest.php:537
$wgHTTPProxy
$wgHTTPProxy
Proxy to use for CURL requests.
Definition: DefaultSettings.php:9639
MWHttpRequest\parseHeader
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
Definition: MWHttpRequest.php:415
MWHttpRequest\$connectTimeout
$connectTimeout
Definition: MWHttpRequest.php:58
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:776
MWHttpRequest\$postData
$postData
Definition: MWHttpRequest.php:43
MWHttpRequest\getContent
getContent()
Get the body, or content, of the response to the request.
Definition: MWHttpRequest.php:207
MWHttpRequest\SUPPORTS_FILE_POSTS
const SUPPORTS_FILE_POSTS
Definition: MWHttpRequest.php:34
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MWHttpRequest\parseCookies
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
Definition: MWHttpRequest.php:578
MWHttpRequest\$respStatus
$respStatus
Definition: MWHttpRequest.php:67
CookieJar\setCookie
setCookie( $name, $value, $attr)
Set a cookie in the cookie jar.
Definition: CookieJar.php:36
MWHttpRequest\setData
setData(array $args)
Set the parameters of the request.
Definition: MWHttpRequest.php:217
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:29
$matches
$matches
Definition: NoLocalSettings.php:24
MWHttpRequest\isRedirect
isRedirect()
Returns true if the last status code was a redirect.
Definition: MWHttpRequest.php:480
Profiler
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:36
MWHttpRequest\getCookieJar
getCookieJar()
Returns the cookie jar in use.
Definition: MWHttpRequest.php:546
MWHttpRequest\$timeout
int string $timeout
Definition: MWHttpRequest.php:39
$args
if( $line===false) $args
Definition: mcc.php:124
MWHttpRequest\isLocalURL
static isLocalURL( $url)
Check if the URL can be served by localhost.
Definition: MWHttpRequest.php:254
MWHttpRequest\$cookieJar
CookieJar $cookieJar
Definition: MWHttpRequest.php:63
$wgHTTPTimeout
float int $wgHTTPTimeout
Timeout for HTTP requests done internally, in seconds.
Definition: DefaultSettings.php:9595
MWHttpRequest\read
read( $fh, $content)
A generic callback to read the body of the response from a remote server.
Definition: MWHttpRequest.php:376
$header
$header
Definition: updateCredits.php:37
MWHttpRequest\$method
$method
Definition: MWHttpRequest.php:49
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:33
MWHttpRequest\setCallback
setCallback( $callback)
Set a read callback to accept data read from the HTTP request.
Definition: MWHttpRequest.php:346
MWHttpRequest\setHeader
setHeader( $name, $value)
Set an arbitrary header.
Definition: MWHttpRequest.php:301
MWHttpRequest\getHeaderList
getHeaderList()
Get an array of the headers.
Definition: MWHttpRequest.php:310
MWHttpRequest\$parsedUrl
$parsedUrl
Definition: MWHttpRequest.php:53
MWHttpRequest\$profileName
string $profileName
Definition: MWHttpRequest.php:82
MWHttpRequest\getResponseHeaders
getResponseHeaders()
Returns an associative array of response headers after the request has been executed.
Definition: MWHttpRequest.php:503
wfIniGetBool
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
Definition: GlobalFunctions.php:1847
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:43
$wgLocalHTTPProxy
string bool $wgLocalHTTPProxy
Proxy to use to requests to domains in $wgLocalVirtualHosts.
Definition: DefaultSettings.php:9664
MWHttpRequest\$sslVerifyHost
$sslVerifyHost
Definition: MWHttpRequest.php:46
MWHttpRequest\setCookie
setCookie( $name, $value, array $attr=[])
Sets a cookie.
Definition: MWHttpRequest.php:563
MWHttpRequest\$respHeaders
string[][] $respHeaders
Definition: MWHttpRequest.php:69
MWHttpRequest\$reqHeaders
array $reqHeaders
Definition: MWHttpRequest.php:51
CookieJar
Cookie jar to use with MWHttpRequest.
Definition: CookieJar.php:25
WebRequest\getRequestId
static getRequestId()
Get the current request ID.
Definition: WebRequest.php:333
MWHttpRequest\$caInfo
$caInfo
Definition: MWHttpRequest.php:48
MWHttpRequest\canMakeRequests
static canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
Definition: MWHttpRequest.php:180
MWHttpRequest\isValidURI
static isValidURI( $uri)
Check that the given URI is a valid one.
Definition: MWHttpRequest.php:699
MWHttpRequest\getResponseHeader
getResponseHeader( $header)
Returns the value of the given response header.
Definition: MWHttpRequest.php:517
MWHttpRequest\setUserAgent
setUserAgent( $UA)
Definition: MWHttpRequest.php:292
MWHttpRequest\execute
execute()
Take care of whatever is necessary to perform the URI request.
Definition: MWHttpRequest.php:387
MWHttpRequest\canFollowRedirects
canFollowRedirects()
Returns true if the backend can follow redirects.
Definition: MWHttpRequest.php:649
MWHttpRequest\$proxy
$proxy
Definition: MWHttpRequest.php:44
MWHttpRequest\setLogger
setLogger(LoggerInterface $logger)
Definition: MWHttpRequest.php:171
MWHttpRequest\getFinalUrl
getFinalUrl()
Returns the final URL after all redirections.
Definition: MWHttpRequest.php:607
MWHttpRequest\setOriginalRequest
setOriginalRequest( $originalRequest)
Set information about the original request.
Definition: MWHttpRequest.php:665
PROTO_HTTP
const PROTO_HTTP
Definition: Defines.php:192
MWHttpRequest\$url
$url
Definition: MWHttpRequest.php:52
MWHttpRequest\factory
static factory( $url, array $options=null, $caller=__METHOD__)
Generate a new request object.
Definition: MWHttpRequest.php:194
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:474
MWHttpRequest\prepare
prepare()
Definition: MWHttpRequest.php:391