예제 #1
0
function _header($content, $replace = true, $http_response_code = 0)
{
    if (!defined('WORKERMAN_ROOT_DIR')) {
        return header($content, $replace, $http_response_code);
    }
    return \Man\Common\Protocols\Http\header($content, $replace, $http_response_code);
}
예제 #2
0
 /**
  * 数据接收完整后处理业务逻辑
  * @see Man\Core.SocketWorker::dealProcess()
  */
 public function dealProcess($recv_buffer)
 {
     // http请求处理开始。解析http协议,生成$_POST $_GET $_COOKIE
     Man\Common\Protocols\Http\http_start($recv_buffer);
     // 记录访问日志
     $this->logAccess($recv_buffer);
     // 请求的文件
     $url_info = parse_url($_SERVER['REQUEST_URI']);
     if (!$url_info) {
         Man\Common\Protocols\Http\header('HTTP/1.1 400 Bad Request');
         return $this->sendToClient(Man\Common\Protocols\Http\http_end('<h1>400 Bad Request</h1>'));
     }
     $path = $url_info['path'];
     $path_info = pathinfo($path);
     $extension = isset($path_info['extension']) ? $path_info['extension'] : '';
     if ($extension == '') {
         $path = ($len = strlen($path)) && $path[$len - 1] == '/' ? $path . 'index.php' : $path . '/index.php';
         $extension = 'php';
     }
     // 命中缓存,直接返回
     if (isset(self::$fileCache[$path])) {
         $file_content = self::$fileCache[$path];
         // 发送给客户端
         return $this->sendToClient(Man\Common\Protocols\Http\http_end($file_content));
     }
     $root_dir = isset(self::$serverRoot[$_SERVER['HTTP_HOST']]) ? self::$serverRoot[$_SERVER['HTTP_HOST']] : current(self::$serverRoot);
     $file = "{$root_dir}/{$path}";
     // 对应的php文件不存在则直接使用根目录的index.php
     if ($extension == 'php' && !is_file($file)) {
         $file = "{$root_dir}/index.php";
     }
     // 请求的文件存在
     if (is_file($file)) {
         // 判断是否是站点目录里的文件
         if (!($request_realpath = realpath($file)) || !($root_dir_realpath = realpath($root_dir)) || 0 !== strpos($request_realpath, $root_dir_realpath)) {
             Man\Common\Protocols\Http\header('HTTP/1.1 400 Bad Request');
             return $this->sendToClient(Man\Common\Protocols\Http\http_end('<h1>400 Bad Request</h1>'));
         }
         $file = realpath($file);
         // 如果请求的是php文件
         if ($extension == 'php') {
             $cwd = getcwd();
             chdir($root_dir);
             ini_set('display_errors', 'off');
             // 缓冲输出
             ob_start();
             // 载入php文件
             try {
                 // $_SERVER变量
                 $_SERVER['SCRIPT_NAME'] = $path;
                 $_SERVER['REMOTE_ADDR'] = $this->getRemoteIp();
                 $_SERVER['REMOTE_PORT'] = $this->getRemotePort();
                 $_SERVER['SERVER_ADDR'] = $this->getLocalIp();
                 $_SERVER['DOCUMENT_ROOT'] = $root_dir;
                 $_SERVER['SCRIPT_FILENAME'] = $file;
                 include $file;
             } catch (\Exception $e) {
                 // 如果不是exit
                 if ($e->getMessage() != 'jump_exit') {
                     echo $e;
                 }
             }
             $content = ob_get_clean();
             ini_set('display_errors', 'on');
             $buffer = Man\Common\Protocols\Http\http_end($content);
             $this->sendToClient($buffer);
             // 执行php每执行一次就退出(原因是有的业务使用了require_once类似的语句,不能重复加载业务逻辑)
             //return $this->stop();
             chdir($cwd);
             return;
         }
         // 请求的是静态资源文件
         if (isset(self::$mimeTypeMap[$extension])) {
             Man\Common\Protocols\Http\header('Content-Type: ' . self::$mimeTypeMap[$extension]);
         } else {
             Man\Common\Protocols\Http\header('Content-Type: ' . self::$defaultMimeType);
         }
         // 获取文件信息
         $info = stat($file);
         $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : '';
         // 如果有$_SERVER['HTTP_IF_MODIFIED_SINCE']
         if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
             // 文件没有更改则直接304
             if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
                 // 304
                 Man\Common\Protocols\Http\header('HTTP/1.1 304 Not Modified');
                 // 发送给客户端
                 return $this->sendToClient(Man\Common\Protocols\Http\http_end(''));
             }
         }
         if (!isset(self::$fileCache[$file])) {
             $file_content = file_get_contents($file);
             // 缓存文件
             if ($info['size'] < self::MAX_CACHE_FILE_SIZE && $file_content) {
                 self::$fileCache[$file] = $file_content;
                 // 缓存满了删除一个文件
                 if (count(self::$fileCache) > self::MAX_CACHE_FILE_COUNT) {
                     // 删除第一个缓存的文件
                     reset(self::$fileCache);
                     unset(self::$fileCache[key(self::$fileCache)]);
                 }
             }
         } else {
             $file_content = self::$fileCache[$file];
         }
         if ($modified_time) {
             Man\Common\Protocols\Http\header("Last-Modified: {$modified_time}");
         }
         // 发送给客户端
         return $this->sendToClient(Man\Common\Protocols\Http\http_end($file_content));
     } else {
         // 404
         Man\Common\Protocols\Http\header("HTTP/1.1 404 Not Found");
         return $this->sendToClient(Man\Common\Protocols\Http\http_end('<html><head><title>页面不存在</title></head><body><center><h3>页面不存在</h3></center></body></html>'));
     }
 }