diff --git a/core/Tracker/Response.php b/core/Tracker/Response.php index 3edd2b27c69b98e0b386c016e156b78bf02a59f3..e4f2f7d5ae575d361aa68f42e9735611186b6c10 100644 --- a/core/Tracker/Response.php +++ b/core/Tracker/Response.php @@ -74,7 +74,9 @@ class Response $this->outputApiResponse($tracker); Common::printDebug("Logging disabled, display transparent logo"); } elseif (!$tracker->hasLoggedRequests()) { - Common::sendResponseCode(400); + if (!$this->isHttpGetRequest() || !empty($_GET) || !empty($_POST)) { + Common::sendResponseCode(400); + } Common::printDebug("Empty request => Piwik page"); echo "<a href='/'>Piwik</a> is a free/libre web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data."; } else { @@ -100,15 +102,20 @@ class Response private function outputAccessControlHeaders() { - $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; - - if ($requestMethod !== 'GET') { + if (!$this->isHttpGetRequest()) { $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*'; Common::sendHeader('Access-Control-Allow-Origin: ' . $origin); Common::sendHeader('Access-Control-Allow-Credentials: true'); } } + private function isHttpGetRequest() + { + $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; + + return strtoupper($requestMethod) === 'GET'; + } + private function getOutputBuffer() { return ob_get_contents(); diff --git a/tests/PHPUnit/Integration/TrackerTest.php b/tests/PHPUnit/Integration/TrackerTest.php index 260e8d721b7e4698074c586914350c7e8ec6320d..300e26f6643c99d998ee92918d6f4fc286614b44 100644 --- a/tests/PHPUnit/Integration/TrackerTest.php +++ b/tests/PHPUnit/Integration/TrackerTest.php @@ -19,7 +19,6 @@ use Piwik\Tests\Framework\TestCase\IntegrationTestCase; use Piwik\Tracker; use Piwik\Tracker\RequestSet; use Piwik\Tracker\Request; -use Piwik\Translate; class TestTracker extends Tracker { diff --git a/tests/PHPUnit/System/TrackerResponseTest.php b/tests/PHPUnit/System/TrackerResponseTest.php index 16ab4a4e0104ab15939211d2dd11272fb5902635..337aceb30c914d39ffa3c48bef422fb9355d0dfe 100755 --- a/tests/PHPUnit/System/TrackerResponseTest.php +++ b/tests/PHPUnit/System/TrackerResponseTest.php @@ -94,10 +94,21 @@ class TrackerResponseTest extends SystemTestCase $this->assertResponseCode(400, $url . '1'); // has to be 16 char, but is 17 now } - public function test_response_ShouldReturnPiwikMessage_InCaseOfEmptyRequest() + // See https://github.com/piwik/piwik/issues/7850 piwik.php is used by plugins and monitoring systems to test for Piwik installation. + // it is important to return a 200 if someone does a GET request with no parameters + public function test_response_ShouldReturnPiwikMessageWithHttp200_InCaseOfEmptyGETRequest() { $url = Fixture::getTrackerUrl(); - $this->assertResponseCode(400, $url); + $this->assertResponseCode(200, $url); + + $expected = "<a href='/'>Piwik</a> is a free/libre web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data."; + $this->assertHttpResponseText($expected, $url); + } + + public function test_response_ShouldReturnPiwikMessageWithHttp400_InCaseOfInvalidRequestOrIfNothingIsTracked() + { + $url = Fixture::getTrackerUrl(); + $this->assertResponseCode(400, $url . '?rec=1'); $expected = "<a href='/'>Piwik</a> is a free/libre web <a href='http://piwik.org'>analytics</a> that lets you keep control of your data."; $this->assertHttpResponseText($expected, $url);