Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[lhc/web/wiklou.git] / includes / HttpFunctions.php
index 47dc682..8dd6525 100644 (file)
@@ -20,8 +20,9 @@ class Http {
         *    - timeout             Timeout length in seconds
         *    - postData            An array of key-value pairs or a url-encoded form data
         *    - proxy               The proxy to use.
-        *                          Will use $wgHTTPProxy (if set) otherwise.
-        *    - noProxy             Override $wgHTTPProxy (if set) and don't use any proxy at all.
+        *                          Otherwise it will use $wgHTTPProxy (if set)
+        *                          Otherwise it will use the environment variable "http_proxy" (if set)
+        *    - noProxy             Don't use any proxy at all. Takes precedence over proxy value(s).
         *    - sslVerifyHost       (curl only) Verify hostname against certificate
         *    - sslVerifyCert       (curl only) Verify SSL certificate
         *    - caInfo              (curl only) Provide CA information
@@ -29,6 +30,8 @@ class Http {
         *    - followRedirects     Whether to follow redirects (defaults to false).
         *                                  Note: this should only be used when the target URL is trusted,
         *                                  to avoid attacks on intranet services accessible by HTTP.
+        *    - userAgent           A user agent, if you want to override the default
+        *                          MediaWiki/$wgVersion
         * @return Mixed: (bool)false on failure or a string on success
         */
        public static function request( $method, $url, $options = array() ) {
@@ -53,6 +56,9 @@ class Http {
         * Simple wrapper for Http::request( 'GET' )
         * @see Http::request()
         *
+        * @param $url
+        * @param $timeout string
+        * @param $options array
         * @return string
         */
        public static function get( $url, $timeout = 'default', $options = array() ) {
@@ -64,6 +70,8 @@ class Http {
         * Simple wrapper for Http::request( 'POST' )
         * @see Http::request()
         *
+        * @param $url
+        * @param $options array
         * @return string
         */
        public static function post( $url, $options = array() ) {
@@ -124,6 +132,8 @@ class Http {
         * protocols, because we only want protocols that both cURL
         * and php support.
         *
+        * file:// should not be allowed here for security purpose (r67684)
+        *
         * @fixme this is wildly inaccurate and fails to actually check most stuff
         *
         * @param $uri Mixed: URI to check for validity
@@ -197,6 +207,9 @@ class MWHttpRequest {
                } else {
                        $this->timeout = $wgHTTPTimeout;
                }
+               if( isset( $options['userAgent'] ) ) {
+                       $this->setUserAgent( $options['userAgent'] );
+               }
 
                $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
                                  "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
@@ -206,6 +219,10 @@ class MWHttpRequest {
                                $this->$o = $options[$o];
                        }
                }
+
+               if ( $this->noProxy ) {
+                       $this->proxy = ''; // noProxy takes precedence
+               }
        }
 
        /**
@@ -221,6 +238,7 @@ class MWHttpRequest {
         * Generate a new request object
         * @param $url String: url to use
         * @param $options Array: (optional) extra params to pass (see Http::request())
+        * @return CurlHttpRequest|PhpHttpRequest
         * @see MWHttpRequest::__construct
         */
        public static function factory( $url, $options = null ) {
@@ -265,19 +283,18 @@ class MWHttpRequest {
        }
 
        /**
-        * Take care of setting up the proxy
-        * (override in subclass)
+        * Take care of setting up the proxy (do nothing if "noProxy" is set)
         *
-        * @return String
+        * @return void
         */
        public function proxySetup() {
                global $wgHTTPProxy;
 
-               if ( $this->proxy ) {
+               if ( $this->proxy || !$this->noProxy ) {
                        return;
                }
 
-               if ( Http::isLocalURL( $this->url ) ) {
+               if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
                        $this->proxy = '';
                } elseif ( $wgHTTPProxy ) {
                        $this->proxy = $wgHTTPProxy ;
@@ -295,6 +312,7 @@ class MWHttpRequest {
 
        /**
         * Set the user agent
+        * @param $UA string
         */
        public function setUserAgent( $UA ) {
                $this->setHeader( 'User-Agent', $UA );
@@ -302,6 +320,8 @@ class MWHttpRequest {
 
        /**
         * Set an arbitrary header
+        * @param $name
+        * @param $value
         */
        public function setHeader( $name, $value ) {
                // I feel like I should normalize the case here...
@@ -310,6 +330,7 @@ class MWHttpRequest {
 
        /**
         * Get an array of the headers
+        * @return array
         */
        public function getHeaderList() {
                $list = array();
@@ -359,6 +380,7 @@ class MWHttpRequest {
         *
         * @param $fh handle
         * @param $content String
+        * @return int
         */
        public function read( $fh, $content ) {
                $this->content .= $content;
@@ -383,9 +405,7 @@ class MWHttpRequest {
                        $this->setReferer( wfExpandUrl( $wgTitle->getFullURL(), PROTO_CURRENT ) );
                }
 
-               if ( !$this->noProxy ) {
-                       $this->proxySetup();
-               }
+               $this->proxySetup(); // set up any proxy as needed
 
                if ( !$this->callback ) {
                        $this->setCallback( array( $this, 'read' ) );
@@ -400,8 +420,6 @@ class MWHttpRequest {
         * Parses the headers, including the HTTP status code and any
         * Set-Cookie headers.  This function expectes the headers to be
         * found in an array in the member variable headerList.
-        *
-        * @return nothing
         */
        protected function parseHeader() {
                $lastname = "";
@@ -429,8 +447,6 @@ class MWHttpRequest {
         * RFC2616, section 10,
         * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for a
         * list of status codes.)
-        *
-        * @return nothing
         */
        protected function setStatus() {
                if ( !$this->respHeaders ) {
@@ -540,6 +556,9 @@ class MWHttpRequest {
         * cookies.      Used internally after a request to parse the
         * Set-Cookie headers.
         * @see Cookie::set
+        * @param $name
+        * @param $value null
+        * @param $attr null
         */
        public function setCookie( $name, $value = null, $attr = null ) {
                if ( !$this->cookieJar ) {
@@ -571,6 +590,10 @@ class MWHttpRequest {
         * Relative values of the "Location" header are incorrect as stated in RFC, however they do happen and modern browsers support them.
         * This function loops backwards through all locations in order to build the proper absolute URI - Marooned at wikia-inc.com
         *
+        * 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 handling rewrite).
+        *
         * @return string
         */
        public function getFinalUrl() {
@@ -614,6 +637,7 @@ class MWHttpRequest {
        /**
         * Returns true if the backend can follow redirects. Overridden by the
         * child classes.
+        * @return bool
         */
        public function canFollowRedirects() {
                return true;
@@ -634,6 +658,11 @@ class CurlHttpRequest extends MWHttpRequest {
        protected $curlOptions = array();
        protected $headerText = "";
 
+       /**
+        * @param $fh
+        * @param $content
+        * @return int
+        */
        protected function readHeader( $fh, $content ) {
                $this->headerText .= $content;
                return strlen( $content );
@@ -725,6 +754,9 @@ class CurlHttpRequest extends MWHttpRequest {
                return $this->status;
        }
 
+       /**
+        * @return bool
+        */
        public function canFollowRedirects() {
                if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
                        wfDebug( "Cannot follow redirects in safe mode\n" );
@@ -741,6 +773,11 @@ class CurlHttpRequest extends MWHttpRequest {
 }
 
 class PhpHttpRequest extends MWHttpRequest {
+
+       /**
+        * @param $url string
+        * @return string
+        */
        protected function urlToTcp( $url ) {
                $parsedUrl = parse_url( $url );
 
@@ -767,7 +804,7 @@ class PhpHttpRequest extends MWHttpRequest {
                }
 
                $options = array();
-               if ( $this->proxy && !$this->noProxy ) {
+               if ( $this->proxy ) {
                        $options['proxy'] = $this->urlToTCP( $this->proxy );
                        $options['request_fulluri'] = true;
                }
@@ -828,7 +865,7 @@ class PhpHttpRequest extends MWHttpRequest {
                        # Check security of URL
                        $url = $this->getResponseHeader( "Location" );
 
-                       if ( substr( $url, 0, 7 ) !== 'http://' ) {
+                       if ( !Http::isValidURI( $url ) ) {
                                wfDebug( __METHOD__ . ": insecure redirection\n" );
                                break;
                        }