This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Response.php
625 lines (566 loc) · 16.8 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<?php
/**
* @see https://github.com/zendframework/zend-http for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Http;
use Zend\Http\Exception\RuntimeException;
use Zend\Stdlib\ErrorHandler;
use Zend\Stdlib\ResponseInterface;
/**
* HTTP Response
*
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6
*/
class Response extends AbstractMessage implements ResponseInterface
{
/**#@+
* @const int Status codes
*/
const STATUS_CODE_CUSTOM = 0;
const STATUS_CODE_100 = 100;
const STATUS_CODE_101 = 101;
const STATUS_CODE_102 = 102;
const STATUS_CODE_200 = 200;
const STATUS_CODE_201 = 201;
const STATUS_CODE_202 = 202;
const STATUS_CODE_203 = 203;
const STATUS_CODE_204 = 204;
const STATUS_CODE_205 = 205;
const STATUS_CODE_206 = 206;
const STATUS_CODE_207 = 207;
const STATUS_CODE_208 = 208;
const STATUS_CODE_226 = 226;
const STATUS_CODE_300 = 300;
const STATUS_CODE_301 = 301;
const STATUS_CODE_302 = 302;
const STATUS_CODE_303 = 303;
const STATUS_CODE_304 = 304;
const STATUS_CODE_305 = 305;
const STATUS_CODE_306 = 306;
const STATUS_CODE_307 = 307;
const STATUS_CODE_308 = 308;
const STATUS_CODE_400 = 400;
const STATUS_CODE_401 = 401;
const STATUS_CODE_402 = 402;
const STATUS_CODE_403 = 403;
const STATUS_CODE_404 = 404;
const STATUS_CODE_405 = 405;
const STATUS_CODE_406 = 406;
const STATUS_CODE_407 = 407;
const STATUS_CODE_408 = 408;
const STATUS_CODE_409 = 409;
const STATUS_CODE_410 = 410;
const STATUS_CODE_411 = 411;
const STATUS_CODE_412 = 412;
const STATUS_CODE_413 = 413;
const STATUS_CODE_414 = 414;
const STATUS_CODE_415 = 415;
const STATUS_CODE_416 = 416;
const STATUS_CODE_417 = 417;
const STATUS_CODE_418 = 418;
const STATUS_CODE_422 = 422;
const STATUS_CODE_423 = 423;
const STATUS_CODE_424 = 424;
const STATUS_CODE_425 = 425;
const STATUS_CODE_426 = 426;
const STATUS_CODE_428 = 428;
const STATUS_CODE_429 = 429;
const STATUS_CODE_431 = 431;
const STATUS_CODE_451 = 451;
const STATUS_CODE_444 = 444;
const STATUS_CODE_499 = 499;
const STATUS_CODE_500 = 500;
const STATUS_CODE_501 = 501;
const STATUS_CODE_502 = 502;
const STATUS_CODE_503 = 503;
const STATUS_CODE_504 = 504;
const STATUS_CODE_505 = 505;
const STATUS_CODE_506 = 506;
const STATUS_CODE_507 = 507;
const STATUS_CODE_508 = 508;
const STATUS_CODE_510 = 510;
const STATUS_CODE_511 = 511;
const STATUS_CODE_599 = 599;
/**#@-*/
/**
* @var array Recommended Reason Phrases
*/
protected $recommendedReasonPhrases = [
// INFORMATIONAL CODES
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
// SUCCESS CODES
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-status',
208 => 'Already Reported',
226 => 'IM Used',
// REDIRECTION CODES
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy', // Deprecated
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
// CLIENT ERROR
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Too Early',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'Connection Closed Without Response',
451 => 'Unavailable For Legal Reasons',
499 => 'Client Closed Request',
// SERVER ERROR
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
511 => 'Network Authentication Required',
599 => 'Network Connect Timeout Error',
];
/**
* @var int Status code
*/
protected $statusCode = 200;
/**
* @var string|null Null means it will be looked up from the $reasonPhrase list above
*/
protected $reasonPhrase;
/**
* Populate object from string
*
* @param string $string
* @return self
* @throws Exception\InvalidArgumentException
*/
public static function fromString($string)
{
$lines = explode("\r\n", $string);
if (! is_array($lines) || count($lines) === 1) {
$lines = explode("\n", $string);
}
$firstLine = array_shift($lines);
$response = new static();
$response->parseStatusLine($firstLine);
/**
* @link https://tools.ietf.org/html/rfc7231#section-6.2.1
*/
if ($response->statusCode === static::STATUS_CODE_100) {
$next = array_shift($lines); // take next line
$next = empty($next) ? array_shift($lines) : $next; // take next or skip if empty
$response->parseStatusLine($next);
}
if (count($lines) === 0) {
return $response;
}
$isHeader = true;
$headers = $content = [];
foreach ($lines as $line) {
if ($isHeader && $line === '') {
$isHeader = false;
continue;
}
if ($isHeader) {
if (preg_match("/[\r\n]/", $line)) {
throw new Exception\RuntimeException('CRLF injection detected');
}
$headers[] = $line;
continue;
}
if (empty($content)
&& preg_match('/^[a-z0-9!#$%&\'*+.^_`|~-]+:$/i', $line)
) {
throw new Exception\RuntimeException('CRLF injection detected');
}
$content[] = $line;
}
if ($headers) {
$response->headers = implode("\r\n", $headers);
}
if ($content) {
$response->setContent(implode("\r\n", $content));
}
return $response;
}
/**
* @param string $line
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
protected function parseStatusLine($line)
{
$regex = '/^HTTP\/(?P<version>1\.[01]|2) (?P<status>\d{3})(?:[ ]+(?P<reason>.*))?$/';
$matches = [];
if (! preg_match($regex, $line, $matches)) {
throw new Exception\InvalidArgumentException(
'A valid response status line was not found in the provided string'
);
}
$this->version = $matches['version'];
$this->setStatusCode($matches['status']);
$this->setReasonPhrase((isset($matches['reason']) ? $matches['reason'] : ''));
}
/**
* @return Header\SetCookie[]
*/
public function getCookie()
{
return $this->getHeaders()->get('Set-Cookie');
}
/**
* Set HTTP status code and (optionally) message
*
* @param int $code
* @throws Exception\InvalidArgumentException
* @return self
*/
public function setStatusCode($code)
{
$const = get_class($this) . '::STATUS_CODE_' . $code;
if (! is_numeric($code) || ! defined($const)) {
$code = is_scalar($code) ? $code : gettype($code);
throw new Exception\InvalidArgumentException(sprintf(
'Invalid status code provided: "%s"',
$code
));
}
return $this->saveStatusCode($code);
}
/**
* Retrieve HTTP status code
*
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* Set custom HTTP status code
*
* @param int $code
* @throws Exception\InvalidArgumentException
* @return self
*/
public function setCustomStatusCode($code)
{
if (! is_numeric($code)) {
$code = is_scalar($code) ? $code : gettype($code);
throw new Exception\InvalidArgumentException(sprintf(
'Invalid status code provided: "%s"',
$code
));
}
return $this->saveStatusCode($code);
}
/**
* Assign status code
*
* @param int $code
* @return self
*/
protected function saveStatusCode($code)
{
$this->reasonPhrase = null;
$this->statusCode = (int) $code;
return $this;
}
/**
* @param string $reasonPhrase
* @return self
*/
public function setReasonPhrase($reasonPhrase)
{
$this->reasonPhrase = trim($reasonPhrase);
return $this;
}
/**
* Get HTTP status message
*
* @return string
*/
public function getReasonPhrase()
{
if (null == $this->reasonPhrase && isset($this->recommendedReasonPhrases[$this->statusCode])) {
$this->reasonPhrase = $this->recommendedReasonPhrases[$this->statusCode];
}
return $this->reasonPhrase;
}
/**
* Get the body of the response
*
* @return string
*/
public function getBody()
{
$body = (string) $this->getContent();
$transferEncoding = $this->getHeaders()->get('Transfer-Encoding');
if (! empty($transferEncoding)) {
if (strtolower($transferEncoding->getFieldValue()) === 'chunked') {
$body = $this->decodeChunkedBody($body);
}
}
$contentEncoding = $this->getHeaders()->get('Content-Encoding');
if (! empty($contentEncoding)) {
$contentEncoding = $contentEncoding->getFieldValue();
if ($contentEncoding === 'gzip') {
$body = $this->decodeGzip($body);
} elseif ($contentEncoding === 'deflate') {
$body = $this->decodeDeflate($body);
}
}
return $body;
}
/**
* Does the status code indicate a client error?
*
* @return bool
*/
public function isClientError()
{
$code = $this->getStatusCode();
return ($code < 500 && $code >= 400);
}
/**
* Is the request forbidden due to ACLs?
*
* @return bool
*/
public function isForbidden()
{
return (403 === $this->getStatusCode());
}
/**
* Is the current status "informational"?
*
* @return bool
*/
public function isInformational()
{
$code = $this->getStatusCode();
return ($code >= 100 && $code < 200);
}
/**
* Does the status code indicate the resource is not found?
*
* @return bool
*/
public function isNotFound()
{
return (404 === $this->getStatusCode());
}
/**
* Does the status code indicate the resource is gone?
*
* @return bool
*/
public function isGone()
{
return (410 === $this->getStatusCode());
}
/**
* Do we have a normal, OK response?
*
* @return bool
*/
public function isOk()
{
return (200 === $this->getStatusCode());
}
/**
* Does the status code reflect a server error?
*
* @return bool
*/
public function isServerError()
{
$code = $this->getStatusCode();
return (500 <= $code && 600 > $code);
}
/**
* Do we have a redirect?
*
* @return bool
*/
public function isRedirect()
{
$code = $this->getStatusCode();
return (300 <= $code && 400 > $code);
}
/**
* Was the response successful?
*
* @return bool
*/
public function isSuccess()
{
$code = $this->getStatusCode();
return (200 <= $code && 300 > $code);
}
/**
* Render the status line header
*
* @return string
*/
public function renderStatusLine()
{
$status = sprintf(
'HTTP/%s %d %s',
$this->getVersion(),
$this->getStatusCode(),
$this->getReasonPhrase()
);
return trim($status);
}
/**
* Render entire response as HTTP response string
*
* @return string
*/
public function toString()
{
$str = $this->renderStatusLine() . "\r\n";
$str .= $this->getHeaders()->toString();
$str .= "\r\n";
$str .= $this->getContent();
return $str;
}
/**
* Decode a "chunked" transfer-encoded body and return the decoded text
*
* @param string $body
* @return string
* @throws Exception\RuntimeException
*/
protected function decodeChunkedBody($body)
{
$decBody = '';
$offset = 0;
while (true) {
if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m, 0, $offset)) {
if (trim(substr($body, $offset))) {
// Message was not consumed completely!
throw new Exception\RuntimeException(
'Error parsing body - doesn\'t seem to be a chunked message'
);
}
// Message was consumed completely
break;
}
$length = hexdec(trim($m[1]));
$cut = strlen($m[0]);
$decBody .= substr($body, $offset + $cut, $length);
$offset += $cut + $length + 2;
}
return $decBody;
}
/**
* Decode a gzip encoded message (when Content-encoding = gzip)
*
* Currently requires PHP with zlib support
*
* @param string $body
* @return string
* @throws Exception\RuntimeException
*/
protected function decodeGzip($body)
{
if (! function_exists('gzinflate')) {
throw new Exception\RuntimeException(
'zlib extension is required in order to decode "gzip" encoding'
);
}
if ($this->getHeaders()->has('content-length')
&& 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) {
return '';
}
ErrorHandler::start();
$return = gzinflate(substr($body, 10));
$test = ErrorHandler::stop();
if ($test) {
throw new Exception\RuntimeException(
'Error occurred during gzip inflation',
0,
$test
);
}
return $return;
}
/**
* Decode a zlib deflated message (when Content-encoding = deflate)
*
* Currently requires PHP with zlib support
*
* @param string $body
* @return string
* @throws Exception\RuntimeException
*/
protected function decodeDeflate($body)
{
if (! function_exists('gzuncompress')) {
throw new Exception\RuntimeException(
'zlib extension is required in order to decode "deflate" encoding'
);
}
if ($this->getHeaders()->has('content-length')
&& 0 === (int) $this->getHeaders()->get('content-length')->getFieldValue()) {
return '';
}
/**
* Some servers (IIS ?) send a broken deflate response, without the
* RFC-required zlib header.
*
* We try to detect the zlib header, and if it does not exist we
* teat the body is plain DEFLATE content.
*
* This method was adapted from PEAR HTTP_Request2 by (c) Alexey Borzov
*
* @link http://framework.zend.com/issues/browse/ZF-6040
*/
$zlibHeader = unpack('n', substr($body, 0, 2));
if ($zlibHeader[1] % 31 === 0) {
return gzuncompress($body);
}
return gzinflate($body);
}
}