Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加支持grpc-web-text, 关闭http_compression #4805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/grpc-server/src/CoreMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
use FastRoute\Dispatcher;
use Google\Protobuf\Internal\Message;
use Google\Protobuf\Internal\Message as ProtobufMessage;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\MethodDefinitionCollector;
use Hyperf\Di\ReflectionManager;
use Hyperf\Grpc\Parser;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\CoreMiddleware as HttpCoreMiddleware;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Server\Exception\ServerException;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
Expand All @@ -46,6 +48,7 @@ class CoreMiddleware extends HttpCoreMiddleware
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->checkGrpcWebProxy();
$request = Context::set(ServerRequestInterface::class, $request);

/** @var Dispatched $dispatched */
Expand Down Expand Up @@ -135,7 +138,7 @@ protected function parseMethodParameters(string $controller, string $action, arr
$parentClass = $class->getParentClass();
if ($parentClass && $parentClass->getName() === ProtobufMessage::class) {
$request = $this->request();
$stream = $request->getBody();
$stream = $this->isGrpcWebText() ? @base64_decode((string) $request->getBody()) : $request->getBody();
return Parser::deserializeMessage([$class->getName(), null], (string) $stream);
}

Expand All @@ -153,6 +156,24 @@ protected function parseMethodParameters(string $controller, string $action, arr
return $injections;
}

protected function checkGrpcWebProxy()
{
$isGrpcWebText = $this->isGrpcWebText();
$accet = $this->request()->getHeaderLine('accept') == 'application/grpc-web-text';
$xGrpcWeb = $this->request()->getHeaderLine('x-grpc-web') == '1';
if ($isGrpcWebText || $accet || $xGrpcWeb) {
$httpCompression = ApplicationContext::getContainer()->get(ConfigInterface::class)->get('server.settings.http_compression');
if ($httpCompression !== false) {
throw new \RuntimeException('Grpc Web Text setting http_compression must is false in config/server.php');
}
}
}

protected function isGrpcWebText(): bool
{
return $this->request()->getHeaderLine('content-type') == 'application/grpc-web-text';
}

/**
* @return RequestInterface
*/
Expand All @@ -167,10 +188,13 @@ protected function request()
*/
protected function handleResponse(?Message $message, $httpStatus = 200, string $grpcStatus = '0', string $grpcMessage = ''): ResponseInterface
{
$responseContentType = $this->isGrpcWebText() ? 'application/grpc-web-text+proto' : 'application/grpc';
$responseContent = $this->isGrpcWebText() ? base64_encode(Parser::serializeMessage($message)) : Parser::serializeMessage($message);

return $this->response()->withStatus($httpStatus)
->withBody(new SwooleStream(Parser::serializeMessage($message)))
->withBody(new SwooleStream($responseContent))
->withAddedHeader('Server', 'Hyperf')
->withAddedHeader('Content-Type', 'application/grpc')
->withAddedHeader('Content-Type', $responseContentType)
->withAddedHeader('trailer', 'grpc-status, grpc-message')
->withTrailer('grpc-status', $grpcStatus)
->withTrailer('grpc-message', $grpcMessage);
Expand Down