1
|
|
|
<?php namespace Comodojo\Dispatcher\Output; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Components\Model as DispatcherClassModel; |
4
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Header manipulation class for dispatcher |
8
|
|
|
* |
9
|
|
|
* This class manage response headers, from the status code to the custom |
10
|
|
|
* header the service will return. |
11
|
|
|
* |
12
|
|
|
* @package Comodojo dispatcher |
13
|
|
|
* @author Marco Giovinazzi <[email protected]> |
14
|
|
|
* @license GPL-3.0+ |
15
|
|
|
* |
16
|
|
|
* LICENSE: |
17
|
|
|
* |
18
|
|
|
* This program is free software: you can redistribute it and/or modify |
19
|
|
|
* it under the terms of the GNU Affero General Public License as |
20
|
|
|
* published by the Free Software Foundation, either version 3 of the |
21
|
|
|
* License, or (at your option) any later version. |
22
|
|
|
* |
23
|
|
|
* This program is distributed in the hope that it will be useful, |
24
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26
|
|
|
* GNU Affero General Public License for more details. |
27
|
|
|
* |
28
|
|
|
* You should have received a copy of the GNU Affero General Public License |
29
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
// Missing statuses: |
|
|
|
|
33
|
|
|
// // Informational 1xx |
34
|
|
|
// 100 => "Continue", |
35
|
|
|
// 101 => "Switching Protocols", |
36
|
|
|
// // Successful 2xx |
37
|
|
|
// 203 => "Non-Authoritative Information", |
38
|
|
|
// 205 => "Reset Content", |
39
|
|
|
// 206 => "Partial Content", |
40
|
|
|
// // Redirection 3xx |
41
|
|
|
// 300 => "Multiple Choices", |
42
|
|
|
// 305 => "Use Proxy", |
43
|
|
|
// // Client Error 4xx |
44
|
|
|
// 401 => "Unauthorized", |
45
|
|
|
// 402 => "Payment Required", |
46
|
|
|
// 406 => "Not Acceptable", |
47
|
|
|
// 407 => "Proxy Authentication Required", |
48
|
|
|
// 408 => "Request Timeout", |
49
|
|
|
// 409 => "Conflict", |
50
|
|
|
// 410 => "Gone", |
51
|
|
|
// 411 => "Length Required", |
52
|
|
|
// 412 => "Precondition Failed", |
53
|
|
|
// 413 => "Request Entity Too Large", |
54
|
|
|
// 414 => "Request-URI Too Long", |
55
|
|
|
// 415 => "Unsupported Media Type", |
56
|
|
|
// 416 => "Requested Range Not Satisfiable", |
57
|
|
|
// 417 => "Expectation Failed", |
58
|
|
|
// // Server Error 5xx |
59
|
|
|
// 502 => "Bad Gateway", |
60
|
|
|
// 504 => "Gateway Timeout", |
61
|
|
|
// 505 => "HTTP Version Not Supported" |
62
|
|
|
|
63
|
|
|
class Processor extends DispatcherClassModel { |
64
|
|
|
|
65
|
|
|
private $response; |
66
|
|
|
|
67
|
|
|
public function __construct(Configuration $configuration, Logger $logger, Response $response) { |
68
|
|
|
|
69
|
|
|
parent::__construct($configuration, $logger); |
70
|
|
|
|
71
|
|
|
$this->response = $response; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function compose() { |
|
|
|
|
76
|
|
|
|
77
|
|
|
$status = $this->response->status()->get(); |
78
|
|
|
|
79
|
|
|
$content = $this->response->content(); |
80
|
|
|
|
81
|
|
|
$cookies = $this->response->cookies(); |
82
|
|
|
|
83
|
|
|
$headers = $this->response->headers(); |
84
|
|
|
|
85
|
|
|
$location = $this->response->location(); |
86
|
|
|
|
87
|
|
|
// return value, just in case... |
88
|
|
|
$return = $content->get(); |
|
|
|
|
89
|
|
|
|
90
|
|
|
switch ($status) { |
91
|
|
|
|
92
|
|
|
case 200: //OK |
93
|
|
|
|
94
|
|
|
header('Content-Length: '.$content->length()); |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
|
98
|
|
View Code Duplication |
case 202: //Accepted |
|
|
|
|
99
|
|
|
|
100
|
|
|
//PLEASE NOTE: according to HTTP/1.1, 202 header SHOULD HAVE status description in body... just in case |
101
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 202 Accepted'); |
102
|
|
|
header('Status: 202 Accepted'); |
103
|
|
|
header('Content-Length: '.$content->length()); |
104
|
|
|
|
105
|
|
|
break; |
106
|
|
|
|
107
|
|
|
case 204: //OK - No Content |
108
|
|
|
|
109
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 204 No Content'); |
110
|
|
|
header('Status: 204 No Content'); |
111
|
|
|
header('Content-Length: 0',true); |
112
|
|
|
|
113
|
|
|
$return = null; |
|
|
|
|
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
|
117
|
|
|
case 201: //Created |
118
|
|
|
case 301: //Moved Permanent |
119
|
|
|
case 302: //Found |
120
|
|
|
case 303: //See Other |
121
|
|
|
case 307: //Temporary Redirect |
122
|
|
|
|
123
|
|
|
header("Location: ".$location->get(),true,$status); |
124
|
|
|
|
125
|
|
|
break; |
126
|
|
|
|
127
|
|
|
case 304: //Not Modified |
128
|
|
|
|
129
|
|
|
$last_modified = $headers->get('Last-Modified'); |
130
|
|
|
|
131
|
|
|
if ( is_null($last_modified) ) { |
132
|
|
|
|
133
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified'); |
134
|
|
|
|
135
|
|
|
} else if ( is_int($last_modified) ) { |
136
|
|
|
|
137
|
|
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT', true, 304); |
138
|
|
|
|
139
|
|
|
} else { |
140
|
|
|
|
141
|
|
|
header('Last-Modified: '.$last_modified, true, 304); |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
header('Content-Length: '.$content->length()); |
146
|
|
|
$headers->remove('Last-Modified'); |
147
|
|
|
|
148
|
|
|
break; |
149
|
|
|
|
150
|
|
View Code Duplication |
case 400: //Bad Request |
|
|
|
|
151
|
|
|
|
152
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 400 Bad Request', true, 400); |
153
|
|
|
header('Content-Length: '.$content->length()); |
154
|
|
|
|
155
|
|
|
break; |
156
|
|
|
|
157
|
|
|
case 403: |
158
|
|
|
|
159
|
|
|
header('Origin not allowed', true, 403); //Not originated from allowed source |
160
|
|
|
|
161
|
|
|
break; |
162
|
|
|
|
163
|
|
View Code Duplication |
case 404: //Not Found |
|
|
|
|
164
|
|
|
|
165
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 404 Not Found'); |
166
|
|
|
header('Status: 404 Not Found'); |
167
|
|
|
header('Content-Length: '.$content->length()); |
168
|
|
|
|
169
|
|
|
break; |
170
|
|
|
|
171
|
|
|
case 405: //Not allowed |
172
|
|
|
|
173
|
|
|
header('Allow: ' . $value, true, 405); |
|
|
|
|
174
|
|
|
|
175
|
|
|
break; |
176
|
|
|
|
177
|
|
|
case 500: //Internal Server Error |
178
|
|
|
|
179
|
|
|
header('500 Internal Server Error', true, 500); |
180
|
|
|
header('Content-Length: '.$content->length()); |
181
|
|
|
|
182
|
|
|
break; |
183
|
|
|
|
184
|
|
|
case 501: //Not implemented |
185
|
|
|
|
186
|
|
|
header('Allow: ' . $value, true, 501); |
187
|
|
|
|
188
|
|
|
break; |
189
|
|
|
|
190
|
|
|
case 503: //Service Unavailable |
191
|
|
|
|
192
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable'); |
193
|
|
|
header('Status: 503 Service Temporarily Unavailable'); |
194
|
|
|
|
195
|
|
|
// if ( $value !== false AND @is_int($value) ) { |
|
|
|
|
196
|
|
|
// header('Retry-After: '.$value); |
|
|
|
|
197
|
|
|
// } |
198
|
|
|
|
199
|
|
|
break; |
200
|
|
|
|
201
|
|
|
default: |
202
|
|
|
|
203
|
|
|
header($_SERVER["SERVER_PROTOCOL"].' '.$this->response->status(), true, $status); |
204
|
|
|
header('Content-Length: '.$content->length()); |
205
|
|
|
|
206
|
|
|
break; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$headers->send(); |
211
|
|
|
|
212
|
|
|
$cookies->save(); |
213
|
|
|
|
214
|
|
|
return $content; |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public static function parse(Configuration $configuration, Logger $logger, Response $response) { |
219
|
|
|
|
220
|
|
|
$processor = new Processor($configuration, $logger, $response); |
221
|
|
|
|
222
|
|
|
return $processor->compose(); |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.