| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Http; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* HTTP-specific tasks. |
| 13 |
*/ |
| 14 |
class Context |
| 15 |
{ |
| 16 |
use \Packetery\Nette\SmartObject; |
| 17 |
/** @var IRequest */ |
| 18 |
private $request; |
| 19 |
/** @var IResponse */ |
| 20 |
private $response; |
| 21 |
public function __construct(IRequest $request, IResponse $response) |
| 22 |
{ |
| 23 |
$this->request = $request; |
| 24 |
$this->response = $response; |
| 25 |
} |
| 26 |
/** |
| 27 |
* Attempts to cache the sent entity by its last modification date. |
| 28 |
* @param string|int|\DateTimeInterface $lastModified |
| 29 |
*/ |
| 30 |
public function isModified($lastModified = null, ?string $etag = null) : bool |
| 31 |
{ |
| 32 |
if ($lastModified) { |
| 33 |
$this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified)); |
| 34 |
} |
| 35 |
if ($etag) { |
| 36 |
$this->response->setHeader('ETag', '"' . \addslashes($etag) . '"'); |
| 37 |
} |
| 38 |
$ifNoneMatch = $this->request->getHeader('If-None-Match'); |
| 39 |
if ($ifNoneMatch === '*') { |
| 40 |
$match = \true; |
| 41 |
// match, check if-modified-since |
| 42 |
} elseif ($ifNoneMatch !== null) { |
| 43 |
$etag = $this->response->getHeader('ETag'); |
| 44 |
if ($etag === null || \strpos(' ' . \strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === \false) { |
| 45 |
return \true; |
| 46 |
} else { |
| 47 |
$match = \true; |
| 48 |
// match, check if-modified-since |
| 49 |
} |
| 50 |
} |
| 51 |
$ifModifiedSince = $this->request->getHeader('If-Modified-Since'); |
| 52 |
if ($ifModifiedSince !== null) { |
| 53 |
$lastModified = $this->response->getHeader('Last-Modified'); |
| 54 |
if ($lastModified !== null && \strtotime($lastModified) <= \strtotime($ifModifiedSince)) { |
| 55 |
$match = \true; |
| 56 |
} else { |
| 57 |
return \true; |
| 58 |
} |
| 59 |
} |
| 60 |
if (empty($match)) { |
| 61 |
return \true; |
| 62 |
} |
| 63 |
$this->response->setCode(IResponse::S304_NotModified); |
| 64 |
return \false; |
| 65 |
} |
| 66 |
public function getRequest() : IRequest |
| 67 |
{ |
| 68 |
return $this->request; |
| 69 |
} |
| 70 |
public function getResponse() : IResponse |
| 71 |
{ |
| 72 |
return $this->response; |
| 73 |
} |
| 74 |
} |
| 75 |
|