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:
8: namespace Nette\Http;
9:
10: use Nette;
11:
12:
13: /**
14: * HTTP-specific tasks.
15: */
16: class Context extends Nette\Object
17: {
18: /** @var IRequest */
19: private $request;
20:
21: /** @var IResponse */
22: private $response;
23:
24:
25: public function __construct(IRequest $request, IResponse $response)
26: {
27: $this->request = $request;
28: $this->response = $response;
29: }
30:
31:
32: /**
33: * Attempts to cache the sent entity by its last modification date.
34: * @param string|int|\DateTime last modified time
35: * @param string strong entity tag validator
36: * @return bool
37: */
38: public function isModified($lastModified = NULL, $etag = NULL)
39: {
40: if ($lastModified) {
41: $this->response->setHeader('Last-Modified', Helpers::formatDate($lastModified));
42: }
43: if ($etag) {
44: $this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
45: }
46:
47: $ifNoneMatch = $this->request->getHeader('If-None-Match');
48: if ($ifNoneMatch === '*') {
49: $match = TRUE; // match, check if-modified-since
50:
51: } elseif ($ifNoneMatch !== NULL) {
52: $etag = $this->response->getHeader('ETag');
53:
54: if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
55: return TRUE;
56:
57: } else {
58: $match = TRUE; // match, check if-modified-since
59: }
60: }
61:
62: $ifModifiedSince = $this->request->getHeader('If-Modified-Since');
63: if ($ifModifiedSince !== NULL) {
64: $lastModified = $this->response->getHeader('Last-Modified');
65: if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
66: $match = TRUE;
67:
68: } else {
69: return TRUE;
70: }
71: }
72:
73: if (empty($match)) {
74: return TRUE;
75: }
76:
77: $this->response->setCode(IResponse::S304_NOT_MODIFIED);
78: return FALSE;
79: }
80:
81:
82: /**
83: * @return IRequest
84: */
85: public function getRequest()
86: {
87: return $this->request;
88: }
89:
90:
91: /**
92: * @return IResponse
93: */
94: public function getResponse()
95: {
96: return $this->response;
97: }
98:
99: }
100: