|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Narrowspark\MimeType\MimeTypeExtensionGuesser; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use React\Cache\ArrayCache; |
|
9
|
|
|
use React\Cache\CacheInterface; |
|
10
|
|
|
use RingCentral\Psr7\Response; |
|
11
|
|
|
use function RingCentral\Psr7\stream_for; |
|
12
|
|
|
use ScriptFUSION\Byte\ByteFormatter; |
|
13
|
|
|
|
|
14
|
|
|
final class WebrootPreloadMiddleware |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var CacheInterface */ |
|
17
|
|
|
private $cache; |
|
18
|
|
|
|
|
19
|
17 |
|
public function __construct(string $webroot, LoggerInterface $logger = null, CacheInterface $cache = null) |
|
20
|
|
|
{ |
|
21
|
17 |
|
$this->cache = $cache ?? new ArrayCache(); |
|
22
|
|
|
|
|
23
|
17 |
|
$totalSize = 0; |
|
24
|
17 |
|
$count = 0; |
|
25
|
17 |
|
$byteFormatter = (new ByteFormatter())->setPrecision(2)->setFormat('%v%u'); |
|
26
|
17 |
|
$directory = new \RecursiveDirectoryIterator($webroot); |
|
27
|
17 |
|
$directory = new \RecursiveIteratorIterator($directory); |
|
28
|
17 |
|
$directory = \iterator_to_array($directory); |
|
29
|
|
|
\usort($directory, function ($a, $b) { |
|
30
|
17 |
|
return $a->getPathname() <=> $b->getPathname(); |
|
31
|
17 |
|
}); |
|
32
|
17 |
|
foreach ($directory as $fileinfo) { |
|
33
|
17 |
|
if (!$fileinfo->isFile()) { |
|
34
|
17 |
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
17 |
|
$filePath = \str_replace( |
|
38
|
|
|
[ |
|
39
|
17 |
|
$webroot, |
|
40
|
|
|
\DIRECTORY_SEPARATOR, |
|
41
|
17 |
|
'//', |
|
42
|
|
|
], |
|
43
|
|
|
[ |
|
44
|
17 |
|
\DIRECTORY_SEPARATOR, |
|
45
|
|
|
'/', |
|
46
|
|
|
'/', |
|
47
|
|
|
], |
|
48
|
17 |
|
$fileinfo->getPathname() |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$item = [ |
|
52
|
17 |
|
'contents' => \file_get_contents($fileinfo->getPathname()), |
|
53
|
|
|
]; |
|
54
|
17 |
|
$item['etag'] = \md5($item['contents']) . '-' . \filesize($fileinfo->getPathname()); |
|
55
|
|
|
|
|
56
|
17 |
|
$mime = MimeTypeExtensionGuesser::guess($fileinfo->getExtension()); |
|
57
|
17 |
|
if (\is_null($mime)) { |
|
58
|
17 |
|
$mime = 'application/octet-stream'; |
|
59
|
|
|
} |
|
60
|
17 |
|
list($mime) = \explode(';', $mime); |
|
61
|
17 |
|
if (\strpos($mime, '/') !== false) { |
|
62
|
17 |
|
$item['mime'] = $mime; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
17 |
|
$this->cache->set($filePath, $item); |
|
66
|
17 |
|
$count++; |
|
67
|
17 |
|
if ($logger instanceof LoggerInterface) { |
|
68
|
1 |
|
$fileSize = \strlen($item['contents']); |
|
69
|
1 |
|
$totalSize += $fileSize; |
|
70
|
1 |
|
$logger->debug($filePath . ': ' . $byteFormatter->format($fileSize) . ' (' . $item['mime'] . ')'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
17 |
|
if ($logger instanceof LoggerInterface) { |
|
75
|
1 |
|
$logger->info('Preloaded ' . $count . ' file(s) with a combined size of ' . $byteFormatter->format($totalSize) . ' from "' . $webroot . '" into memory'); |
|
76
|
|
|
} |
|
77
|
17 |
|
} |
|
78
|
|
|
|
|
79
|
16 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
|
80
|
|
|
{ |
|
81
|
16 |
|
$path = $request->getUri()->getPath(); |
|
82
|
|
|
|
|
83
|
|
|
return $this->cache->get($path)->then(function ($item) use ($next, $request) { |
|
84
|
16 |
|
if ($item === null) { |
|
85
|
1 |
|
return $next($request); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
15 |
|
if ($request->hasHeader('If-None-Match')) { |
|
89
|
3 |
|
$etag = \current($request->getHeader('If-None-Match')); |
|
90
|
3 |
|
$etag = \trim($etag, '"'); |
|
91
|
3 |
|
if ($etag === $item['etag']) { |
|
92
|
2 |
|
return new Response(304); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
13 |
|
if ($request->hasHeader('If-Match')) { |
|
97
|
4 |
|
$expectedEtag = \current($request->getHeader('If-Match')); |
|
98
|
4 |
|
$expectedEtag = \trim($expectedEtag, '"'); |
|
99
|
4 |
|
if ($expectedEtag !== $item['etag']) { |
|
100
|
2 |
|
return new Response(412); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
11 |
|
$response = (new Response(200))-> |
|
105
|
11 |
|
withBody(stream_for($item['contents']))-> |
|
106
|
11 |
|
withHeader('ETag', '"' . $item['etag'] . '"') |
|
107
|
|
|
; |
|
108
|
11 |
|
if (!isset($item['mime'])) { |
|
109
|
|
|
return $response; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
11 |
|
return $response->withHeader('Content-Type', $item['mime']); |
|
113
|
16 |
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|