MediaWiki REL1_29
MWHttpRequest.php
Go to the documentation of this file.
1<?php
22use Psr\Log\LoggerInterface;
23use Psr\Log\LoggerAwareInterface;
24use Psr\Log\NullLogger;
25
33class 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;
50 protected $callback;
51 protected $maxRedirects = 5;
52 protected $followRedirects = false;
53 protected $connectTimeout;
54
58 protected $cookieJar;
59
60 protected $headerList = [];
61 protected $respVersion = "0.9";
62 protected $respStatus = "200 Ok";
63 protected $respHeaders = [];
64
66 protected $status;
67
71 protected $profiler;
72
76 protected $profileName;
77
81 protected $logger;
82
89 protected function __construct(
90 $url, $options = [], $caller = __METHOD__, $profiler = null
91 ) {
93
94 $this->url = wfExpandUrl( $url, PROTO_HTTP );
95 $this->parsedUrl = wfParseUrl( $this->url );
96
97 if ( isset( $options['logger'] ) ) {
98 $this->logger = $options['logger'];
99 } else {
100 $this->logger = new NullLogger();
101 }
102
103 if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
104 $this->status = StatusValue::newFatal( 'http-invalid-url', $url );
105 } else {
106 $this->status = StatusValue::newGood( 100 ); // continue
107 }
108
109 if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
110 $this->timeout = $options['timeout'];
111 } else {
112 $this->timeout = $wgHTTPTimeout;
113 }
114 if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
115 $this->connectTimeout = $options['connectTimeout'];
116 } else {
117 $this->connectTimeout = $wgHTTPConnectTimeout;
118 }
119 if ( isset( $options['userAgent'] ) ) {
120 $this->setUserAgent( $options['userAgent'] );
121 }
122 if ( isset( $options['username'] ) && isset( $options['password'] ) ) {
123 $this->setHeader(
124 'Authorization',
125 'Basic ' . base64_encode( $options['username'] . ':' . $options['password'] )
126 );
127 }
128 if ( isset( $options['originalRequest'] ) ) {
129 $this->setOriginalRequest( $options['originalRequest'] );
130 }
131
132 $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
133 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
134
135 foreach ( $members as $o ) {
136 if ( isset( $options[$o] ) ) {
137 // ensure that MWHttpRequest::method is always
138 // uppercased. T38137
139 if ( $o == 'method' ) {
140 $options[$o] = strtoupper( $options[$o] );
141 }
142 $this->$o = $options[$o];
143 }
144 }
145
146 if ( $this->noProxy ) {
147 $this->proxy = ''; // noProxy takes precedence
148 }
149
150 // Profile based on what's calling us
151 $this->profiler = $profiler;
152 $this->profileName = $caller;
153 }
154
158 public function setLogger( LoggerInterface $logger ) {
159 $this->logger = $logger;
160 }
161
167 public static function canMakeRequests() {
168 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
169 }
170
180 public static function factory( $url, $options = null, $caller = __METHOD__ ) {
181 if ( !Http::$httpEngine ) {
182 Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
183 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
184 throw new DomainException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
185 ' Http::$httpEngine is set to "curl"' );
186 }
187
188 if ( !is_array( $options ) ) {
189 $options = [];
190 }
191
192 if ( !isset( $options['logger'] ) ) {
193 $options['logger'] = LoggerFactory::getInstance( 'http' );
194 }
195
196 switch ( Http::$httpEngine ) {
197 case 'curl':
198 return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
199 case 'php':
200 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
201 throw new DomainException( __METHOD__ . ': allow_url_fopen ' .
202 'needs to be enabled for pure PHP http requests to ' .
203 'work. If possible, curl should be used instead. See ' .
204 'http://php.net/curl.'
205 );
206 }
207 return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
208 default:
209 throw new DomainException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
210 }
211 }
212
218 public function getContent() {
219 return $this->content;
220 }
221
228 public function setData( $args ) {
229 $this->postData = $args;
230 }
231
237 protected function proxySetup() {
238 // If there is an explicit proxy set and proxies are not disabled, then use it
239 if ( $this->proxy && !$this->noProxy ) {
240 return;
241 }
242
243 // Otherwise, fallback to $wgHTTPProxy if this is not a machine
244 // local URL and proxies are not disabled
245 if ( self::isLocalURL( $this->url ) || $this->noProxy ) {
246 $this->proxy = '';
247 } else {
248 $this->proxy = Http::getProxy();
249 }
250 }
251
258 private static function isLocalURL( $url ) {
260
261 if ( $wgCommandLineMode ) {
262 return false;
263 }
264
265 // Extract host part
266 $matches = [];
267 if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
268 $host = $matches[1];
269 // Split up dotwise
270 $domainParts = explode( '.', $host );
271 // Check if this domain or any superdomain is listed as a local virtual host
272 $domainParts = array_reverse( $domainParts );
273
274 $domain = '';
275 $countParts = count( $domainParts );
276 for ( $i = 0; $i < $countParts; $i++ ) {
277 $domainPart = $domainParts[$i];
278 if ( $i == 0 ) {
279 $domain = $domainPart;
280 } else {
281 $domain = $domainPart . '.' . $domain;
282 }
283
284 if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
285 return true;
286 }
287 }
288 }
289
290 return false;
291 }
292
297 public function setUserAgent( $UA ) {
298 $this->setHeader( 'User-Agent', $UA );
299 }
300
306 public function setHeader( $name, $value ) {
307 // I feel like I should normalize the case here...
308 $this->reqHeaders[$name] = $value;
309 }
310
315 protected function getHeaderList() {
316 $list = [];
317
318 if ( $this->cookieJar ) {
319 $this->reqHeaders['Cookie'] =
320 $this->cookieJar->serializeToHttpRequest(
321 $this->parsedUrl['path'],
322 $this->parsedUrl['host']
323 );
324 }
325
326 foreach ( $this->reqHeaders as $name => $value ) {
327 $list[] = "$name: $value";
328 }
329
330 return $list;
331 }
332
351 public function setCallback( $callback ) {
352 if ( is_null( $callback ) ) {
353 $callback = [ $this, 'read' ];
354 } elseif ( !is_callable( $callback ) ) {
355 throw new InvalidArgumentException( __METHOD__ . ': invalid callback' );
356 }
357 $this->callback = $callback;
358 }
359
369 public function read( $fh, $content ) {
370 $this->content .= $content;
371 return strlen( $content );
372 }
373
380 public function execute() {
381 throw new LogicException( 'children must override this' );
382 }
383
384 protected function prepare() {
385 $this->content = "";
386
387 if ( strtoupper( $this->method ) == "HEAD" ) {
388 $this->headersOnly = true;
389 }
390
391 $this->proxySetup(); // set up any proxy as needed
392
393 if ( !$this->callback ) {
394 $this->setCallback( null );
395 }
396
397 if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
398 $this->setUserAgent( Http::userAgent() );
399 }
400 }
401
407 protected function parseHeader() {
408 $lastname = "";
409
410 foreach ( $this->headerList as $header ) {
411 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
412 $this->respVersion = $match[1];
413 $this->respStatus = $match[2];
414 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
415 $last = count( $this->respHeaders[$lastname] ) - 1;
416 $this->respHeaders[$lastname][$last] .= "\r\n$header";
417 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
418 $this->respHeaders[strtolower( $match[1] )][] = $match[2];
419 $lastname = strtolower( $match[1] );
420 }
421 }
422
423 $this->parseCookies();
424 }
425
434 protected function setStatus() {
435 if ( !$this->respHeaders ) {
436 $this->parseHeader();
437 }
438
439 if ( (int)$this->respStatus > 399 ) {
440 list( $code, $message ) = explode( " ", $this->respStatus, 2 );
441 $this->status->fatal( "http-bad-status", $code, $message );
442 }
443 }
444
452 public function getStatus() {
453 if ( !$this->respHeaders ) {
454 $this->parseHeader();
455 }
456
457 return (int)$this->respStatus;
458 }
459
465 public function isRedirect() {
466 if ( !$this->respHeaders ) {
467 $this->parseHeader();
468 }
469
470 $status = (int)$this->respStatus;
471
472 if ( $status >= 300 && $status <= 303 ) {
473 return true;
474 }
475
476 return false;
477 }
478
487 public function getResponseHeaders() {
488 if ( !$this->respHeaders ) {
489 $this->parseHeader();
490 }
491
492 return $this->respHeaders;
493 }
494
501 public function getResponseHeader( $header ) {
502 if ( !$this->respHeaders ) {
503 $this->parseHeader();
504 }
505
506 if ( isset( $this->respHeaders[strtolower( $header )] ) ) {
507 $v = $this->respHeaders[strtolower( $header )];
508 return $v[count( $v ) - 1];
509 }
510
511 return null;
512 }
513
521 public function setCookieJar( $jar ) {
522 $this->cookieJar = $jar;
523 }
524
530 public function getCookieJar() {
531 if ( !$this->respHeaders ) {
532 $this->parseHeader();
533 }
534
535 return $this->cookieJar;
536 }
537
547 public function setCookie( $name, $value, $attr = [] ) {
548 if ( !$this->cookieJar ) {
549 $this->cookieJar = new CookieJar;
550 }
551
552 if ( $this->parsedUrl && !isset( $attr['domain'] ) ) {
553 $attr['domain'] = $this->parsedUrl['host'];
554 }
555
556 $this->cookieJar->setCookie( $name, $value, $attr );
557 }
558
562 protected function parseCookies() {
563 if ( !$this->cookieJar ) {
564 $this->cookieJar = new CookieJar;
565 }
566
567 if ( isset( $this->respHeaders['set-cookie'] ) ) {
568 $url = parse_url( $this->getFinalUrl() );
569 foreach ( $this->respHeaders['set-cookie'] as $cookie ) {
570 $this->cookieJar->parseCookieResponseHeader( $cookie, $url['host'] );
571 }
572 }
573 }
574
591 public function getFinalUrl() {
592 $headers = $this->getResponseHeaders();
593
594 // return full url (fix for incorrect but handled relative location)
595 if ( isset( $headers['location'] ) ) {
596 $locations = $headers['location'];
597 $domain = '';
598 $foundRelativeURI = false;
599 $countLocations = count( $locations );
600
601 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
602 $url = parse_url( $locations[$i] );
603
604 if ( isset( $url['host'] ) ) {
605 $domain = $url['scheme'] . '://' . $url['host'];
606 break; // found correct URI (with host)
607 } else {
608 $foundRelativeURI = true;
609 }
610 }
611
612 if ( !$foundRelativeURI ) {
613 return $locations[$countLocations - 1];
614 }
615 if ( $domain ) {
616 return $domain . $locations[$countLocations - 1];
617 }
618 $url = parse_url( $this->url );
619 if ( isset( $url['host'] ) ) {
620 return $url['scheme'] . '://' . $url['host'] .
621 $locations[$countLocations - 1];
622 }
623 }
624
625 return $this->url;
626 }
627
633 public function canFollowRedirects() {
634 return true;
635 }
636
649 public function setOriginalRequest( $originalRequest ) {
650 if ( $originalRequest instanceof WebRequest ) {
651 $originalRequest = [
652 'ip' => $originalRequest->getIP(),
653 'userAgent' => $originalRequest->getHeader( 'User-Agent' ),
654 ];
655 } elseif (
656 !is_array( $originalRequest )
657 || array_diff( [ 'ip', 'userAgent' ], array_keys( $originalRequest ) )
658 ) {
659 throw new InvalidArgumentException( __METHOD__ . ': $originalRequest must be a '
660 . "WebRequest or an array with 'ip' and 'userAgent' keys" );
661 }
662
663 $this->reqHeaders['X-Forwarded-For'] = $originalRequest['ip'];
664 $this->reqHeaders['X-Original-User-Agent'] = $originalRequest['userAgent'];
665 }
666}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgHTTPTimeout
Timeout for HTTP requests done internally, in seconds.
$wgHTTPConnectTimeout
Timeout for connections done internally (in seconds) Only works for curl.
$wgLocalVirtualHosts
Local virtual hosts.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
global $wgCommandLineMode
Definition Setup.php:503
if( $line===false) $args
Definition cdb.php:63
Cookie jar to use with MWHttpRequest.
Definition CookieJar.php:25
setCookie( $name, $value, $attr)
Set a cookie in the cookie jar.
Definition CookieJar.php:36
MWHttpRequest implemented using internal curl compiled into PHP.
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
getContent()
Get the body, or content, of the response to the request.
setLogger(LoggerInterface $logger)
getResponseHeaders()
Returns an associative array of response headers after the request has been executed.
static isLocalURL( $url)
Check if the URL can be served by localhost.
setCookie( $name, $value, $attr=[])
Sets a cookie.
setHeader( $name, $value)
Set an arbitrary header.
getCookieJar()
Returns the cookie jar in use.
setOriginalRequest( $originalRequest)
Set information about the original request.
__construct( $url, $options=[], $caller=__METHOD__, $profiler=null)
isRedirect()
Returns true if the last status code was a redirect.
read( $fh, $content)
A generic callback to read the body of the response from a remote server.
getFinalUrl()
Returns the final URL after all redirections.
setStatus()
Sets HTTPRequest status member to a fatal value with the error message if the returned integer value ...
setUserAgent( $UA)
Set the user agent.
parseHeader()
Parses the headers, including the HTTP status code and any Set-Cookie headers.
static factory( $url, $options=null, $caller=__METHOD__)
Generate a new request object.
canFollowRedirects()
Returns true if the backend can follow redirects.
setCallback( $callback)
Set a read callback to accept data read from the HTTP request.
static canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
getStatus()
Get the integer value of the HTTP status code (e.g.
StatusValue $status
const SUPPORTS_FILE_POSTS
CookieJar $cookieJar
execute()
Take care of whatever is necessary to perform the URI request.
getResponseHeader( $header)
Returns the value of the given response header.
proxySetup()
Take care of setting up the proxy (do nothing if "noProxy" is set)
parseCookies()
Parse the cookies in the response headers and store them in the cookie jar.
setCookieJar( $jar)
Tells the MWHttpRequest object to use this pre-loaded CookieJar.
getHeaderList()
Get an array of the headers.
setData( $args)
Set the parameters of the request.
callable $callback
Profiler $profiler
PSR-3 logger instance factory.
Profiler base class that defines the interface and some trivial functionality.
Definition Profiler.php:33
static instance()
Singleton.
Definition Profiler.php:62
Generic operation result class Has warning/error list, boolean status and arbitrary value.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
per default it will return the text for text based content
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
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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
const PROTO_HTTP
Definition Defines.php:217
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1102
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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 $content
Definition hooks.txt:1100
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:865
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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 $status
Definition hooks.txt:1049
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:37
$last
$header