diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-14 02:14:37 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-14 02:35:29 +0300 |
commit | 25aace1fb7bf949e2ac18a3ab0e84e84e1d91596 (patch) | |
tree | cb9c73461a263baa81317f2cb5a6abdeecad3cc6 /core | |
parent | 74436365533366123a78bf646b245116b8c0c565 (diff) |
refs #6661 initial work on triggering a 400 bad request instead of 500 in case Idsite is wrong
Diffstat (limited to 'core')
-rw-r--r-- | core/Common.php | 41 | ||||
-rw-r--r-- | core/Exception/UnexpectedWebsiteFoundException.php | 13 | ||||
-rw-r--r-- | core/ProxyHttp.php | 24 | ||||
-rw-r--r-- | core/Site.php | 3 | ||||
-rw-r--r-- | core/Tracker.php | 50 |
5 files changed, 107 insertions, 24 deletions
diff --git a/core/Common.php b/core/Common.php index 823e884af1..dd46ada0de 100644 --- a/core/Common.php +++ b/core/Common.php @@ -1150,6 +1150,47 @@ class Common } /** + * Sends the given response code if supported. + * + * @param int $code Eg 204 + * + * @throws Exception + */ + public static function sendResponseCode($code) + { + $messages = array( + 200 => 'Ok', + 204 => 'No Response', + 301 => 'Moved Permanently', + 302 => 'Found', + 304 => 'Not Modified', + 400 => 'Bad Request', + 401 => 'Unauthorized', + 403 => 'Forbidden', + 404 => 'Not Found', + 500 => 'Internal Server Error' + ); + + if (!array_key_exists($code, $messages)) { + throw new Exception('Response code not supported: ' . $code); + } + + if (strpos(PHP_SAPI, '-fcgi') === false) { + $key = $_SERVER['SERVER_PROTOCOL']; + } else { + // FastCGI + $key = 'Status:'; + } + + if (strlen($key) > 11 || 0 !== strpos(strtolower($key), 'http')) { + $key = 'HTTP/1.1'; + } + + $message = $messages[$code]; + Common::sendHeader($key . ' ' . $code . ' ' . $message); + } + + /** * Returns the ID of the current LocationProvider (see UserCountry plugin code) from * the Tracker cache. */ diff --git a/core/Exception/UnexpectedWebsiteFoundException.php b/core/Exception/UnexpectedWebsiteFoundException.php new file mode 100644 index 0000000000..68178d8599 --- /dev/null +++ b/core/Exception/UnexpectedWebsiteFoundException.php @@ -0,0 +1,13 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Exception; + +class UnexpectedWebsiteFoundException extends Exception +{ +}
\ No newline at end of file diff --git a/core/ProxyHttp.php b/core/ProxyHttp.php index 69f7144b44..b2939e9a39 100644 --- a/core/ProxyHttp.php +++ b/core/ProxyHttp.php @@ -66,7 +66,7 @@ class ProxyHttp { // if the file cannot be found return HTTP status code '404' if (!file_exists($file)) { - self::setHttpStatus('404 Not Found'); + Common::sendResponseCode(404); return; } @@ -87,7 +87,7 @@ class ProxyHttp // Return 304 if the file has not modified since if ($modifiedSince === $lastModified) { - self::setHttpStatus('304 Not Modified'); + Common::sendResponseCode(304); return; } @@ -158,7 +158,7 @@ class ProxyHttp } if (!_readfile($file, $byteStart, $byteEnd)) { - self::setHttpStatus('505 Internal server error'); + Common::sendResponseCode(500); } } @@ -220,24 +220,6 @@ class ProxyHttp } /** - * Set response header, e.g., HTTP/1.0 200 Ok - * - * @param string $status Status - * @return bool - */ - protected static function setHttpStatus($status) - { - if (strpos(PHP_SAPI, '-fcgi') === false) { - $key = $_SERVER['SERVER_PROTOCOL']; - } else { - // FastCGI - $key = 'Status:'; - } - - Common::sendHeader($key . ' ' . $status); - } - - /** * Returns a formatted Expires HTTP header for a certain number of days in the future. The result * can be used in a call to `header()`. */ diff --git a/core/Site.php b/core/Site.php index 96b7316b76..deefbd4911 100644 --- a/core/Site.php +++ b/core/Site.php @@ -10,6 +10,7 @@ namespace Piwik; use Exception; +use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Plugins\SitesManager\API; /** @@ -95,7 +96,7 @@ class Site protected static function setSite($idSite, $infoSite) { if (empty($idSite) || empty($infoSite)) { - throw new Exception("An unexpected website was found, check idSite in the request."); + throw new UnexpectedWebsiteFoundException("An unexpected website was found, check idSite in the request."); } /** diff --git a/core/Tracker.php b/core/Tracker.php index 38467fc65d..e0b7e78e3d 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -9,6 +9,7 @@ namespace Piwik; use Exception; +use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; use Piwik\Plugins\SitesManager\SiteUrls; use Piwik\Tracker\Cache; @@ -412,15 +413,16 @@ class Tracker * * @param Exception $e * @param bool $authenticated + * @param int $statusCode eg 500 */ - protected function exitWithException($e, $authenticated = false) + protected function exitWithException($e, $authenticated = false, $statusCode = 500) { if ($this->hasRedirectUrl()) { $this->performRedirectToUrlIfSet(); exit; } - Common::sendHeader('HTTP/1.1 500 Internal Server Error'); + Common::sendResponseCode($statusCode); error_log(sprintf("Error in Piwik (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e)))); if ($this->usingBulkTracking) { @@ -456,6 +458,7 @@ class Tracker } else { $this->sendResponse(); } + die(1); exit; } @@ -841,6 +844,9 @@ class Tracker } else { Common::printDebug("The request is invalid: empty request, or maybe tracking is disabled in the config.ini.php via record_statistics=0"); } + } catch (UnexpectedWebsiteFoundException $e) { + Common::printDebug("Exception: " . $e->getMessage()); + $this->exitWithException($e, $isAuthenticated, 400); } catch (DbException $e) { Common::printDebug("Exception: " . $e->getMessage()); $this->exitWithException($e, $isAuthenticated); @@ -937,4 +943,44 @@ class Tracker return array_unique($siteIds); } + /** + * @param $e + * @param $authenticated + */ + private function outputException($e, $authenticated) + { + if ($this->usingBulkTracking) { + // when doing bulk tracking we return JSON so the caller will know how many succeeded + $result = array( + 'status' => 'error', + 'tracked' => $this->countOfLoggedRequests + ); + // send error when in debug mode or when authenticated (which happens when doing log importing, + if ((isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) + || $authenticated + ) { + $result['message'] = $this->getMessageFromException($e); + } + Common::sendHeader('Content-Type: application/json'); + echo Common::json_encode($result); + return; + } + + if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) { + Common::sendHeader('Content-Type: text/html; charset=utf-8'); + $trailer = '<span style="color: #888888">Backtrace:<br /><pre>' . $e->getTraceAsString() . '</pre></span>'; + $headerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutHeader.tpl'); + $footerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutFooter.tpl'); + $headerPage = str_replace('{$HTML_TITLE}', 'Piwik › Error', $headerPage); + + echo $headerPage . '<p>' . $this->getMessageFromException($e) . '</p>' . $trailer . $footerPage; + } // If not debug, but running authenticated (eg. during log import) then we display raw errors + elseif ($authenticated) { + Common::sendHeader('Content-Type: text/html; charset=utf-8'); + echo $this->getMessageFromException($e); + } else { + $this->outputTransparentGif(); + } + } + } |