正在显示
2 个修改的文件
包含
43 行增加
和
11 行删除
| @@ -11,6 +11,7 @@ class SimpleRequest | @@ -11,6 +11,7 @@ class SimpleRequest | ||
| 11 | public string $method; | 11 | public string $method; |
| 12 | public array $header; | 12 | public array $header; |
| 13 | public array $cookie; | 13 | public array $cookie; |
| 14 | + public string $rawContent; | ||
| 14 | 15 | ||
| 15 | public function __construct($data) | 16 | public function __construct($data) |
| 16 | { | 17 | { |
| @@ -21,5 +22,14 @@ class SimpleRequest | @@ -21,5 +22,14 @@ class SimpleRequest | ||
| 21 | $this->method = $data['method']; | 22 | $this->method = $data['method']; |
| 22 | $this->header = $data['header']; | 23 | $this->header = $data['header']; |
| 23 | $this->cookie = $data['cookie']; | 24 | $this->cookie = $data['cookie']; |
| 25 | + $this->rawContent = isset($data['rawContent']) ? $data['rawContent'] : ''; | ||
| 26 | + if ($this->method == 'POST') { | ||
| 27 | + if (count($this->post) == 0) { | ||
| 28 | + $data = json_decode($this->rawContent, true); | ||
| 29 | + if ($data) { | ||
| 30 | + $this->post = $data; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| 24 | } | 34 | } |
| 25 | -} | ||
| 35 | +} |
| 1 | <?php | 1 | <?php |
| 2 | + | ||
| 2 | namespace Jiaoyin; | 3 | namespace Jiaoyin; |
| 4 | + | ||
| 5 | +require_once __DIR__ . '/func.php'; | ||
| 6 | + | ||
| 3 | use Swoole\Http\Request; | 7 | use Swoole\Http\Request; |
| 4 | use Swoole\Http\Response; | 8 | use Swoole\Http\Response; |
| 5 | use Swoole\Coroutine\Http\Server; | 9 | use Swoole\Coroutine\Http\Server; |
| 10 | +use function Jiaoyin\output; | ||
| 6 | 11 | ||
| 7 | class SimpleServerCoroutine | 12 | class SimpleServerCoroutine |
| 8 | { | 13 | { |
| @@ -11,35 +16,52 @@ class SimpleServerCoroutine | @@ -11,35 +16,52 @@ class SimpleServerCoroutine | ||
| 11 | { | 16 | { |
| 12 | $this->httpServer = new Server($host, $port, $ssl); | 17 | $this->httpServer = new Server($host, $port, $ssl); |
| 13 | } | 18 | } |
| 14 | - public function router($method, $path, $callback){ | ||
| 15 | - $this->httpServer->handle($path, function (Request $request, Response $response) use ($method,$callback) { | 19 | + public function router(array $method, $path, $callback) |
| 20 | + { | ||
| 21 | + $this->httpServer->handle($path, function (Request $request, Response $response) use ($method, $callback) { | ||
| 16 | $m = $request->getMethod(); | 22 | $m = $request->getMethod(); |
| 17 | - if(!in_array($m,$method)){ | 23 | + foreach ($method as $k => $v) { |
| 24 | + $method[$k] = strtolower($v); | ||
| 25 | + } | ||
| 26 | + $m = strtolower($m); | ||
| 27 | + if (!in_array($m, $method)) { | ||
| 18 | $response->status(405); | 28 | $response->status(405); |
| 19 | - $response->end(); | 29 | + $data = [ |
| 30 | + 'code' => 405, | ||
| 31 | + 'msg' => 'Method Not Allowed', | ||
| 32 | + 'currentMethod' => $m, | ||
| 33 | + 'allowMethod' => $method, | ||
| 34 | + ]; | ||
| 35 | + $response->end(json_encode($data)); | ||
| 20 | return; | 36 | return; |
| 21 | } | 37 | } |
| 22 | $requestInfo = [ | 38 | $requestInfo = [ |
| 39 | + 'method' => $request->getMethod(), | ||
| 23 | 'path' => $request->server['path_info'], | 40 | 'path' => $request->server['path_info'], |
| 24 | 'uri' => $request->server['request_uri'], | 41 | 'uri' => $request->server['request_uri'], |
| 25 | 'get' => $request->get ?: [], | 42 | 'get' => $request->get ?: [], |
| 26 | 'post' => $request->post ?: [], | 43 | 'post' => $request->post ?: [], |
| 27 | - 'method' => $request->getMethod(), | ||
| 28 | 'header' => $request->header ?: [], | 44 | 'header' => $request->header ?: [], |
| 29 | 'cookie' => $request->cookie ?: [], | 45 | 'cookie' => $request->cookie ?: [], |
| 46 | + 'rawContent' => $request->rawContent() ?: '' | ||
| 30 | ]; | 47 | ]; |
| 48 | + output($requestInfo['method'], $requestInfo['path'], "GET:" . json_encode($requestInfo['get']), "POST:" . json_encode($requestInfo['post']), "rawContent:" . $requestInfo['rawContent']); | ||
| 31 | $simpleRequest = new SimpleRequest($requestInfo); | 49 | $simpleRequest = new SimpleRequest($requestInfo); |
| 32 | $res = call_user_func($callback, $simpleRequest); | 50 | $res = call_user_func($callback, $simpleRequest); |
| 33 | - if(empty($res)){ | ||
| 34 | - $response->end(json_encode(['code'=>-1,'msg'=>'nothing return'])); | ||
| 35 | - }else{ | 51 | + if (empty($res)) { |
| 52 | + $response->end(json_encode(['code' => -1, 'msg' => 'nothing return'])); | ||
| 53 | + } else { | ||
| 36 | $response->end($res); | 54 | $response->end($res); |
| 37 | } | 55 | } |
| 38 | }); | 56 | }); |
| 39 | } | 57 | } |
| 40 | 58 | ||
| 41 | - public function start(){ | 59 | + public function start() |
| 60 | + { | ||
| 42 | $this->httpServer->start(); | 61 | $this->httpServer->start(); |
| 43 | } | 62 | } |
| 63 | + public function stop() | ||
| 64 | + { | ||
| 65 | + $this->httpServer->shutdown(); | ||
| 66 | + } | ||
| 44 | } | 67 | } |
| 45 | - |
-
mentioned in commit e32c4bd2
-
请 注册 或 登录 后发表评论