-
Notifications
You must be signed in to change notification settings - Fork 319
/
HttpServer.php
456 lines (405 loc) · 16 KB
/
HttpServer.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/**
* http服务器
*
* @author camera360_server@camera360.com
* @copyright Chengdu pinguo Technology Co.,Ltd.
*/
namespace PG\MSF;
use League\Plates\Engine;
use PG\MSF\Helpers\Context;
use PG\MSF\Base\Input;
use PG\MSF\Base\Output;
use PG\MSF\Base\AOPFactory;
/**
* Class HttpServer
* @package PG\MSF
*/
abstract class HttpServer extends Server
{
/**
* @var string HTTP服务监听地址如: 0.0.0.0
*/
public $httpSocketName;
/**
* @var integer HTTP服务监听端口
*/
public $httpPort;
/**
* @var bool 是否启用HTTP服务
*/
public $httpEnable;
/**
* @var Engine 内置模板引擎
*/
public $templateEngine;
/**
* 视图文件存储路径,您可以指定多个路径以便让框架载入.
* 数组顺序即加载顺序,当然,框架会自动将msf视图目录放在最后resolve.
*
* @var array
*/
public $viewResolvePaths;
/**
* HttpServer constructor.
*/
public function __construct()
{
parent::__construct();
$this->initViewResolvePaths();
}
/**
* 初始化需要检索的视图目录,请在配置中指定http_server.view_paths数组.
* 如果未指定,系统默认会尝试加载app/Views目录,任何时候,框架都会加载$MSFSrcDir/Views下的视图.
*/
protected function initViewResolvePaths()
{
$this->viewResolvePaths = $this->config->get('http_server.view_paths', [
APP_DIR.'/Views'
]);
foreach ($this->viewResolvePaths as $path) {
if (!is_dir($path)) {
writeln(sprintf('The view path %s does not exist, please check it again.', $path));
exit();
}
}
// 框架自带的视图目录.
$this->viewResolvePaths[] = $this->MSFSrcDir.'/Views';
}
/**
* 设置并解析配置
*
* @return $this
*/
public function setConfig()
{
parent::setConfig();
$this->httpEnable = $this->config->get('http_server.enable', true);
$this->httpSocketName = $this->config['http_server']['socket'];
$this->httpPort = $this->config['http_server']['port'];
return $this;
}
/**
* 启动服务
*
* @return $this
*/
public function start()
{
if (!$this->httpEnable) {
parent::start();
return $this;
}
if (static::mode == 'console') {
$this->beforeSwooleStart();
$this->onWorkerStart(null, null);
} else {
//开启一个http服务器
$this->server = new \swoole_http_server($this->httpSocketName, $this->httpPort);
$this->server->on('Start', [$this, 'onStart']);
$this->server->on('WorkerStart', [$this, 'onWorkerStart']);
$this->server->on('WorkerStop', [$this, 'onWorkerStop']);
$this->server->on('Task', [$this, 'onTask']);
$this->server->on('Finish', [$this, 'onFinish']);
$this->server->on('PipeMessage', [$this, 'onPipeMessage']);
$this->server->on('WorkerError', [$this, 'onWorkerError']);
$this->server->on('ManagerStart', [$this, 'onManagerStart']);
$this->server->on('ManagerStop', [$this, 'onManagerStop']);
$this->server->on('request', [$this, 'onRequest']);
$set = $this->setServerSet();
$set['daemonize'] = self::$daemonize ? 1 : 0;
$this->server->set($set);
$this->beforeSwooleStart();
$this->server->start();
}
return $this;
}
/**
* Swoole Worker进程启动回调
*
* @param \swoole_server $serv server实例
* @param int $workerId worker id
*/
public function onWorkerStart($serv, $workerId)
{
parent::onWorkerStart($serv, $workerId);
$this->setTemplateEngine();
}
/**
* 设置模板引擎
*
* @return $this
*/
public function setTemplateEngine()
{
$this->templateEngine = new Engine();
return $this;
}
/**
* HTTP请求回调
*
* @param \swoole_http_request $request 请求对象
* @param \swoole_http_response $response 响应对象
*/
public function onRequest($request, $response)
{
$this->requestId++;
$error = '';
$httpCode = 500;
$logId = $this->genLogId($request);
$instance = null;
$this->route->handleHttpRequest($request);
// 构造请求日志对象
$PGLog = clone getInstance()->log;
$PGLog->accessRecord['beginTime'] = microtime(true);
$PGLog->accessRecord['uri'] = $this->route->getPath() ?: '/';
$PGLog->logId = $logId;
defined('SYSTEM_NAME') && $PGLog->channel = SYSTEM_NAME;
$PGLog->init();
$PGLog->pushLog('verb', $this->route->getVerb());
$PGLog->pushLog('user-agent', $request->header['user-agent'] ?? 'unknown');
$PGLog->pushLog('remote-addr', self::getRemoteAddr($request));
do {
if ($this->route->getPath() == '') {
$indexFile = $this->config['http']['domain'][$this->route->getHost()]['index'] ?? null;
$response->header('X-Ngx-LogId', $PGLog->logId);
$httpCode = $this->sendFile($indexFile, $request, $response);
$PGLog->pushLog('http-code', $httpCode);
$PGLog->appendNoticeLog();
return;
}
if ($this->route->getFile()) {
$response->header('X-Ngx-LogId', $PGLog->logId);
$httpCode = $this->sendFile($this->route->getFile(), $request, $response);
$PGLog->pushLog('http-code', $httpCode);
$PGLog->appendNoticeLog();
return;
}
$controllerName = $this->route->getControllerName();
$controllerClassName = $this->route->getControllerClassName();
if ($controllerClassName == '') {
$error = 'Api not found controller(' . $controllerName . ')';
$httpCode = 404;
break;
}
$methodPrefix = $this->config->get('http.method_prefix', 'action');
$methodName = $methodPrefix . $this->route->getMethodName();
try {
/**
* @var \PG\MSF\Controllers\Controller $instance
*/
$instance = $this->objectPool->get($controllerClassName, [$controllerName, $methodName]);
$instance->__useCount++;
if (empty($instance->getObjectPool())) {
$instance->setObjectPool(AOPFactory::getObjectPool(getInstance()->objectPool, $instance));
}
if (!method_exists($instance, $methodName)) {
$error = 'Api not found method(' . $methodName . ')';
$httpCode = 404;
break;
}
$instance->context = $instance->getObjectPool()->get(Context::class, [$this->requestId]);
// 初始化控制器
$instance->requestStartTime = microtime(true);
// 构造请求上下文成员
$instance->context->setLogId($PGLog->logId);
$instance->context->setLog($PGLog);
$instance->context->setObjectPool($instance->getObjectPool());
/**
* @var $input Input
*/
$input = $instance->context->getObjectPool()->get(Input::class);
$input->set($request);
/**
* @var $output Output
*/
$output = $instance->context->getObjectPool()->get(Output::class, [$instance]);
$output->set($request, $response);
$instance->context->setInput($input);
$instance->context->setOutput($output);
$instance->context->setControllerName($controllerName);
$instance->context->setActionName($methodName);
$instance->setRequestType(Marco::HTTP_REQUEST);
$init = $instance->__construct($controllerName, $methodName);
if ($init instanceof \Generator) {
$this->scheduler->start(
$init,
$instance,
function () use ($instance, $methodName) {
try {
if ($instance->getContext()->getOutput()->__isEnd) {
$instance->destroy();
return false;
}
$generator = $instance->$methodName(...array_values($this->route->getParams()));
if ($generator instanceof \Generator) {
$this->scheduler->taskMap[$instance->context->getRequestId()]->resetRoutine($generator);
$this->scheduler->schedule(
$this->scheduler->taskMap[$instance->context->getRequestId()],
function () use ($instance) {
if (!$instance->getContext()->getOutput()->__isEnd) {
$instance->getContext()->getOutput()->output('Not Implemented', 501);
}
$instance->destroy();
}
);
} else {
if (!$instance->getContext()->getOutput()->__isEnd) {
$instance->getContext()->getOutput()->output('Not Implemented', 501);
}
$instance->destroy();
}
} catch (\Throwable $e) {
if (!$instance->getContext()->getOutput()->__isEnd) {
$instance->onExceptionHandle($e);
}
$instance->destroy();
}
}
);
} else {
$generator = $instance->$methodName(...array_values($this->route->getParams()));
if ($generator instanceof \Generator) {
$this->scheduler->start(
$generator,
$instance,
function () use ($instance) {
if (!$instance->getContext()->getOutput()->__isEnd) {
$instance->getContext()->getOutput()->output('Not Implemented', 501);
}
$instance->destroy();
}
);
} else {
if (!$instance->getContext()->getOutput()->__isEnd) {
$instance->getContext()->getOutput()->output('Not Implemented', 501);
}
$instance->destroy();
}
}
if ($this->route->getEnableCache() && !$this->route->getRouteCache($this->route->getPath())) {
$this->route->setRouteCache(
$this->route->getPath(),
[$controllerName, $this->route->getMethodName(), $controllerClassName]
);
}
break;
} catch (\Throwable $e) {
$instance->onExceptionHandle($e);
$instance->destroy();
}
} while (0);
if ($error !== '') {
if ($instance != null) {
$instance->destroy();
}
$PGLog->pushLog('http-code', $httpCode);
$PGLog->appendNoticeLog();
$response->status($httpCode);
$response->end($error);
}
}
/**
* 获取远程客户端IP
* 优先获取负载器转发ip
* @param \swoole_http_request $request 请求对象
* @return string
*/
public static function getRemoteAddr($request)
{
$ip = $request->header['x-forwarded-for'] ??
$request->header['http_x_forwarded_for'] ??
$request->header['http_forwarded'] ??
$request->header['http_forwarded_for'] ??
'';
if ($ip) {
$ip = explode(',', $ip);
$ip = trim($ip[0]);
return $ip;
}
$ip = $request->header['http_client_ip'] ??
$request->header['x-real-ip'] ??
$request->header['remote_addr'] ??
$request->server['remote_addr'] ??
'';
return $ip;
}
/**
* 产生日志ID
*
* @param \swoole_http_request $request 请求对象
* @return string
*/
public function genLogId($request)
{
static $i = 0;
$i || $i = mt_rand(1, 0x7FFFFF);
$logId = $request->header['x-ngx-logid'] ?? $request->header['log_id'] ?? '' ;
if (!$logId) {
$logId = sprintf(
"%08x%06x%04x%06x",
time() & 0xFFFFFFFF,
crc32(substr((string)gethostname(), 0, 256)) >> 8 & 0xFFFFFF,
getmypid() & 0xFFFF,
$i = $i > 0xFFFFFE ? 1 : $i + 1
);
}
return $logId;
}
/**
* 直接响应静态文件
*
* @param string $path
* @param \swoole_http_request $request 请求对象
* @param \swoole_http_response $response 响应对象
* @return int
*/
public function sendFile($path, $request, $response)
{
if (empty($path)) {
$path = __DIR__ . '/Views/index.html';
if (!$response->sendfile($path)) {
swoole_async_readfile($path, function ($filename, $content) use ($response) {
$response->end($content);
});
}
return Marco::SEND_FILE_200;
}
$path = realpath(urldecode($path));
$root = $this->config['http']['domain'][$this->route->getHost()]['root'] ?? null;
// 判断文件是否存在
if (!file_exists($path)) {
$response->status(404);
$response->end('');
return Marco::SEND_FILE_404;
}
// 判断文件是否有权限(非root目录不能访问)
if (empty($root) || strpos($path, $root) === false) {
$response->status(403);
$response->end('');
return Marco::SEND_FILE_403;
}
$info = pathinfo($path);
$extension = strtolower($info['extension'] ?? '');
// 判断缓存
$lastModified = gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT';
if (isset($request->header['if-modified-since']) && $request->header['if-modified-since'] == $lastModified) {
$response->status(304);
$response->end('');
return Marco::SEND_FILE_304;
}
$normalHeaders = getInstance()->config->get("fileHeader.normal", ['Content-Type: application/octet-stream']);
$headers = getInstance()->config->get("fileHeader.$extension", $normalHeaders);
foreach ($headers as $value) {
list($hk, $hv) = explode(': ', $value);
$response->header($hk, $hv);
}
$response->header('Last-Modified', $lastModified);
if (!$response->sendfile($path)) {
swoole_async_readfile($path, function ($filename, $content) use ($response) {
$response->end($content);
});
}
return Marco::SEND_FILE_200;
}
}