Skip to content

Commit 5eeeca4

Browse files
committed
优化处理静态文件逻辑
1 parent 99dd03e commit 5eeeca4

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/Server/Manager.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -466,27 +466,41 @@ protected function handleStaticRequest($illuminateRequest, $swooleResponse)
466466
$uri = $illuminateRequest->getRequestUri();
467467
$blackList = ['php', 'htaccess', 'config'];
468468
$extension = substr(strrchr($uri, '.'), 1);
469-
if ($extension && in_array($extension, $blackList)) {
470-
return;
469+
if (!$extension || ($extension && in_array($extension, $blackList))) {
470+
return false;
471471
}
472472

473473
$publicPath = $this->container->make('config')->get('swoole_http.server.public_path', base_path('public'));
474474
$filename = $publicPath . $uri;
475475

476-
if (! is_file($filename) || filesize($filename) === 0) {
477-
return;
476+
$mime = "text/html";
477+
if (!$isFile = file_exists($filename)) {
478+
$swooleResponse->status(404);
479+
} else {
480+
$swooleResponse->status(200);
481+
$mime = mime_content_type($filename);
482+
if ($extension === 'js') {
483+
$mime = 'text/javascript';
484+
} elseif ($extension === 'css') {
485+
$mime = 'text/css';
486+
}
478487
}
479488

480-
$swooleResponse->status(200);
481-
$mime = mime_content_type($filename);
482-
if ($extension === 'js') {
483-
$mime = 'text/javascript';
484-
} elseif ($extension === 'css') {
485-
$mime = 'text/css';
486-
}
487489
$swooleResponse->header('Content-Type', $mime);
488-
$swooleResponse->sendfile($filename);
489-
490+
if ($isFile) {
491+
if (filesize($filename)) {
492+
$swooleResponse->sendfile($filename);
493+
} else {
494+
$name = basename($filename);
495+
$swooleResponse->Header("Content-Transfer-Encoding", "binary");
496+
$swooleResponse->Header("Accept-Ranges", "bytes");
497+
$swooleResponse->Header("Content-Length", 0);
498+
$swooleResponse->Header("Content-Disposition", "attachment; filename={$name}");
499+
$swooleResponse->end();
500+
}
501+
} else {
502+
$swooleResponse->end("404 not found...");
503+
}
490504
return true;
491505
}
492506

0 commit comments

Comments
 (0)