Author: Tianfeng.Han
Beispiel #1
0
 /**
  * 静态请求
  * @param $request
  * @param $response
  * @return unknown_type
  */
 function processStatic($request, Swoole\Response $response)
 {
     $path = $this->document_root . '/' . $request->meta['path'];
     if (is_file($path)) {
         $read_file = true;
         if ($this->expire) {
             $expire = intval($this->config['server']['expire_time']);
             $fstat = stat($path);
             //过期控制信息
             if (isset($request->head['If-Modified-Since'])) {
                 $lastModifiedSince = strtotime($request->head['If-Modified-Since']);
                 if ($lastModifiedSince and $fstat['mtime'] <= $lastModifiedSince) {
                     //不需要读文件了
                     $read_file = false;
                     $response->setHttpStatus(304);
                 }
             } else {
                 $response->head['Cache-Control'] = "max-age={$expire}";
                 $response->head['Pragma'] = "max-age={$expire}";
                 $response->head['Last-Modified'] = date(self::DATE_FORMAT_HTTP, $fstat['mtime']);
                 $response->head['Expires'] = "max-age={$expire}";
             }
         }
         $ext_name = Swoole\Upload::getFileExt($request->meta['path']);
         if ($read_file) {
             $response->head['Content-Type'] = $this->mime_types[$ext_name];
             $response->body = file_get_contents($path);
         }
         return true;
     } else {
         return false;
     }
 }
Beispiel #2
0
 function doStatic(\swoole_http_request $req, \swoole_http_response $resp)
 {
     $file = $this->document_root . $req->server['request_uri'];
     $read_file = true;
     $fstat = stat($file);
     //过期控制信息
     if (isset($req->header['if-modified-since'])) {
         $lastModifiedSince = strtotime($req->header['if-modified-since']);
         if ($lastModifiedSince and $fstat['mtime'] <= $lastModifiedSince) {
             //不需要读文件了
             $read_file = false;
             $resp->status(304);
         }
     } else {
         $resp->header('Cache-Control', "max-age={$this->expire_time}");
         $resp->header('Pragma', "max-age={$this->expire_time}");
         $resp->header('Last-Modified', date(self::DATE_FORMAT_HTTP, $fstat['mtime']));
         $resp->header('Expires', "max-age={$this->expire_time}");
     }
     if ($read_file) {
         $extname = Swoole\Upload::getFileExt($file);
         if (empty($this->types[$extname])) {
             $mime_type = 'text/html; charset=' . $this->charest;
         } else {
             $mime_type = $this->types[$extname];
         }
         $resp->header('Content-Type', $mime_type);
         $resp->sendfile($file);
     } else {
         $resp->end();
     }
     return true;
 }
Beispiel #3
0
 function doStatic(\swoole_http_request $req, \swoole_http_response $resp)
 {
     $file = $this->document_root . $req->server['request_uri'];
     $extname = Swoole\Upload::getFileExt($file);
     if (empty($this->types[$extname])) {
         $mime_type = 'text/html';
     } else {
         $mime_type = $this->types[$extname];
     }
     if (isset(self::$gzip_extname[$extname])) {
         $resp->gzip();
     }
     $resp->header('Content-Type', $mime_type);
     $resp->end(file_get_contents($this->document_root . $req->server['request_uri']));
 }