null,
'protocol' => null,
'rootUrl' => null,
'pageUrl' => null,
'date' => null,
'year' => null,
'dateTimestamp' => null,
];
// 快速获取请求路径(优化 parse_url 调用)
$requestUri = $_SERVER['REQUEST_URI'] ?? '/';
$path = $requestUri;
if (($pos = strpos($requestUri, '?')) !== false) {
$path = substr($requestUri, 0, $pos);
}
if ($path === '' || $path === '/') {
$path = '/index.html';
}
// 构建文件路径(避免 ltrim 和多次字符串操作)
$requestPath = $path[0] === '/' ? substr($path, 1) : $path;
$filePath = TEMPLATE_DIR . '/' . $requestPath;
// 快速文件存在检查(先检查文件,再检查路径安全)
if (!file_exists($filePath)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
echo '
404404
';
return;
}
// 安全检查:快速路径验证(避免 realpath 系统调用)
$realPath = realpath($filePath);
if ($realPath === false || !str_starts_with($realPath, TEMPLATE_DIR)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
echo '404404
';
return;
}
// 读取文件内容(一次性读取,避免多次 I/O)
$content = file_get_contents($filePath);
if ($content === false) {
http_response_code(500);
echo '500500
';
return;
}
// 快速获取服务器变量(避免多次访问 $_SERVER)
$host = $_SERVER['HTTP_HOST'] ?? '';
$https = $_SERVER['HTTPS'] ?? '';
$port = $_SERVER['SERVER_PORT'] ?? '';
// 缓存域名(移除端口号)
if ($cache['domain'] === null) {
$cache['domain'] = ($pos = strpos($host, ':')) !== false ? substr($host, 0, $pos) : $host;
}
// 缓存协议判断(优化条件判断)
if ($cache['protocol'] === null) {
$cache['protocol'] = ($https !== '' && $https !== 'off') || $port === '443' ? 'https://' : 'http://';
}
// 缓存根地址
if ($cache['rootUrl'] === null) {
$cache['rootUrl'] = $cache['protocol'] . $cache['domain'] . '/';
}
// 缓存页面地址
if ($cache['pageUrl'] === null) {
$cache['pageUrl'] = $cache['protocol'] . $cache['domain'] . $path;
}
// 缓存日期和年份(同一天内复用,减少 date() 调用)
if ($cache['date'] === null) {
$cache['dateTimestamp'] = time();
$cache['date'] = date('Y-m-d', $cache['dateTimestamp']);
$cache['year'] = date('Y', $cache['dateTimestamp']);
}
// 获取文件扩展名(优化 pathinfo 调用)
$extension = '';
if (($pos = strrpos($filePath, '.')) !== false) {
$extension = strtolower(substr($filePath, $pos + 1));
}
// 静态文件检查:CSS、JS、图片等应由 Nginx 直接服务,不经过 PHP
$staticExtensions = ['css', 'js', 'js1', 'png', 'jpg', 'jpeg', 'gif', 'svg', 'ico', 'woff', 'woff2', 'ttf', 'eot', 'mp4', 'webm', 'mp3', 'pdf', 'zip'];
if (in_array($extension, $staticExtensions, true)) {
http_response_code(404);
header('Cache-Control: no-cache, no-store, must-revalidate');
echo '404404
';
return;
}
// 设置 Content-Type(快速查找)
$contentType = match($extension) {
'html' => 'text/html; charset=UTF-8',
'xml' => 'application/xml; charset=UTF-8',
'txt' => 'text/plain; charset=UTF-8',
default => 'text/html; charset=UTF-8',
};
// 生成 ETag(基于文件路径和日期)
$etag = md5($filePath . $cache['date']);
// 计算 Last-Modified(基于当前日期的 00:00:00)
$lastModified = strtotime($cache['date'] . ' 00:00:00');
// 一次性设置所有 header(减少系统调用)
header('Content-Type: ' . $contentType);
header('ETag: "' . $etag . '"');
header('Cache-Control: public, max-age=86400, must-revalidate');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
// 执行标签替换(使用 strtr 代替多次 str_replace,性能更好)
$content = strtr($content, [
'www.hongqianfrp.com' => $cache['domain'],
'http://www.hongqianfrp.com/' => $cache['rootUrl'],
'http://www.hongqianfrp.com/index.php' => $cache['pageUrl'],
'2025-12-13' => $cache['date'],
'2025' => $cache['year'],
]);
// 输出内容
echo $content;
}
// 执行主处理逻辑
processRequest();