| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- /**
- * This file is part of workerman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Workerman;
- use Workerman\Protocols\Http;
- use Workerman\Protocols\HttpCache;
- /**
- * WebServer.
- */
- class WebServer extends Worker
- {
- /**
- * Mime.
- * @var string
- */
- protected static $defaultMimeType = 'text/html; charset=utf-8';
-
- /**
- * Virtual host to path mapping.
- * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
- */
- protected $serverRoot = array();
-
- /**
- * Mime mapping.
- * @var array
- */
- protected static $mimeTypeMap = array();
-
-
- /**
- * Used to save user OnWorkerStart callback settings.
- * @var callback
- */
- protected $_onWorkerStart = null;
-
- /**
- * Add virtual host.
- * @param string $domain
- * @param string $root_path
- * @return void
- */
- public function addRoot($domain, $root_path)
- {
- $this->serverRoot[$domain] = $root_path;
- }
-
- /**
- * Construct.
- * @param string $socket_name
- * @param array $context_option
- */
- public function __construct($socket_name, $context_option = array())
- {
- list(, $address) = explode(':', $socket_name, 2);
- parent::__construct('http:'.$address, $context_option);
- $this->name = 'WebServer';
- }
-
- /**
- * Run webserver instance.
- * @see Workerman.Worker::run()
- */
- public function run()
- {
- $this->_onWorkerStart = $this->onWorkerStart;
- $this->onWorkerStart = array($this, 'onWorkerStart');
- $this->onMessage = array($this, 'onMessage');
- parent::run();
- }
-
- /**
- * Emit when process start.
- * @throws \Exception
- */
- public function onWorkerStart()
- {
- if(empty($this->serverRoot))
- {
- throw new \Exception('server root not set, please use WebServer::addRoot($domain, $root_path) to set server root path');
- }
- // Init HttpCache.
- HttpCache::init();
- // Init mimeMap.
- $this->initMimeTypeMap();
-
- // Try to emit onWorkerStart callback.
- if($this->_onWorkerStart)
- {
- try
- {
- call_user_func($this->_onWorkerStart, $this);
- }
- catch(\Exception $e)
- {
- echo $e;
- exit(250);
- }
- }
- }
-
- /**
- * Init mime map.
- * @return void
- */
- public function initMimeTypeMap()
- {
- $mime_file = Http::getMimeTypesFile();
- if(!is_file($mime_file))
- {
- $this->log("$mime_file mime.type file not fond");
- return;
- }
- $items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- if(!is_array($items))
- {
- $this->log("get $mime_file mime.type content fail");
- return;
- }
- foreach($items as $content)
- {
- if(preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match))
- {
- $mime_type = $match[1];
- $workerman_file_extension_var = $match[2];
- $workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
- foreach($workerman_file_extension_array as $workerman_file_extension)
- {
- self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
- }
- }
- }
- }
-
- /**
- * Emit when http message coming.
- * @param Connection\TcpConnection $connection
- * @return void
- */
- public function onMessage($connection)
- {
- // REQUEST_URI.
- $workerman_url_info = parse_url($_SERVER['REQUEST_URI']);
- if(!$workerman_url_info)
- {
- Http::header('HTTP/1.1 400 Bad Request');
- $connection->close('<h1>400 Bad Request</h1>');
- return;
- }
-
- $workerman_path = $workerman_url_info['path'];
-
- $workerman_path_info = pathinfo($workerman_path);
- $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '' ;
- if($workerman_file_extension === '')
- {
- $workerman_path = ($len = strlen($workerman_path)) && $workerman_path[$len -1] === '/' ? $workerman_path.'index.php' : $workerman_path . '/index.php';
- $workerman_file_extension = 'php';
- }
-
- $workerman_root_dir = isset($this->serverRoot[$_SERVER['SERVER_NAME']]) ? $this->serverRoot[$_SERVER['SERVER_NAME']] : current($this->serverRoot);
-
- $workerman_file = "$workerman_root_dir/$workerman_path";
-
- if($workerman_file_extension === 'php' && !is_file($workerman_file))
- {
- $workerman_file = "$workerman_root_dir/index.php";
- if(!is_file($workerman_file))
- {
- $workerman_file = "$workerman_root_dir/index.html";
- $workerman_file_extension = 'html';
- }
- }
-
- // File exsits.
- if(is_file($workerman_file))
- {
- // Security check.
- if((!($workerman_request_realpath = realpath($workerman_file)) || !($workerman_root_dir_realpath = realpath($workerman_root_dir))) || 0 !== strpos($workerman_request_realpath, $workerman_root_dir_realpath))
- {
- Http::header('HTTP/1.1 400 Bad Request');
- $connection->close('<h1>400 Bad Request</h1>');
- return;
- }
-
- $workerman_file = realpath($workerman_file);
-
- // Request php file.
- if($workerman_file_extension === 'php')
- {
- $workerman_cwd = getcwd();
- chdir($workerman_root_dir);
- ini_set('display_errors', 'off');
- ob_start();
- // Try to include php file.
- try
- {
- // $_SERVER.
- $_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
- $_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
- include $workerman_file;
- }
- catch(\Exception $e)
- {
- // Jump_exit?
- if($e->getMessage() != 'jump_exit')
- {
- echo $e;
- }
- }
- $content = ob_get_clean();
- ini_set('display_errors', 'on');
- $connection->close($content);
- chdir($workerman_cwd);
- return;
- }
-
- // Static resource file request.
- if(isset(self::$mimeTypeMap[$workerman_file_extension]))
- {
- Http::header('Content-Type: '. self::$mimeTypeMap[$workerman_file_extension]);
- }
- else
- {
- Http::header('Content-Type: '. self::$defaultMimeType);
- }
-
- // Get file stat.
- $info = stat($workerman_file);
-
- $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' GMT' : '';
-
- if(!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info)
- {
- // Http 304.
- if($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE'])
- {
- // 304
- Http::header('HTTP/1.1 304 Not Modified');
- // Send nothing but http headers..
- $connection->close('');
- return;
- }
- }
-
- if($modified_time)
- {
- Http::header("Last-Modified: $modified_time");
- }
- // Send to client.
- $connection->close(file_get_contents($workerman_file));
- return;
- }
- else
- {
- // 404
- Http::header("HTTP/1.1 404 Not Found");
- $connection->close('<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>');
- return;
- }
- }
- }
|