-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
AssetMiddleware.php
173 lines (155 loc) · 5.72 KB
/
AssetMiddleware.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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Routing\Middleware;
use Cake\Core\Exception\CakeException;
use Cake\Core\Plugin;
use Cake\Http\Response;
use Cake\Utility\Inflector;
use Laminas\Diactoros\Stream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SplFileInfo;
/**
* Handles serving plugin assets in development mode.
*
* This should not be used in production environments as it
* has sub-optimal performance when compared to serving files
* with a real webserver.
*/
class AssetMiddleware implements MiddlewareInterface
{
/**
* The amount of time to cache the asset.
*
* @var string
*/
protected string $cacheTime = '+1 day';
/**
* Constructor.
*
* @param array<string, mixed> $options The options to use
*/
public function __construct(array $options = [])
{
if (!empty($options['cacheTime'])) {
$this->cacheTime = $options['cacheTime'];
}
}
/**
* Serve assets if the path matches one.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$url = $request->getUri()->getPath();
if (str_contains($url, '..') || !str_contains($url, '.')) {
return $handler->handle($request);
}
if (str_contains($url, '/.')) {
return $handler->handle($request);
}
$assetFile = $this->_getAssetFile($url);
if ($assetFile === null || !is_file($assetFile)) {
return $handler->handle($request);
}
$file = new SplFileInfo($assetFile);
$modifiedTime = $file->getMTime();
if ($this->isNotModified($request, $file)) {
return (new Response())
->withStringBody('')
->withStatus(304)
->withHeader(
'Last-Modified',
date(DATE_RFC850, $modifiedTime)
);
}
return $this->deliverAsset($request, $file);
}
/**
* Check the not modified header.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to check.
* @param \SplFileInfo $file The file object to compare.
* @return bool
*/
protected function isNotModified(ServerRequestInterface $request, SplFileInfo $file): bool
{
$modifiedSince = $request->getHeaderLine('If-Modified-Since');
if (!$modifiedSince) {
return false;
}
return strtotime($modifiedSince) === $file->getMTime();
}
/**
* Builds asset file path based off url
*
* @param string $url Asset URL
* @return string|null Absolute path for asset file, null on failure
*/
protected function _getAssetFile(string $url): ?string
{
$parts = explode('/', ltrim($url, '/'));
$pluginPart = [];
for ($i = 0; $i < 2; $i++) {
if (!isset($parts[$i])) {
break;
}
$pluginPart[] = Inflector::camelize($parts[$i]);
$plugin = implode('/', $pluginPart);
if (Plugin::isLoaded($plugin)) {
$parts = array_slice($parts, $i + 1);
$fileFragment = implode(DIRECTORY_SEPARATOR, $parts);
$pluginWebroot = Plugin::path($plugin) . 'webroot' . DIRECTORY_SEPARATOR;
return $pluginWebroot . $fileFragment;
}
}
return null;
}
/**
* Sends an asset file to the client
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request object to use.
* @param \SplFileInfo $file The file wrapper for the file.
* @return \Cake\Http\Response The response with the file & headers.
*/
protected function deliverAsset(ServerRequestInterface $request, SplFileInfo $file): Response
{
$resource = fopen($file->getPathname(), 'rb');
if ($resource === false) {
throw new CakeException(sprintf('Cannot open resource `%s`', $file->getPathname()));
}
$stream = new Stream($resource);
$response = new Response(['stream' => $stream]);
$contentType = (array)($response->getMimeType($file->getExtension()) ?: 'application/octet-stream');
$modified = $file->getMTime();
$expire = strtotime($this->cacheTime);
if ($expire === false) {
throw new CakeException(sprintf('Invalid cache time value `%s`', $this->cacheTime));
}
$maxAge = $expire - time();
return $response
->withHeader('Content-Type', $contentType[0])
->withHeader('Cache-Control', 'public,max-age=' . $maxAge)
->withHeader('Date', gmdate(DATE_RFC7231, time()))
->withHeader('Last-Modified', gmdate(DATE_RFC7231, $modified))
->withHeader('Expires', gmdate(DATE_RFC7231, $expire));
}
}