123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace fcgisrv;
- require_once(BASE_ROOT_PATH . '/helper/http_header.php');
- require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
- require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
- use http_header;
- use Log;
- use request_helper;
- abstract class BaseServer
- {
- public function __construct() {
- $this->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);
- }
- }
|