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: public function __construct(IHttpRequest $request, IHttpResponse $response)
35: {
36: $this->request = $request;
37: $this->response = $response;
38: }
39:
40:
41: /**
42: * Attempts to cache the sent entity by its last modification date.
43: * @param string|int|DateTime last modified time
44: * @param string strong entity tag validator
45: * @return bool
46: */
47: public function isModified($lastModified = NULL, $etag = NULL)
48: {
49: if ($lastModified) {
50: $this->response->setHeader('Last-Modified', $this->response->date($lastModified));
51: }
52: if ($etag) {
53: $this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
54: }
55:
56: $ifNoneMatch = $this->request->getHeader('If-None-Match');
57: if ($ifNoneMatch === '*') {
58: $match = TRUE; // match, check if-modified-since
59:
60: } elseif ($ifNoneMatch !== NULL) {
61: $etag = $this->response->getHeader('ETag');
62:
63: if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
64: return TRUE;
65:
66: } else {
67: $match = TRUE; // match, check if-modified-since
68: }
69: }
70:
71: $ifModifiedSince = $this->request->getHeader('If-Modified-Since');
72: if ($ifModifiedSince !== NULL) {
73: $lastModified = $this->response->getHeader('Last-Modified');
74: if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
75: $match = TRUE;
76:
77: } else {
78: return TRUE;
79: }
80: }
81:
82: if (empty($match)) {
83: return TRUE;
84: }
85:
86: $this->response->setCode(IHttpResponse::S304_NOT_MODIFIED);
87: return FALSE;
88: }
89:
90:
91: /**
92: * @return IHttpRequest
93: */
94: public function getRequest()
95: {
96: return $this->request;
97: }
98:
99:
100: /**
101: * @return IHttpResponse
102: */
103: public function getResponse()
104: {
105: return $this->response;
106: }
107:
108: }
109: