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
17: {
18: use Nette\SmartObject;
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|\DateTimeInterface 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', Helpers::formatDate($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: