1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Onnov\JsonRpcServer; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use JsonMapper; |
9
|
|
|
use Onnov\JsonRpcServer\Model\RunModel; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use stdClass; |
13
|
|
|
use Throwable; |
14
|
|
|
use Onnov\JsonRpcServer\Exception\InvalidAuthorizeException; |
15
|
|
|
use Onnov\JsonRpcServer\Exception\InvalidParamsException; |
16
|
|
|
use Onnov\JsonRpcServer\Exception\InvalidRequestException; |
17
|
|
|
use Onnov\JsonRpcServer\Exception\MethodErrorException; |
18
|
|
|
use Onnov\JsonRpcServer\Exception\MethodNotFoundException; |
19
|
|
|
use Onnov\JsonRpcServer\Exception\ParseErrorException; |
20
|
|
|
use Onnov\JsonRpcServer\Service\ApiExecService; |
21
|
|
|
use Onnov\JsonRpcServer\Service\RpcService; |
22
|
|
|
use Onnov\JsonRpcServer\Validator\JsonRpcSchema; |
23
|
|
|
use Onnov\JsonRpcServer\Validator\JsonSchemaValidator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class JsonRpcHandler |
27
|
|
|
* |
28
|
|
|
* @package Onnov\JsonRpcServer |
29
|
|
|
*/ |
30
|
|
|
class JsonRpcHandler |
31
|
|
|
{ |
32
|
|
|
/** @var LoggerInterface|null */ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** @var RpcService */ |
36
|
|
|
private $rpcService; |
37
|
|
|
|
38
|
|
|
/** @var ApiExecService */ |
39
|
|
|
private $apiExecService; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* JsonRpcHandler constructor. |
43
|
|
|
* |
44
|
|
|
* @param LoggerInterface|null $logger |
45
|
|
|
*/ |
46
|
6 |
|
public function __construct(LoggerInterface $logger = null) |
47
|
|
|
{ |
48
|
6 |
|
$this->logger = $logger; |
49
|
|
|
|
50
|
6 |
|
$validator = new JsonSchemaValidator(); |
51
|
6 |
|
$rpcSchema = new JsonRpcSchema(); |
52
|
6 |
|
$mapper = new JsonMapper(); |
53
|
|
|
|
54
|
6 |
|
$this->rpcService = new RpcService($validator, $rpcSchema, $mapper); |
55
|
6 |
|
$this->apiExecService = new ApiExecService($validator, $rpcSchema, $mapper); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RunModel $model |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
6 |
|
public function run(RunModel $model): string |
63
|
|
|
{ |
64
|
6 |
|
$rpcService = $this->getRpcService(); |
65
|
|
|
|
66
|
|
|
/** Парсим */ |
67
|
6 |
|
$data = $rpcService->jsonParse($model->getJson()); |
68
|
|
|
|
69
|
5 |
|
$resArr = []; |
70
|
5 |
|
foreach ($data as $rpc) { |
71
|
|
|
// TODO впилить паралельное выполнение, возможно amphp/amp |
72
|
5 |
|
$resArr[] = $this->oneRun( |
73
|
5 |
|
$model, |
74
|
|
|
$rpc |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
$res = implode(',', $resArr); |
79
|
|
|
|
80
|
5 |
|
if ($rpcService->isBatch()) { |
81
|
|
|
$res = '[' . $res . ']'; |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
return $res; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param RunModel $model |
89
|
|
|
* @param stdClass $rpc |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
5 |
|
private function oneRun( |
93
|
|
|
RunModel $model, |
94
|
|
|
stdClass $rpc |
95
|
|
|
): string { |
96
|
|
|
$res = [ |
97
|
5 |
|
'jsonrpc' => '2.0', |
98
|
|
|
]; |
99
|
|
|
|
100
|
5 |
|
$errorLevel = null; |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
/** валидируем и парсим JsonRPC */ |
104
|
5 |
|
$rpcObj = $this->getRpcService()->getRpc($rpc); |
105
|
|
|
|
106
|
|
|
/** Проверим авторизацию */ |
107
|
4 |
|
$this->authCheck($rpcObj->getMethod(), $model->isResultAuth(), $model->getMethodsWithoutAuth()); |
108
|
|
|
|
109
|
|
|
/** Пытаемся выполнить запрос */ |
110
|
3 |
|
$res['result'] = $this->getApiExeService()->exe( |
111
|
3 |
|
$model, |
112
|
|
|
$rpcObj |
113
|
|
|
); |
114
|
3 |
|
} catch (InvalidAuthorizeException $e) { |
115
|
1 |
|
$res['error'] = [ |
116
|
1 |
|
'code' => -32001, |
117
|
1 |
|
'message' => $e->getMessage(), |
118
|
|
|
]; |
119
|
1 |
|
$errorLevel = LogLevel::INFO; |
120
|
2 |
|
} catch (ParseErrorException $e) { |
121
|
|
|
$res['error'] = [ |
122
|
|
|
'code' => -32700, |
123
|
|
|
'message' => 'Parse error: ' . $e->getMessage(), |
124
|
|
|
]; |
125
|
|
|
$errorLevel = LogLevel::ERROR; |
126
|
2 |
|
} catch (InvalidRequestException $e) { |
127
|
|
|
$res['error'] = [ |
128
|
|
|
'code' => -32600, |
129
|
|
|
'message' => 'Invalid Request: ' . $e->getMessage(), |
130
|
|
|
]; |
131
|
|
|
$errorLevel = LogLevel::ERROR; |
132
|
2 |
|
} catch (MethodNotFoundException $e) { |
133
|
1 |
|
$res['error'] = [ |
134
|
1 |
|
'code' => -32601, |
135
|
1 |
|
'message' => $e->getMessage(), // 'Method not found', |
136
|
|
|
]; |
137
|
1 |
|
$errorLevel = LogLevel::ERROR; |
138
|
1 |
|
} catch (InvalidParamsException $e) { |
139
|
1 |
|
$res['error'] = [ |
140
|
1 |
|
'code' => -32602, |
141
|
1 |
|
'message' => $e->getMessage(), // 'Invalid params', |
142
|
1 |
|
'data' => $e->getData(), |
143
|
|
|
]; |
144
|
1 |
|
$errorLevel = LogLevel::ERROR; |
145
|
|
|
} catch (MethodErrorException $e) { |
146
|
|
|
$res['error'] = [ |
147
|
|
|
'code' => $e->getCode(), |
148
|
|
|
'message' => $e->getMessage(), |
149
|
|
|
'data' => $e->getData(), |
150
|
|
|
]; |
151
|
|
|
$errorLevel = LogLevel::NOTICE; |
152
|
|
|
} catch (Throwable $t) { |
153
|
|
|
$res['error'] = [ |
154
|
|
|
'code' => -32603, |
155
|
|
|
'message' => 'Internal error: ' . $t->getMessage(), |
156
|
|
|
'data' => [ |
157
|
|
|
'exception' => get_class($t), |
158
|
|
|
'code' => $t->getCode(), |
159
|
|
|
'file' => $t->getFile(), |
160
|
|
|
'line' => $t->getLine(), |
161
|
|
|
] |
162
|
|
|
]; |
163
|
|
|
$errorLevel = LogLevel::EMERGENCY; |
164
|
|
|
} |
165
|
|
|
|
166
|
5 |
|
$this->log($errorLevel, $res); |
167
|
|
|
|
168
|
5 |
|
return $this->getJsonResult($rpc, $res); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $method |
173
|
|
|
* @param bool $resultAuth |
174
|
|
|
* @param string[] $methodsWithoutAuth |
175
|
|
|
*/ |
176
|
4 |
|
private function authCheck(string $method, bool $resultAuth, array $methodsWithoutAuth): void |
177
|
|
|
{ |
178
|
|
|
if ( |
179
|
4 |
|
$resultAuth === false |
180
|
4 |
|
&& in_array($method, $methodsWithoutAuth, true) === false |
181
|
|
|
) { |
182
|
1 |
|
throw new InvalidAuthorizeException( |
183
|
1 |
|
'Access denied, you are not authorized', // 'Доступ запрещен, вы не авторизованы' |
184
|
|
|
); |
185
|
|
|
} |
186
|
3 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param stdClass $rpc |
190
|
|
|
* @param mixed[] $res |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
5 |
|
private function getJsonResult(stdClass $rpc, array &$res): string |
194
|
|
|
{ |
195
|
|
|
// ??? |
196
|
5 |
|
$strId = 'error'; |
197
|
5 |
|
if (isset($rpc->id)) { |
198
|
5 |
|
$res['id'] = $rpc->id; |
199
|
5 |
|
$strId = $rpc->id; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
try { |
203
|
5 |
|
$result = json_encode($res, JSON_THROW_ON_ERROR); |
204
|
|
|
} catch (JsonException $e) { |
205
|
|
|
$result = '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "' |
206
|
|
|
. $e->getMessage() . '"}, "id": ' . $strId . '}'; |
207
|
|
|
} |
208
|
|
|
|
209
|
5 |
|
return $result; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string|null $errorLevel |
214
|
|
|
* @param mixed[] $res |
215
|
|
|
*/ |
216
|
5 |
|
private function log(?string $errorLevel, array $res): void |
217
|
|
|
{ |
218
|
5 |
|
$logger = $this->getLogger(); |
219
|
5 |
|
if ($errorLevel !== null && $logger instanceof LoggerInterface) { |
220
|
|
|
$error = $res['error'] ?? []; |
221
|
|
|
$logger->log($errorLevel, $error['message'], $error); |
222
|
|
|
} |
223
|
5 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return LoggerInterface|null |
227
|
|
|
*/ |
228
|
5 |
|
public function getLogger(): ?LoggerInterface |
229
|
|
|
{ |
230
|
5 |
|
return $this->logger; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param LoggerInterface|null $logger |
235
|
|
|
*/ |
236
|
|
|
public function setLogger(?LoggerInterface $logger): void |
237
|
|
|
{ |
238
|
|
|
$this->logger = $logger; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return RpcService |
243
|
|
|
*/ |
244
|
6 |
|
public function getRpcService(): RpcService |
245
|
|
|
{ |
246
|
6 |
|
return $this->rpcService; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return ApiExecService |
251
|
|
|
*/ |
252
|
3 |
|
public function getApiExeService(): ApiExecService |
253
|
|
|
{ |
254
|
3 |
|
return $this->apiExecService; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|