-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrpcResponse.php
146 lines (119 loc) · 3.69 KB
/
GrpcResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Haskel\GrpcWebBundle;
use Haskel\GrpcWebBundle\Message\CompressedFlag;
use Haskel\GrpcWebBundle\Message\GrpcMode;
use Haskel\GrpcWebBundle\Message\LengthPrefixedMessage;
use Haskel\GrpcWebBundle\Message\StatusCode;
use Google\Protobuf\GPBEmpty;
use Google\Protobuf\Internal\Message;
use Symfony\Component\HttpFoundation\Response;
class GrpcResponse extends Response
{
private string $payload;
public function __construct(
Message $message = new GPBEmpty(),
StatusCode $status = StatusCode::Ok,
string $statusMessage = 'OK',
array $trailers = [],
array $headers = [],
private GrpcMode $type = GrpcMode::GrpcWeb,
private array $metadata = [],
) {
$trailers = [
'grpc-status' => $status->value,
'grpc-message' => $statusMessage,
...$trailers,
];
// todo: clean key and value
$trailersBody = implode(
"",
array_map(
fn ($key, $value) => $key . ': ' . str_replace("\n", " ", (string)$value) . "\r\n",
array_keys($trailers),
$trailers
)
);
$this->payload = (new LengthPrefixedMessage($message->serializeToString()))->encode()
. (new LengthPrefixedMessage($trailersBody, CompressedFlag::Compressed))->encode();
$content = $this->payload;
if ($type === GrpcMode::GrpcWebText) {
$content = base64_encode($this->payload);
}
$contentType = 'application/grpc-web';
if ($type === GrpcMode::GrpcWebText) {
$contentType = 'application/grpc-web-text';
}
parent::__construct(
$content,
Response::HTTP_OK, // @todo: map statuses grpc <-> http
[
'Content-Length' => strlen($content),
'Content-Type' => $contentType,
...$headers,
]
);
}
public function setType(GrpcMode $type): self
{
$content = $this->payload;
if ($type === GrpcMode::GrpcWebText) {
$content = base64_encode($this->payload);
}
$this->setContent($content);
$this->headers->set('Content-Length', (string)strlen($content));
$contentType = 'application/grpc-web';
if ($type === GrpcMode::GrpcWebText) {
$contentType = 'application/grpc-web-text';
}
$this->headers->set('Content-Type', $contentType);
return $this;
}
public function getMessage(): Message
{
return $this->message;
}
public function setMessage(Message $message): GrpcResponse
{
$this->message = $message;
return $this;
}
public function getStatus(): StatusCode
{
return $this->status;
}
public function setStatus(StatusCode $status): GrpcResponse
{
$this->status = $status;
return $this;
}
public function getStatusMessage(): ?string
{
return $this->statusMessage;
}
public function setStatusMessage(?string $statusMessage): GrpcResponse
{
$this->statusMessage = $statusMessage;
return $this;
}
/**
* @return array<string, string>
*/
public function getMetadata(): array
{
return $this->metadata;
}
/**
* @param array<string, string> $metadata
*/
public function setMetadata(array $metadata): GrpcResponse
{
$this->metadata = $metadata;
return $this;
}
public function addMetadata(string $key, string $value): GrpcResponse
{
$this->metadata[$key] = $value;
return $this;
}
}