support If-Modified-Since

This commit is contained in:
Andrew Dolgov 2013-03-30 15:10:53 +04:00
parent 60b88b254c
commit 7a01dc776e
2 changed files with 25 additions and 5 deletions

View File

@ -6,6 +6,7 @@
define('PLUGIN_FEED_BASE_INDEX', -128); define('PLUGIN_FEED_BASE_INDEX', -128);
$fetch_last_error = false; $fetch_last_error = false;
$fetch_last_error_code = false;
$pluginhost = false; $pluginhost = false;
function __autoload($class) { function __autoload($class) {
@ -287,9 +288,10 @@
} }
} }
function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false, $timeout = false) { function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false, $timeout = false, $timestamp = 0) {
global $fetch_last_error; global $fetch_last_error;
global $fetch_last_error_code;
if (function_exists('curl_init') && !ini_get("open_basedir")) { if (function_exists('curl_init') && !ini_get("open_basedir")) {
@ -299,6 +301,11 @@
$ch = curl_init($url); $ch = curl_init($url);
} }
if ($timestamp) {
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp)));
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : 15); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : 15);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : 45); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : 45);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode")); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode"));
@ -335,6 +342,8 @@
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$fetch_last_error_code = $http_code;
if ($http_code != 200 || $type && strpos($content_type, "$type") === false) { if ($http_code != 200 || $type && strpos($content_type, "$type") === false) {
if (curl_errno($ch) != 0) { if (curl_errno($ch) != 0) {
$fetch_last_error = curl_errno($ch) . " " . curl_error($ch); $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);

View File

@ -239,6 +239,7 @@
$rss = false; $rss = false;
$rss_hash = false; $rss_hash = false;
$cache_timestamp = file_exists($cache_filename) ? filemtime($cache_filename) : 0;
if (file_exists($cache_filename) && if (file_exists($cache_filename) &&
is_readable($cache_filename) && is_readable($cache_filename) &&
@ -261,11 +262,11 @@
if (!$feed_data) { if (!$feed_data) {
if ($debug_enabled) { if ($debug_enabled) {
_debug("update_rss_feed: fetching [$fetch_url]..."); _debug("update_rss_feed: fetching [$fetch_url] (ts: $cache_timestamp)");
} }
$feed_data = fetch_file_contents($fetch_url, false, $feed_data = fetch_file_contents($fetch_url, false,
$auth_login, $auth_pass, false, $no_cache ? 15 : 45); $auth_login, $auth_pass, false, $no_cache ? 15 : 45, $cache_timestamp);
if ($debug_enabled) { if ($debug_enabled) {
_debug("update_rss_feed: fetch done."); _debug("update_rss_feed: fetch done.");
@ -275,12 +276,22 @@
if (!$feed_data) { if (!$feed_data) {
global $fetch_last_error; global $fetch_last_error;
global $fetch_last_error_code;
if ($debug_enabled) { if ($debug_enabled) {
_debug("update_rss_feed: unable to fetch: $fetch_last_error"); _debug("update_rss_feed: unable to fetch: $fetch_last_error [$fetch_last_error_code]");
} }
$error_escaped = db_escape_string($link, $fetch_last_error); $error_escaped = '';
// If-Modified-Since
if ($fetch_last_error_code != 304) {
$error_escaped = db_escape_string($link, $fetch_last_error);
} else {
if ($debug_enabled) {
_debug("update_rss_feed: source claims data not modified, nothing to do.");
}
}
db_query($link, db_query($link,
"UPDATE ttrss_feeds SET last_error = '$error_escaped', "UPDATE ttrss_feeds SET last_error = '$error_escaped',