WebServer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * Virtual host to path mapping.
  24. *
  25. * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
  26. */
  27. protected $serverRoot = array();
  28. /**
  29. * Mime mapping.
  30. *
  31. * @var array
  32. */
  33. protected static $mimeTypeMap = array();
  34. /**
  35. * Used to save user OnWorkerStart callback settings.
  36. *
  37. * @var callback
  38. */
  39. protected $_onWorkerStart = null;
  40. /**
  41. * Add virtual host.
  42. *
  43. * @param string $domain
  44. * @param string $config
  45. * @return void
  46. */
  47. public function addRoot($domain, $config)
  48. {
  49. if (is_string($config)) {
  50. $config = array('root' => $config);
  51. }
  52. $this->serverRoot[$domain] = $config;
  53. }
  54. /**
  55. * Construct.
  56. *
  57. * @param string $socket_name
  58. * @param array $context_option
  59. */
  60. public function __construct($socket_name, $context_option = array())
  61. {
  62. list(, $address) = explode(':', $socket_name, 2);
  63. parent::__construct('http:' . $address, $context_option);
  64. $this->name = 'WebServer';
  65. }
  66. /**
  67. * Run webserver instance.
  68. *
  69. * @see Workerman.Worker::run()
  70. */
  71. public function run()
  72. {
  73. $this->_onWorkerStart = $this->onWorkerStart;
  74. $this->onWorkerStart = array($this, 'onWorkerStart');
  75. $this->onMessage = array($this, 'onMessage');
  76. parent::run();
  77. }
  78. /**
  79. * Emit when process start.
  80. *
  81. * @throws \Exception
  82. */
  83. public function onWorkerStart()
  84. {
  85. if (empty($this->serverRoot)) {
  86. Worker::safeEcho(new \Exception('server root not set, please use WebServer::addRoot($domain, $root_path) to set server root path'));
  87. exit(250);
  88. }
  89. // Init mimeMap.
  90. $this->initMimeTypeMap();
  91. // Try to emit onWorkerStart callback.
  92. if ($this->_onWorkerStart) {
  93. try {
  94. call_user_func($this->_onWorkerStart, $this);
  95. } catch (\Exception $e) {
  96. self::log($e);
  97. exit(250);
  98. } catch (\Error $e) {
  99. self::log($e);
  100. exit(250);
  101. }
  102. }
  103. }
  104. /**
  105. * Init mime map.
  106. *
  107. * @return void
  108. */
  109. public function initMimeTypeMap()
  110. {
  111. $mime_file = Http::getMimeTypesFile();
  112. if (!is_file($mime_file)) {
  113. $this->log("$mime_file mime.type file not fond");
  114. return;
  115. }
  116. $items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  117. if (!is_array($items)) {
  118. $this->log("get $mime_file mime.type content fail");
  119. return;
  120. }
  121. foreach ($items as $content) {
  122. if (preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
  123. $mime_type = $match[1];
  124. $workerman_file_extension_var = $match[2];
  125. $workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
  126. foreach ($workerman_file_extension_array as $workerman_file_extension) {
  127. self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Emit when http message coming.
  134. *
  135. * @param Connection\TcpConnection $connection
  136. * @return void
  137. */
  138. public function onMessage($connection)
  139. {
  140. // REQUEST_URI.
  141. $workerman_url_info = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  142. if (!$workerman_url_info) {
  143. Http::header('HTTP/1.1 400 Bad Request');
  144. $connection->close('<h1>400 Bad Request</h1>');
  145. return;
  146. }
  147. $workerman_path = isset($workerman_url_info['path']) ? $workerman_url_info['path'] : '/';
  148. $workerman_path_info = pathinfo($workerman_path);
  149. $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '';
  150. if ($workerman_file_extension === '') {
  151. $workerman_path = ($len = strlen($workerman_path)) && $workerman_path[$len - 1] === '/' ? $workerman_path . 'index.php' : $workerman_path . '/index.php';
  152. $workerman_file_extension = 'php';
  153. }
  154. $workerman_siteConfig = isset($this->serverRoot[$_SERVER['SERVER_NAME']]) ? $this->serverRoot[$_SERVER['SERVER_NAME']] : current($this->serverRoot);
  155. $workerman_root_dir = $workerman_siteConfig['root'];
  156. $workerman_file = "$workerman_root_dir/$workerman_path";
  157. if(isset($workerman_siteConfig['additionHeader'])){
  158. Http::header($workerman_siteConfig['additionHeader']);
  159. }
  160. if ($workerman_file_extension === 'php' && !is_file($workerman_file)) {
  161. $workerman_file = "$workerman_root_dir/index.php";
  162. if (!is_file($workerman_file)) {
  163. $workerman_file = "$workerman_root_dir/index.html";
  164. $workerman_file_extension = 'html';
  165. }
  166. }
  167. // File exsits.
  168. if (is_file($workerman_file)) {
  169. // Security check.
  170. if ((!($workerman_request_realpath = realpath($workerman_file)) || !($workerman_root_dir_realpath = realpath($workerman_root_dir))) || 0 !== strpos($workerman_request_realpath,
  171. $workerman_root_dir_realpath)
  172. ) {
  173. Http::header('HTTP/1.1 400 Bad Request');
  174. $connection->close('<h1>400 Bad Request</h1>');
  175. return;
  176. }
  177. $workerman_file = realpath($workerman_file);
  178. // Request php file.
  179. if ($workerman_file_extension === 'php') {
  180. $workerman_cwd = getcwd();
  181. chdir($workerman_root_dir);
  182. ini_set('display_errors', 'off');
  183. ob_start();
  184. // Try to include php file.
  185. try {
  186. // $_SERVER.
  187. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
  188. $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
  189. include $workerman_file;
  190. } catch (\Exception $e) {
  191. // Jump_exit?
  192. if ($e->getMessage() != 'jump_exit') {
  193. Worker::safeEcho($e);
  194. }
  195. }
  196. $content = ob_get_clean();
  197. ini_set('display_errors', 'on');
  198. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  199. $connection->send($content);
  200. } else {
  201. $connection->close($content);
  202. }
  203. chdir($workerman_cwd);
  204. return;
  205. }
  206. // Send file to client.
  207. return self::sendFile($connection, $workerman_file);
  208. } else {
  209. // 404
  210. Http::header("HTTP/1.1 404 Not Found");
  211. if(isset($workerman_siteConfig['custom404']) && file_exists($workerman_siteConfig['custom404'])){
  212. $html404 = file_get_contents($workerman_siteConfig['custom404']);
  213. }else{
  214. $html404 = '<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>';
  215. }
  216. $connection->close($html404);
  217. return;
  218. }
  219. }
  220. public static function sendFile($connection, $file_path)
  221. {
  222. // Check 304.
  223. $info = stat($file_path);
  224. $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' ' . date_default_timezone_get() : '';
  225. if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
  226. // Http 304.
  227. if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
  228. // 304
  229. Http::header('HTTP/1.1 304 Not Modified');
  230. // Send nothing but http headers..
  231. $connection->close('');
  232. return;
  233. }
  234. }
  235. // Http header.
  236. if ($modified_time) {
  237. $modified_time = "Last-Modified: $modified_time\r\n";
  238. }
  239. $file_size = filesize($file_path);
  240. $file_info = pathinfo($file_path);
  241. $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
  242. $file_name = isset($file_info['filename']) ? $file_info['filename'] : '';
  243. $header = "HTTP/1.1 200 OK\r\n";
  244. if (isset(self::$mimeTypeMap[$extension])) {
  245. $header .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n";
  246. } else {
  247. $header .= "Content-Type: application/octet-stream\r\n";
  248. $header .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
  249. }
  250. $header .= "Connection: keep-alive\r\n";
  251. $header .= $modified_time;
  252. $header .= "Content-Length: $file_size\r\n\r\n";
  253. $trunk_limit_size = 1024*1024;
  254. if ($file_size < $trunk_limit_size) {
  255. return $connection->send($header.file_get_contents($file_path), true);
  256. }
  257. $connection->send($header, true);
  258. // Read file content from disk piece by piece and send to client.
  259. $connection->fileHandler = fopen($file_path, 'r');
  260. $do_write = function()use($connection)
  261. {
  262. // Send buffer not full.
  263. while(empty($connection->bufferFull))
  264. {
  265. // Read from disk.
  266. $buffer = fread($connection->fileHandler, 8192);
  267. // Read eof.
  268. if($buffer === '' || $buffer === false)
  269. {
  270. return;
  271. }
  272. $connection->send($buffer, true);
  273. }
  274. };
  275. // Send buffer full.
  276. $connection->onBufferFull = function($connection)
  277. {
  278. $connection->bufferFull = true;
  279. };
  280. // Send buffer drain.
  281. $connection->onBufferDrain = function($connection)use($do_write)
  282. {
  283. $connection->bufferFull = false;
  284. $do_write();
  285. };
  286. $do_write();
  287. }
  288. }