WebServer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  145. $connection->send('<h1>400 Bad Request</h1>');
  146. } else {
  147. $connection->close('<h1>400 Bad Request</h1>');
  148. }
  149. return;
  150. }
  151. $workerman_path = isset($workerman_url_info['path']) ? $workerman_url_info['path'] : '/';
  152. $workerman_path_info = pathinfo($workerman_path);
  153. $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '';
  154. if ($workerman_file_extension === '') {
  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_siteConfig = isset($this->serverRoot[$_SERVER['SERVER_NAME']]) ? $this->serverRoot[$_SERVER['SERVER_NAME']] : current($this->serverRoot);
  159. $workerman_root_dir = $workerman_siteConfig['root'];
  160. $workerman_file = "$workerman_root_dir/$workerman_path";
  161. if(isset($workerman_siteConfig['additionHeader'])){
  162. Http::header($workerman_siteConfig['additionHeader']);
  163. }
  164. if ($workerman_file_extension === 'php' && !is_file($workerman_file)) {
  165. $workerman_file = "$workerman_root_dir/index.php";
  166. if (!is_file($workerman_file)) {
  167. $workerman_file = "$workerman_root_dir/index.html";
  168. $workerman_file_extension = 'html';
  169. }
  170. }
  171. // File exsits.
  172. if (is_file($workerman_file)) {
  173. // Security check.
  174. if ((!($workerman_request_realpath = realpath($workerman_file)) || !($workerman_root_dir_realpath = realpath($workerman_root_dir))) || 0 !== strpos($workerman_request_realpath,
  175. $workerman_root_dir_realpath)
  176. ) {
  177. Http::header('HTTP/1.1 400 Bad Request');
  178. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  179. $connection->send('<h1>400 Bad Request</h1>');
  180. } else {
  181. $connection->close('<h1>400 Bad Request</h1>');
  182. }
  183. return;
  184. }
  185. $workerman_file = realpath($workerman_file);
  186. // Request php file.
  187. if ($workerman_file_extension === 'php') {
  188. $workerman_cwd = getcwd();
  189. chdir($workerman_root_dir);
  190. ini_set('display_errors', 'off');
  191. ob_start();
  192. // Try to include php file.
  193. try {
  194. // $_SERVER.
  195. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
  196. $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
  197. include $workerman_file;
  198. } catch (\Exception $e) {
  199. // Jump_exit?
  200. if ($e->getMessage() != 'jump_exit') {
  201. Worker::safeEcho($e);
  202. }
  203. }
  204. $content = ob_get_clean();
  205. ini_set('display_errors', 'on');
  206. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  207. $connection->send($content);
  208. } else {
  209. $connection->close($content);
  210. }
  211. chdir($workerman_cwd);
  212. return;
  213. }
  214. // Send file to client.
  215. return self::sendFile($connection, $workerman_file);
  216. } else {
  217. // 404
  218. Http::header("HTTP/1.1 404 Not Found");
  219. if(isset($workerman_siteConfig['custom404']) && file_exists($workerman_siteConfig['custom404'])){
  220. $html404 = file_get_contents($workerman_siteConfig['custom404']);
  221. }else{
  222. $html404 = '<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>';
  223. }
  224. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  225. $connection->send($html404);
  226. } else {
  227. $connection->close($html404);
  228. }
  229. return;
  230. }
  231. }
  232. public static function sendFile($connection, $file_path)
  233. {
  234. // Check 304.
  235. $info = stat($file_path);
  236. $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' ' . date_default_timezone_get() : '';
  237. if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
  238. // Http 304.
  239. if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
  240. // 304
  241. Http::header('HTTP/1.1 304 Not Modified');
  242. // Send nothing but http headers..
  243. if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  244. $connection->send('');
  245. } else {
  246. $connection->close('');
  247. }
  248. return;
  249. }
  250. }
  251. // Http header.
  252. if ($modified_time) {
  253. $modified_time = "Last-Modified: $modified_time\r\n";
  254. }
  255. $file_size = filesize($file_path);
  256. $file_info = pathinfo($file_path);
  257. $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
  258. $file_name = isset($file_info['filename']) ? $file_info['filename'] : '';
  259. $header = "HTTP/1.1 200 OK\r\n";
  260. if (isset(self::$mimeTypeMap[$extension])) {
  261. $header .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n";
  262. } else {
  263. $header .= "Content-Type: application/octet-stream\r\n";
  264. $header .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
  265. }
  266. $header .= "Connection: keep-alive\r\n";
  267. $header .= $modified_time;
  268. $header .= "Content-Length: $file_size\r\n\r\n";
  269. $trunk_limit_size = 1024*1024;
  270. if ($file_size < $trunk_limit_size) {
  271. return $connection->send($header.file_get_contents($file_path), true);
  272. }
  273. $connection->send($header, true);
  274. // Read file content from disk piece by piece and send to client.
  275. $connection->fileHandler = fopen($file_path, 'r');
  276. $do_write = function()use($connection)
  277. {
  278. // Send buffer not full.
  279. while(empty($connection->bufferFull))
  280. {
  281. // Read from disk.
  282. $buffer = fread($connection->fileHandler, 8192);
  283. // Read eof.
  284. if($buffer === '' || $buffer === false)
  285. {
  286. return;
  287. }
  288. $connection->send($buffer, true);
  289. }
  290. };
  291. // Send buffer full.
  292. $connection->onBufferFull = function($connection)
  293. {
  294. $connection->bufferFull = true;
  295. };
  296. // Send buffer drain.
  297. $connection->onBufferDrain = function($connection)use($do_write)
  298. {
  299. $connection->bufferFull = false;
  300. $do_write();
  301. };
  302. $do_write();
  303. }
  304. }