HttpServer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\JsonRpc;
  12. use Hyperf\Context\RequestContext;
  13. use Hyperf\Context\ResponseContext;
  14. use Hyperf\Contract\PackerInterface;
  15. use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
  16. use Hyperf\HttpMessage\Server\Request as Psr7Request;
  17. use Hyperf\HttpMessage\Server\Response as Psr7Response;
  18. use Hyperf\HttpServer\Contract\CoreMiddlewareInterface;
  19. use Hyperf\HttpServer\ResponseEmitter;
  20. use Hyperf\HttpServer\Server;
  21. use Hyperf\JsonRpc\Exception\Handler\HttpExceptionHandler;
  22. use Hyperf\Rpc\Context as RpcContext;
  23. use Hyperf\Rpc\Protocol;
  24. use Hyperf\Rpc\ProtocolManager;
  25. use Hyperf\RpcServer\RequestDispatcher;
  26. use Psr\Container\ContainerInterface;
  27. use Psr\Http\Message\RequestInterface;
  28. use function Hyperf\Support\make;
  29. class HttpServer extends Server
  30. {
  31. protected Protocol $protocol;
  32. protected PackerInterface $packer;
  33. protected ResponseBuilder $responseBuilder;
  34. public function __construct(
  35. ContainerInterface $container,
  36. RequestDispatcher $dispatcher,
  37. ExceptionHandlerDispatcher $exceptionHandlerDispatcher,
  38. ResponseEmitter $responseEmitter,
  39. ProtocolManager $protocolManager
  40. ) {
  41. parent::__construct($container, $dispatcher, $exceptionHandlerDispatcher, $responseEmitter);
  42. $this->protocol = new Protocol($container, $protocolManager, 'jsonrpc-http');
  43. $this->packer = $this->protocol->getPacker();
  44. $this->responseBuilder = make(ResponseBuilder::class, [
  45. 'dataFormatter' => $this->protocol->getDataFormatter(),
  46. 'packer' => $this->packer,
  47. ]);
  48. }
  49. protected function getDefaultExceptionHandler(): array
  50. {
  51. return [
  52. HttpExceptionHandler::class,
  53. ];
  54. }
  55. protected function createCoreMiddleware(): CoreMiddlewareInterface
  56. {
  57. return new HttpCoreMiddleware($this->container, $this->protocol, $this->responseBuilder, $this->serverName);
  58. }
  59. protected function initRequestAndResponse($request, $response): array
  60. {
  61. ResponseContext::set($psr7Response = new Psr7Response());
  62. // Initialize PSR-7 Request and Response objects.
  63. $psr7Request = Psr7Request::loadFromSwooleRequest($request);
  64. if (! $this->isHealthCheck($psr7Request)) {
  65. if (! str_contains($psr7Request->getHeaderLine('content-type'), 'application/json')) {
  66. $psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::PARSE_ERROR);
  67. }
  68. // @TODO Optimize the error handling of encode.
  69. $content = $this->packer->unpack((string) $psr7Request->getBody());
  70. if (! isset($content['jsonrpc'], $content['method'], $content['params'])) {
  71. $psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::INVALID_REQUEST);
  72. }
  73. }
  74. $psr7Request = $psr7Request->setUri($psr7Request->getUri()->withPath($content['method'] ?? '/'))
  75. ->setParsedBody($content['params'] ?? null)
  76. ->setAttribute('data', $content ?? [])
  77. ->setAttribute('request_id', $content['id'] ?? null);
  78. $this->getContext()->setData($content['context'] ?? []);
  79. RequestContext::set($psr7Request);
  80. ResponseContext::set($psr7Response);
  81. return [$psr7Request, $psr7Response];
  82. }
  83. protected function isHealthCheck(RequestInterface $request): bool
  84. {
  85. return $request->getHeaderLine('user-agent') === 'Consul Health Check';
  86. }
  87. protected function getContext()
  88. {
  89. return $this->container->get(RpcContext::class);
  90. }
  91. }