1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NHttpContext extends NObject
22: {
23:
24: private $request;
25:
26:
27: private $response;
28:
29:
30:
31: public function __construct(IHttpRequest $request, IHttpResponse $response)
32: {
33: $this->request = $request;
34: $this->response = $response;
35: }
36:
37:
38:
39: 40: 41: 42: 43: 44:
45: public function isModified($lastModified = NULL, $etag = NULL)
46: {
47: if ($lastModified) {
48: $this->response->setHeader('Last-Modified', $this->response->date($lastModified));
49: }
50: if ($etag) {
51: $this->response->setHeader('ETag', '"' . addslashes($etag) . '"');
52: }
53:
54: $ifNoneMatch = $this->request->getHeader('If-None-Match');
55: if ($ifNoneMatch === '*') {
56: $match = TRUE;
57:
58: } elseif ($ifNoneMatch !== NULL) {
59: $etag = $this->response->getHeader('ETag');
60:
61: if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
62: return TRUE;
63:
64: } else {
65: $match = TRUE;
66: }
67: }
68:
69: $ifModifiedSince = $this->request->getHeader('If-Modified-Since');
70: if ($ifModifiedSince !== NULL) {
71: $lastModified = $this->response->getHeader('Last-Modified');
72: if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
73: $match = TRUE;
74:
75: } else {
76: return TRUE;
77: }
78: }
79:
80: if (empty($match)) {
81: return TRUE;
82: }
83:
84: $this->response->setCode(IHttpResponse::S304_NOT_MODIFIED);
85: return FALSE;
86: }
87:
88:
89:
90: 91: 92:
93: public function getRequest()
94: {
95: return $this->request;
96: }
97:
98:
99:
100: 101: 102:
103: public function getResponse()
104: {
105: return $this->response;
106: }
107:
108: }
109: