1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TCP accel HTTP spec |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2018/7/30 |
6
|
|
|
* Time: 5:13 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\HRPC\Accel\Chips; |
10
|
|
|
|
11
|
|
|
use Carno\HRPC\Accel\Contracts\Config; |
12
|
|
|
use Carno\HRPC\Accel\Exception\InvalidPacketException; |
13
|
|
|
use Carno\HTTP\Standard\Helper; |
14
|
|
|
use Carno\HTTP\Standard\Response; |
15
|
|
|
use Carno\HTTP\Standard\ServerRequest; |
16
|
|
|
use Carno\HTTP\Standard\Uri; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
|
21
|
|
|
trait Specification |
22
|
|
|
{ |
23
|
|
|
use Helper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RequestInterface $request |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
protected function request2s(RequestInterface $request) : string |
30
|
|
|
{ |
31
|
|
|
$server = $request->getUri()->getHost(); |
32
|
|
|
|
33
|
|
|
[2 => $service, 3 => $method] = explode('/', $request->getUri()->getPath()); |
34
|
|
|
|
35
|
|
|
return json_encode([ |
36
|
|
|
'server' => $server, |
37
|
|
|
'service' => $service, |
38
|
|
|
'method' => $method, |
39
|
|
|
'status' => 0, |
40
|
|
|
'meta' => $this->getHeaderLines($request), |
41
|
|
|
'payload' => base64_encode($request->getBody()), |
42
|
|
|
], Config::JSON_CODEC_OPTS); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $message |
47
|
|
|
* @return ServerRequestInterface |
48
|
|
|
* @throws InvalidPacketException |
49
|
|
|
*/ |
50
|
|
|
protected function s2request(string $message) : ServerRequestInterface |
51
|
|
|
{ |
52
|
|
|
if (empty($packet = json_decode($message, true))) { |
53
|
|
|
throw new InvalidPacketException; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$srq = new ServerRequest( |
57
|
|
|
[], |
58
|
|
|
[], |
59
|
|
|
[], |
60
|
|
|
'POST', |
61
|
|
|
$packet['meta'], |
62
|
|
|
base64_decode($packet['payload']) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$srq->withUri( |
66
|
|
|
new Uri( |
67
|
|
|
'http', |
68
|
|
|
$packet['server'], |
69
|
|
|
null, |
70
|
|
|
sprintf('/invoke/%s/%s', $packet['service'], $packet['method']) |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $srq; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ResponseInterface $response |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function response2s(ResponseInterface $response) : string |
82
|
|
|
{ |
83
|
|
|
return json_encode([ |
84
|
|
|
'server' => '', |
85
|
|
|
'service' => '', |
86
|
|
|
'method' => '', |
87
|
|
|
'status' => $response->getStatusCode(), |
88
|
|
|
'meta' => $this->getHeaderLines($response), |
89
|
|
|
'payload' => base64_encode($response->getBody()), |
90
|
|
|
], Config::JSON_CODEC_OPTS); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $message |
95
|
|
|
* @return ResponseInterface |
96
|
|
|
* @throws InvalidPacketException |
97
|
|
|
*/ |
98
|
|
|
protected function s2response(string $message) : ResponseInterface |
99
|
|
|
{ |
100
|
|
|
if (empty($packet = json_decode($message, true))) { |
101
|
|
|
throw new InvalidPacketException; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return new Response( |
105
|
|
|
$packet['status'], |
106
|
|
|
$packet['meta'], |
107
|
|
|
base64_decode($packet['payload']) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|