1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Http;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * HTTP-specific tasks.
20: *
21: * @author David Grudl
22: *
23: * @property-read bool $modified
24: * @property-read IRequest $request
25: * @property-read IResponse $response
26: */
27: class Context extends Nette\Object
28: {
29: /** @var IRequest */
30: private $request;
31:
32: /** @var IResponse */
33: private $response;
34:
35:
36:
37: public function __construct(IRequest $request, IResponse $response)
38: {
39: $this->request = $request;
40: $this->response = $response;
41: }
42:
43:
44:
45: /**
46: * Attempts to cache the sent entity by its last modification date.
47: * @param string|int|DateTime last modified time
48: * @param string strong entity tag validator
49: * @return bool
50: */
51: public function isModified($lastModified = NULL, $etag = NULL)
52: {
53: if ($lastModified) {
54: $this->response->setHeader('Last-Modified', $this->response->date($lastModified));
55: }
56: if ($etag) {
57: $this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
58: }
59:
60: $ifNoneMatch = $this->request->getHeader('If-None-Match');
61: if ($ifNoneMatch === '*') {
62: $match = TRUE; // match, check if-modified-since
63:
64: } elseif ($ifNoneMatch !== NULL) {
65: $etag = $this->response->getHeader('ETag');
66:
67: if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
68: return TRUE;
69:
70: } else {
71: $match = TRUE; // match, check if-modified-since
72: }
73: }
74:
75: $ifModifiedSince = $this->request->getHeader('If-Modified-Since');
76: if ($ifModifiedSince !== NULL) {
77: $lastModified = $this->response->getHeader('Last-Modified');
78: if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
79: $match = TRUE;
80:
81: } else {
82: return TRUE;
83: }
84: }
85:
86: if (empty($match)) {
87: return TRUE;
88: }
89:
90: $this->response->setCode(IResponse::S304_NOT_MODIFIED);
91: return FALSE;
92: }
93:
94:
95:
96: /**
97: * @return IRequest
98: */
99: public function getRequest()
100: {
101: return $this->request;
102: }
103:
104:
105:
106: /**
107: * @return IResponse
108: */
109: public function getResponse()
110: {
111: return $this->response;
112: }
113:
114: }
115: