forked from webvul/php-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.php
65 lines (47 loc) · 1.6 KB
/
http_server.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
65
<?php
/**
* http://blog.41ms.com/post/41.html
*/
// 设置脚本最大运行内存,根据字典大小调整
ini_set('memory_limit', '512M');
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 加载助手文件
require_once('FilterHelper.php');
// http服务绑定的ip及端口
$serv = new swoole_http_server("127.0.0.1", 9502);
$serv->set(array(
'daemonize' => 1,
'log_file' => '/data/www/filter/log/swoole.log',
'user' => 'www',
'group' => 'www'
));
/**
* 处理请求
*/
$serv->on('Request', function($request, $response) {
// 接收get请求参数
$content = isset($request->get['content']) ? $request->get['content']: '';
$result = '';
if (!empty($content)) {
// 字典树文件路径,默认当时目录下
$tree_file = 'blackword.tree';
// 清除文件状态缓存
clearstatcache();
// 获取请求时,字典树文件的修改时间
$new_mtime = filemtime($tree_file);
// 获取最新trie-tree对象
$resTrie = FilterHelper::getResTrie($tree_file, $new_mtime);
// 执行过滤
$arrRet = trie_filter_search_all($resTrie, $content);
// 提取过滤出的敏感词
$a_data = FilterHelper::getFilterWords($content, $arrRet);
$result = json_encode($a_data);
}
// 定义http服务信息及响应处理结果
$response->cookie("User", "W.Y.P");
$response->header("X-Server", "W.Y.P WebServer(Unix) (Red-Hat/Linux)");
$response->header('Content-Type', 'Content-Type: text/html; charset=utf-8');
$response->end($result);
});
$serv->start();