Repository navigation
Expand file tree
/
Copy pathParser.php
More file actions
135 lines (118 loc) · 4.18 KB
/
Copy pathParser.php
File metadata and controls
135 lines (118 loc) · 4.18 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Grpc;
use Google\Protobuf\GPBEmpty;
use Google\Protobuf\Internal\Message;
use Google\Rpc\Status;
use Grpc\StringifyAble;
use Swoole\Http\Response;
use Swoole\Http2\Response as Http2Response;
class Parser
{
public const GRPC_ERROR_NO_RESPONSE = -1;
public static function pack(string $data): string
{
return pack('CN', 0, strlen($data)) . $data;
}
public static function unpack(string $data): string
{
// it's the way to verify the package length
// 1 + 4 + data
// $len = unpack('N', substr($data, 1, 4))[1];
// assert(strlen($data) - 5 === $len);
return substr($data, 5);
}
public static function serializeMessage($data)
{
return self::pack(self::serializeUnpackedMessage($data));
}
public static function deserializeMessage($deserialize, string $value)
{
if (empty($value)) {
return null;
}
return self::deserializeUnpackedMessage($deserialize, self::unpack($value));
}
/**
* @param null|Http2Response $response
* @param mixed $deserialize
* @return Http2Response[]|Message[]|StringifyAble[]
*/
public static function parseResponse($response, $deserialize): array
{
if (! $response) {
return ['No response', self::GRPC_ERROR_NO_RESPONSE, $response];
}
if (self::isInvalidStatus($response->statusCode)) {
$message = $response->headers['grpc-message'] ?? 'Http status Error';
$code = $response->headers['grpc-status'] ?? ($response->errCode ?: $response->statusCode);
return [$message, (int) $code, $response];
}
$grpcStatus = (int) ($response->headers['grpc-status'] ?? 0);
if ($grpcStatus !== 0) {
return [$response->headers['grpc-message'] ?? 'Unknown error', $grpcStatus, $response];
}
$data = $response->data ?? '';
$reply = self::deserializeMessage($deserialize, $data);
$status = (int) ($response->headers['grpc-status'] ?? 0);
return [$reply, $status, $response];
}
/**
* @param Response $response
*/
public static function statusFromResponse($response): ?Status
{
$detailsEncoded = $response->headers['grpc-status-details-bin'] ?? '';
if (! $detailsEncoded || ! $detailsBin = base64_decode($detailsEncoded, true)) {
return null;
}
return self::deserializeUnpackedMessage([Status::class, ''], $detailsBin);
}
public static function statusToDetailsBin(Status $status): string
{
return base64_encode(self::serializeUnpackedMessage($status));
}
private static function deserializeUnpackedMessage($deserialize, string $unpacked)
{
if (is_array($deserialize)) {
[$className, $deserializeFunc] = $deserialize;
/** @var Message $object */
$object = new $className();
if ($deserializeFunc && method_exists($object, $deserializeFunc)) {
$object->{$deserializeFunc}($unpacked);
} else {
// @noinspection PhpUndefinedMethodInspection
$object->mergeFromString($unpacked);
}
return $object;
}
return call_user_func($deserialize, $unpacked);
}
private static function serializeUnpackedMessage($data): string
{
if ($data === null) {
$data = new GPBEmpty();
}
if (method_exists($data, 'encode')) {
$data = $data->encode();
} elseif (method_exists($data, 'serializeToString')) {
$data = $data->serializeToString();
} elseif (method_exists($data, 'serialize')) {
/** @noinspection PhpUndefinedMethodInspection */
$data = $data->serialize();
}
return (string) $data;
}
private static function isInvalidStatus(int $code): bool
{
return $code !== 0 && $code !== 200 && $code !== 400;
}
}