-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.php
519 lines (438 loc) · 13.2 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
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\HTTP;
use Closure;
use ICanBoogie\Accessor\AccessorTrait;
use Stringable;
use Throwable;
use function header;
use function header_remove;
use function headers_sent;
use function ICanBoogie\format;
use function ob_get_clean;
use function ob_start;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* A response to an HTTP request.
*
* @property Status|int $status
* The status of the response.
* @property int|null $ttl
* The response's time-to-live in second for shared caches.
* Setting this property also adjust the `s-maxage` directive of the `Cache-Control` header according to
* the `Age` header.
* When the responses TTL is <= 0, the response may not be served from cache without first
* re-validating with the origin.
* @property int|null $age
* Shortcut to the `Age` header.
* @property Headers\Date|mixed $expires
* Adjusts the `Expires` header and the `max_age` directive of the `Cache-Control` header.
* @property-read bool $is_cacheable
* Whether the response is worth caching under any circumstance.
* Responses marked _private_ with an explicit `Cache-Control` directive are considered
* not cacheable. Responses with neither a freshness lifetime (Expires, max-age) nor cache validator
* (`Last-Modified`, `ETag`) are considered not cacheable.
* @property-read bool $is_fresh
* Whether the response is fresh.
* A response is considered fresh when its TTL is greater than 0.
* @property-read bool $is_validateable
* Whether the response includes header fields that can be used to validate the response
* with the origin server using a conditional GET request.
*
* @link https://tools.ietf.org/html/rfc2616
*/
class Response implements ResponseStatus
{
/**
* @uses get_age
* @uses set_age
* @uses get_cache_control
* @uses set_cache_control
* @uses get_content_length
* @uses set_content_length
* @uses get_content_type
* @uses set_content_type
* @uses get_etag
* @uses set_etag
* @uses get_expires
* @uses set_expires
* @uses get_date
* @uses set_date
* @uses get_is_cacheable
* @uses get_is_fresh
* @uses get_is_validateable
* @uses get_last_modified
* @uses set_last_modified
* @uses get_location
* @uses set_location
* @uses get_status
* @uses set_status
* @uses get_ttl
* @uses set_ttl
*/
use AccessorTrait;
public Headers $headers;
/**
* The HTTP protocol version (1.0 or 1.1), defaults to '1.1'
*/
public string $version = '1.1';
/**
* Initializes the `$body`, `$status`, `$headers`, and `$date` properties.
*
* @param int|Status $status The status code of the response.
* @param array<string, mixed>|Headers $headers The initial header fields of the response.
*/
public function __construct(
public string|Stringable|Closure|null $body = null,
int|Status $status = ResponseStatus::STATUS_OK,
Headers|array $headers = []
) {
if (!$headers instanceof Headers) {
$headers = new Headers($headers);
}
$this->headers = $headers;
if ($this->headers->date->is_empty) {
$this->headers->date = 'now';
}
$this->set_status($status);
}
/**
* Clones the `$headers` and `$status` properties.
*/
public function __clone()
{
$this->headers = clone $this->headers;
$this->status = clone $this->status;
}
/**
* Renders the response as an HTTP string.
*/
public function __toString(): string
{
try {
$header = clone $this->headers;
$body = $this->body;
$this->finalize($header, $body);
ob_start();
$this->send_body($body);
$body = ob_get_clean();
return "HTTP/$this->version $this->status\r\n"
. $header
. "\r\n"
. $body;
} catch (Throwable $e) {
return $e->getMessage();
}
}
/**
* Issues the HTTP response.
*
* {@see finalize()} is invoked to finalize the headers (a cloned actually)
* and the body. {@see send_headers} is invoked to send the headers and {@see send_body()}
*is invoked to send the body, if the body is not `null`.
*
* The body is not send in the following instances:
*
* - The finalized body is `null`.
* - The status is not ok.
*/
public function __invoke(): void
{
$headers = clone $this->headers;
$body = $this->body;
$this->finalize($headers, $body);
$this->send_headers($headers);
if ($body === null) {
return;
}
$this->send_body($body);
}
/**
* Finalize the body.
*
* Subclasses might want to override this method if they wish to alter the header or the body
* before the response is sent or transformed into a string.
*
* @param Headers $headers Reference to the final header.
* @param mixed $body Reference to the final body.
*/
protected function finalize(Headers &$headers, mixed &$body): void
{
if ($body instanceof Closure || !$body instanceof Stringable) {
return;
}
$body = (string) $body;
}
protected function send_headers(Headers $headers): bool // @codeCoverageIgnoreStart
{
if (headers_sent($file, $line)) {
trigger_error(
format("Cannot modify header information because it was already sent. Output started at !at.", [
'at' => $file . ':' . $line,
])
);
return false;
}
header_remove('Pragma');
header_remove('X-Powered-By');
header("HTTP/$this->version $this->status");
$headers();
return true;
} // @codeCoverageIgnoreEnd
protected function send_body(mixed $body): void
{
if ($body instanceof Closure) {
$body($this);
return;
}
echo $body;
}
/**
* Status of the HTTP response.
*/
private Status $status;
private function set_status(int|Status $status): void
{
$this->status = Status::from($status);
}
private function get_status(): Status
{
return $this->status;
}
/**
* Sets the value of the `Date` header field.
*/
protected function set_date(mixed $time): void
{
trigger_error('$response->date is deprecated use $response->headers->date instead.', E_USER_DEPRECATED);
$this->headers->date = $time;
}
/**
* Returns the value of the `Date` header field.
*/
protected function get_date(): Headers\Date
{
trigger_error('$response->date is deprecated use $response->headers->date instead.', E_USER_DEPRECATED);
return $this->headers->date;
}
/**
* Sets the value of the `Age` header field.
*
* @param int|null $age
*/
protected function set_age(?int $age): void
{
$this->headers['Age'] = $age;
}
/**
* Returns the age of the response.
*
* @return int|null
*/
protected function get_age(): ?int
{
$age = $this->headers['Age'];
if ($age) {
return (int) $age;
}
if (!$this->headers->date->is_empty) {
return max(0, time() - $this->headers->date->timestamp);
}
return null;
}
/**
* Sets the value of the `Expires` header field.
*
* The method updates the `max-age` Cache Control directive accordingly.
*/
protected function set_expires(mixed $time): void
{
$this->headers->expires = $time;
$expires = $this->headers->expires;
$this->headers->cache_control->max_age = $expires->is_empty ? null : $expires->timestamp - time();
}
/**
* Returns the value of the `Expires` header field.
*/
protected function get_expires(): Headers\Date
{
return $this->headers->expires;
}
/**
* @deprecated 6.0
* @see Headers::$cache_control
*/
protected function get_cache_control(): Headers\CacheControl
{
trigger_error(
'$response->cache_control is deprecated, use $response->headers->cache_control instead.',
E_USER_DEPRECATED
);
return $this->headers->cache_control;
}
/**
* @deprecated 6.0
* @see Headers::$cache_control
*/
protected function set_cache_control(?string $cache_directives): void
{
trigger_error(
'$response->cache_control is deprecated, use $response->headers->cache_control instead.',
E_USER_DEPRECATED
);
$this->headers->cache_control = $cache_directives;
}
/**
* @deprecated 6.0
* @see Headers::$content_length
*/
private function get_content_length(): ?int
{
trigger_error(
'$response->content_length is deprecated use $response->headers->content_length instead.',
E_USER_DEPRECATED
);
return $this->headers->content_length;
}
/**
* @deprecated 6.0
* @see Headers::$content_length
*/
private function set_content_length(?int $length): void
{
trigger_error(
'$response->content_length is deprecated use $response->headers->content_length instead.',
E_USER_DEPRECATED
);
$this->headers->content_length = $length;
}
/**
* @deprecated 6.0
* @see Headers::$content_type
*/
protected function get_content_type(): Headers\ContentType
{
trigger_error(
'$response->content_type is deprecated use $response->headers->content_type instead.',
E_USER_DEPRECATED
);
return $this->headers->content_type;
}
/**
* @deprecated 6.0
* @see Headers::$content_type
*/
protected function set_content_type(mixed $content_type): void
{
trigger_error(
'$response->content_type is deprecated use $response->headers->content_type instead.',
E_USER_DEPRECATED
);
$this->headers->content_type = $content_type;
}
/**
* @deprecated 6.0
* @see Headers::$etag
*/
private function get_etag(): ?string
{
trigger_error('$response->etag is deprecated use $response->headers->etag instead.', E_USER_DEPRECATED);
return $this->headers->etag;
}
/**
* @deprecated 6.0
* @see Headers::$etag
*/
private function set_etag(?string $value): void
{
trigger_error('$response->etag is deprecated use $response->headers->etag instead.', E_USER_DEPRECATED);
$this->headers->etag = $value;
}
/**
* @deprecated 6.0
* @see Headers::$last_modified
*/
private function get_last_modified(): Headers\Date
{
trigger_error(
'$response->last_modified is deprecated, use $response->headers->last_modified instead.',
E_USER_DEPRECATED
);
return $this->headers->last_modified;
}
/**
* @deprecated 6.0
* @see Headers::$last_modified
*/
private function set_last_modified(mixed $value): void
{
trigger_error(
'$response->last_modified is deprecated, use $response->headers->last_modified instead.',
E_USER_DEPRECATED
);
$this->headers->last_modified = $value;
}
/**
* @deprecated 6.0
* @see Headers::$location
*/
private function get_location(): ?string
{
trigger_error(
'$response->location is deprecated, use $response->headers->location instead.',
E_USER_DEPRECATED
);
return $this->headers->location;
}
/**
* @deprecated 6.0
* @see Headers::$location
*/
private function set_location(?string $url): void
{
trigger_error(
'$response->location is deprecated, use $response->headers->location instead.',
E_USER_DEPRECATED
);
$this->headers->location = $url;
}
private function get_ttl(): ?int
{
$max_age = $this->headers->cache_control->max_age;
if ($max_age) {
return $max_age - $this->age;
}
return null;
}
private function set_ttl(?int $seconds): void
{
$this->headers->cache_control->s_maxage = $this->age + $seconds;
}
private function get_is_validateable(): bool
{
return !$this->headers->last_modified->is_empty || $this->headers->etag;
}
private function get_is_cacheable(): bool
{
if (
!$this->status->is_cacheable
|| $this->headers->cache_control->no_store
|| $this->headers->cache_control->cacheable == 'private'
) {
return false;
}
return $this->is_validateable || $this->is_fresh;
}
private function get_is_fresh(): bool
{
return $this->ttl > 0;
}
}