-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileResponse.php
399 lines (328 loc) · 11.1 KB
/
FileResponse.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
<?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 ICanBoogie\DateTime;
use InvalidArgumentException;
use LogicException;
use SplFileInfo;
use function array_filter;
use function base64_encode;
use function fclose;
use function finfo_file;
use function finfo_open;
use function fopen;
use function function_exists;
use function hash_file;
use function stream_copy_to_stream;
use const FILEINFO_MIME_TYPE;
/**
* Representation of an HTTP response delivering a file.
*
* @property-read SplFileInfo $file
* @property-read int $modified_time
* @property-read RequestRange|null $range
* @property-read bool $is_modified
*/
class FileResponse extends Response
{
/**
* Specifies the `ETag` header field of the response. If it is not defined the
* SHA-384 of the file is used instead.
*/
public const OPTION_ETAG = 'etag';
/**
* Specifies the expiration date as a {@link DateTime} instance or a relative date
* such as "+3 month", which maps to the `Expires` header field. The `max-age` directive of
* the `Cache-Control` header field is computed from the current time. If it is not
* defined {@link DEFAULT_EXPIRES} is used instead.
*/
public const OPTION_EXPIRES = 'expires';
/**
* Specifies the filename of the file and forces download. The following header are updated:
* `Content-Transfer-Encoding`, `Content-Description`, and `Content-Dispositon`.
*/
public const OPTION_FILENAME = 'filename';
/**
* Specifies the MIME of the file, which maps to the `Content-Type` header field. If it is
* not defined the MIME is guessed using `finfo::file()`.
*/
public const OPTION_MIME = 'mime';
public const DEFAULT_EXPIRES = '+1 month';
public const DEFAULT_MIME = 'application/octet-stream';
/**
* Hashes a file using SHA-348.
*
* @return string A base64 string
*/
public static function hash_file(string $pathname): string
{
return base64_encode(hash_file('sha384', $pathname, true));
}
private SplFileInfo $file;
protected function get_file(): SplFileInfo
{
return $this->file;
}
/**
* @param array<string, mixed> $options
* @param Headers|array<string, mixed> $headers
*/
public function __construct(
string|SplFileInfo $file,
private readonly Request $request,
array $options = [],
Headers|array $headers = []
) {
if (!$headers instanceof Headers) {
$headers = new Headers($headers);
}
$this->file = $this->ensure_file_info($file);
$this->apply_options($options, $headers);
$this->ensure_content_type($this->file, $headers);
parent::__construct(function () {
if (!$this->status->is_successful) {
return;
}
$this->send_file($this->file);
}, ResponseStatus::STATUS_OK, $headers);
}
/**
* Ensures the provided file is a {@link \SplFileInfo} instance.
*
* @throws LogicException if the file is a directory, or does not exist.
*/
private function ensure_file_info(mixed $file): SplFileInfo
{
$file = $file instanceof SplFileInfo ? $file : new SplFileInfo($file);
if ($file->isDir()) {
throw new LogicException("Expected file, got directory: $file");
}
if (!$file->isFile()) {
throw new LogicException("File does not exist: $file");
}
return $file;
}
/**
* @param array<string, mixed> $options
*/
private function apply_options(array $options, Headers $headers): void
{
foreach (array_filter($options) as $option => $value) {
switch ($option) {
case self::OPTION_ETAG:
if ($headers->etag) {
throw new InvalidArgumentException("Can only use one of OPTION_ETAG, HEADER_ETAG.");
}
$headers->etag = $value;
break;
case self::OPTION_EXPIRES:
$headers->expires = $value;
break;
case self::OPTION_FILENAME:
$headers['Content-Transfer-Encoding'] = 'binary';
$headers['Content-Description'] = 'File Transfer';
$headers->content_disposition->type = 'attachment';
$headers->content_disposition->filename = $value === true ? $this->file->getFilename() : $value;
break;
case self::OPTION_MIME:
$headers->content_type = $value;
break;
}
}
$headers->etag ??= $this->make_etag();
}
/**
* If the content type is empty in the headers the method tries to obtain it from
* the file, if it fails {@link DEFAULT_MIME} is used as fallback.
*/
private function ensure_content_type(SplFileInfo $file, Headers $headers): void
{
if ($headers->content_type->value) {
return;
}
$mime = null;
if (function_exists('finfo_file')) {
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
}
$headers->content_type = $mime ?? self::DEFAULT_MIME;
}
/**
* Changes the status to `Status::NOT_MODIFIED` if the request's Cache-Control has
* 'no-cache' and `is_modified` is false.
*/
public function __invoke(): void
{
$range = $this->range;
if ($range) {
if (!$range->is_satisfiable) {
$this->status = ResponseStatus::STATUS_REQUESTED_RANGE_NOT_SATISFIABLE;
} elseif (!$range->is_total) {
$this->status = ResponseStatus::STATUS_PARTIAL_CONTENT;
}
}
if ($this->request->headers->cache_control->cacheable != 'no-cache' && !$this->is_modified) {
$this->status = ResponseStatus::STATUS_NOT_MODIFIED;
}
parent::__invoke();
}
/**
* The following headers are always modified:
*
* - `Cache-Control`: sets _cacheable_ to _public_.
* - `Expires`: is set to "+1 month".
*
* If the status code is `Stauts::NOT_MODIFIED` the following headers are unset:
*
* - `Content-Type`
* - `Content-Length`
*
* Otherwise, the following header is set:
*
* - `Content-Type`:
*
* @inheritdoc
*/
protected function finalize(Headers &$headers, &$body): void
{
parent::finalize($headers, $body);
$status = $this->status->code;
$expires = $this->expires;
$headers->expires = $expires;
$headers->cache_control->cacheable = 'public';
$headers->cache_control->max_age = $expires->timestamp - DateTime::now()->timestamp;
if ($status === ResponseStatus::STATUS_NOT_MODIFIED) {
$this->finalize_for_not_modified($headers);
return;
}
if ($status === ResponseStatus::STATUS_PARTIAL_CONTENT) {
$this->finalize_for_partial_content($headers);
return;
}
$this->finalize_for_other($headers);
}
/**
* Finalizes the response for `Status::NOT_MODIFIED`.
*/
private function finalize_for_not_modified(Headers &$headers): void
{
$headers->content_length = null;
}
/**
* Finalizes the response for `Status::PARTIAL_CONTENT`.
*/
private function finalize_for_partial_content(Headers &$headers): void
{
$range = $this->range;
$headers->last_modified = $this->modified_time;
$headers['Content-Range'] = (string) $range;
$headers->content_length = $range->length;
}
/**
* Finalizes the response for status other than `Status::NOT_MODIFIED` or
* `Status::PARTIAL_CONTENT`.
*/
private function finalize_for_other(Headers &$headers): void
{
$headers->last_modified = $this->modified_time;
if (!$headers['Accept-Ranges']) {
$request = $this->request;
$headers['Accept-Ranges'] = $request->method->is_get() || $request->method->is_head() ? 'bytes' : 'none';
}
$headers->content_length = $this->file->getSize();
}
/**
* Sends the file.
*
* @param SplFileInfo $file
*
* @codeCoverageIgnore
*/
protected function send_file(SplFileInfo $file): void
{
[ $max_length, $offset ] = $this->resolve_max_length_and_offset();
$out = fopen('php://output', 'wb');
$source = fopen($file->getPathname(), 'rb');
stream_copy_to_stream($source, $out, $max_length, $offset);
fclose($out);
fclose($source);
}
/**
* Resolves `max_length` and `offset` parameters for stream copy.
*
* @return array{ 0: int, 1: int }
*/
private function resolve_max_length_and_offset(): array
{
$range = $this->range;
if ($range && $range->max_length) {
return [ $range->max_length, $range->offset ];
}
return [ -1, 0 ];
}
/**
* Returns a SHA-384 of the file.
*/
private function make_etag(): string
{
return self::hash_file($this->file->getPathname());
}
private function ensure_etag(): string
{
return $this->headers->etag ??= $this->make_etag();
}
/**
* If the date returned by the parent is empty the method returns a date created from
* {@link DEFAULT_EXPIRES}.
*/
protected function get_expires(): Headers\Date
{
$expires = parent::get_expires();
if (!$expires->is_empty) {
return $expires;
}
return Headers\Date::from(self::DEFAULT_EXPIRES); // @phpstan-ignore-line
}
/**
* Returns the timestamp at which the file was last modified.
*/
protected function get_modified_time(): false|int
{
return $this->file->getMTime();
}
/**
* Whether the file as been modified since the last response.
*
* The file is considered modified if one of the following conditions is met:
*
* - The `If-Modified-Since` request header is empty.
* - The `If-Modified-Since` value is inferior to `$modified_time`.
* - The `If-None-Match` value doesn't match `$etag`.
*/
protected function get_is_modified(): bool
{
$headers = $this->request->headers;
// HTTP/1.1
if ((string) $headers[Headers::HEADER_IF_NONE_MATCH] !== $this->headers->etag) {
return true;
}
// HTTP/1.0
$if_modified_since = $headers->if_modified_since;
return $if_modified_since->is_empty || $if_modified_since->timestamp < $this->modified_time;
}
private ?RequestRange $range_;
protected function get_range(): ?RequestRange
{
return $this->range_ ??= RequestRange::from(
$this->request->headers,
$this->file->getSize(),
$this->headers->etag
);
}
}