Skip to content

Commit

Permalink
Merge branch 'juliangut-3.x' into 3.x
Browse files Browse the repository at this point in the history
Closes #1574
  • Loading branch information
akrabat committed Nov 9, 2015
2 parents 4b91e8f + 7dcad1d commit c181a3a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 41 deletions.
37 changes: 22 additions & 15 deletions Slim/Handlers/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
Expand All @@ -56,8 +68,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
break;

case 'text/html':
default:
$contentType = 'text/html';
$output = $this->renderHtmlErrorMessage($exception);
break;
}
Expand Down Expand Up @@ -97,8 +107,8 @@ private function renderHtmlErrorMessage(Exception $exception)
$output = sprintf(
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
"<title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," .
"sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;" .
"width:65px;}</style></head><body><h1>%s</h1>%s</body></html>",
"sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" .
"display:inline-block;width:65px;}</style></head><body><h1>%s</h1>%s</body></html>",
$title,
$title,
$html
Expand Down Expand Up @@ -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';
Expand Down
34 changes: 21 additions & 13 deletions Slim/Handlers/NotAllowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 . '"}';
Expand All @@ -51,7 +63,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
break;

case 'text/html':
default:
$contentType = 'text/html';
$output = <<<END
<html>
Expand Down Expand Up @@ -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';
Expand Down
34 changes: 21 additions & 13 deletions Slim/Handlers/NotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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"}';
Expand All @@ -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 = <<<END
Expand Down Expand Up @@ -91,21 +102,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';
Expand Down

0 comments on commit c181a3a

Please sign in to comment.