-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.php
304 lines (257 loc) · 7.1 KB
/
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/local/php5.3/bin/php
<?php
error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set("Asia/Shanghai");
$debug = false; //调试模式
$user=''; //以什么用户运行程序
$worker_processes=0; //开启的工作进程数量
$listen_ip = "127.0.0.1"; //监听IP
$listen_port = 1215; //监听端口
//解析ini配置文件
function pare_conf_file(){
global $user,$worker_processes,$listen_ip,$listen_port,$vhost;
$ini_array = parse_ini_file("./http.ini", true);
$user=$ini_array['user'];
$worker_processes=$ini_array['worker_processes'];
$listen_ip = $ini_array['listen_ip'];
$listen_port = $ini_array['listen_port'];
$vhost = $ini_array['vhost'];
}
pare_conf_file();
//返回不支持的方法404
function response_method_404(){
$result = "";
$result .="HTTP/1.1 404\r\n";
$result .="Content-Length: 14\r\n";
$result .= "Content-Type: text/html\r\n";
$result .="\r\nserver is not \r\n";
return $result;
}
//返回不存在的文件404
function response_file_404(){
$result = "";
$result .="HTTP/1.1 404\r\n";
$result .="Content-Length:0\r\n";
$result .= "Content-Type: text/html\r\n";
$result .="\r\n\r\n";
return $result;
}
//返回不支持的文件类型404
function response_file_type_404(){
$result = "";
$result .="HTTP/1.1 404\r\n";
$result .="Content-Length:11\r\n";
$result .= "Content-Type: text/html\r\n";
$result .="\r\ntype is not\r\n";
return $result;
}
//返回图片类型
function response_file_img($fileurl){
$type = mime_content_type($fileurl);
$body = file_get_contents($fileurl);
$len = strlen($body);
$result="";
$result .="HTTP/1.1 200\r\n";
$result .="Content-Length:$len\r\n";
$result .= "Content-Type: {$type}\r\n";
$result .="\r\n$body\r\n";
return $result;
}
//返回JS类型
function response_file_js($fileurl){
global $request;
$body = file_get_contents($fileurl);
$len = strlen($body);
$date = date('D, d M Y G:i:s ').'GMT';
clearstatcache();
$last_modified = date('D, d M Y G:i:s ',filemtime($fileurl)).'GMT';
$result="";
$result .="HTTP/1.1 200\r\n";
$result .="Cache-Control: public\r\n";
$result .="Date:$date\r\n";
$result .="Last-Modified:$last_modified\r\n";
$result .="Expires:Fri, 24 Jan 2016 10:06:05 GMT\r\n";
$result .="Content-Length:$len\r\n";
$result .= "Content-Type: application/x-javascript\r\n";
$result .="\r\n$body\r\n";
return $result;
}
//返回HTML类型
function response_file_html($fileurl){
$body = file_get_contents($fileurl);
$len = strlen($body);
$result = "";
$result .="HTTP/1.1 200\r\n";
$result .="Content-Length:$len\r\n";
$result .= "Content-Type: text/html;charset=utf-8\r\n";
$result .="\r\n$body\r\n";
return $result;
}
//解析http请求
function pare_request($str){
clearstatcache();
global $web_root,$request;
$arr = explode("\r\n",$str);
foreach( $arr as $r){
$pos = stripos($r,":");
if ( $pos ){
$key = trim(substr($r,0,$pos));
$val = trim(substr($r,$pos+1));
$request[$key] = $val;
}
}
writelog(var_export($request,true));
$line0 = explode(" ",$arr[0]);
if ( $line0[0] == "GET" ){
$fileurl = $web_root.$line0[1];
if ( file_exists($fileurl) ){
$pathinfo = pathinfo($fileurl);
if ( $pathinfo['extension'] == "js" ){
$result = response_file_js($fileurl);
}else if ( $pathinfo['extension'] == "png" || $pathinfo['extension'] == "jpg" || $pathinfo['extension'] == "gif" ){
$result = response_file_img($fileurl);
}else if ( $pathinfo['extension'] == "html" ){
$result = response_file_html($fileurl);
}else{
$result = response_file_type_404();
}
}else{
$result = response_file_404();
}
}else{
$result = response_method_404();
}
return $result;
}
//信号处理
function sig_handler($signo)
{
switch ($signo) {
case SIGTERM:// 处理中断信号
echo "SIGTERM\r\n";
exit;
break;
case SIGHUP:
// 处理重启信号
echo "SIGHUP\r\n";
exit;
break;
default:
echo $signo."DEFAULT\r\n";
exit;
// 处理所有其他信号
}
}
//安装信号处理器
function register_sig_handler(){
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
pcntl_signal(IGCHLD, "sig_handler" );
}
//日志方法,应该按标准写日志文件,待优化
function writelog($msg){
global $debug;
if ( $debug ){
echo $msg;
}else{
file_put_contents('./log.txt',$msg,FILE_APPEND | LOCK_EX);
}
}
if ( $user ){
$user_info = posix_getpwnam($user);
$uid = $user_info['uid'];
}else{
$uid = posix_getuid();
}
posix_setuid($uid);
if ( !$debug ){
//产生子进程分支
$pid = pcntl_fork();
if ($pid == -1) {
writelog("could not fork\r\n");
die("could not fork"); //pcntl_fork返回-1标明创建子进程失败
} else if ($pid) {
exit(); //父进程中pcntl_fork返回创建的子进程进程号
} else {
// 子进程pcntl_fork返回的时0
}
// 从当前终端分离
if (posix_setsid() == -1) {
writelog("could not detach form terminal \r\n");
die("could not detach from terminal");
}
register_sig_handler();
}
$web_root = "/home/";
$request = array();
$sock = false;
//创建socket
function listen_server_socket(){
global $sock,$listen_ip,$listen_port;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
writelog("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
exit;
}
socket_set_nonblock($sock);
if (socket_bind($sock, $listen_ip, $listen_port) === false) {
writelog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
exit;
}else{
writelog('Socket ' . $listen_ip . ':' . $listen_port . " has been opened\n");
}
if (socket_listen($sock, 5) === false) {
writelog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
exit;
}else{
writelog("Listening for new clients..\n");
}
}
listen_server_socket();
//启动工作进程,就利用线程处理每次请求,待优化
function start_work_process(){
global $sock,$client_id;
$pid = pcntl_fork();
if ( $pid == 0 ){
do{
if ( ($msgsock = socket_accept($sock)) === false ) {
//writelog("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
usleep(5000);
continue;
} else {
$client_id += 1;
writelog(date('Y-m-d G:i:s')."--Client #" .$client_id .": Connect\n");
}
$cur_buf = '';
do {
if (false === ($buf = socket_read($msgsock, 2048))) {
writelog("socket_read() ppid0 xtgxiso failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n");
break;
}
writelog("read start:\r\n");
writelog($buf);
writelog("read end:\r\n\r\n");
$talkback = pare_request($buf);
writelog("\r\nwrite start:\r\n");
socket_write($msgsock, $talkback, strlen($talkback));
//writelog($talkback);
writelog("write end:\r\n\r\n\r\n");
break;
} while (true);
socket_close($msgsock);
}while(true);
}else if ( $pid > 0 ){
socket_close($sock);
}
}
//主守护进程,可处理信号或管理工作进程...待优化
$client_id = 0;
for( $i = 1; $i <= $worker_processes;$i++){
start_work_process();
}
while(1){
sleep(1);
}
writelog("socket_close \r\n");
socket_close($sock);