WebServer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. use Workerman\Connection\TcpConnection;
  18. /**
  19. * WebServer.
  20. */
  21. class WebServer extends Worker
  22. {
  23. /**
  24. * Virtual host to path mapping.
  25. *
  26. * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
  27. */
  28. protected $serverRoot = array();
  29. /**
  30. * Mime mapping.
  31. *
  32. * @var array
  33. */
  34. protected static $mimeTypeMap = array();
  35. /**
  36. * Used to save user OnWorkerStart callback settings.
  37. *
  38. * @var callable
  39. */
  40. protected $_onWorkerStart = null;
  41. /**
  42. * Add virtual host.
  43. *
  44. * @param string $domain
  45. * @param string $config
  46. * @return void
  47. */
  48. public function addRoot($domain, $config)
  49. {
  50. if (\is_string($config)) {
  51. $config = array('root' => $config);
  52. }
  53. $this->serverRoot[$domain] = $config;
  54. }
  55. /**
  56. * Construct.
  57. *
  58. * @param string $socket_name
  59. * @param array $context_option
  60. */
  61. public function __construct($socket_name, array $context_option = array())
  62. {
  63. list(, $address) = \explode(':', $socket_name, 2);
  64. parent::__construct('http:' . $address, $context_option);
  65. $this->name = 'WebServer';
  66. }
  67. /**
  68. * Run webserver instance.
  69. *
  70. * @see Workerman.Worker::run()
  71. */
  72. public function run()
  73. {
  74. $this->_onWorkerStart = $this->onWorkerStart;
  75. $this->onWorkerStart = array($this, 'onWorkerStart');
  76. $this->onMessage = array($this, 'onMessage');
  77. parent::run();
  78. }
  79. /**
  80. * Emit when process start.
  81. *
  82. * @throws \Exception
  83. */
  84. public function onWorkerStart()
  85. {
  86. if (empty($this->serverRoot)) {
  87. Worker::safeEcho(new \Exception('server root not set, please use WebServer::addRoot($domain, $root_path) to set server root path'));
  88. exit(250);
  89. }
  90. // Init mimeMap.
  91. $this->initMimeTypeMap();
  92. // Try to emit onWorkerStart callback.
  93. if ($this->_onWorkerStart) {
  94. try {
  95. \call_user_func($this->_onWorkerStart, $this);
  96. } catch (\Exception $e) {
  97. self::log($e);
  98. exit(250);
  99. } catch (\Error $e) {
  100. self::log($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 TcpConnection $connection
  137. * @return void
  138. */
  139. public function onMessage(TcpConnection $connection)
  140. {
  141. // REQUEST_URI.
  142. $workerman_url_info = \parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  143. if (!$workerman_url_info) {
  144. Http::header('HTTP/1.1 400 Bad Request');
  145. if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  146. $connection->send('<h1>400 Bad Request</h1>');
  147. } else {
  148. $connection->close('<h1>400 Bad Request</h1>');
  149. }
  150. return;
  151. }
  152. $workerman_path = isset($workerman_url_info['path']) ? $workerman_url_info['path'] : '/';
  153. $workerman_path_info = \pathinfo($workerman_path);
  154. $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '';
  155. if ($workerman_file_extension === '') {
  156. $workerman_path = ($len = \strlen($workerman_path)) && $workerman_path[$len - 1] === '/' ? $workerman_path . 'index.php' : $workerman_path . '/index.php';
  157. $workerman_file_extension = 'php';
  158. }
  159. $workerman_siteConfig = isset($this->serverRoot[$_SERVER['SERVER_NAME']]) ? $this->serverRoot[$_SERVER['SERVER_NAME']] : \current($this->serverRoot);
  160. $workerman_root_dir = $workerman_siteConfig['root'];
  161. $workerman_file = "$workerman_root_dir/$workerman_path";
  162. if(isset($workerman_siteConfig['additionHeader'])){
  163. Http::header($workerman_siteConfig['additionHeader']);
  164. }
  165. if ($workerman_file_extension === 'php' && !\is_file($workerman_file)) {
  166. $workerman_file = "$workerman_root_dir/index.php";
  167. if (!\is_file($workerman_file)) {
  168. $workerman_file = "$workerman_root_dir/index.html";
  169. $workerman_file_extension = 'html';
  170. }
  171. }
  172. // File exsits.
  173. if (\is_file($workerman_file)) {
  174. // Security check.
  175. if ((!($workerman_request_realpath = \realpath($workerman_file)) || !($workerman_root_dir_realpath = \realpath($workerman_root_dir))) || 0 !== \strpos($workerman_request_realpath,
  176. $workerman_root_dir_realpath)
  177. ) {
  178. Http::header('HTTP/1.1 400 Bad Request');
  179. if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  180. $connection->send('<h1>400 Bad Request</h1>');
  181. } else {
  182. $connection->close('<h1>400 Bad Request</h1>');
  183. }
  184. return;
  185. }
  186. $workerman_file = \realpath($workerman_file);
  187. // Request php file.
  188. if ($workerman_file_extension === 'php') {
  189. $workerman_cwd = \getcwd();
  190. \chdir($workerman_root_dir);
  191. \ini_set('display_errors', 'off');
  192. \ob_start();
  193. // Try to include php file.
  194. try {
  195. // $_SERVER.
  196. $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
  197. $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
  198. include $workerman_file;
  199. } catch (\Exception $e) {
  200. // Jump_exit?
  201. if ($e->getMessage() !== 'jump_exit') {
  202. Worker::safeEcho($e);
  203. }
  204. }
  205. $content = \ob_get_clean();
  206. \ini_set('display_errors', 'on');
  207. if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  208. $connection->send($content);
  209. } else {
  210. $connection->close($content);
  211. }
  212. \chdir($workerman_cwd);
  213. return;
  214. }
  215. // Send file to client.
  216. return self::sendFile($connection, $workerman_file);
  217. } else {
  218. // 404
  219. Http::header("HTTP/1.1 404 Not Found");
  220. if(isset($workerman_siteConfig['custom404']) && \file_exists($workerman_siteConfig['custom404'])){
  221. $html404 = \file_get_contents($workerman_siteConfig['custom404']);
  222. }else{
  223. $html404 = '<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>';
  224. }
  225. if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  226. $connection->send($html404);
  227. } else {
  228. $connection->close($html404);
  229. }
  230. return;
  231. }
  232. }
  233. public static function sendFile($connection, $file_path)
  234. {
  235. // Check 304.
  236. $info = \stat($file_path);
  237. $modified_time = $info ? \date('D, d M Y H:i:s', $info['mtime']) . ' ' . \date_default_timezone_get() : '';
  238. if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
  239. // Http 304.
  240. if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
  241. // 304
  242. Http::header('HTTP/1.1 304 Not Modified');
  243. // Send nothing but http headers..
  244. if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
  245. $connection->send('');
  246. } else {
  247. $connection->close('');
  248. }
  249. return;
  250. }
  251. }
  252. // Http header.
  253. if ($modified_time) {
  254. $modified_time = "Last-Modified: $modified_time\r\n";
  255. }
  256. $file_size = \filesize($file_path);
  257. $file_info = \pathinfo($file_path);
  258. $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
  259. $file_name = isset($file_info['filename']) ? $file_info['filename'] : '';
  260. $start = 0;
  261. $content_length = $file_size;
  262. if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])) {
  263. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  264. list($start, $end) = explode('-', $range);
  265. $end = is_numeric($end) ? $end : $file_size - 1;
  266. $content_length = $end - $start + 1;
  267. $header = "HTTP/1.1 206 Partial Content\r\n";
  268. $header .= "Accept-Ranges: bytes\r\n";
  269. $header .= "Content-Length: ${content_length}\r\n";
  270. $header .= "Content-Range: bytes ${start}-${end}/${file_size}\r\n";
  271. } else {
  272. $header = "HTTP/1.1 200 OK\r\n";
  273. $header .= "Content-Length: ${content_length}\r\n";
  274. }
  275. if (isset(self::$mimeTypeMap[$extension])) {
  276. $header .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n";
  277. }
  278. $header .= "Content-Type: application/octet-stream\r\n";
  279. $header .= "Cache-Control: public\r\n";
  280. $header .= "Pragma: public\r\n";
  281. $header .= "Content-Disposition:attachment; filename=${file_name}\r\n";
  282. $header .= "Connection: keep-alive\r\n";
  283. $header .= $modified_time;
  284. $header .= "\r\n";
  285. $connection->send($header, true);
  286. // Read file content from disk piece by piece and send to client.
  287. $connection->fileHandler = \fopen($file_path, 'r');
  288. \fseek($connection->fileHandler, $start);
  289. $do_write = function()use($connection, $content_length)
  290. {
  291. $step_length = 8192;
  292. // Send buffer not full.
  293. while(empty($connection->bufferFull))
  294. {
  295. $read_length = $content_length > $step_length ? $step_length : $content_length;
  296. // Warning: fread(): Length parameter must be greater than 0
  297. if ($read_length <= 0) {
  298. $read_length = $step_length;
  299. }
  300. $content_length -= $read_length;
  301. // Read from disk.
  302. $buffer = \fread($connection->fileHandler, $read_length);
  303. // Read eof.
  304. if($buffer === '' || $buffer === false)
  305. {
  306. return;
  307. }
  308. if ($content_length === 0) {
  309. $connection->close($buffer, true);
  310. return;
  311. } else {
  312. $connection->send($buffer, true);
  313. }
  314. }
  315. };
  316. // Send buffer full.
  317. $connection->onBufferFull = function($connection)
  318. {
  319. $connection->bufferFull = true;
  320. };
  321. // Send buffer drain.
  322. $connection->onBufferDrain = function($connection)use($do_write)
  323. {
  324. $connection->bufferFull = false;
  325. $do_write();
  326. };
  327. $do_write();
  328. }
  329. }