public onRequest ( Icicle\Http\Message\Request $request, Icicle\Socket\Socket $socket ) : Generator | Icicle\Awaitable\Awaitable | Icicle\Http\Message\Response | ||
$request | Icicle\Http\Message\Request | |
$socket | Icicle\Socket\Socket | |
Résultat | Generator | Icicle\Awaitable\Awaitable | Icicle\Http\Message\Response |
/** * @coroutine * * @param \Icicle\Http\Message\Request $request * @param \Icicle\Socket\Socket $socket * * @return \Generator * * @resolve \Icicle\Http\Message\Response $response * * @throws \Icicle\Http\Exception\InvalidResultError If a Response object is not returned from * Application::onHandshake(). */ public function onRequest(Request $request, Socket $socket) { $application = $this->handler->onRequest($request, $socket); if ($application instanceof \Generator) { $application = (yield from $application); } elseif ($application instanceof Awaitable) { $application = (yield $application); } if (!$application instanceof Application) { return $application; // Other response returned, let HTTP server handle it. } $response = $this->matcher->createResponse($application, $request, $socket); if (!$response instanceof WebSocketResponse) { return $response; } $message = $response->getMessage(); $result = $application->onHandshake($message, $request, $socket); if ($result instanceof \Generator) { $result = (yield from $result); } elseif ($result instanceof Awaitable) { $result = (yield $result); } if (!$result instanceof Response) { throw new InvalidResultError(sprintf('A %s object was not returned from %s::onHandshake().', Response::class, Application::class), $result); } if ($result !== $message) { $response = new WebSocketResponse($response->getApplication(), $response->getConnection(), $result); } return $response; }
/** * @coroutine * * @param \Icicle\Http\Message\Request $request * @param \Icicle\Socket\Socket $socket * * @return \Generator * * @resolve \Icicle\Http\Message|Response */ private function createResponse(Request $request, Socket $socket) : \Generator { try { assert(yield from $this->log->log(Log::DEBUG, 'Received request from %s:%d for %s', $socket->getRemoteAddress(), $socket->getRemotePort(), $request->getUri())); $response = $this->handler->onRequest($request, $socket); if ($response instanceof \Generator) { $response = (yield from $response); } elseif ($response instanceof Awaitable) { $response = (yield $response); } if (!$response instanceof Response) { throw new InvalidResultError(sprintf('A %s object was not returned from %::onRequest().', Response::class, get_class($this->handler)), $response); } } catch (Throwable $exception) { yield from $this->log->log(Log::ERROR, "Uncaught exception when creating response to a request from %s:%d on %s:%d in file %s on line %d: %s", $socket->getRemoteAddress(), $socket->getRemotePort(), $socket->getLocalAddress(), $socket->getLocalPort(), $exception->getFile(), $exception->getLine(), $exception->getMessage()); $response = (yield from $this->createDefaultErrorResponse(500)); } return $response; }