/
Response.php
313 lines (266 loc) · 7.29 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace Bundle\ServerBundle;
use Bundle\ServerBundle\ResponseInterface,
Bundle\ServerBundle\RequestInterface,
Bundle\ServerBundle\Request,
Symfony\Components\HttpKernel\ParameterBag,
Symfony\Components\HttpKernel\Response as SymfonyResponse;
/*
* This file is part of the ServerBundle package.
*
* (c) Pierre Minnieur <pm@pierre-minnieur.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @package ServerBundle
* @subpackage Response
* @author Pierre Minnieur <pm@pierre-minnieur.de>
*/
class Response implements ResponseInterface, \Serializable
{
protected $request;
protected $httpVersion;
protected $statusCode;
protected $statusText;
protected $headers;
protected $body;
/**
* @param Bundle\ServerBundle\RequestInterface $request
* @param string $httpVersion (optional)
* @param integer $statusCode (optional)
* @param string $statusText (optional)
* @param array $headers (optional)
* @param string $body (optional)
*/
public function __construct(RequestInterface $request, $httpVersion = Request::HTTP_11, $statusCode = 200, $statusText = 'OK', array $headers = array(), $body = null)
{
$this->request = $request;
$this->httpVersion = $httpVersion;
$this->statusCode = $statusCode;
$this->statusText = $statusText;
$this->headers = new ParameterBag();
$this->body = $body;
if (count($headers) > 0) {
$this->headers->replace($headers);
}
}
/**
* @return string
*/
public function getHttpVersion()
{
return $this->httpVersion;
}
/**
* @param string $httpVersion
*
* @throws \InvalidArgumentException If the HTTP version is not valid
*/
public function setHttpVersion($httpVersion)
{
if (!in_array($httpVersion, Request::getHttpVersions())) {
throw new \InvalidArgumentException(sprintf('HTTP "%s" version is not valid', $httpVersion));
}
$this->httpVersion = $httpVersion;
}
/**
* @return integer
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* @param integer $statusCode
* @param string $statusText (optional)
*
* @throws \InvalidArgumentException If the HTTP status code is not valid
*/
public function setStatusCode($statusCode, $statusText = null)
{
$this->statusCode = (integer) $statusCode;
if ($this->statusCode < 100 || $this->statusCode > 599) {
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $statusCode));
}
$this->statusText = false === $statusText ? '' : (null === $statusText ? SymfonyResponse::$statusTexts[$this->statusCode] : $statusText);
}
/**
* @return string
*/
public function getStatusText()
{
return $this->statusText;
}
/**
* @param string $statusText
*/
public function setStatusText($statusText)
{
$this->statusText = $statusText;
}
/**
* @param string $name
* @return mixed
*/
public function getHeader($name)
{
return $this->headers->get($name);
}
/**
* @param string $name
* @param mixed $value
*/
public function setHeader($name, $value)
{
$this->headers->set($name, $value);
}
/**
* @param string $name
* @return boolean
*/
public function hasHeader($name)
{
return $this->headers->has($name);
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers->all();
}
/**
* @param array $headers
*/
public function setHeaders(array $headers)
{
$this->headers->repace($headers);
}
/**
* @param array $headers
* @param boolean $replace (optional)
*/
public function addHeaders(array $headers)
{
foreach ($headers as $name => $value) {
$this->headers->set($name, $value);
}
}
/**
* @param string $name
*/
public function deleteHeader($name)
{
$this->headers->delete($name);
}
/**
* @param string $body
*/
public function getBody()
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* @return string
*/
public function toString()
{
// HTTP/0.9 (Simple Response)
if (Request::HTTP_09 == $this->httpVersion) {
return $this->body;
}
// HTTP/1.x (Full Response)
if ($this->request->getRequestMethod() == Request::METHOD_HEAD ||
$this->isEmpty() || $this->isInformational()) {
foreach ($this->headers->all() as $name => $value) {
if (false !== strpos(strtolower($name), 'content')) {
$this->headers->delete($name);
}
}
$this->body = null;
}
$message = sprintf("HTTP/%s %d %s\r\n", $this->httpVersion, $this->statusCode, $this->statusText);
foreach ($this->headers->all() as $name => $value) {
$message .= sprintf("%s: %s\r\n", trim($name), trim($value));
}
// body may be empty
$message .= "\r\n".$this->body;
return $message;
}
/**
* @see Response::toString()
*/
public function __toString()
{
return $this->toString();
}
/**
* @see \Serializable
*/
public function serialize()
{
return serialize(array($this->httpVersion, $this->statusCode, $this->statusText, $this->headers->all(), $this->body));
}
/**
* @see \Serializable
*/
public function unserialize($serialized)
{
list($this->httpVersion, $this->statusCode, $this->statusText, $headers, $this->body) = unserialize($serialized);
$this->headers->replace($headers);
}
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
public function isInvalid()
{
return $this->statusCode < 100 || $this->statusCode >= 600;
}
public function isInformational()
{
return $this->statusCode >= 100 && $this->statusCode < 200;
}
public function isSuccessful()
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
public function isRedirection()
{
return $this->statusCode >= 300 && $this->statusCode < 400;
}
public function isClientError()
{
return $this->statusCode >= 400 && $this->statusCode < 500;
}
public function isServerError()
{
return $this->statusCode >= 500 && $this->statusCode < 600;
}
public function isOk()
{
return 200 === $this->statusCode;
}
public function isForbidden()
{
return 403 === $this->statusCode;
}
public function isNotFound()
{
return 404 === $this->statusCode;
}
public function isRedirect()
{
return in_array($this->statusCode, array(301, 302, 303, 307));
}
public function isEmpty()
{
return in_array($this->statusCode, array(201, 204, 304));
}
}