1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class HttpContext extends Object
26: {
27:
28: private $request;
29:
30:
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: 45: 46: 47: 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;
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;
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: 96:
97: public function getRequest()
98: {
99: return $this->request;
100: }
101:
102:
103:
104: 105: 106:
107: public function getResponse()
108: {
109: return $this->response;
110: }
111:
112: }
113: