Repository navigation
Expand file tree
/
Copy pathWorkerHttp.php
More file actions
108 lines (97 loc) · 2.94 KB
/
Copy pathWorkerHttp.php
File metadata and controls
108 lines (97 loc) · 2.94 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace MyQEE\Server;
class WorkerHttp extends Worker
{
/**
* 静态文件类型
*
* @var array
*/
protected $assetTypes = [
'js' => 'application/x-javascript',
'css' => 'text/css',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'json' => 'application/json',
'svg' => 'image/svg+xml',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
'ttf' => 'application/x-font-ttf',
'eot' => 'application/vnd.ms-fontobject',
'html' => 'text/html',
];
/**
* HTTP 接口请求处理的方法
*
* @param \Swoole\Http\Request $request
* @param \Swoole\Http\Response $response
*/
public function onRequest($request, $response)
{
$arr = explode('/', ltrim($request->server['request_uri'], '/'));
if ($arr[0] === 'assets')
{
# 静态路径
array_shift($arr);
$this->assets(implode('/', $arr), $response);
}
else
{
# 访问请求页面
$uri = str_replace(['\\', '../'], ['/', '/'], implode('/', $arr));
$file = __DIR__ .'/../../../../pages/'. $uri . (substr($uri, -1) === '/' ? 'index' : '') . '.php';
if (!is_file($file))
{
$response->status(404);
$response->end('page not found');
return;
}
ob_start();
include $file;
$html = ob_get_clean();
$response->end($html);
}
}
/**
* 输出静态文件
*
* @param $uri
* @param \Swoole\Http\Response $response
*/
protected function assets($uri, $response)
{
$uri = str_replace(['\\', '../'], ['/', '/'], $uri);
$rPos = strrpos($uri, '.');
if (false === $rPos)
{
# 没有任何后缀
$response->status(404);
$response->end('assets not found');
return;
}
$type = strtolower(substr($uri, $rPos + 1));
if (isset($this->assetTypes[$type]))
{
$response->header('Content-Type', $this->assetTypes[$type]);
}
$file = __DIR__ .'/../../../../assets/'. $uri;
if (is_file($file))
{
# 设置缓存头信息
$time = 86400;
$response->header('Cache-Control', 'max-age='. $time);
$response->header('Pragma' , 'cache');
$response->header('Last-Modified', date('D, d M Y H:i:s \G\M\T', filemtime($file)));
$response->header('Expires' , date('D, d M Y H:i:s \G\M\T', time() + $time));
# 直接发送文件
$response->sendfile($file);
}
else
{
$response->status(404);
$response->end('assets not found');
}
}
}