1- <?php
2- namespace Swango \HttpServer \Controller ;
3- class StaticResourceController extends \Swango \HttpServer \Controller {
4- private static $ file_exists_cache = [];
5- protected static function file_exists (string $ file ): bool {
6- if (array_key_exists ($ file , self ::$ file_exists_cache ))
7- return self ::$ file_exists_cache [$ file ];
8- $ result = file_exists ($ file );
9- if (count (self ::$ file_exists_cache ) > 2048 ) {
10- array_shift (self ::$ file_exists_cache );
11- array_shift (self ::$ file_exists_cache );
12- array_shift (self ::$ file_exists_cache );
13- }
14- self ::$ file_exists_cache [$ file ] = $ result ;
15- return $ result ;
16- }
17- protected function handle (): void {
18- $ uri = $ this ->swoole_http_request ->server ['request_uri ' ];
19- $ type = substr ($ uri , strrpos ($ uri , '. ' ) + 1 );
20- if (! array_key_exists ($ type , \Swlib \Http \ContentType::MAP )) {
21- $ this ->E_404 ();
22- return ;
23- }
24- static $ appdir ;
25- if (! isset ($ appdir ))
26- $ appdir = substr (\Swango \Environment::getDir ()->app , 0 , - 1 );
27- $ file = $ appdir . $ uri ;
28-
29- if (array_key_exists ('accept-encoding ' , $ this ->swoole_http_request ->header ) &&
30- strpos ($ this ->swoole_http_request ->header ['accept-encoding ' ], 'gzip ' ) !== false ) {
31- $ gz_file = "$ file.gz " ;
32- if (self ::file_exists ($ gz_file )) {
33- $ this ->swoole_http_response ->header ('Cache-Control ' , 'max-age=315360000 ' );
34- $ this ->swoole_http_response ->header ('Content-Encoding ' , 'gzip ' );
35- $ this ->swoole_http_response ->header ('Content-Type ' , \Swlib \Http \ContentType::MAP [$ type ]);
36- if ($ this ->method === 'GET ' )
37- $ this ->swoole_http_response ->sendfile ($ gz_file );
38- else
39- $ this ->swoole_http_response ->end ();
40-
41- $ this ->json_response_code = 200 ;
42- $ this ->response_finished = true ;
43- return ;
44- }
45- }
46- if (! self ::file_exists ($ file )) {
47- self ::E_404 ();
48- return ;
49- }
50- $ this ->swoole_http_response ->header ('Content-Type ' , \Swlib \Http \ContentType::MAP [$ type ]);
51- if ($ this ->method === 'GET ' )
52- $ this ->swoole_http_response ->sendfile ($ file );
53- else
54- $ this ->swoole_http_response ->end ();
55- $ this ->json_response_code = 200 ;
56- $ this ->response_finished = true ;
57- }
1+ <?php
2+ namespace Swango \HttpServer \Controller ;
3+ class StaticResourceController extends \Swango \HttpServer \Controller {
4+ private static $ file_exists_cache = [];
5+ protected static function file_exists (string $ file ): bool {
6+ if (array_key_exists ($ file , self ::$ file_exists_cache ))
7+ return self ::$ file_exists_cache [$ file ];
8+ $ result = file_exists ($ file );
9+ if (count (self ::$ file_exists_cache ) > 2048 ) {
10+ array_shift (self ::$ file_exists_cache );
11+ array_shift (self ::$ file_exists_cache );
12+ array_shift (self ::$ file_exists_cache );
13+ }
14+ self ::$ file_exists_cache [$ file ] = $ result ;
15+ return $ result ;
16+ }
17+ protected function handle (): void {
18+ $ uri = $ this ->swoole_http_request ->server ['request_uri ' ];
19+ $ type = substr ($ uri , strrpos ($ uri , '. ' ) + 1 );
20+ if (! array_key_exists ($ type , \Swlib \Http \ContentType::MAP )) {
21+ $ this ->E_404 ();
22+ return ;
23+ }
24+ static $ appdir ;
25+ if (! isset ($ appdir ))
26+ $ appdir = substr (\Swango \Environment::getDir ()->app , 0 , - 1 );
27+ $ file = $ appdir . $ uri ;
28+
29+ if (array_key_exists ('accept-encoding ' , $ this ->swoole_http_request ->header ) &&
30+ strpos ($ this ->swoole_http_request ->header ['accept-encoding ' ], 'gzip ' ) !== false ) {
31+ $ gz_file = "$ file.gz " ;
32+ if (self ::file_exists ($ gz_file )) {
33+ $ this ->swoole_http_response ->header ('Cache-Control ' , 'max-age=315360000 ' );
34+ $ this ->swoole_http_response ->header ('Content-Encoding ' , 'gzip ' );
35+ $ this ->swoole_http_response ->header ('Content-Type ' , \Swlib \Http \ContentType::MAP [$ type ]);
36+ if ($ this ->method === 'GET ' )
37+ $ this ->swoole_http_response ->sendfile ($ gz_file );
38+ else
39+ $ this ->swoole_http_response ->end ();
40+
41+ $ this ->json_response_code = 200 ;
42+ $ this ->response_finished = true ;
43+ return ;
44+ }
45+ }
46+ if (! self ::file_exists ($ file )) {
47+ self ::E_404 ();
48+ return ;
49+ }
50+ $ this ->swoole_http_response ->header ('Content-Type ' , \Swlib \Http \ContentType::MAP [$ type ]);
51+ if ($ this ->method === 'GET ' )
52+ $ this ->swoole_http_response ->sendfile ($ file );
53+ else
54+ $ this ->swoole_http_response ->end ();
55+ $ this ->json_response_code = 200 ;
56+ $ this ->response_finished = true ;
57+ }
5858}
0 commit comments