mExFiles = []; } protected $mExFiles; protected function setExFiles(array $files) { $this->mExFiles = $files; } public function isIndex($file) { $path = BASE_PATH . "/index.php"; return $path == $file; } protected function is_exclude($file) { $exister = function ($file, $subex) { $path = BASE_PATH . $subex; $dir = dirname($file); return ($path == $dir); }; $file = basename($file); if (in_array($file,$this->mExFiles)) { return true; } global $config; $exclude_dirs = $config['access_include_dirs']; foreach ($exclude_dirs as $dir) { if($exister($file,$dir)) { return true; } } return false; } private function clear_global() { $_SESSION = []; $_COOKIE = []; $_POST = []; $_GET = []; } public function handle_error($level, $message, $file, $line) { if($level == E_NOTICE) return; $trace = "handle_error: level=$level,msg=$message file=$file,line=$line\n"; $backtrace = debug_backtrace(); foreach ($backtrace as $item) { $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n"; } Log::record($trace, Log::ERR); } protected function preLooper() { set_error_handler([$this, 'handle_error']); } private function cross_domain() { if(defined('OPEN_CROSS_DOAMIN') && OPEN_CROSS_DOAMIN == true) { if(defined('CROSS_DOAMIN_HOST') && !empty(CROSS_DOAMIN_HOST)) { $host = CROSS_DOAMIN_HOST; fcgi_header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding"); fcgi_header("Content-Type: text/html; charset=UTF-8"); fcgi_header("Access-Control-Allow-Credentials: true"); fcgi_header("Access-Control-Allow-Origin: $host"); fcgi_header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,PATCH'); } } } abstract function handle_request($file); public function run_looper() { $this->preLooper(); Log::record('Waiting......', Log::DEBUG); $index = 0; fcgi_init(); while(fcgi_accept() >= 0) { $this->clear_global(); perfor_clear(); $start = microtime(true); Log::start_sql_log(); { ob_start(); init_request(); init_cookie($_SERVER['HTTP_COOKIE']); http_header::instance()->start(); $file = request_helper::script_file(); $this->cross_domain(); $this->handle_request($file); fcgi_headers_sent(); $contents = ob_get_clean(); fcgi_echo($contents); } Log::end_sql_log(); $ret = fcgi_fini(); //fcgi_finish();//单线程的情况下不需要调用 $mem = memory_get_usage(); $msg = sprintf("index=$index memory=$mem isCgi=$ret request time=%.6f\r\n\r\n", microtime(true) - $start); ++$index; Log::record($msg ,Log::DEBUG); } fcgi_fini(); Log::record('Waiting quit......',Log::DEBUG); } }