[BUGFIX] t3lib_div::getUrl() providing wrong error information
authorIngo Renner <ingo@typo3.org>
Wed, 11 Apr 2012 11:54:57 +0000 (13:54 +0200)
committerIngo Renner <ingo@typo3.org>
Wed, 11 Apr 2012 15:06:49 +0000 (17:06 +0200)
When using t3lib_div::getUrl() with lib file/context it uses
file_get_contents() to read a URL. When this fails it tries to provide the
reason for the error but does not return the actual issue.

The error key in the returned array contains the error retrieved by
error_get_last() which does not provide the error that occurred with
file_get_contents(), but with PHP. Usually this is a notice.

The actual error message of file_get_contents() is in the variable
$http_response_header which is available after calling file_get_contents().

See http://de2.php.net/manual/en/reserved.variables.httpresponseheader.php

Change-Id: I8058db5d2a70a6d94b7c3c511e549cccc8079a2c
Fixes: #35847
Releases: 6.0, 4.7, 4.6, 4.5
Reviewed-on: http://review.typo3.org/10258
Reviewed-by: Ingo Renner
Tested-by: Ingo Renner
t3lib/class.t3lib_div.php
tests/t3lib/t3lib_divTest.php

index a535aab..aebe71e 100644 (file)
@@ -3149,26 +3149,23 @@ final class t3lib_div {
                                )
                        )
                        );
-                       $content = @file_get_contents($url, FALSE, $ctx);
+
+                       $content = file_get_contents($url, FALSE, $ctx);
+
                        if ($content === FALSE && isset($report)) {
-                               $phpError = error_get_last();
-                               $report['error'] = $phpError['type'];
-                               $report['message'] = $phpError['message'];
+                               $report['error']   = -1;
+                               $report['message'] = 'Couldn\'t get URL: ' . implode(LF, $http_response_header);
                        }
                } else {
                        if (isset($report)) {
                                $report['lib'] = 'file';
                        }
-                       $content = @file_get_contents($url);
+
+                       $content = file_get_contents($url);
+
                        if ($content === FALSE && isset($report)) {
-                               if (function_exists('error_get_last')) {
-                                       $phpError = error_get_last();
-                                       $report['error'] = $phpError['type'];
-                                       $report['message'] = $phpError['message'];
-                               } else {
-                                       $report['error'] = -1;
-                                       $report['message'] = 'Couldn\'t get URL.';
-                               }
+                               $report['error']   = -1;
+                               $report['message'] = 'Couldn\'t get URL: ' . implode(LF, $http_response_header);
                        }
                }
 
@@ -6263,4 +6260,4 @@ final class t3lib_div {
        }
 }
 
-?>
\ No newline at end of file
+?>
index 444d1c7..5ea4e88 100644 (file)
@@ -1966,6 +1966,50 @@ class t3lib_divTest extends tx_phpunit_testcase {
        }
 
 
+       ///////////////////////////
+       // Tests concerning getUrl
+       ///////////////////////////
+
+       /**
+        * @test
+        */
+       public function getUrlWithAdditionalRequestHeadersProvidesHttpHeaderOnError() {
+               $url = 'http://typo3.org/i-do-not-exist-' . time();
+
+               $report = array();
+               t3lib_div::getUrl(
+                       $url,
+                       0,
+                       array(),
+                       $report
+               );
+               $this->assertContains(
+                       '404',
+                       $report['message']
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function getUrlProvidesWithoutAdditionalRequestHeadersHttpHeaderOnError() {
+               $url = 'http://typo3.org/i-do-not-exist-' . time();
+
+               $report = array();
+               t3lib_div::getUrl(
+                       $url,
+                       0,
+                       FALSE,
+                       $report
+               );
+               $this->assertContains(
+                       '404',
+                       $report['message'],
+                       'Did not provide the HTTP response header when requesting a failing URL.'
+               );
+       }
+
+
        ///////////////////////////////
        // Tests concerning fixPermissions
        ///////////////////////////////