Skip to content

Commit 2f826f7

Browse files
authored
Send response directly when the handler does not exists. (#3308)
1 parent d3f0199 commit 2f826f7

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/CoreMiddleware.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
class CoreMiddleware extends HttpCoreMiddleware
2323
{
24+
const HANDLER_NAME = 'class';
25+
2426
/**
2527
* Handle the response when found.
2628
*/
@@ -42,6 +44,6 @@ protected function handleFound(Dispatched $dispatched, ServerRequestInterface $r
4244
$response = $response->withHeader(Security::SEC_WEBSOCKET_PROTOCOL, $wsProtocol);
4345
}
4446

45-
return $response->withAttribute('class', $controller);
47+
return $response->withAttribute(self::HANDLER_NAME, $controller);
4648
}
4749
}

src/Exception/Handler/WebSocketExceptionHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hyperf\Contract\StdoutLoggerInterface;
1515
use Hyperf\ExceptionHandler\ExceptionHandler;
1616
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
17+
use Hyperf\HttpMessage\Exception\HttpException;
1718
use Hyperf\HttpMessage\Stream\SwooleStream;
1819
use Psr\Http\Message\ResponseInterface;
1920
use Throwable;
@@ -39,6 +40,9 @@ public function __construct(StdoutLoggerInterface $logger, FormatterInterface $f
3940
public function handle(Throwable $throwable, ResponseInterface $response)
4041
{
4142
$this->logger->warning($this->formatter->format($throwable));
43+
if ($throwable instanceof HttpException) {
44+
$response = $response->withStatus($throwable->getStatusCode());
45+
}
4246
$stream = new SwooleStream((string) $throwable->getMessage());
4347
return $response->withBody($stream);
4448
}

src/Server.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
163163
/** @var Response $psr7Response */
164164
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
165165

166-
$class = $psr7Response->getAttribute('class');
166+
$class = $psr7Response->getAttribute(CoreMiddleware::HANDLER_NAME);
167167

168168
if (empty($class)) {
169-
throw new WebSocketHandeShakeException('WebSocket hande shake failed, because the class does not exists.');
169+
$this->logger->warning('WebSocket hande shake failed, because the class does not exists.');
170+
return;
170171
}
171172

172173
FdCollector::set($fd, $class);

0 commit comments

Comments
 (0)