Skip to content

Commit

Permalink
Fixed bug that jsonrpc-http cannot support swow. (#7043)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored Sep 2, 2024
1 parent 9179bb6 commit 7472482
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@

namespace Hyperf\JsonRpc;

use Hyperf\Context\RequestContext;
use Hyperf\Context\ResponseContext;
use Hyperf\Contract\PackerInterface;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpMessage\Server\Request as Psr7Request;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\HttpServer\Contract\CoreMiddlewareInterface;
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\HttpServer\Server;
Expand All @@ -28,6 +24,8 @@
use Hyperf\RpcServer\RequestDispatcher;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Swow\Psr7\Message\ResponsePlusInterface;
use Swow\Psr7\Message\ServerRequestPlusInterface;

use function Hyperf\Support\make;

Expand Down Expand Up @@ -69,9 +67,12 @@ protected function createCoreMiddleware(): CoreMiddlewareInterface

protected function initRequestAndResponse($request, $response): array
{
ResponseContext::set($psr7Response = new Psr7Response());
// Initialize PSR-7 Request and Response objects.
$psr7Request = Psr7Request::loadFromSwooleRequest($request);
/**
* @var ServerRequestPlusInterface $psr7Request
* @var ResponsePlusInterface $psr7Response
*/
[$psr7Request, $psr7Response] = parent::initRequestAndResponse($request, $response);

if (! $this->isHealthCheck($psr7Request)) {
if (! str_contains($psr7Request->getHeaderLine('content-type'), 'application/json')) {
$psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::PARSE_ERROR);
Expand All @@ -82,15 +83,14 @@ protected function initRequestAndResponse($request, $response): array
$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];
}

Expand Down

0 comments on commit 7472482

Please sign in to comment.