Skip to content

Handle redirections before creating HTML response #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\RedirectResponse;
use Zend\Diactoros\Response\Serializer;
use Zend\Diactoros\Stream;

Expand All @@ -34,8 +35,8 @@ public function __construct(DebugBarRenderer $debugbarRenderer)

/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
Expand All @@ -47,7 +48,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

$outResponse = $next($request, $response);

if (!$this->isHtmlAccepted($request)) {
if (!$this->isHtmlAccepted($request) || $this->isRedirectResponse($outResponse)) {
return $outResponse;
}

Expand All @@ -64,10 +65,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $outResponse;
}

$outResponseBody = Serializer::toString($outResponse);
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
$outResponseBody = Serializer::toString($outResponse);
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody);

return new HtmlResponse($result);
}
Expand All @@ -91,9 +92,9 @@ private function getStaticFile(UriInterface $uri)
return;
}

$stream = new Stream($fullPathToFile, 'r');
$stream = new Stream($fullPathToFile, 'r');
$staticResponse = new Response($stream);
$contentType = $this->getContentTypeByFileName($fullPathToFile);
$contentType = $this->getContentTypeByFileName($fullPathToFile);

return $staticResponse->withHeader('Content-type', $contentType);
}
Expand All @@ -108,13 +109,13 @@ private function getContentTypeByFileName($filename)
$ext = pathinfo($filename, PATHINFO_EXTENSION);

$map = [
'css' => 'text/css',
'js' => 'text/javascript',
'otf' => 'font/opentype',
'eot' => 'application/vnd.ms-fontobject',
'svg' => 'image/svg+xml',
'ttf' => 'application/font-sfnt',
'woff' => 'application/font-woff',
'css' => 'text/css',
'js' => 'text/javascript',
'otf' => 'font/opentype',
'eot' => 'application/vnd.ms-fontobject',
'svg' => 'image/svg+xml',
'ttf' => 'application/font-sfnt',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
];

Expand Down Expand Up @@ -147,13 +148,25 @@ private function isHtmlAccepted(ServerRequestInterface $request)

/**
* @param MessageInterface $message
* @param string $headerName
* @param string $value
* @param string $headerName
* @param string $value
*
* @return bool
*/
private function hasHeaderContains(MessageInterface $message, $headerName, $value)
{
return strpos($message->getHeaderLine($headerName), $value) !== false;
}

/**
* Returns a boolean TRUE for if the response is a redirect.
*
* @param MessageInterface $message
*
* @return bool
*/
private function isRedirectResponse(ResponseInterface $response)
{
return $response instanceof RedirectResponse;
Copy link
Member

@snapshotpl snapshotpl May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be working with Zend Diactoros, however we need to remember that's psr-7 implementation and someone can use other library. I prefere to check location header in response object

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, my first implementation was based on httpStatusCode 301 and 302 values. Since this repository depends on diactoros via composer.json and there are less-common status codes exists like 303, 304 etc.. i decided to check instance type. What you think about diactoros dependency?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will add diactoros dependency and then? What if someone use eg https://github.com/guzzle/psr7 ?

Copy link
Author

@edigu edigu May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that middleware already depends on diactoros anyway I got the point, diactoros based redirection check would break other psr implementations. I'll send a patch shortly.

}
}