-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResponse.php
More file actions
273 lines (248 loc) · 8.66 KB
/
Copy pathResponse.php
File metadata and controls
273 lines (248 loc) · 8.66 KB
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
<?php
declare(strict_types=1);
namespace Rancoud\Http\Message;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
class Response implements ResponseInterface
{
use MessageTrait;
/** @var string[] */
public const array PHRASES = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
103 => 'Early Hints',
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',
210 => 'Content Different',
226 => 'IM Used',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
310 => 'Too many Redirects',
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 Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'No Response',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
451 => 'Unavailable For Legal Reasons',
456 => 'Unrecoverable Error',
495 => 'SSL Certificate Error',
496 => 'SSL Certificate Required',
497 => 'HTTP Request Sent to HTTPS Port',
499 => 'Client has closed connection',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not extended',
511 => 'Network Authentication Required',
520 => 'Unknown Error',
521 => 'Web Server Is Down',
522 => 'Connection Timed Out',
523 => 'Origin Is Unreachable',
524 => 'A Timeout Occurred',
525 => 'SSL Handshake Failed',
526 => 'Invalid SSL Certificate',
527 => 'Railgun Error',
599 => 'Network Connect Timeout Error',
701 => 'Meh',
702 => 'Emacs',
703 => 'Explosion',
704 => 'Goto Fail',
705 => 'I wrote the code and missed the necessary validation by an oversight (see 795)',
706 => 'Delete Your Account',
707 => 'Can\'t quit vi',
710 => 'PHP',
711 => 'Convenience Store',
712 => 'NoSQL',
718 => 'I am not a teapot',
719 => 'Haskell',
720 => 'Unpossible',
721 => 'Known Unknowns',
722 => 'Unknown Unknowns',
723 => 'Tricky',
724 => 'This line should be unreachable',
725 => 'It works on my machine',
726 => 'It\'s a feature, not a bug',
727 => '32 bits is plenty',
728 => 'It works in my timezone',
730 => 'Fucking npm',
731 => 'Fucking Rubygems',
732 => 'Fucking Unic💩de',
733 => 'Fucking Deadlocks',
734 => 'Fucking Deferreds',
735 => 'Fucking IE',
736 => 'Fucking Race Conditions',
737 => 'FuckThreadsing',
738 => 'Fucking Exactly-once Delivery',
739 => 'Fucking Windows',
740 => 'Got the brains trust on the case.',
750 => 'Didn\'t bother to compile it',
753 => 'Syntax Error',
754 => 'Too many semi-colons',
755 => 'Not enough semi-colons',
756 => 'Insufficiently polite',
757 => 'Excessively polite',
759 => 'Unexpected "T_PAAMAYIM_NEKUDOTAYIM"',
761 => 'Hungover',
762 => 'Stoned',
763 => 'Under-Caffeinated',
764 => 'Over-Caffeinated',
765 => 'Railscamp',
766 => 'Sober',
767 => 'Drunk',
768 => 'Accidentally Took Sleeping Pills Instead Of Migraine Pills During Crunch Week',
771 => 'Cached for too long',
772 => 'Not cached long enough',
773 => 'Not cached at all',
774 => 'Why was this cached?',
775 => 'Out of cash',
776 => 'Error on the Exception',
777 => 'Coincidence',
778 => 'Off By One Error',
779 => 'Off By Too Many To Count Error',
780 => 'Project owner not responding',
781 => 'Operations',
782 => 'QA',
783 => 'It was a customer request, honestly',
784 => 'Management, obviously',
785 => 'TPS Cover Sheet not attached',
786 => 'Try it now',
787 => 'Further Funding Required',
788 => 'Designer\'s final designs weren\'t',
789 => 'Not my department',
791 => 'The Internet shut down due to copyright restrictions',
792 => 'Climate change driven catastrophic weather event',
793 => 'Zombie Apocalypse',
794 => 'Someone let PG near a REPL',
795 => '#heartbleed (see 705)',
796 => 'Some DNS fuckery idno',
797 => 'This is the last page of the Internet. Go back',
798 => 'I checked the db backups cupboard and the cupboard was bare',
799 => 'End of the world'
];
protected string $reasonPhrase = '';
protected int $statusCode = 200;
/**
* @param resource|StreamInterface|string|null $body
*
* @throws \InvalidArgumentException
*/
public function __construct(
int $status = 200,
array $headers = [],
mixed $body = null,
string $version = '1.1',
?string $reason = null
) {
if (!isset(static::PHRASES[$status])) {
throw new \InvalidArgumentException('Status code has to be an integer between 100 and 799');
}
$this->statusCode = $status;
$this->setHeaders($headers);
if ($body !== '' && $body !== null) {
$this->stream = Stream::create($body);
}
$this->protocol = $this->validateProtocolVersion($version);
if (($reason === null || $reason === '') && isset(static::PHRASES[$this->statusCode])) {
$this->reasonPhrase = static::PHRASES[$status];
} else {
$this->reasonPhrase = $reason ?? '';
}
}
public function getStatusCode(): int
{
return $this->statusCode;
}
/** @throws \InvalidArgumentException */
public function withStatus(int $code, string $reasonPhrase = ''): self
{
if (!isset(static::PHRASES[$code])) {
throw new \InvalidArgumentException('Status code has to be an integer between 100 and 799');
}
$new = clone $this;
$new->statusCode = $code;
if (($reasonPhrase === null || $reasonPhrase === '') && isset(static::PHRASES[$this->statusCode])) {
$reasonPhrase = static::PHRASES[$new->statusCode];
}
$new->reasonPhrase = $reasonPhrase ?? '';
return $new;
}
public function getReasonPhrase(): string
{
return $this->reasonPhrase;
}
/**
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function send(int $bodyChunkSize = 8192): void
{
$statusCode = $this->getStatusCode();
$httpLine = \sprintf(
'HTTP/%s %s %s',
$this->getProtocolVersion(),
$statusCode,
$this->getReasonPhrase()
);
\header($httpLine, true, $statusCode);
foreach ($this->getHeaders() as $name => $values) {
foreach ($values as $value) {
\header("{$name}: {$value}", false);
}
}
$stream = $this->getBody();
if ($stream->isSeekable()) {
$stream->rewind();
}
while (!$stream->eof()) {
echo $stream->read($bodyChunkSize);
}
}
}