1
|
|
|
<?php namespace Comodojo\Dispatcher\Response; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel; |
4
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
5
|
|
|
use \Comodojo\Dispatcher\Response\Headers; |
6
|
|
|
use \Comodojo\Dispatcher\Response\Status; |
7
|
|
|
use \Comodojo\Dispatcher\Response\Content; |
8
|
|
|
use \Comodojo\Dispatcher\Response\Location; |
9
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
10
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
11
|
|
|
use \Comodojo\Cookies\CookieManager; |
12
|
|
|
use \Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @package Comodojo Dispatcher |
16
|
|
|
* @author Marco Giovinazzi <[email protected]> |
17
|
|
|
* @author Marco Castiello <[email protected]> |
18
|
|
|
* @license GPL-3.0+ |
19
|
|
|
* |
20
|
|
|
* LICENSE: |
21
|
|
|
* |
22
|
|
|
* This program is free software: you can redistribute it and/or modify |
23
|
|
|
* it under the terms of the GNU Affero General Public License as |
24
|
|
|
* published by the Free Software Foundation, either version 3 of the |
25
|
|
|
* License, or (at your option) any later version. |
26
|
|
|
* |
27
|
|
|
* This program is distributed in the hope that it will be useful, |
28
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
29
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30
|
|
|
* GNU Affero General Public License for more details. |
31
|
|
|
* |
32
|
|
|
* You should have received a copy of the GNU Affero General Public License |
33
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
class Model extends DispatcherClassModel { |
37
|
|
|
|
38
|
|
|
private $content_type = "text/plain"; |
|
|
|
|
39
|
|
|
|
40
|
|
|
private $charset; |
|
|
|
|
41
|
|
|
|
42
|
5 |
|
public function __construct(Configuration $configuration, LoggerInterface $logger) { |
43
|
|
|
|
44
|
5 |
|
parent::__construct($configuration, $logger); |
45
|
|
|
|
46
|
5 |
|
$this->headers = new Headers(); |
|
|
|
|
47
|
|
|
|
48
|
5 |
|
$this->cookies = new CookieManager(); |
|
|
|
|
49
|
|
|
|
50
|
5 |
|
$this->status = new Status(); |
|
|
|
|
51
|
|
|
|
52
|
5 |
|
$this->content = new Content(); |
|
|
|
|
53
|
|
|
|
54
|
5 |
|
$this->location = new Location(); |
|
|
|
|
55
|
|
|
|
56
|
5 |
|
} |
57
|
|
|
|
58
|
2 |
|
public function consolidate(Request $request, Route $route = null) { |
59
|
|
|
|
60
|
2 |
|
$status = $this->status->get(); |
|
|
|
|
61
|
|
|
|
62
|
2 |
|
$output_class_name = "\\Comodojo\\Dispatcher\\Response\\Preprocessor\\Status".$status; |
63
|
|
|
|
64
|
|
|
// @TODO: this condition will be removed when all preprocessors ready |
65
|
2 |
|
if ( class_exists($output_class_name) ) { |
66
|
2 |
|
$output = new $output_class_name($this); |
67
|
2 |
|
} else { |
68
|
|
|
$output = new \Comodojo\Dispatcher\Response\Preprocessor\Status200($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$output->consolidate(); |
72
|
|
|
|
73
|
2 |
|
if ( $route != null ) { |
74
|
|
|
$this->setClientCache($request, $route); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// extra checks |
78
|
|
|
|
79
|
2 |
|
if ( $request->method->get() == 'HEAD' && !in_array($status, array(100,101,102,204,304)) ) { |
|
|
|
|
80
|
|
|
$length = $this->content->length(); |
|
|
|
|
81
|
|
|
$this->content->set(null); |
|
|
|
|
82
|
|
|
if ($length) $this->headers->set('Content-Length', $length); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
if ($this->headers->get('Transfer-Encoding') != null) { |
|
|
|
|
86
|
|
|
$this->headers->delete('Content-Length'); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
if ( $request->version->get() == '1.0' && false !== strpos($this->headers->get('Cache-Control'), 'no-cache')) { |
|
|
|
|
90
|
|
|
$this->headers->set('pragma', 'no-cache'); |
|
|
|
|
91
|
|
|
$this->headers->set('expires', -1); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
private function setClientCache(Request $request, Route $route) { |
97
|
|
|
|
98
|
|
|
$cache = strtoupper($route->getParameter('cache')); |
99
|
|
|
$ttl = $route->getParameter('ttl'); |
100
|
|
|
|
101
|
|
|
if ( |
102
|
|
|
($cache == 'CLIENT' || $cache == 'BOTH') && |
103
|
|
|
in_array($request->method->get(), array('GET', 'HEAD', 'POST', 'PUT')) && |
|
|
|
|
104
|
|
|
in_array($this->status->get(), array(200, 203, 300, 301, 302, 404, 410)) |
|
|
|
|
105
|
|
|
// @TODO: here we should also check for Cache-Control no-store or private; |
106
|
|
|
// the cache layer will be improoved in future versions. |
107
|
|
|
) { |
108
|
|
|
|
109
|
|
|
if ( $ttl > 0 ) { |
110
|
|
|
|
111
|
|
|
$this->headers->set("Cache-Control","max-age=".$ttl.", must-revalidate"); |
|
|
|
|
112
|
|
|
$this->headers->set("Expires",gmdate("D, d M Y H:i:s", (int)$this->getTimestamp() + $ttl)." GMT"); |
|
|
|
|
113
|
|
|
|
114
|
|
|
} else { |
115
|
|
|
|
116
|
|
|
$this->headers->set("Cache-Control","no-cache, must-revalidate"); |
|
|
|
|
117
|
|
|
$this->headers->set("Expires","Mon, 26 Jul 1997 05:00:00 GMT"); |
|
|
|
|
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.