2015-04-04 21:46:31 +08:00
|
|
|
<?php
|
2016-09-20 21:27:41 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-04-04 21:46:31 +08:00
|
|
|
namespace Workerman;
|
|
|
|
|
2016-09-20 21:27:41 +08:00
|
|
|
use Workerman\Protocols\Http;
|
|
|
|
use Workerman\Protocols\HttpCache;
|
2015-04-04 21:46:31 +08:00
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* WebServer.
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
class WebServer extends Worker
|
|
|
|
{
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Virtual host to path mapping.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var array ['workerman.net'=>'/home', 'www.workerman.net'=>'home/www']
|
|
|
|
*/
|
|
|
|
protected $serverRoot = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Mime mapping.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $mimeTypeMap = array();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Used to save user OnWorkerStart callback settings.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
protected $_onWorkerStart = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Add virtual host.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param string $domain
|
|
|
|
* @param string $root_path
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function addRoot($domain, $root_path)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
|
|
|
$this->serverRoot[$domain] = $root_path;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Construct.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param string $socket_name
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param array $context_option
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function __construct($socket_name, $context_option = array())
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
list(, $address) = explode(':', $socket_name, 2);
|
|
|
|
parent::__construct('http:' . $address, $context_option);
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->name = 'WebServer';
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Run webserver instance.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @see Workerman.Worker::run()
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->_onWorkerStart = $this->onWorkerStart;
|
2016-09-20 21:27:41 +08:00
|
|
|
$this->onWorkerStart = array($this, 'onWorkerStart');
|
|
|
|
$this->onMessage = array($this, 'onMessage');
|
2015-04-04 21:46:31 +08:00
|
|
|
parent::run();
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emit when process start.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function onWorkerStart()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if (empty($this->serverRoot)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
throw new \Exception('server root not set, please use WebServer::addRoot($domain, $root_path) to set server root path');
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
// Init HttpCache.
|
2015-04-04 21:46:31 +08:00
|
|
|
HttpCache::init();
|
2016-09-20 21:27:41 +08:00
|
|
|
// Init mimeMap.
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->initMimeTypeMap();
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
// Try to emit onWorkerStart callback.
|
|
|
|
if ($this->_onWorkerStart) {
|
|
|
|
try {
|
|
|
|
call_user_func($this->_onWorkerStart, $this);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
self::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
self::log($e);
|
|
|
|
exit(250);
|
|
|
|
}
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Init mime map.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function initMimeTypeMap()
|
|
|
|
{
|
|
|
|
$mime_file = Http::getMimeTypesFile();
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!is_file($mime_file)) {
|
|
|
|
$this->log("$mime_file mime.type file not fond");
|
2015-04-04 21:46:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
2016-09-20 21:27:41 +08:00
|
|
|
if (!is_array($items)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->log("get $mime_file mime.type content fail");
|
|
|
|
return;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emit when http message coming.
|
|
|
|
*
|
|
|
|
* @param Connection\TcpConnection $connection
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function onMessage($connection)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
// REQUEST_URI.
|
|
|
|
$workerman_url_info = parse_url($_SERVER['REQUEST_URI']);
|
|
|
|
if (!$workerman_url_info) {
|
2015-04-04 21:46:31 +08:00
|
|
|
Http::header('HTTP/1.1 400 Bad Request');
|
2016-09-20 21:27:41 +08:00
|
|
|
$connection->close('<h1>400 Bad Request</h1>');
|
|
|
|
return;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
$workerman_path = isset($workerman_url_info['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';
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
$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';
|
|
|
|
}
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
) {
|
2015-04-04 21:46:31 +08:00
|
|
|
Http::header('HTTP/1.1 400 Bad Request');
|
2016-09-20 21:27:41 +08:00
|
|
|
$connection->close('<h1>400 Bad Request</h1>');
|
|
|
|
return;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
$workerman_file = realpath($workerman_file);
|
|
|
|
|
|
|
|
// Request php file.
|
|
|
|
if ($workerman_file_extension === 'php') {
|
|
|
|
$workerman_cwd = getcwd();
|
|
|
|
chdir($workerman_root_dir);
|
2015-04-04 21:46:31 +08:00
|
|
|
ini_set('display_errors', 'off');
|
|
|
|
ob_start();
|
2016-09-20 21:27:41 +08:00
|
|
|
// Try to include php file.
|
|
|
|
try {
|
|
|
|
// $_SERVER.
|
2015-04-04 21:46:31 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = $connection->getRemoteIp();
|
|
|
|
$_SERVER['REMOTE_PORT'] = $connection->getRemotePort();
|
2016-09-20 21:27:41 +08:00
|
|
|
include $workerman_file;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Jump_exit?
|
|
|
|
if ($e->getMessage() != 'jump_exit') {
|
2015-04-04 21:46:31 +08:00
|
|
|
echo $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$content = ob_get_clean();
|
|
|
|
ini_set('display_errors', 'on');
|
2016-09-20 21:27:41 +08:00
|
|
|
if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
|
|
|
|
$connection->send($content);
|
|
|
|
} else {
|
|
|
|
$connection->close($content);
|
|
|
|
}
|
|
|
|
chdir($workerman_cwd);
|
|
|
|
return;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
// Send file to client.
|
|
|
|
return self::sendFile($connection, $workerman_file);
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function sendFile($connection, $file_path)
|
|
|
|
{
|
|
|
|
// Check 304.
|
|
|
|
$info = stat($file_path);
|
|
|
|
$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;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Http header.
|
|
|
|
if ($modified_time) {
|
|
|
|
$modified_time = "Last-Modified: $modified_time\r\n";
|
|
|
|
}
|
|
|
|
$file_size = filesize($file_path);
|
|
|
|
$file_info = pathinfo($file_path);
|
|
|
|
$extension = isset($file_info['extension']) ? $file_info['extension'] : '';
|
|
|
|
$file_name = isset($file_info['filename']) ? $file_info['filename'] : '';
|
|
|
|
$header = "HTTP/1.1 200 OK\r\n";
|
|
|
|
if (isset(self::$mimeTypeMap[$extension])) {
|
|
|
|
$header .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n";
|
|
|
|
} else {
|
|
|
|
$header .= "Content-Type: application/octet-stream\r\n";
|
|
|
|
$header .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
|
|
|
|
}
|
|
|
|
$header .= "Connection: keep-alive\r\n";
|
|
|
|
$header .= $modified_time;
|
|
|
|
$header .= "Content-Length: $file_size\r\n\r\n";
|
|
|
|
$trunk_limit_size = 1024*1024;
|
|
|
|
if ($file_size < $trunk_limit_size) {
|
|
|
|
return $connection->send($header.file_get_contents($file_path), true);
|
|
|
|
}
|
|
|
|
$connection->send($header, true);
|
|
|
|
|
|
|
|
// Read file content from disk piece by piece and send to client.
|
|
|
|
$connection->fileHandler = fopen($file_path, 'r');
|
|
|
|
$do_write = function()use($connection)
|
|
|
|
{
|
|
|
|
// Send buffer not full.
|
|
|
|
while(empty($connection->bufferFull))
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
// Read from disk.
|
|
|
|
$buffer = fread($connection->fileHandler, 8192);
|
|
|
|
// Read eof.
|
|
|
|
if($buffer === '' || $buffer === false)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
return;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
$connection->send($buffer, true);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
};
|
|
|
|
// Send buffer full.
|
|
|
|
$connection->onBufferFull = function($connection)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
$connection->bufferFull = true;
|
|
|
|
};
|
|
|
|
// Send buffer drain.
|
|
|
|
$connection->onBufferDrain = function($connection)use($do_write)
|
|
|
|
{
|
|
|
|
$connection->bufferFull = false;
|
|
|
|
$do_write();
|
|
|
|
};
|
|
|
|
$do_write();
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|