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