From 58f71c7e9eca460bfaf518c6c1fecaca87b58dd3 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Mon, 8 Apr 2013 19:45:05 -0400 Subject: [PATCH] Allow setting of connection timeouts for HTTP requests using cURL Bug: 47027 Change-Id: I6e64a8bfc58e899149463d305eac672c1e8ad2ba --- includes/DefaultSettings.php | 6 ++++++ includes/HttpFunctions.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index c93142a9a1..58eb3d7ec3 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -6167,6 +6167,12 @@ $wgAsyncHTTPTimeout = 25; */ $wgHTTPProxy = false; +/** + * Timeout for connections done internally (in seconds) + * Only works for curl + */ +$wgHTTPConnectTimeout = 5e0; + /** @} */ # End HTTP client } /************************************************************************//** diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index 2da368cd68..1c9ad38bbf 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -40,6 +40,7 @@ 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) @@ -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(); @@ -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" ); -- 2.20.1