Skip to content

Commit 02bfa78

Browse files
author
子旭 苏
committed
Remove useless code & php8 compatible
1 parent 830c978 commit 02bfa78

File tree

7 files changed

+154
-128
lines changed

7 files changed

+154
-128
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"http_server"
88
],
99
"require" : {
10-
"php" : ">=7.4",
10+
"php" : ">=8.0",
1111
"swango/cache" : "^1.0",
12-
"swango/environment" : "^1.0.16",
12+
"swango/environment" : "^1.0",
1313
"swango/stdlib" : "^1.2"
1414
},
1515
"autoload" : {

src/Controller.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
224224
'cnmsg' => &$cnmsg,
225225
'data' => &$data
226226
]));
227-
if (IS_DEV) {
228-
$this->swoole_http_response->header('Access-Control-Expose-Headers', 'Mango-Response-Crypt');
229-
$this->swoole_http_response->header('Access-Control-Allow-Origin', '*');
230-
}
231227
if (isset($this->encrypt_key)) {
232228
if (strlen($echo) < 256) {
233-
$this->swoole_http_response->header('Access-Control-Allow-Headers',
234-
'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type');
235229
$this->swoole_http_response->header('Mango-Response-Crypt', 'On');
236230
$echo = base64_encode(openssl_encrypt($echo, 'aes-128-cbc', $this->encrypt_key, OPENSSL_RAW_DATA,
237231
'1234567890123456'));
@@ -246,7 +240,6 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
246240
}
247241
}
248242
} else {
249-
$this->swoole_http_response->header('Access-Control-Allow-Headers', 'Content-Type, Mango-Request-Rand');
250243
$this->swoole_http_response->header('Content-Type', 'application/json');
251244
$this->endRequest($echo);
252245
}
@@ -265,7 +258,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
265258
}
266259
}
267260
}
268-
public function jsonResponse(?array $data = null, ?string $enmsg = 'ok', ?string $cnmsg = '成功', int $code = 200): void {
261+
public function jsonResponse(?array $data = null, ?string $enmsg = 'ok', ?string $cnmsg = '成功',
262+
int $code = 200): void {
269263
if ($this->response_finished) {
270264
return;
271265
}
@@ -313,7 +307,42 @@ public function deleteCookie(string $key, string $path = '/'): void {
313307
protected function getAction(string $classname): string {
314308
return substr($classname, strpos($classname, "\\") + 1);
315309
}
316-
protected function redirect(string $path, bool $visible = false) {
310+
protected function renderTemplate(string $template, array $variable = [],
311+
?string $content_type = 'text/html; charset=UTF-8',
312+
?string $cache_control = 'no-store'): void {
313+
if ($this->response_finished) {
314+
return;
315+
}
316+
if (! file_exists($template)) {
317+
$this->E_404();
318+
return;
319+
}
320+
foreach ($variable as $k => &$v)
321+
${$k} = $v;
322+
unset($v);
323+
ob_start();
324+
try {
325+
include $template;
326+
$content = ob_get_clean();
327+
if (false === $content) {
328+
throw new \Exception('Output buffering not active');
329+
}
330+
if (isset($content_type)) {
331+
$this->swoole_http_response->header('Content-Type', $content_type);
332+
}
333+
if (isset($cache_control)) {
334+
$this->swoole_http_response->header('Cache-Control', $cache_control);
335+
}
336+
$this->swoole_http_response->end($content);
337+
$this->response_finished = true;
338+
$this->json_response_code ??= 200;
339+
} catch (\Throwable $e) {
340+
ob_end_clean();
341+
$this->E_500();
342+
throw $e;
343+
}
344+
}
345+
protected function redirect(string $path, bool $visible = false): bool {
317346
if ($this->response_finished) {
318347
return false;
319348
}
@@ -336,7 +365,7 @@ protected function E_404(): void {
336365
}
337366
protected function E_500(): void {
338367
$this->swoole_http_response->status(500);
339-
$this->swoole_http_response->end('<html><head><title>500 Internal Server</title></head><body bgcolor="white"><center><h1>500 Internal Server</h1></center><hr><center>nginx</center></body></html>');
368+
$this->swoole_http_response->end('<html><head><title>500 Internal Server Error</title></head><body bgcolor="white"><center><h1>500 Internal Server Error</h1></center><hr><center>nginx</center></body></html>');
340369
$this->json_response_code = 500;
341370
$this->response_finished = true;
342371
}

src/Controller/StaticResourceController.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
class StaticResourceController extends \Swango\HttpServer\Controller {
44
private static $file_exists_cache = [];
55
protected static function file_exists(string $file): bool {
6-
if (array_key_exists($file, self::$file_exists_cache))
6+
if (array_key_exists($file, self::$file_exists_cache)) {
77
return self::$file_exists_cache[$file];
8+
}
89
$result = file_exists($file);
910
if (count(self::$file_exists_cache) > 2048) {
1011
array_shift(self::$file_exists_cache);
@@ -22,21 +23,22 @@ protected function handle(): void {
2223
return;
2324
}
2425
static $appdir;
25-
if (! isset($appdir))
26-
$appdir = substr(\Swango\Environment::getDir()->app, 0, - 1);
26+
if (! isset($appdir)) {
27+
$appdir = substr(\Swango\Environment::getDir()->app, 0, -1);
28+
}
2729
$file = $appdir . $uri;
28-
2930
if (array_key_exists('accept-encoding', $this->swoole_http_request->header) &&
30-
strpos($this->swoole_http_request->header['accept-encoding'], 'gzip') !== false) {
31+
strpos($this->swoole_http_request->header['accept-encoding'], 'gzip') !== false) {
3132
$gz_file = "$file.gz";
3233
if (self::file_exists($gz_file)) {
3334
$this->swoole_http_response->header('Cache-Control', 'max-age=315360000');
3435
$this->swoole_http_response->header('Content-Encoding', 'gzip');
3536
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
36-
if ($this->method === 'GET')
37+
if ($this->method === 'GET') {
3738
$this->swoole_http_response->sendfile($gz_file);
38-
else
39+
} else {
3940
$this->swoole_http_response->end();
41+
}
4042

4143
$this->json_response_code = 200;
4244
$this->response_finished = true;
@@ -48,10 +50,12 @@ protected function handle(): void {
4850
return;
4951
}
5052
$this->swoole_http_response->header('Content-Type', \Swlib\Http\ContentType::MAP[$type]);
51-
if ($this->method === 'GET')
53+
if ($this->method === 'GET') {
54+
$this->swoole_http_response->header('Cache-Control', 'public, max-age=7776000');
5255
$this->swoole_http_response->sendfile($file);
53-
else
56+
} else {
5457
$this->swoole_http_response->end();
58+
}
5559
$this->json_response_code = 200;
5660
$this->response_finished = true;
5761
}

src/Handler.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ public static function start(\Swoole\Http\Request $request, \Swoole\Http\Respons
1010
try {
1111
$router = Router::getInstance($request);
1212
if ($router->getMethod() === 'OPTIONS') {
13-
$response->header('Allow', 'OPTIONS, GET, HEAD, POST');
14-
$response->header('Access-Control-Allow-Methods', 'OPTIONS, GET, HEAD, POST');
15-
$response->header('Access-Control-Allow-Headers',
16-
'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type');
17-
$response->header('Access-Control-Expose-Headers', 'Mango-Response-Crypt');
18-
if (IS_DEV) {
19-
$response->header('Access-Control-Allow-Origin', '*');
20-
}
2113
$response->end('');
2214
return [
2315
200,
@@ -145,7 +137,8 @@ public static function start(\Swoole\Http\Request $request, \Swoole\Http\Respons
145137
* @throws \ExceptionToResponse\DuplicatedRandException
146138
* @throws \Exception
147139
*/
148-
private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $response, Controller $controller): ?bool {
140+
private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $response,
141+
Controller $controller): ?bool {
149142
$inputcontent = $request->rawContent();
150143
if ($inputcontent) {
151144
$server_info = $request->header;
@@ -270,10 +263,6 @@ private static function parseRequest(\Swoole\Http\Request $request, \Swoole\Http
270263
$response->header('Access-Control-Allow-Headers',
271264
'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type');
272265
$response->header('Mango-Response-Crypt', 'On');
273-
if (IS_DEV) {
274-
$response->header('Access-Control-Expose-Headers', 'Mango-Response-Crypt');
275-
$response->header('Access-Control-Allow-Origin', '*');
276-
}
277266
$controller->endRequest($response_string);
278267
return null;
279268
}

src/HttpServer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ protected function _mangoEncryptResponseBody(int $fd, string &$data): void {
378378
if ($resp instanceof \Swoole\Http\Response) {
379379
$encrypt_key = substr($data, 0, 16);
380380
$body = substr($data, 16);
381-
$resp->header('Access-Control-Allow-Headers',
382-
'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type');
383-
$resp->header('Mango-Response-Crypt', 'On');
384381
$resp->end(base64_encode(openssl_encrypt($body, 'aes-128-cbc', $encrypt_key, OPENSSL_RAW_DATA,
385382
'1234567890123456')));
386383
}

src/Validator/Enum.php

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
<?php
2-
namespace Swango\HttpServer\Validator;
3-
class Enum extends \Swango\HttpServer\Validator {
4-
private $enum, $all_string;
5-
public function __construct($cnkey, array $enum) {
6-
parent::__construct($cnkey);
7-
$this->enum = $enum;
8-
$this->all_string = true;
9-
foreach ($enum as $item)
10-
if (! is_string($item) && ! is_numeric($item)) {
11-
$this->all_string = false;
12-
break;
13-
}
14-
}
15-
public function getCnMsg(): string {
16-
if (isset($this->cnmsg))
17-
return $this->cnmsg;
18-
if ($this->all_string)
19-
$ret = $this->cnkey . '必须为(' . implode(',', $this->enum) . ')中的一项';
20-
else
21-
$ret = $this->cnkey . '必须为合法项';
22-
if (! $this->isOptional() || ! $this->couldBeNull())
23-
$ret .= '且不可缺省';
24-
return $ret;
25-
}
26-
protected function check(?string $key, &$value): void {
27-
if (is_array($value) || is_object($value))
28-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
29-
$value = (string)$value;
30-
$value = str_replace([
31-
"\x08",
32-
"\x7F"
33-
], '', $value); // x08为退格字符 x7f为DEL字符
34-
$value = trim($value);
35-
if (preg_match("/\&|\<|\>|\\\/", $value))
36-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
37-
if (in_array($value, $this->enum))
38-
return;
39-
if (! $this->all_string) {
40-
foreach ($this->enum as $validator)
41-
if ($validator instanceof \Swango\HttpServer\Validator) {
42-
try {
43-
$validator->validate($key, $value);
44-
return;
45-
} catch(\Exception $e) {}
46-
}
47-
}
48-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
49-
}
1+
<?php
2+
namespace Swango\HttpServer\Validator;
3+
class Enum extends \Swango\HttpServer\Validator {
4+
private $enum, $all_string;
5+
public function __construct($cnkey, array $enum) {
6+
parent::__construct($cnkey);
7+
$this->enum = $enum;
8+
$this->all_string = true;
9+
foreach ($enum as $item)
10+
if (! is_string($item) && ! is_numeric($item)) {
11+
$this->all_string = false;
12+
break;
13+
}
14+
}
15+
public function getCnMsg(): string {
16+
if (isset($this->cnmsg))
17+
return $this->cnmsg;
18+
if ($this->all_string)
19+
$ret = $this->cnkey . '必须为(' . implode(',', $this->enum) . ')中的一项';
20+
else
21+
$ret = $this->cnkey . '必须为合法项';
22+
if (! $this->isOptional() || ! $this->couldBeNull())
23+
$ret .= '且不可缺省';
24+
return $ret;
25+
}
26+
protected function check(?string $key, &$value): void {
27+
if (is_array($value) || is_object($value))
28+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
29+
$value = (string)$value;
30+
$value = str_replace([
31+
"\x08",
32+
"\x7F"
33+
], '', $value); // x08为退格字符 x7f为DEL字符
34+
$value = trim($value);
35+
if (preg_match("/\&|\<|\>|\\\/", $value))
36+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
37+
if (in_array($value, $this->enum))
38+
return;
39+
if (! $this->all_string) {
40+
foreach ($this->enum as $validator)
41+
if ($validator instanceof \Swango\HttpServer\Validator) {
42+
try {
43+
$validator->validate($key, $value);
44+
return;
45+
} catch(\Exception $e) {}
46+
}
47+
}
48+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
49+
}
5050
}

src/Validator/FixedArray.php

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
<?php
2-
namespace Swango\HttpServer\Validator;
3-
4-
/**
5-
* 用于检测每项含义都相同的数组
6-
*/
7-
class FixedArray extends \Swango\HttpServer\Validator {
8-
private $min_length, $max_length, $content_validator;
9-
public function __construct($cnkey, $min_length = 0, $max_length = 4096) {
10-
parent::__construct($cnkey);
11-
$this->min_length = $min_length;
12-
$this->max_length = $max_length;
13-
}
14-
public function getCnMsg(): string {
15-
if (isset($this->cnmsg))
16-
return $this->cnmsg;
17-
$ret = $this->cnkey . '必须为数组';
18-
if ($this->min_length > 0)
19-
$ret .= ',最少' . $this->min_length . '';
20-
$ret .= ',最长' . $this->max_length . '';
21-
if (! $this->isOptional() || ! $this->couldBeNull())
22-
$ret .= '且不可缺省';
23-
return $ret;
24-
}
25-
protected function check(?string $key, &$value): void {
26-
if (! is_array($value) || is_object($value))
27-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
28-
$i = 0;
29-
foreach ($value as $k=>&$v)
30-
if ($k != $i ++)
31-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
32-
unset($v);
33-
$length = count($value);
34-
if ($length < $this->min_length || $length > $this->max_length)
35-
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
36-
foreach ($value as $k=>&$v)
37-
$this->content_validator->validate("$key.$k", $v);
38-
}
39-
public function setContentValidator(\Swango\HttpServer\Validator $validator) {
40-
$this->content_validator = $validator;
41-
return $this;
42-
}
1+
<?php
2+
namespace Swango\HttpServer\Validator;
3+
4+
/**
5+
* 用于检测每项含义都相同的数组
6+
*/
7+
class FixedArray extends \Swango\HttpServer\Validator {
8+
private \Swango\HttpServer\Validator $content_validator;
9+
public function __construct($cnkey, private int $min_length = 0, private int $max_length = 4096) {
10+
parent::__construct($cnkey);
11+
}
12+
public function getCnMsg(): string {
13+
if (isset($this->cnmsg)) {
14+
return $this->cnmsg;
15+
}
16+
$ret = $this->cnkey . '必须为数组';
17+
if ($this->min_length > 0) {
18+
$ret .= ',最少' . $this->min_length . '';
19+
}
20+
$ret .= ',最长' . $this->max_length . '';
21+
if (! $this->isOptional() || ! $this->couldBeNull()) {
22+
$ret .= '且不可缺省';
23+
}
24+
return $ret;
25+
}
26+
protected function check(?string $key, &$value): void {
27+
if (! is_array($value) || is_object($value)) {
28+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
29+
}
30+
$i = 0;
31+
foreach ($value as $k => &$v)
32+
if ($k != $i++) {
33+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
34+
}
35+
unset($v);
36+
$length = count($value);
37+
if ($length < $this->min_length || $length > $this->max_length) {
38+
throw new \ExceptionToResponse\InvalidParameterException('Invalid ' . $key, $this->getCnMsg());
39+
}
40+
foreach ($value as $k => &$v)
41+
$this->content_validator->validate("$key.$k", $v);
42+
}
43+
public function setContentValidator(\Swango\HttpServer\Validator $validator): self {
44+
$this->content_validator = $validator;
45+
return $this;
46+
}
47+
public function getContentValidator(): \Swango\HttpServer\Validator {
48+
return $this->content_validator;
49+
}
4350
}

0 commit comments

Comments
 (0)