*/ class WebServer extends Worker { /** * 默认mime类型 * @var string */ protected static $defaultMimeType = 'text/html; charset=utf-8'; /** * 服务器名到文件路径的转换 * @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www'] */ protected $serverRoot = array(); /** * mime类型映射关系 * @var array */ protected static $mimeTypeMap = array(); /** * 用来保存用户设置的onWorkerStart回调 * @var callback */ protected $_onWorkerStart = null; /** * 添加站点域名与站点目录的对应关系,类似nginx的 * @param string $domain * @param string $root_path * @return void */ public function addRoot($domain, $root_path) { $this->serverRoot[$domain] = $root_path; } /** * 构造函数 * @param string $socket_name * @param array $context_option */ public function __construct($socket_name, $context_option = array()) { list($scheme, $address) = explode(':', $socket_name, 2); parent::__construct('http:'.$address, $context_option); $this->name = 'WebServer'; } /** * 运行 * @see Workerman.Worker::run() */ public function run() { $this->_onWorkerStart = $this->onWorkerStart; $this->onWorkerStart = array($this, 'onWorkerStart'); $this->onMessage = array($this, 'onMessage'); parent::run(); } /** * 进程启动的时候一些初始化工作 * @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'); } // 初始化HttpCache HttpCache::init(); // 初始化mimeMap $this->initMimeTypeMap(); // 尝试执行开发者设定的onWorkerStart回调 if($this->_onWorkerStart) { call_user_func($this->_onWorkerStart, $this); } } /** * 初始化mimeType * @return void */ public function initMimeTypeMap() { $mime_file = Http::getMimeTypesFile(); if(!is_file($mime_file)) { $this->notice("$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]; $extension_var = $match[2]; $extension_array = explode(' ', substr($extension_var, 0, -1)); foreach($extension_array as $extension) { self::$mimeTypeMap[$extension] = $mime_type; } } } } /** * 当接收到完整的http请求后的处理逻辑 * 1、如果请求的是以php为后缀的文件,则尝试加载 * 2、如果请求的url没有后缀,则尝试加载对应目录的index.php * 3、如果请求的是非php为后缀的文件,尝试读取原始数据并发送 * 4、如果请求的文件不存在,则返回404 * @param TcpConnection $connection * @param mixed $data * @return void */ public function onMessage($connection, $data) { // 请求的文件 $url_info = parse_url($_SERVER['REQUEST_URI']); if(!$url_info) { Http::header('HTTP/1.1 400 Bad Request'); return $connection->close('