Merge "Use utf8 charset for searchindex MySQL table"
[lhc/web/wiklou.git] / includes / HttpFunctions.php
index 2da368c..78c2ac7 100644 (file)
@@ -40,14 +40,15 @@ class Http {
         * @param array $options options to pass to MWHttpRequest object.
         *      Possible keys for the array:
         *    - timeout             Timeout length in seconds
+        *    - connectTimeout      Timeout for connection, in seconds (curl only)
         *    - postData            An array of key-value pairs or a url-encoded form data
         *    - proxy               The proxy to use.
         *                          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
+        *    - sslVerifyHost       Verify hostname against certificate
+        *    - sslVerifyCert       Verify SSL certificate
+        *    - caInfo              Provide CA information
         *    - maxRedirects        Maximum number of redirects to follow (defaults to 5)
         *    - followRedirects     Whether to follow redirects (defaults to false).
         *                                  Note: this should only be used when the target URL is trusted,
@@ -65,6 +66,9 @@ class Http {
                if ( !isset( $options['timeout'] ) ) {
                        $options['timeout'] = 'default';
                }
+               if ( !isset( $options['connectTimeout'] ) ) {
+                       $options['connectTimeout'] = 'default';
+               }
 
                $req = MWHttpRequest::factory( $url, $options );
                $status = $req->execute();
@@ -216,7 +220,7 @@ class MWHttpRequest {
         * @param array $options (optional) extra params to pass (see Http::request())
         */
        protected function __construct( $url, $options = array() ) {
-               global $wgHTTPTimeout;
+               global $wgHTTPTimeout, $wgHTTPConnectTimeout;
 
                $this->url = wfExpandUrl( $url, PROTO_HTTP );
                $this->parsedUrl = wfParseUrl( $this->url );
@@ -232,6 +236,11 @@ class MWHttpRequest {
                } else {
                        $this->timeout = $wgHTTPTimeout;
                }
+               if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
+                       $this->connectTimeout = $options['connectTimeout'];
+               } else {
+                       $this->connectTimeout = $wgHTTPConnectTimeout;
+               }
                if ( isset( $options['userAgent'] ) ) {
                        $this->setUserAgent( $options['userAgent'] );
                }
@@ -721,6 +730,7 @@ class CurlHttpRequest extends MWHttpRequest {
 
                $this->curlOptions[CURLOPT_PROXY] = $this->proxy;
                $this->curlOptions[CURLOPT_TIMEOUT] = $this->timeout;
+               $this->curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = $this->connectTimeout * 1000;
                $this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
                $this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
                $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
@@ -875,7 +885,23 @@ class PhpHttpRequest extends MWHttpRequest {
 
                $options['timeout'] = $this->timeout;
 
-               $context = stream_context_create( array( 'http' => $options ) );
+               if ( $this->sslVerifyHost ) {
+                       $options['CN_match'] = $this->parsedUrl['host'];
+               }
+               if ( $this->sslVerifyCert ) {
+                       $options['verify_peer'] = true;
+               }
+
+               if ( is_dir( $this->caInfo ) ) {
+                       $options['capath'] = $this->caInfo;
+               } elseif ( is_file( $this->caInfo ) ) {
+                       $options['cafile'] = $this->caInfo;
+               } elseif ( $this->caInfo ) {
+                       throw new MWException( "Invalid CA info passed: {$this->caInfo}" );
+               }
+
+               $scheme = $this->parsedUrl['scheme'];
+               $context = stream_context_create( array( "$scheme" => $options ) );
 
                $this->headerList = array();
                $reqCount = 0;