コード例 #1
0
ファイル: WorkerHttp.php プロジェクト: myqee/server
 /**
  * 输出静态文件
  *
  * @param $uri
  * @param \Swoole\Http\Response $response
  */
 protected function assets($uri, $response)
 {
     $uri = str_replace(['\\', '../'], ['/', '/'], $uri);
     $rPos = strrpos($uri, '.');
     if (false === $rPos) {
         # 没有任何后缀
         $response->status(404);
         $response->end('assets not found');
         return;
     }
     $type = strtolower(substr($uri, $rPos + 1));
     $header = ['js' => 'application/x-javascript', 'css' => 'text/css', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'json' => 'application/json', 'svg' => 'image/svg+xml', 'woff' => 'application/font-woff', 'woff2' => 'application/font-woff2', 'ttf' => 'application/x-font-ttf', 'eot' => 'application/vnd.ms-fontobject', 'html' => 'text/html'];
     if (isset($header[$type])) {
         $response->header('Content-Type', $header[$type]);
     }
     $file = __DIR__ . '/../../../../assets/' . $uri;
     if (is_file($file)) {
         # 设置缓存头信息
         $time = 86400;
         $response->header('Cache-Control', 'max-age=' . $time);
         $response->header('Pragma', 'cache');
         $response->header('Last-Modified', date('D, d M Y H:i:s \\G\\M\\T', filemtime($file)));
         $response->header('Expires', date('D, d M Y H:i:s \\G\\M\\T', time() + $time));
         # 直接发送文件
         $response->sendfile($file);
     } else {
         $response->status(404);
         $response->end('assets not found');
     }
 }