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