[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 14:34:13 +0000 (16:34 +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: I6bb9ad7010a633f259da103f71f404592194f743
Fixes: #35847
Releases: 6.0, 4.7, 4.6, 4.5
Reviewed-on: http://review.typo3.org/10229
Reviewed-by: Oliver Klee
Tested-by: Oliver Klee
Reviewed-by: Andy Grunwald
Tested-by: Andy Grunwald
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Ingo Renner
Tested-by: Ingo Renner
t3lib/class.t3lib_div.php
tests/t3lib/class.t3lib_divTest.php

index 8ea0da8..60107e5 100644 (file)
@@ -2728,26 +2728,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);
                        }
                }
 
index 882a3b5..f31e9d8 100644 (file)
@@ -2182,6 +2182,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
        ///////////////////////////////