WebServer.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman;
  15. use \Workerman\Worker;
  16. use \Workerman\Protocols\Http;
  17. use \Workerman\Protocols\HttpCache;
  18. /**
  19. * WebServer.
  20. */
  21. class WebServer extends Worker
  22. {
  23. /**
  24. * Mime.
  25. * @var string
  26. */
  27. protected static $defaultMimeType = 'text/html; charset=utf-8';
  28. /**
  29. * Virtual host to path mapping.
  30. * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
  31. */
  32. protected $serverRoot = array();
  33. /**
  34. * Mime mapping.
  35. * @var array
  36. */
  37. protected static $mimeTypeMap = array();
  38. /**
  39. * Used to save user OnWorkerStart callback settings.
  40. * @var callback
  41. */
  42. protected $_onWorkerStart = null;
  43. /**
  44. * Add virtual host.
  45. * @param string $domain
  46. * @param string $root_path
  47. * @return void
  48. */
  49. public function addRoot($domain, $root_path)
  50. {
  51. $this->serverRoot[$domain] = $root_path;
  52. }
  53. /**
  54. * Construct.
  55. * @param string $socket_name
  56. * @param array $context_option
  57. */
  58. public function __construct($socket_name, $context_option = array())
  59. {
  60. list($scheme, $address) = explode(':', $socket_name, 2);
  61. parent::__construct('http:'.$address, $context_option);
  62. $this->name = 'WebServer';
  63. }
  64. /**
  65. * Run webserver instance.
  66. * @see Workerman.Worker::run()
  67. */
  68. public function run()
  69. {
  70. $this->_onWorkerStart = $this->onWorkerStart;
  71. $this->onWorkerStart = array($this, 'onWorkerStart');
  72. $this->onMessage = array($this, 'onMessage');
  73. parent::run();
  74. }
  75. /**
  76. * Emit when process start.
  77. * @throws \Exception
  78. */
  79. public function onWorkerStart()
  80. {
  81. if(empty($this->serverRoot))
  82. {
  83. throw new \Exception('server root not set, please use WebServer::addRoot($domain, $root_path) to set server root path');
  84. }
  85. // Init HttpCache.
  86. HttpCache::init();
  87. // Init mimeMap.
  88. $this->initMimeTypeMap();
  89. // Try to emit onWorkerStart callback.
  90. if($this->_onWorkerStart)
  91. {
  92. try
  93. {
  94. call_user_func($this->_onWorkerStart, $this);
  95. }
  96. catch(\Exception $e)
  97. {
  98. echo $e;
  99. exit(250);
  100. }
  101. }
  102. }
  103. /**
  104. * Init mime map.
  105. * @return void
  106. */
  107. public function initMimeTypeMap()
  108. {
  109. $mime_file = Http::getMimeTypesFile();
  110. if(!is_file($mime_file))
  111. {
  112. $this->log("$mime_file mime.type file not fond");
  113. return;
  114. }
  115. $items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  116. if(!is_array($items))
  117. {
  118. $this->log("get $mime_file mime.type content fail");
  119. return;
  120. }
  121. foreach($items as $content)
  122. {
  123. if(preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match))
  124. {
  125. $mime_type = $match[1];
  126. $workerman_file_extension_var = $match[2];
  127. $workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
  128. foreach($workerman_file_extension_array as $workerman_file_extension)
  129. {
  130. self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Emit when http message coming.
  137. * @param TcpConnection $connection
  138. * @param mixed $data
  139. * @return void
  140. */
  141. public function onMessage($connection)
  142. {
  143. // REQUEST_URI.
  144. $workerman_url_info = parse_url($_SERVER['REQUEST_URI']);
  145. if(!$workerman_url_info)
  146. {
  147. Http::header('HTTP/1.1 400 Bad Request');
  148. return $connection->close('<h1>400 Bad Request</h1>');
  149. }
  150. $workerman_path = $workerman_url_info['path'];
  151. $workerman_path_info = pathinfo($workerman_path);
  152. $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '' ;
  153. if($workerman_file_extension === '')
  154. {
  155. $workerman_path = ($len = strlen($workerman_path)) && $workerman_path[$len -1] === '/' ? $workerman_path.'index.php' : $workerman_path . '/index.php';
  156. $workerman_file_extension = 'php';
  157. }
  158. $workerman_root_dir = isset($this->serverRoot[$_SERVER['HTTP_HOST']]) ? $this->serverRoot[$_SERVER['HTTP_HOST']] : current($this->serverRoot);
  159. $workerman_file = "$workerman_root_dir/$workerman_path";
  160. if($workerman_file_extension === 'php' && !is_file($workerman_file))
  161. {
  162. $workerman_file = "$workerman_root_dir/index.php";
  163. if(!is_file($workerman_file))
  164. {
  165. $workerman_file = "$workerman_root_dir/index.html";
  166. $workerman_file_extension = 'html';
  167. }
  168. }
  169. // File exsits.
  170. if(is_file($workerman_file))
  171. {
  172. // Security check.
  173. if((!($workerman_request_realpath = realpath($workerman_file)) || !($workerman_root_dir_realpath = realpath($workerman_root_dir))) || 0 !== strpos($workerman_request_realpath, $workerman_root_dir_realpath))
  174. {
  175. Http::header('HTTP/1.1 400 Bad Request');
  176. return $connection->close('<h1>400 Bad Request</h1>');
  177. }
  178. $workerman_file = realpath($workerman_file);
  179. // Request php file.
  180. if($workerman_file_extension === 'php')
  181. {
  182. $workerman_cwd = getcwd();
  183. chdir($workerman_root_dir);
  184. ini_set('display_errors', 'off');
  185. ob_start();
  186. // Try to include php file.
  187. try
  188. {
  189. // $_SERVER.
  190. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
  191. $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
  192. include $workerman_file;
  193. }
  194. catch(\Exception $e)
  195. {
  196. // Jump_exit?
  197. if($e->getMessage() != 'jump_exit')
  198. {
  199. echo $e;
  200. }
  201. }
  202. $content = ob_get_clean();
  203. ini_set('display_errors', 'on');
  204. $connection->close($content);
  205. chdir($workerman_cwd);
  206. return ;
  207. }
  208. // Static resource file request.
  209. if(isset(self::$mimeTypeMap[$workerman_file_extension]))
  210. {
  211. Http::header('Content-Type: '. self::$mimeTypeMap[$workerman_file_extension]);
  212. }
  213. else
  214. {
  215. Http::header('Content-Type: '. self::$defaultMimeType);
  216. }
  217. // Get file stat.
  218. $info = stat($workerman_file);
  219. $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : '';
  220. if(!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info)
  221. {
  222. // Http 304.
  223. if($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE'])
  224. {
  225. // 304
  226. Http::header('HTTP/1.1 304 Not Modified');
  227. // Send nothing but http headers..
  228. return $connection->close('');
  229. }
  230. }
  231. if($modified_time)
  232. {
  233. Http::header("Last-Modified: $modified_time");
  234. }
  235. // Send to client.
  236. return $connection->close(file_get_contents($workerman_file));
  237. }
  238. else
  239. {
  240. // 404
  241. Http::header("HTTP/1.1 404 Not Found");
  242. return $connection->close('<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>');
  243. }
  244. }
  245. }