|
5 | 5 | * @contact mondagroup_php@163.com |
6 | 6 | * |
7 | 7 | */ |
| 8 | + |
8 | 9 | namespace framework\server; |
9 | 10 |
|
10 | 11 | use ErrorException; |
@@ -131,6 +132,13 @@ public function onMessage(TcpConnection $connection, Request $request) |
131 | 132 | static::$_request = $httpRequest; |
132 | 133 | //response |
133 | 134 | $httpResponse = HttpResponse::init(new Response(200)); |
| 135 | + |
| 136 | + //遇到option |
| 137 | + $cors = config('cors', null); |
| 138 | + if (!empty($cors) && isset($cors['enable']) && $cors['enable'] && $request->method() == 'OPTIONS') { |
| 139 | + self::send($connection, $httpResponse->body('')->end(), $request); |
| 140 | + return; |
| 141 | + } |
134 | 142 | try { |
135 | 143 | $routeInfo = $this->dispatcher->dispatch($httpRequest->getMethod(), $httpRequest->getPath()); |
136 | 144 | switch ($routeInfo[0]) { |
@@ -179,8 +187,14 @@ public static function request() |
179 | 187 | * @param $response |
180 | 188 | * @param \Workerman\Protocols\Http\Request $request |
181 | 189 | */ |
182 | | - protected static function send(TcpConnection $connection, $response, Request $request) |
| 190 | + protected static function send(TcpConnection $connection, Response $response, Request $request) |
183 | 191 | { |
| 192 | + $cors = config('cors', null); |
| 193 | + if (!empty($cors) && isset($cors['enable']) && $cors['enable'] && is_array($cors)) { |
| 194 | + foreach ($cors ?? [] as $key => $value) { |
| 195 | + $response->header($key, $value); |
| 196 | + } |
| 197 | + } |
184 | 198 | $keepAlive = $request->header('connection'); |
185 | 199 | if ((null === $keepAlive && '1.1' === $request->protocolVersion()) |
186 | 200 | || 'keep-alive' === $keepAlive || 'Keep-Alive' === $keepAlive |
|
0 commit comments