Repository navigation
Expand file tree
/
Copy pathserver.php
More file actions
84 lines (71 loc) · 2.59 KB
/
Copy pathserver.php
File metadata and controls
84 lines (71 loc) · 2.59 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
<?php
$publicPath = getcwd();
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? '');
$types = [
'js' => 'text/javascript; charset=utf-8',
'mjs' => 'text/javascript; charset=utf-8',
'css' => 'text/css; charset=utf-8',
'json' => 'application/json',
'svg' => 'image/svg+xml',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
'avif' => 'image/avif',
'ico' => 'image/x-icon',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'txt' => 'text/plain; charset=utf-8',
'webmanifest' => 'application/manifest+json',
];
$file = $uri !== '/' ? realpath($publicPath.$uri) : false;
$extension = $file ? strtolower(pathinfo($file, PATHINFO_EXTENSION)) : '';
if (
$file === false
|| ! is_file($file)
|| ! str_starts_with($file, rtrim(realpath($publicPath), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR)
|| ! isset($types[$extension])
|| ! in_array($_SERVER['REQUEST_METHOD'], ['GET', 'HEAD'], true)
) {
// Compress pages and API responses the way the nginx configuration does.
ini_set('zlib.output_compression', '1');
return require __DIR__.'/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php';
}
$immutable = str_starts_with($uri, '/build/assets/');
$body = $file;
$encoding = null;
$accepted = [];
foreach (explode(',', strtolower($_SERVER['HTTP_ACCEPT_ENCODING'] ?? '')) as $part) {
$pieces = array_map('trim', explode(';', $part));
$quality = preg_match('/^q=([\d.]+)$/', $pieces[1] ?? '', $q) ? (float) $q[1] : 1.0;
$accepted[$pieces[0]] = $quality;
}
foreach (['br' => '.br', 'gzip' => '.gz'] as $name => $suffix) {
if (($accepted[$name] ?? 0) > 0 && is_file($file.$suffix)) {
$body = $file.$suffix;
$encoding = $name;
break;
}
}
$etag = sprintf('"%x-%x%s"', filemtime($body), filesize($body), $encoding ? '-'.$encoding : '');
header('Content-Type: '.$types[$extension]);
header('Vary: Accept-Encoding');
header('ETag: '.$etag);
header('Cache-Control: '.($immutable ? 'public, max-age=31536000, immutable' : 'public, no-cache'));
header('X-Content-Type-Options: nosniff');
$match = array_map('trim', explode(',', $_SERVER['HTTP_IF_NONE_MATCH'] ?? ''));
if (in_array($etag, $match, true) || in_array('W/'.$etag, $match, true)) {
http_response_code(304);
return true;
}
if ($encoding) {
header('Content-Encoding: '.$encoding);
}
header('Content-Length: '.filesize($body));
if ($_SERVER['REQUEST_METHOD'] !== 'HEAD') {
readfile($body);
}
return true;