]> git.wh0rd.org - tt-rss.git/commitdiff
fetch_file_contents: improve error handling
authorAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Mon, 30 Oct 2017 10:13:10 +0000 (13:13 +0300)
committerAndrew Dolgov <noreply@madoka.volgo-balt.ru>
Mon, 30 Oct 2017 10:13:10 +0000 (13:13 +0300)
1. if request fails get error string from http  response status line
2. do not override http error with possible CURL/php specific last error
3. fix silent php error generated while processing response headers to get last modified value

classes/rssutils.php
include/functions.php

index 30d35dfae6cbad6cc374d76b41b298d16b5dd62a..d6fe048f977a13e48dd469697c72273499acc8d6 100644 (file)
@@ -419,7 +419,7 @@ class RSSUtils {
                        $feed_data = trim($feed_data);
 
                        _debug("fetch done.", $debug_enabled);
-                       _debug("source last modified: " . $fetch_last_modified);
+                       _debug("source last modified: " . $fetch_last_modified, $debug_enabled);
 
                        if ($feed_data && $fetch_last_modified != $stored_last_modified) {
                                $last_modified_escaped = db_escape_string(substr($fetch_last_modified, 0, 245));
index 392042b901f2b109ded436659e0283f2acef9c01..f883a12e7a4752608b20867d14ac2de52df96205 100644 (file)
                        $contents = substr($ret, $headers_length);
 
                        foreach ($headers as $header) {
-                               list ($key, $value) = explode(": ", $header);
+                if (strstr($header, ": ") !== FALSE) {
+                    list ($key, $value) = explode(": ", $header);
 
-                               if (strtolower($key) == "last-modified") {
-                                       $fetch_last_modified = $value;
-                               }
+                    if (strtolower($key) == "last-modified") {
+                        $fetch_last_modified = $value;
+                    }
+                }
+
+                if (substr(strtolower($header), 0, 7) == 'http/1.') {
+                    $fetch_last_error_code = (int) substr($header, 9, 3);
+                    $fetch_last_error = $header;
+                }
                        }
 
                        if (curl_errno($ch) === 23 || curl_errno($ch) === 61) {
                        $fetch_last_error_code = $http_code;
 
                        if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) {
+
                                if (curl_errno($ch) != 0) {
-                                       $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
-                               } else {
-                                       $fetch_last_error = "HTTP Code: $http_code";
+                                       $fetch_last_error .=  "; " . curl_errno($ch) . " " . curl_error($ch);
                                }
+
                                $fetch_last_error_content = $contents;
                                curl_close($ch);
                                return false;
                                return false;
                        }
 
-                       /*$fetch_last_modified = curl_getinfo($ch, CURLINFO_FILETIME);
-
-                       if ($fetch_last_modified != -1) {
-                               echo date("Y-m-d H:i:s", $fetch_last_modified); die;
-                       }*/
-
                        curl_close($ch);
 
                        return $contents;
                        $data = @file_get_contents($url, false, $context);
 
                        if (isset($http_response_header) && is_array($http_response_header)) {
-                               foreach ($http_response_header as $h) {
-                                       list ($key, $value) = explode(": ", $h);
-
-                                       $key = strtolower($key);
-
-                                       if ($key == 'content-type') {
-                                               $fetch_last_content_type = $value;
-                                               // don't abort here b/c there might be more than one
-                                               // e.g. if we were being redirected -- last one is the right one
-                                       } else if ($key == 'last-modified') {
-                                               $fetch_last_modified = $value;
-                                       }
+                               foreach ($http_response_header as $header) {
+                                   if (strstr($header, ": ") !== FALSE) {
+                        list ($key, $value) = explode(": ", $header);
+
+                        $key = strtolower($key);
+
+                        if ($key == 'content-type') {
+                            $fetch_last_content_type = $value;
+                            // don't abort here b/c there might be more than one
+                            // e.g. if we were being redirected -- last one is the right one
+                        } else if ($key == 'last-modified') {
+                            $fetch_last_modified = $value;
+                        }
+                    }
 
-                                       if (substr(strtolower($h), 0, 7) == 'http/1.') {
-                                               $fetch_last_error_code = (int) substr($h, 9, 3);
+                                       if (substr(strtolower($header), 0, 7) == 'http/1.') {
+                                               $fetch_last_error_code = (int) substr($header, 9, 3);
+                                               $fetch_last_error = $header;
                                        }
                                }
                        }
                                $error = error_get_last();
 
                                if ($error['message'] != $old_error['message']) {
-                                       $fetch_last_error = $error["message"];
-                               } else {
-                                       $fetch_last_error = "HTTP Code: $fetch_last_error_code";
+                                       $fetch_last_error .= "; " . $error["message"];
                                }
 
                                $fetch_last_error_content = $data;