WebServer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
  3. require_once WORKERMAN_ROOT_DIR . 'applications/Common/Protocols/Http/Http.php';
  4. /**
  5. *
  6. * WebServer
  7. * HTTP协议
  8. *
  9. * @author walkor <worker-man@qq.com>
  10. */
  11. class WebServer extends Man\Core\SocketWorker
  12. {
  13. /**
  14. * 缓存最多多少静态文件
  15. * @var integer
  16. */
  17. const MAX_CACHE_FILE_COUNT = 100;
  18. /**
  19. * 大于这个值则文件不缓存
  20. * @var integer
  21. */
  22. const MAX_CACHE_FILE_SIZE = 300000;
  23. /**
  24. * 缓存静态文件内容
  25. * @var array
  26. */
  27. public static $fileCache = array();
  28. /**
  29. * 默认mime类型
  30. * @var string
  31. */
  32. protected static $defaultMimeType = 'text/html; charset=utf-8';
  33. /**
  34. * 服务器名到文件路径的转换
  35. * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
  36. */
  37. protected static $serverRoot = array();
  38. /**
  39. * 默认访问日志目录
  40. * @var string
  41. */
  42. protected static $defaultAccessLog = './logs/access.log';
  43. /**
  44. * 访问日志存储路径
  45. * @var array
  46. */
  47. protected static $accessLog = array();
  48. /**
  49. * mime类型映射关系
  50. * @var array
  51. */
  52. protected static $mimeTypeMap = array();
  53. /**
  54. * 进程启动的时候一些初始化工作
  55. * @see Man\Core.SocketWorker::onStart()
  56. */
  57. public function onStart()
  58. {
  59. // 初始化HttpCache
  60. App\Common\Protocols\Http\HttpCache::init();
  61. // 初始化mimeMap
  62. $this->initMimeTypeMap();
  63. // 初始化ServerRoot
  64. $this->initServerRoot();
  65. // 初始化访问路径
  66. $this->initAccessLog();
  67. }
  68. /**
  69. * 初始化mimeType
  70. * @return void
  71. */
  72. public function initMimeTypeMap()
  73. {
  74. $mime_file = \Man\Core\Lib\Config::get($this->workerName.'.include');
  75. if(!is_file($mime_file))
  76. {
  77. $this->notice("$mime_file mime.type file not fond");
  78. return;
  79. }
  80. $items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  81. if(!is_array($items))
  82. {
  83. $this->notice("get $mime_file mime.type content fail");
  84. return;
  85. }
  86. foreach($items as $content)
  87. {
  88. if(preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match))
  89. {
  90. $mime_type = $match[1];
  91. $extension_var = $match[2];
  92. $extension_array = explode(' ', substr($extension_var, 0, -1));
  93. foreach($extension_array as $extension)
  94. {
  95. self::$mimeTypeMap[$extension] = $mime_type;
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * 初始化ServerRoot
  102. * @return void
  103. */
  104. public function initServerRoot()
  105. {
  106. self::$serverRoot = \Man\Core\Lib\Config::get($this->workerName.'.root');
  107. }
  108. /**
  109. * 初始化AccessLog
  110. * @return void
  111. */
  112. public function initAccessLog()
  113. {
  114. // 虚拟机访问日志目录
  115. self::$accessLog = \Man\Core\Lib\Config::get($this->workerName.'.access_log');
  116. // 默认访问日志目录
  117. if($default_access_log = \Man\Core\Lib\Config::get($this->workerName.'.default_access_log'))
  118. {
  119. self::$defaultAccessLog = $default_access_log;
  120. }
  121. }
  122. /**
  123. * 确定数据是否接收完整
  124. * @see Man\Core.SocketWorker::dealInput()
  125. */
  126. public function dealInput($recv_str)
  127. {
  128. return App\Common\Protocols\Http\http_input($recv_str);
  129. }
  130. /**
  131. * 数据接收完整后处理业务逻辑
  132. * @see Man\Core.SocketWorker::dealProcess()
  133. */
  134. public function dealProcess($recv_str)
  135. {
  136. // http请求处理开始。解析http协议,生成$_POST $_GET $_COOKIE
  137. App\Common\Protocols\Http\http_start($recv_str);
  138. // 记录访问日志
  139. $this->logAccess($recv_str);
  140. // 请求的文件
  141. $url_info = parse_url($_SERVER['REQUEST_URI']);
  142. if(!$url_info)
  143. {
  144. App\Common\Protocols\Http\header('HTTP1.1/ 400 Bad Request');
  145. return $this->sendToClient(App\Common\Protocols\Http\http_end('<h1>400 Bad Request</h1>'));
  146. }
  147. $path = $url_info['path'];
  148. $path_info = pathinfo($path);
  149. $extension = isset($path_info['extension']) ? $path_info['extension'] : '' ;
  150. if($extension == '')
  151. {
  152. $path = ($len = strlen($path)) && $path[$len -1] == '/' ? $path.'index.php' : $path . '/index.php';
  153. $extension = 'php';
  154. }
  155. // 命中缓存,直接返回
  156. if(isset(self::$fileCache[$path]) )
  157. {
  158. $file_content = self::$fileCache[$path];
  159. // 发送给客户端
  160. return $this->sendToClient(App\Common\Protocols\Http\http_end($file_content));
  161. }
  162. $root_dir = isset(self::$serverRoot[$_SERVER['HTTP_HOST']]) ? self::$serverRoot[$_SERVER['HTTP_HOST']] : current(self::$serverRoot);
  163. $file = "$root_dir/$path";
  164. // 对应的php文件不存在则直接使用根目录的index.php
  165. if($extension == 'php' && !is_file($file))
  166. {
  167. $file = "$root_dir/index.php";
  168. }
  169. // 请求的文件存在
  170. if(is_file($file))
  171. {
  172. // 判断是否是站点目录里的文件
  173. if((!($request_realpath = realpath($file)) || !($root_dir_realpath = realpath($root_dir))) || 0 !== strpos($request_realpath, $root_dir_realpath))
  174. {
  175. App\Common\Protocols\Http\header('HTTP1.1/ 400 Bad Request');
  176. return $this->sendToClient(App\Common\Protocols\Http\http_end('<h1>400 Bad Request</h1>'));
  177. }
  178. // 如果请求的是php文件
  179. if($extension == 'php')
  180. {
  181. ini_set('display_errors', 'off');
  182. // 缓冲输出
  183. ob_start();
  184. // 载入php文件
  185. try
  186. {
  187. // $_SERVER变量
  188. $_SERVER['SCRIPT_NAME'] = $path;
  189. $_SERVER['REMOTE_ADDR'] = $this->getRemoteIp();
  190. $_SERVER['REMOTE_PORT'] = $this->getRemotePort();
  191. $_SERVER['SERVER_ADDR'] = $this->getLocalIp();
  192. $_SERVER['DOCUMENT_ROOT'] = $root_dir;
  193. $_SERVER['SCRIPT_FILENAME'] = $file;
  194. include $file;
  195. }
  196. catch(\Exception $e)
  197. {
  198. // 如果不是exit
  199. if($e->getMessage() != 'jump_exit')
  200. {
  201. echo $e;
  202. }
  203. }
  204. $content = ob_get_clean();
  205. ini_set('display_errors', 'on');
  206. $buffer = App\Common\Protocols\Http\http_end($content);
  207. $this->sendToClient($buffer);
  208. // 执行php每执行一次就退出(原因是有的业务使用了require_once类似的语句,不能重复加载业务逻辑)
  209. //return $this->stop();
  210. return ;
  211. }
  212. // 请求的是静态资源文件
  213. if(isset(self::$mimeTypeMap[$extension]))
  214. {
  215. App\Common\Protocols\Http\header('Content-Type: '. self::$mimeTypeMap[$extension]);
  216. }
  217. else
  218. {
  219. App\Common\Protocols\Http\header('Content-Type: '. self::$defaultMimeType);
  220. }
  221. // 获取文件信息
  222. $info = stat($file);
  223. $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : '';
  224. // 如果有$_SERVER['HTTP_IF_MODIFIED_SINCE']
  225. if(!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info)
  226. {
  227. // 文件没有更改则直接304
  228. if($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE'])
  229. {
  230. // 304
  231. App\Common\Protocols\Http\header('HTTP/1.1 304 Not Modified');
  232. // 发送给客户端
  233. return $this->sendToClient(App\Common\Protocols\Http\http_end(''));
  234. }
  235. }
  236. if(!isset(self::$fileCache[$file]) )
  237. {
  238. $file_content = file_get_contents($file);
  239. // 缓存文件
  240. if($info['size'] < self::MAX_CACHE_FILE_SIZE && $file_content)
  241. {
  242. self::$fileCache[$file] = $file_content;
  243. // 缓存满了删除一个文件
  244. if(count(self::$fileCache) > self::MAX_CACHE_FILE_COUNT)
  245. {
  246. // 删除第一个缓存的文件
  247. reset(self::$fileCache);
  248. unset(self::$fileCache[key(self::$fileCache)]);
  249. }
  250. }
  251. }
  252. else
  253. {
  254. $file_content = self::$fileCache[$file];
  255. }
  256. if($modified_time)
  257. {
  258. App\Common\Protocols\Http\header("Last-Modified: $modified_time");
  259. }
  260. // 发送给客户端
  261. return $this->sendToClient(App\Common\Protocols\Http\http_end($file_content));
  262. }
  263. else
  264. {
  265. // 404
  266. App\Common\Protocols\Http\header("HTTP/1.1 404 Not Found");
  267. return $this->sendToClient(App\Common\Protocols\Http\http_end('<html><head><title>页面不存在</title></head><body><center><h3>页面不存在</h3></center></body></html>'));
  268. }
  269. }
  270. /**
  271. * 记录访问日志
  272. * @param unknown_type $recv_str
  273. */
  274. public function logAccess($recv_str)
  275. {
  276. // 记录访问日志
  277. $log_data = date('Y-m-d H:i:s') . "\t REMOTE:" . $this->getRemoteAddress()."\n$recv_str";
  278. if(isset(self::$accessLog[$_SERVER['HTTP_HOST']]))
  279. {
  280. file_put_contents(self::$accessLog[$_SERVER['HTTP_HOST']], $log_data, FILE_APPEND);
  281. }
  282. else
  283. {
  284. file_put_contents(self::$defaultAccessLog, $log_data, FILE_APPEND);
  285. }
  286. }
  287. }