作者 karlet

feat:健壮HttpSer

@@ -13,13 +13,23 @@ class SimpleServerCoroutine @@ -13,13 +13,23 @@ class SimpleServerCoroutine
13 { 13 {
14 $this->httpServer = new Server($host, $port, $ssl); 14 $this->httpServer = new Server($host, $port, $ssl);
15 } 15 }
16 - public function router($method, $path, $callback) 16 + public function router(array $method, $path, $callback)
17 { 17 {
18 $this->httpServer->handle($path, function (Request $request, Response $response) use ($method, $callback) { 18 $this->httpServer->handle($path, function (Request $request, Response $response) use ($method, $callback) {
19 $m = $request->getMethod(); 19 $m = $request->getMethod();
  20 + foreach ($method as $k => $v) {
  21 + $method[$k] = strtolower($v);
  22 + }
  23 + $m = strtolower($m);
20 if (!in_array($m, $method)) { 24 if (!in_array($m, $method)) {
21 $response->status(405); 25 $response->status(405);
22 - $response->end(); 26 + $data = [
  27 + 'code' => 405,
  28 + 'msg' => 'Method Not Allowed',
  29 + 'currentMethod' => $m,
  30 + 'allowMethod' => $method,
  31 + ];
  32 + $response->end(json_encode($data));
23 return; 33 return;
24 } 34 }
25 $requestInfo = [ 35 $requestInfo = [