Skip to content
Snippets Groups Projects
Commit 253fbea2 authored by Staratel's avatar Staratel Committed by Chris Leppanen
Browse files

Issue #1480902 by Staratel | faunt: Added Are there timeouts on Feed importers?.

parent b32b4319
No related branches found
No related tags found
No related merge requests found
......@@ -191,6 +191,8 @@ Default: 50
Name: http_request_timeout
Default: 15
Description: Timeout in seconds to wait for an HTTP get request to finish.
Note: This setting could be overridden per importer in admin UI :
admin/structure/feeds/<your_importer>/settings/<your_fetcher> page.
Name: feeds_never_use_curl
Default: FALSE
......
......@@ -5,6 +5,13 @@
* Schema definitions install/update/uninstall hooks.
*/
/**
* Implement hook_uninstall()
*/
function feeds_uninstall() {
variable_del('http_request_timeout');
}
/**
* Implements hook_schema().
*/
......
......@@ -77,11 +77,13 @@ function http_request_get_common_syndication($url, $settings = NULL) {
* If the URL uses authentication, supply the password.
* @param bool $accept_invalid_cert
* Whether to accept invalid certificates.
* @param integer $timeout
* Timeout in seconds to wait for an HTTP get request to finish.
*
* @return stdClass
* An object that describes the data downloaded from $url.
*/
function http_request_get($url, $username = NULL, $password = NULL, $accept_invalid_cert = FALSE) {
function http_request_get($url, $username = NULL, $password = NULL, $accept_invalid_cert = FALSE, $timeout = NULL) {
// Intra-pagedownload cache, avoid to download the same content twice within
// one page download (it's possible, compatible and parse calls).
static $download_cache = array();
......@@ -89,6 +91,9 @@ function http_request_get($url, $username = NULL, $password = NULL, $accept_inva
return $download_cache[$url];
}
// Determine request timeout.
$request_timeout = !empty($timeout) ? $timeout : variable_get('http_request_timeout', 30);
if (!$username && valid_url($url, TRUE)) {
// Handle password protected feeds.
$url_parts = parse_url($url);
......@@ -168,7 +173,7 @@ function http_request_get($url, $username = NULL, $password = NULL, $accept_inva
curl_setopt($download, CURLOPT_HEADER, TRUE);
curl_setopt($download, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($download, CURLOPT_ENCODING, '');
curl_setopt($download, CURLOPT_TIMEOUT, variable_get('http_request_timeout', 30));
curl_setopt($download, CURLOPT_TIMEOUT, $request_timeout);
if ($accept_invalid_cert) {
curl_setopt($download, CURLOPT_SSL_VERIFYPEER, 0);
}
......@@ -212,7 +217,7 @@ function http_request_get($url, $username = NULL, $password = NULL, $accept_inva
}
}
else {
$result = drupal_http_request($url, array('headers' => $headers, 'timeout' => variable_get('http_request_timeout', 30)));
$result = drupal_http_request($url, array('headers' => $headers, 'timeout' => $request_timeout));
}
$result->code = isset($result->code) ? $result->code : 200;
......@@ -228,7 +233,7 @@ function http_request_get($url, $username = NULL, $password = NULL, $accept_inva
// It's a tragedy, this file must exist and contain good data.
// In this case, clear cache and repeat.
cache_clear_all('feeds_http_download_' . md5($url), 'cache');
return http_request_get($url, $username, $password);
return http_request_get($url, $username, $password, $accept_invalid_cert, $request_timeout);
}
}
......
......@@ -13,6 +13,7 @@ feeds_include_library('PuSHSubscriber.inc', 'PuSHSubscriber');
class FeedsHTTPFetcherResult extends FeedsFetcherResult {
protected $url;
protected $file_path;
protected $timeout;
/**
* Constructor.
......@@ -27,12 +28,20 @@ class FeedsHTTPFetcherResult extends FeedsFetcherResult {
*/
public function getRaw() {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->url);
$result = http_request_get($this->url, NULL, NULL, NULL, $this->timeout);
if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
}
return $this->sanitizeRaw($result->data);
}
public function getTimeout() {
return $this->timeout;
}
public function setTimeout($timeout) {
$this->timeout = $timeout;
}
}
/**
......@@ -48,7 +57,10 @@ class FeedsHTTPFetcher extends FeedsFetcher {
if ($this->config['use_pubsubhubbub'] && ($raw = $this->subscriber($source->feed_nid)->receive())) {
return new FeedsFetcherResult($raw);
}
return new FeedsHTTPFetcherResult($source_config['source']);
$fetcher_result = new FeedsHTTPFetcherResult($source_config['source']);
// When request_timeout is empty, the global value is used.
$fetcher_result->setTimeout($this->config['request_timeout']);
return $fetcher_result;
}
/**
......@@ -95,6 +107,7 @@ class FeedsHTTPFetcher extends FeedsFetcher {
'auto_detect_feeds' => FALSE,
'use_pubsubhubbub' => FALSE,
'designated_hub' => '',
'request_timeout' => NULL,
);
}
......@@ -124,6 +137,18 @@ class FeedsHTTPFetcher extends FeedsFetcher {
'edit-use-pubsubhubbub' => array(1),
),
);
// Per importer override of global http request timeout setting.
$form['request_timeout'] = array(
'#type' => 'textfield',
'#title' => t('Request timeout'),
'#description' => t('Timeout in seconds to wait for an HTTP get request to finish.</br>' .
'<b>Note:</b> this setting will override the global setting.</br>' .
'When left empty, the global value is used.'),
'#default_value' => $this->config['request_timeout'],
'#element_validate' => array('element_validate_integer_positive'),
'#maxlength' => 3,
'#size'=> 30,
);
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment