diff --git a/Slim/Handlers/Error.php b/Slim/Handlers/Error.php index 3b1962aa9..cfced9905 100644 --- a/Slim/Handlers/Error.php +++ b/Slim/Handlers/Error.php @@ -23,6 +23,18 @@ class Error { protected $displayErrorDetails; + /** + * Known handled content types + * + * @var array + */ + protected $knownContentTypes = [ + 'application/json', + 'application/xml', + 'text/xml', + 'text/html', + ]; + /** * Constructor * @@ -44,7 +56,7 @@ public function __construct($displayErrorDetails = false) */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception) { - $contentType = $this->determineContentType($request->getHeaderLine('Accept')); + $contentType = $this->determineContentType($request); switch ($contentType) { case 'application/json': $output = $this->renderJsonErrorMessage($exception); @@ -56,8 +68,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res break; case 'text/html': - default: - $contentType = 'text/html'; $output = $this->renderHtmlErrorMessage($exception); break; } @@ -97,8 +107,8 @@ private function renderHtmlErrorMessage(Exception $exception) $output = sprintf( "" . "%s

%s

%s", + "sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" . + "display:inline-block;width:65px;}

%s

%s", $title, $title, $html @@ -210,21 +220,18 @@ private function createCdataSection($content) } /** - * Read the accept header and determine which content type we know about - * is wanted. + * Determine which content type we know about is wanted using Accept header * - * @param string $acceptHeader Accept header from request + * @param ServerRequestInterface $request * @return string */ - private function determineContentType($acceptHeader) + private function determineContentType(ServerRequestInterface $request) { - $list = explode(',', $acceptHeader); - $known = ['application/json', 'application/xml', 'text/xml', 'text/html']; + $acceptHeader = $request->getHeaderLine('Accept'); + $selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes); - foreach ($list as $type) { - if (in_array($type, $known)) { - return $type; - } + if (count($selectedContentTypes)) { + return $selectedContentTypes[0]; } return 'text/html'; diff --git a/Slim/Handlers/NotAllowed.php b/Slim/Handlers/NotAllowed.php index e3af61978..e5b6768dc 100644 --- a/Slim/Handlers/NotAllowed.php +++ b/Slim/Handlers/NotAllowed.php @@ -20,6 +20,18 @@ */ class NotAllowed { + /** + * Known handled content types + * + * @var array + */ + protected $knownContentTypes = [ + 'application/json', + 'application/xml', + 'text/xml', + 'text/html', + ]; + /** * Invoke error handler * @@ -39,7 +51,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res $output = 'Allowed methods: ' . $allow; } else { $status = 405; - $contentType = $this->determineContentType($request->getHeaderLine('Accept')); + $contentType = $this->determineContentType($request); switch ($contentType) { case 'application/json': $output = '{"message":"Method not allowed. Must be one of: ' . $allow . '"}'; @@ -51,7 +63,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res break; case 'text/html': - default: $contentType = 'text/html'; $output = << @@ -92,21 +103,18 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res } /** - * Read the accept header and determine which content type we know about - * is wanted. + * Determine which content type we know about is wanted using Accept header * - * @param string $acceptHeader Accept header from request + * @param ServerRequestInterface $request * @return string */ - private function determineContentType($acceptHeader) + private function determineContentType(ServerRequestInterface $request) { - $list = explode(',', $acceptHeader); - $known = ['application/json', 'application/xml', 'text/xml', 'text/html']; - - foreach ($list as $type) { - if (in_array($type, $known)) { - return $type; - } + $acceptHeader = $request->getHeaderLine('Accept'); + $selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes); + + if (count($selectedContentTypes)) { + return $selectedContentTypes[0]; } return 'text/html'; diff --git a/Slim/Handlers/NotFound.php b/Slim/Handlers/NotFound.php index 797e9ca1b..efb257b76 100644 --- a/Slim/Handlers/NotFound.php +++ b/Slim/Handlers/NotFound.php @@ -20,6 +20,18 @@ */ class NotFound { + /** + * Known handled content types + * + * @var array + */ + protected $knownContentTypes = [ + 'application/json', + 'application/xml', + 'text/xml', + 'text/html', + ]; + /** * Invoke not found handler * @@ -31,7 +43,7 @@ class NotFound public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { - $contentType = $this->determineContentType($request->getHeaderLine('Accept')); + $contentType = $this->determineContentType($request); switch ($contentType) { case 'application/json': $output = '{"message":"Not found"}'; @@ -43,7 +55,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res break; case 'text/html': - default: $homeUrl = (string)($request->getUri()->withPath('')->withQuery('')->withFragment('')); $contentType = 'text/html'; $output = <<getHeaderLine('Accept'); + $selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes); + + if (count($selectedContentTypes)) { + return $selectedContentTypes[0]; } return 'text/html';