protocol = new Protocol($container, $protocolManager, 'jsonrpc-http'); $this->packer = $this->protocol->getPacker(); $this->responseBuilder = make(ResponseBuilder::class, [ 'dataFormatter' => $this->protocol->getDataFormatter(), 'packer' => $this->packer, ]); } protected function getDefaultExceptionHandler(): array { return [ HttpExceptionHandler::class, ]; } protected function createCoreMiddleware(): CoreMiddlewareInterface { return new HttpCoreMiddleware($this->container, $this->protocol, $this->responseBuilder, $this->serverName); } protected function initRequestAndResponse($request, $response): array { ResponseContext::set($psr7Response = new Psr7Response()); // Initialize PSR-7 Request and Response objects. $psr7Request = Psr7Request::loadFromSwooleRequest($request); if (! $this->isHealthCheck($psr7Request)) { if (! str_contains($psr7Request->getHeaderLine('content-type'), 'application/json')) { $psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::PARSE_ERROR); } // @TODO Optimize the error handling of encode. $content = $this->packer->unpack((string) $psr7Request->getBody()); if (! isset($content['jsonrpc'], $content['method'], $content['params'])) { $psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::INVALID_REQUEST); } } $psr7Request = $psr7Request->setUri($psr7Request->getUri()->withPath($content['method'] ?? '/')) ->setParsedBody($content['params'] ?? null) ->setAttribute('data', $content ?? []) ->setAttribute('request_id', $content['id'] ?? null); $this->getContext()->setData($content['context'] ?? []); RequestContext::set($psr7Request); ResponseContext::set($psr7Response); return [$psr7Request, $psr7Response]; } protected function isHealthCheck(RequestInterface $request): bool { return $request->getHeaderLine('user-agent') === 'Consul Health Check'; } protected function getContext() { return $this->container->get(RpcContext::class); } }