WebServer.php 8.1 KB

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