Skip to content

Commit 5d8f278

Browse files
authored
Update HttpServer.php
1 parent 45f99f5 commit 5d8f278

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/server/HttpServer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @contact mondagroup_php@163.com
66
*
77
*/
8+
89
namespace framework\server;
910

1011
use ErrorException;
@@ -131,6 +132,13 @@ public function onMessage(TcpConnection $connection, Request $request)
131132
static::$_request = $httpRequest;
132133
//response
133134
$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+
}
134142
try {
135143
$routeInfo = $this->dispatcher->dispatch($httpRequest->getMethod(), $httpRequest->getPath());
136144
switch ($routeInfo[0]) {
@@ -179,8 +187,14 @@ public static function request()
179187
* @param $response
180188
* @param \Workerman\Protocols\Http\Request $request
181189
*/
182-
protected static function send(TcpConnection $connection, $response, Request $request)
190+
protected static function send(TcpConnection $connection, Response $response, Request $request)
183191
{
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+
}
184198
$keepAlive = $request->header('connection');
185199
if ((null === $keepAlive && '1.1' === $request->protocolVersion())
186200
|| 'keep-alive' === $keepAlive || 'Keep-Alive' === $keepAlive

0 commit comments

Comments
 (0)