-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
start_web.php
64 lines (57 loc) · 1.84 KB
/
start_web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
use Workerman\Worker;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Connection\TcpConnection;
include __DIR__ . '/vendor/autoload.php';
// 启动一个webserver,用于吐html css js,方便展示
// 这个webserver服务不是必须的,可以将这些html css js文件放到你的项目下用nginx或者apache跑
$web = new Worker('http://0.0.0.0:2123');
$web->name = 'web';
define('WEBROOT', __DIR__ . DIRECTORY_SEPARATOR . 'web');
$web->onMessage = function (TcpConnection $connection, Request $request) {
$path = $request->path();
if ($path === '/') {
$connection->send(exec_php_file(WEBROOT.'/index.html'));
return;
}
$file = realpath(WEBROOT. $path);
if (false === $file) {
$connection->send(new Response(404, array(), '<h3>404 Not Found</h3>'));
return;
}
// Security check! Very important!!!
if (strpos($file, WEBROOT) !== 0) {
$connection->send(new Response(400));
return;
}
if (\pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$connection->send(exec_php_file($file));
return;
}
$if_modified_since = $request->header('if-modified-since');
if (!empty($if_modified_since)) {
// Check 304.
$info = \stat($file);
$modified_time = $info ? \date('D, d M Y H:i:s', $info['mtime']) . ' ' . \date_default_timezone_get() : '';
if ($modified_time === $if_modified_since) {
$connection->send(new Response(304));
return;
}
}
$connection->send((new Response())->withFile($file));
};
function exec_php_file($file) {
\ob_start();
// Try to include php file.
try {
include $file;
} catch (\Exception $e) {
echo $e;
}
return \ob_get_clean();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}