Merge "Rephrase enotif_lastdiff and enotif_lastvisited"
[lhc/web/wiklou.git] / includes / http / MWHttpRequest.php
index fac052f..88cc510 100644 (file)
@@ -125,6 +125,9 @@ class MWHttpRequest implements LoggerAwareInterface {
                                'Basic ' . base64_encode( $options['username'] . ':' . $options['password'] )
                        );
                }
+               if ( isset( $options['originalRequest'] ) ) {
+                       $this->setOriginalRequest( $options['originalRequest'] );
+               }
 
                $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
                                "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
@@ -132,7 +135,7 @@ class MWHttpRequest implements LoggerAwareInterface {
                foreach ( $members as $o ) {
                        if ( isset( $options[$o] ) ) {
                                // ensure that MWHttpRequest::method is always
-                               // uppercased. Bug 36137
+                               // uppercased. T38137
                                if ( $o == 'method' ) {
                                        $options[$o] = strtoupper( $options[$o] );
                                }
@@ -580,7 +583,7 @@ class MWHttpRequest implements LoggerAwareInterface {
         *
         * Note that the multiple Location: headers are an artifact of
         * CURL -- they shouldn't actually get returned this way. Rewrite
-        * this when bug 29232 is taken care of (high-level redirect
+        * this when T31232 is taken care of (high-level redirect
         * handling rewrite).
         *
         * @return string
@@ -606,19 +609,17 @@ class MWHttpRequest implements LoggerAwareInterface {
                                }
                        }
 
-                       if ( $foundRelativeURI ) {
-                               if ( $domain ) {
-                                       return $domain . $locations[$countLocations - 1];
-                               } else {
-                                       $url = parse_url( $this->url );
-                                       if ( isset( $url['host'] ) ) {
-                                               return $url['scheme'] . '://' . $url['host'] .
-                                                       $locations[$countLocations - 1];
-                                       }
-                               }
-                       } else {
+                       if ( !$foundRelativeURI ) {
                                return $locations[$countLocations - 1];
                        }
+                       if ( $domain ) {
+                               return $domain . $locations[$countLocations - 1];
+                       }
+                       $url = parse_url( $this->url );
+                       if ( isset( $url['host'] ) ) {
+                               return $url['scheme'] . '://' . $url['host'] .
+                                       $locations[$countLocations - 1];
+                       }
                }
 
                return $this->url;
@@ -632,4 +633,34 @@ class MWHttpRequest implements LoggerAwareInterface {
        public function canFollowRedirects() {
                return true;
        }
+
+       /**
+        * Set information about the original request. This can be useful for
+        * endpoints/API modules which act as a proxy for some service, and
+        * throttling etc. needs to happen in that service.
+        * Calling this will result in the X-Forwarded-For and X-Original-User-Agent
+        * headers being set.
+        * @param WebRequest|array $originalRequest When in array form, it's
+        *   expected to have the keys 'ip' and 'userAgent'.
+        * @note IP/user agent is personally identifiable information, and should
+        *   only be set when the privacy policy of the request target is
+        *   compatible with that of the MediaWiki installation.
+        */
+       public function setOriginalRequest( $originalRequest ) {
+               if ( $originalRequest instanceof WebRequest ) {
+                       $originalRequest = [
+                               'ip' => $originalRequest->getIP(),
+                               'userAgent' => $originalRequest->getHeader( 'User-Agent' ),
+                       ];
+               } elseif (
+                       !is_array( $originalRequest )
+                       || array_diff( [ 'ip', 'userAgent' ], array_keys( $originalRequest ) )
+               ) {
+                       throw new InvalidArgumentException( __METHOD__ . ': $originalRequest must be a '
+                               . "WebRequest or an array with 'ip' and 'userAgent' keys" );
+               }
+
+               $this->reqHeaders['X-Forwarded-For'] = $originalRequest['ip'];
+               $this->reqHeaders['X-Original-User-Agent'] = $originalRequest['userAgent'];
+       }
 }