-
Notifications
You must be signed in to change notification settings - Fork 26
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
Truncating response body #55
base: 1.x
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,11 @@ | |
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use Slim\Psr7\Factory\StreamFactory; | ||
use function in_array; | ||
use function is_array; | ||
use function is_numeric; | ||
|
@@ -45,18 +47,28 @@ class Cache implements MiddlewareInterface | |
*/ | ||
protected $mustRevalidate; | ||
|
||
/** | ||
* @var StreamFactoryInterface | ||
*/ | ||
protected $streamFactory; | ||
|
||
/** | ||
* Create new HTTP cache | ||
* | ||
* @param string $type The cache type: "public" or "private" | ||
* @param int $maxAge The maximum age of client-side cache | ||
* @param bool $mustRevalidate must-revalidate | ||
*/ | ||
public function __construct(string $type = 'private', int $maxAge = 86400, bool $mustRevalidate = false) | ||
{ | ||
public function __construct( | ||
StreamFactoryInterface $streamFactory, | ||
string $type = 'private', | ||
int $maxAge = 86400, | ||
bool $mustRevalidate = false | ||
) { | ||
$this->type = $type; | ||
$this->maxAge = $maxAge; | ||
$this->mustRevalidate = $mustRevalidate; | ||
$this->streamFactory = $streamFactory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe for now it's better to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather require There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only the user should decide which StreamFactoryInterface implementation should be used in their application. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flavioheleno @odan thank you for taken the time to review the PR I’ll update when I get some time. |
||
} | ||
|
||
/** | ||
|
@@ -101,7 +113,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface | |
if ($ifNoneMatch) { | ||
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch); | ||
if (is_array($etagList) && (in_array($etag, $etagList) || in_array('*', $etagList))) { | ||
return $response->withStatus(304); | ||
return $response->withStatus(304) | ||
->withBody($this->streamFactory->createStream('')); | ||
} | ||
} | ||
} | ||
|
@@ -118,7 +131,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface | |
$ifModifiedSince = $request->getHeaderLine('If-Modified-Since'); | ||
|
||
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) { | ||
return $response->withStatus(304); | ||
return $response->withStatus(304) | ||
->withBody($this->streamFactory->createStream('')); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dependency is not needed.