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