Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / http / MWHttpRequest.php
index 1991239..41ea1dc 100644 (file)
@@ -85,13 +85,13 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
 
        /**
         * @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
-        * @param array $options (optional) extra params to pass (see Http::request())
+        * @param array $options (optional) extra params to pass (see HttpRequestFactory::create())
         * @param string $caller The method making this request, for profiling
         * @param Profiler|null $profiler An instance of the profiler for profiling, or null
         * @throws Exception
         */
        public function __construct(
-               $url, array $options = [], $caller = __METHOD__, $profiler = null
+               $url, array $options = [], $caller = __METHOD__, Profiler $profiler = null
        ) {
                global $wgHTTPTimeout, $wgHTTPConnectTimeout;
 
@@ -129,6 +129,8 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
                        $this->setOriginalRequest( $options['originalRequest'] );
                }
 
+               $this->setHeader( 'X-Request-Id', WebRequest::getRequestId() );
+
                $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
                                "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
 
@@ -170,9 +172,9 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
 
        /**
         * Generate a new request object
-        * Deprecated: @see HttpRequestFactory::create
+        * @deprecated since 1.34, use HttpRequestFactory instead
         * @param string $url Url to use
-        * @param array|null $options (optional) extra params to pass (see Http::request())
+        * @param array|null $options (optional) extra params to pass (see HttpRequestFactory::create())
         * @param string $caller The method making this request, for profiling
         * @throws DomainException
         * @return MWHttpRequest
@@ -202,7 +204,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
         * @param array $args
         * @todo overload the args param
         */
-       public function setData( $args ) {
+       public function setData( array $args ) {
                $this->postData = $args;
        }
 
@@ -222,7 +224,8 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
                if ( self::isLocalURL( $this->url ) || $this->noProxy ) {
                        $this->proxy = '';
                } else {
-                       $this->proxy = Http::getProxy();
+                       global $wgHTTPProxy;
+                       $this->proxy = (string)$wgHTTPProxy;
                }
        }
 
@@ -326,6 +329,17 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
         * @throws InvalidArgumentException
         */
        public function setCallback( $callback ) {
+               return $this->doSetCallback( $callback );
+       }
+
+       /**
+        * Worker function for setting callbacks.  Calls can originate both internally and externally
+        * via setCallback).  Defaults to the internal read callback if $callback is null.
+        *
+        * @param callable|null $callback
+        * @throws InvalidArgumentException
+        */
+       protected function doSetCallback( $callback ) {
                if ( is_null( $callback ) ) {
                        $callback = [ $this, 'read' ];
                } elseif ( !is_callable( $callback ) ) {
@@ -369,7 +383,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
                $this->proxySetup(); // set up any proxy as needed
 
                if ( !$this->callback ) {
-                       $this->setCallback( null );
+                       $this->doSetCallback( null );
                }
 
                if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
@@ -504,7 +518,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
         *
         * @param CookieJar $jar
         */
-       public function setCookieJar( $jar ) {
+       public function setCookieJar( CookieJar $jar ) {
                $this->cookieJar = $jar;
        }
 
@@ -530,7 +544,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
         * @param string $value
         * @param array $attr
         */
-       public function setCookie( $name, $value, $attr = [] ) {
+       public function setCookie( $name, $value, array $attr = [] ) {
                if ( !$this->cookieJar ) {
                        $this->cookieJar = new CookieJar;
                }
@@ -649,4 +663,27 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
                $this->reqHeaders['X-Forwarded-For'] = $originalRequest['ip'];
                $this->reqHeaders['X-Original-User-Agent'] = $originalRequest['userAgent'];
        }
+
+       /**
+        * Check that the given URI is a valid one.
+        *
+        * This hardcodes a small set of protocols only, because we want to
+        * deterministically reject protocols not supported by all HTTP-transport
+        * methods.
+        *
+        * "file://" specifically must not be allowed, for security reasons
+        * (see <https://www.mediawiki.org/wiki/Special:Code/MediaWiki/r67684>).
+        *
+        * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
+        *
+        * @since 1.34
+        * @param string $uri URI to check for validity
+        * @return bool
+        */
+       public static function isValidURI( $uri ) {
+               return (bool)preg_match(
+                       '/^https?:\/\/[^\/\s]\S*$/D',
+                       $uri
+               );
+       }
 }