BaseServer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace fcgisrv;
  3. require_once(BASE_ROOT_PATH . '/helper/http_header.php');
  4. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  5. require_once(BASE_ROOT_PATH . '/helper/request_helper.php');
  6. use http_header;
  7. use Log;
  8. use request_helper;
  9. abstract class BaseServer
  10. {
  11. public function __construct() {
  12. $this->mExFiles = [];
  13. }
  14. protected $mExFiles;
  15. protected function setExFiles(array $files)
  16. {
  17. $this->mExFiles = $files;
  18. }
  19. public function isIndex($file)
  20. {
  21. $path = BASE_PATH . "/index.php";
  22. return $path == $file;
  23. }
  24. protected function is_exclude($file)
  25. {
  26. $exister = function ($file, $subex)
  27. {
  28. $path = BASE_PATH . $subex;
  29. $dir = dirname($file);
  30. return ($path == $dir);
  31. };
  32. $file = basename($file);
  33. if (in_array($file,$this->mExFiles)) {
  34. return true;
  35. }
  36. global $config;
  37. $exclude_dirs = $config['access_include_dirs'];
  38. foreach ($exclude_dirs as $dir)
  39. {
  40. if($exister($file,$dir)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. private function clear_global()
  47. {
  48. $_SESSION = [];
  49. $_COOKIE = [];
  50. $_POST = [];
  51. $_GET = [];
  52. }
  53. public function handle_error($level, $message, $file, $line)
  54. {
  55. if($level == E_NOTICE) return;
  56. $trace = "handle_error: level=$level,msg=$message file=$file,line=$line\n";
  57. $backtrace = debug_backtrace();
  58. foreach ($backtrace as $item) {
  59. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  60. }
  61. Log::record($trace, Log::ERR);
  62. }
  63. protected function preLooper()
  64. {
  65. set_error_handler([$this, 'handle_error']);
  66. }
  67. private function cross_domain()
  68. {
  69. if(defined('OPEN_CROSS_DOAMIN') && OPEN_CROSS_DOAMIN == true)
  70. {
  71. if(defined('CROSS_DOAMIN_HOST') && !empty(CROSS_DOAMIN_HOST))
  72. {
  73. $host = CROSS_DOAMIN_HOST;
  74. 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");
  75. fcgi_header("Content-Type: text/html; charset=UTF-8");
  76. fcgi_header("Access-Control-Allow-Credentials: true");
  77. fcgi_header("Access-Control-Allow-Origin: $host");
  78. fcgi_header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,PATCH');
  79. }
  80. }
  81. }
  82. abstract function handle_request($file);
  83. public function run_looper()
  84. {
  85. $this->preLooper();
  86. Log::record('Waiting......', Log::DEBUG);
  87. $index = 0;
  88. fcgi_init();
  89. while(fcgi_accept() >= 0)
  90. {
  91. $this->clear_global();
  92. perfor_clear();
  93. $start = microtime(true);
  94. Log::start_sql_log();
  95. {
  96. ob_start();
  97. init_request();
  98. init_cookie($_SERVER['HTTP_COOKIE']);
  99. http_header::instance()->start();
  100. $file = request_helper::script_file();
  101. $this->cross_domain();
  102. $this->handle_request($file);
  103. fcgi_headers_sent();
  104. $contents = ob_get_clean();
  105. fcgi_echo($contents);
  106. }
  107. Log::end_sql_log();
  108. $ret = fcgi_fini();
  109. //fcgi_finish();//单线程的情况下不需要调用
  110. $mem = memory_get_usage();
  111. $msg = sprintf("index=$index memory=$mem isCgi=$ret request time=%.6f\r\n\r\n", microtime(true) - $start);
  112. ++$index;
  113. Log::record($msg ,Log::DEBUG);
  114. }
  115. fcgi_fini();
  116. Log::record('Waiting quit......',Log::DEBUG);
  117. }
  118. }