3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\MainConfigNames
;
22 use MediaWiki\MediaWikiServices
;
23 use MediaWiki\Request\WebRequest
;
24 use MediaWiki\Status\Status
;
25 use MediaWiki\Utils\UrlUtils
;
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
28 use Psr\Log\NullLogger
;
29 use Wikimedia\Http\TelemetryHeadersInterface
;
32 * This wrapper class will call out to curl (if available) or fallback
33 * to regular PHP if necessary for handling internal HTTP requests.
35 * Renamed from HttpRequest to MWHttpRequest to avoid conflict with
36 * PHP's HTTP extension.
38 abstract class MWHttpRequest
implements LoggerAwareInterface
{
39 public const SUPPORTS_FILE_POSTS
= false;
44 protected $timeout = 'default';
46 /** @var string|null */
49 protected $headersOnly = null;
50 /** @var array|null */
51 protected $postData = null;
52 /** @var string|null */
53 protected $proxy = null;
55 protected $noProxy = false;
57 protected $sslVerifyHost = true;
59 protected $sslVerifyCert = true;
60 /** @var string|null */
61 protected $caInfo = null;
63 protected $method = "GET";
65 protected $reqHeaders = [];
68 /** @var array|false */
73 protected $maxRedirects = 5;
75 protected $followRedirects = false;
77 protected $connectTimeout;
85 protected $headerList = [];
87 protected $respVersion = "0.9";
89 protected $respStatus = "200 Ok";
90 /** @var string[][] */
91 protected $respHeaders = [];
93 /** @var StatusValue */
104 protected $profileName;
107 * @var LoggerInterface
111 private UrlUtils
$urlUtils;
114 * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
115 * @param array $options extra params to pass (see HttpRequestFactory::create())
116 * @phpcs:ignore Generic.Files.LineLength
117 * @phan-param array{timeout?:int|string,connectTimeout?:int|string,postData?:array,proxy?:string,noProxy?:bool,sslVerifyHost?:bool,sslVerifyCert?:bool,caInfo?:string,maxRedirects?:int,followRedirects?:bool,userAgent?:string,logger?:LoggerInterface,username?:string,password?:string,originalRequest?:WebRequest|array{ip:string,userAgent:string},method?:string} $options
118 * @param string $caller The method making this request, for profiling @phan-mandatory-param
119 * @param Profiler|null $profiler An instance of the profiler for profiling, or null
122 public function __construct(
123 $url, array $options, $caller = __METHOD__
, ?Profiler
$profiler = null
125 $this->urlUtils
= MediaWikiServices
::getInstance()->getUrlUtils();
126 if ( !array_key_exists( 'timeout', $options )
127 ||
!array_key_exists( 'connectTimeout', $options ) ) {
128 throw new InvalidArgumentException( "timeout and connectionTimeout options are required" );
130 $this->url
= $this->urlUtils
->expand( $url, PROTO_HTTP
) ??
false;
131 $this->parsedUrl
= $this->urlUtils
->parse( (string)$this->url
) ??
false;
133 $this->logger
= $options['logger'] ??
new NullLogger();
134 $this->timeout
= $options['timeout'];
135 $this->connectTimeout
= $options['connectTimeout'];
137 if ( !$this->parsedUrl ||
!self
::isValidURI( $this->url
) ) {
138 $this->status
= StatusValue
::newFatal( 'http-invalid-url', $url );
140 $this->status
= StatusValue
::newGood( 100 ); // continue
143 if ( isset( $options['userAgent'] ) ) {
144 $this->setUserAgent( $options['userAgent'] );
146 if ( isset( $options['username'] ) && isset( $options['password'] ) ) {
149 'Basic ' . base64_encode( $options['username'] . ':' . $options['password'] )
152 if ( isset( $options['originalRequest'] ) ) {
153 $this->setOriginalRequest( $options['originalRequest'] );
156 $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
157 "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
159 foreach ( $members as $o ) {
160 if ( isset( $options[$o] ) ) {
161 // ensure that MWHttpRequest::method is always
162 // uppercased. T38137
163 if ( $o == 'method' ) {
164 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
165 $options[$o] = strtoupper( $options[$o] );
167 $this->$o = $options[$o];
171 if ( $this->noProxy
) {
172 $this->proxy
= ''; // noProxy takes precedence
175 // Profile based on what's calling us
176 $this->profiler
= $profiler;
177 $this->profileName
= $caller;
180 public function setLogger( LoggerInterface
$logger ) {
181 $this->logger
= $logger;
185 * Simple function to test if we can make any sort of requests at all, using
189 public static function canMakeRequests() {
190 return function_exists( 'curl_init' ) ||
wfIniGetBool( 'allow_url_fopen' );
194 * Get the body, or content, of the response to the request
198 public function getContent() {
199 return $this->content
;
203 * Set the parameters of the request
206 * @todo overload the args param
208 public function setData( array $args ) {
209 $this->postData
= $args;
213 * Add Telemetry information to the request
215 * @param TelemetryHeadersInterface $telemetry
218 public function addTelemetry( TelemetryHeadersInterface
$telemetry ): void
{
219 foreach ( $telemetry->getRequestHeaders() as $header => $value ) {
220 $this->setHeader( $header, $value );
225 * Take care of setting up the proxy (do nothing if "noProxy" is set)
229 protected function proxySetup() {
230 $httpProxy = MediaWikiServices
::getInstance()->getMainConfig()->get(
231 MainConfigNames
::HTTPProxy
);
232 $localHTTPProxy = MediaWikiServices
::getInstance()->getMainConfig()->get(
233 MainConfigNames
::LocalHTTPProxy
);
234 // If proxies are disabled, clear any other proxy
235 if ( $this->noProxy
) {
240 // If there is an explicit proxy already set, use it
241 if ( $this->proxy
) {
245 // Otherwise, fallback to $wgLocalHTTPProxy for local URLs
246 // or $wgHTTPProxy for everything else
247 if ( self
::isLocalURL( $this->url
) ) {
248 if ( $localHTTPProxy !== false ) {
249 $this->setReverseProxy( $localHTTPProxy );
252 $this->proxy
= (string)$httpProxy;
257 * Enable use of a reverse proxy in which the hostname is
258 * passed as a "Host" header, and the request is sent to the
259 * proxy's host:port instead.
261 * Note that any custom port in the request URL will be lost
262 * and cookies and redirects may not work properly.
264 * @param string $proxy URL of proxy
266 protected function setReverseProxy( string $proxy ) {
267 $parsedProxy = $this->urlUtils
->parse( $proxy );
268 if ( $parsedProxy === null ) {
269 throw new InvalidArgumentException( "Invalid reverseProxy configured: $proxy" );
271 // Set the current host in the Host header
272 $this->setHeader( 'Host', $this->parsedUrl
['host'] );
273 // Replace scheme, host and port in the request
274 $this->parsedUrl
['scheme'] = $parsedProxy['scheme'];
275 $this->parsedUrl
['host'] = $parsedProxy['host'];
276 if ( isset( $parsedProxy['port'] ) ) {
277 $this->parsedUrl
['port'] = $parsedProxy['port'];
279 unset( $this->parsedUrl
['port'] );
281 $this->url
= UrlUtils
::assemble( $this->parsedUrl
);
282 // Mark that we're already using a proxy
283 $this->noProxy
= true;
287 * Check if the URL can be served by a local endpoint
289 * @param string $url Full url to check
292 private static function isLocalURL( $url ) {
293 $localVirtualHosts = MediaWikiServices
::getInstance()->getMainConfig()->get(
294 MainConfigNames
::LocalVirtualHosts
);
298 if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
301 $domainParts = explode( '.', $host );
302 // Check if this domain or any superdomain is listed as a local virtual host
303 $domainParts = array_reverse( $domainParts );
306 $countParts = count( $domainParts );
307 for ( $i = 0; $i < $countParts; $i++
) {
308 $domainPart = $domainParts[$i];
310 $domain = $domainPart;
312 $domain = $domainPart . '.' . $domain;
315 if ( in_array( $domain, $localVirtualHosts ) ) {
327 public function setUserAgent( $UA ) {
328 $this->setHeader( 'User-Agent', $UA );
332 * Set an arbitrary header
333 * @param string $name
334 * @param string $value
336 public function setHeader( $name, $value ) {
337 // I feel like I should normalize the case here...
338 $this->reqHeaders
[$name] = $value;
342 * Get an array of the headers
345 protected function getHeaderList() {
348 if ( $this->cookieJar
) {
349 $this->reqHeaders
['Cookie'] =
350 $this->cookieJar
->serializeToHttpRequest(
351 $this->parsedUrl
['path'],
352 $this->parsedUrl
['host']
356 foreach ( $this->reqHeaders
as $name => $value ) {
357 $list[] = "$name: $value";
364 * Set a read callback to accept data read from the HTTP request.
365 * By default, data is appended to an internal buffer which can be
366 * retrieved through $req->getContent().
368 * To handle data as it comes in -- especially for large files that
369 * would not fit in memory -- you can instead set your own callback,
370 * in the form function($resource, $buffer) where the first parameter
371 * is the low-level resource being read (implementation specific),
372 * and the second parameter is the data buffer.
374 * You MUST return the number of bytes handled in the buffer; if fewer
375 * bytes are reported handled than were passed to you, the HTTP fetch
378 * @param callable|null $callback
379 * @throws InvalidArgumentException
381 public function setCallback( $callback ) {
382 $this->doSetCallback( $callback );
386 * Worker function for setting callbacks. Calls can originate both internally and externally
387 * via setCallback). Defaults to the internal read callback if $callback is null.
389 * @param callable|null $callback
390 * @throws InvalidArgumentException
392 protected function doSetCallback( $callback ) {
393 if ( $callback === null ) {
394 $callback = [ $this, 'read' ];
395 } elseif ( !is_callable( $callback ) ) {
396 $this->status
->fatal( 'http-internal-error' );
397 throw new InvalidArgumentException( __METHOD__
. ': invalid callback' );
399 $this->callback
= $callback;
403 * A generic callback to read the body of the response from a remote
406 * @param resource $fh
407 * @param string $content
411 public function read( $fh, $content ) {
412 $this->content
.= $content;
413 return strlen( $content );
417 * Take care of whatever is necessary to perform the URI request.
420 * @note currently returns Status for B/C
422 public function execute() {
423 throw new LogicException( 'children must override this' );
426 protected function prepare() {
429 if ( strtoupper( $this->method
) == "HEAD" ) {
430 $this->headersOnly
= true;
433 $this->proxySetup(); // set up any proxy as needed
435 if ( !$this->callback
) {
436 $this->doSetCallback( null );
439 if ( !isset( $this->reqHeaders
['User-Agent'] ) ) {
440 $http = MediaWikiServices
::getInstance()->getHttpRequestFactory();
441 $this->setUserAgent( $http->getUserAgent() );
446 * Parses the headers, including the HTTP status code and any
447 * Set-Cookie headers. This function expects the headers to be
448 * found in an array in the member variable headerList.
450 protected function parseHeader() {
453 // Failure without (valid) headers gets a response status of zero
454 if ( !$this->status
->isOK() ) {
455 $this->respStatus
= '0 Error';
458 foreach ( $this->headerList
as $header ) {
459 if ( preg_match( "#^HTTP/([0-9.]+) (.*)#", $header, $match ) ) {
460 $this->respVersion
= $match[1];
461 $this->respStatus
= $match[2];
462 } elseif ( preg_match( "#^[ \t]#", $header ) ) {
463 $last = count( $this->respHeaders
[$lastname] ) - 1;
464 $this->respHeaders
[$lastname][$last] .= "\r\n$header";
465 } elseif ( preg_match( "#^([^:]*):[\t ]*(.*)#", $header, $match ) ) {
466 $this->respHeaders
[strtolower( $match[1] )][] = $match[2];
467 $lastname = strtolower( $match[1] );
471 $this->parseCookies();
475 * Sets HTTPRequest status member to a fatal value with the error
476 * message if the returned integer value of the status code was
477 * not successful (1-299) or a redirect (300-399).
478 * See RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
479 * for a list of status codes.
481 protected function setStatus() {
482 if ( !$this->respHeaders
) {
483 $this->parseHeader();
486 if ( (int)$this->respStatus
> 0 && (int)$this->respStatus
< 400 ) {
487 $this->status
->setResult( true, (int)$this->respStatus
);
489 [ $code, $message ] = explode( " ", $this->respStatus
, 2 );
490 $this->status
->setResult( false, (int)$this->respStatus
);
491 $this->status
->fatal( "http-bad-status", $code, $message );
496 * Get the integer value of the HTTP status code (e.g. 200 for "200 Ok")
497 * (see RFC2616, section 10, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
498 * for a list of status codes.)
502 public function getStatus() {
503 if ( !$this->respHeaders
) {
504 $this->parseHeader();
507 return (int)$this->respStatus
;
511 * Returns true if the last status code was a redirect.
515 public function isRedirect() {
516 if ( !$this->respHeaders
) {
517 $this->parseHeader();
520 $status = (int)$this->respStatus
;
522 if ( $status >= 300 && $status <= 303 ) {
530 * Returns an associative array of response headers after the
531 * request has been executed. Because some headers
532 * (e.g. Set-Cookie) can appear more than once the, each value of
533 * the associative array is an array of the values given.
534 * Header names are always in lowercase.
538 public function getResponseHeaders() {
539 if ( !$this->respHeaders
) {
540 $this->parseHeader();
543 return $this->respHeaders
;
547 * Returns the value of the given response header.
549 * @param string $header case-insensitive
550 * @return string|null
552 public function getResponseHeader( $header ) {
553 if ( !$this->respHeaders
) {
554 $this->parseHeader();
557 if ( isset( $this->respHeaders
[strtolower( $header )] ) ) {
558 $v = $this->respHeaders
[strtolower( $header )];
559 return $v[count( $v ) - 1];
566 * Tells the MWHttpRequest object to use this pre-loaded CookieJar.
568 * To read response cookies from the jar, getCookieJar must be called first.
570 public function setCookieJar( CookieJar
$jar ) {
571 $this->cookieJar
= $jar;
575 * Returns the cookie jar in use.
579 public function getCookieJar() {
580 if ( !$this->respHeaders
) {
581 $this->parseHeader();
584 return $this->cookieJar
;
588 * Sets a cookie. Used before a request to set up any individual
589 * cookies. Used internally after a request to parse the
590 * Set-Cookie headers.
592 * @param string $name
593 * @param string $value
596 public function setCookie( $name, $value, array $attr = [] ) {
597 if ( !$this->cookieJar
) {
598 $this->cookieJar
= new CookieJar
;
601 if ( $this->parsedUrl
&& !isset( $attr['domain'] ) ) {
602 $attr['domain'] = $this->parsedUrl
['host'];
605 $this->cookieJar
->setCookie( $name, $value, $attr );
609 * Parse the cookies in the response headers and store them in the cookie jar.
611 protected function parseCookies() {
612 if ( !$this->cookieJar
) {
613 $this->cookieJar
= new CookieJar
;
616 if ( isset( $this->respHeaders
['set-cookie'] ) ) {
617 $url = parse_url( $this->getFinalUrl() );
618 if ( !isset( $url['host'] ) ) {
619 $this->status
->fatal( 'http-invalid-url', $this->getFinalUrl() );
621 foreach ( $this->respHeaders
['set-cookie'] as $cookie ) {
622 $this->cookieJar
->parseCookieResponseHeader( $cookie, $url['host'] );
629 * Returns the final URL after all redirections.
631 * Relative values of the "Location" header are incorrect as
632 * stated in RFC, however they do happen and modern browsers
633 * support them. This function loops backwards through all
634 * locations in order to build the proper absolute URI - Marooned
637 * Note that the multiple Location: headers are an artifact of
638 * CURL -- they shouldn't actually get returned this way. Rewrite
639 * this when T31232 is taken care of (high-level redirect
644 public function getFinalUrl() {
645 $headers = $this->getResponseHeaders();
647 // return full url (fix for incorrect but handled relative location)
648 if ( isset( $headers['location'] ) ) {
649 $locations = $headers['location'];
651 $foundRelativeURI = false;
652 $countLocations = count( $locations );
654 for ( $i = $countLocations - 1; $i >= 0; $i-- ) {
655 $url = parse_url( $locations[$i] );
657 if ( isset( $url['scheme'] ) && isset( $url['host'] ) ) {
658 $domain = $url['scheme'] . '://' . $url['host'];
659 break; // found correct URI (with host)
661 $foundRelativeURI = true;
665 if ( !$foundRelativeURI ) {
666 return $locations[$countLocations - 1];
669 return $domain . $locations[$countLocations - 1];
671 $url = parse_url( $this->url
);
672 if ( isset( $url['scheme'] ) && isset( $url['host'] ) ) {
673 return $url['scheme'] . '://' . $url['host'] .
674 $locations[$countLocations - 1];
682 * Returns true if the backend can follow redirects. Overridden by the
686 public function canFollowRedirects() {
691 * Set information about the original request. This can be useful for
692 * endpoints/API modules which act as a proxy for some service, and
693 * throttling etc. needs to happen in that service.
694 * Calling this will result in the X-Forwarded-For and X-Original-User-Agent
696 * @param WebRequest|array $originalRequest When in array form, it's
697 * expected to have the keys 'ip' and 'userAgent'.
698 * @note IP/user agent is personally identifiable information, and should
699 * only be set when the privacy policy of the request target is
700 * compatible with that of the MediaWiki installation.
702 public function setOriginalRequest( $originalRequest ) {
703 if ( $originalRequest instanceof WebRequest
) {
705 'ip' => $originalRequest->getIP(),
706 'userAgent' => $originalRequest->getHeader( 'User-Agent' ),
709 !is_array( $originalRequest )
710 ||
array_diff( [ 'ip', 'userAgent' ], array_keys( $originalRequest ) )
712 throw new InvalidArgumentException( __METHOD__
. ': $originalRequest must be a '
713 . "WebRequest or an array with 'ip' and 'userAgent' keys" );
716 $this->reqHeaders
['X-Forwarded-For'] = $originalRequest['ip'];
717 $this->reqHeaders
['X-Original-User-Agent'] = $originalRequest['userAgent'];
721 * Check that the given URI is a valid one.
723 * This hardcodes a small set of protocols only, because we want to
724 * deterministically reject protocols not supported by all HTTP-transport
727 * "file://" specifically must not be allowed, for security reasons
728 * (see <https://www.mediawiki.org/wiki/Special:Code/MediaWiki/r67684>).
730 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
733 * @param string $uri URI to check for validity
736 public static function isValidURI( $uri ) {
737 return (bool)preg_match(
738 '/^https?:\/\/[^\/\s]\S*$/D',