|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace PeeHaa\AsyncTwitter\Api\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Amp\Artax\Notify; |
|
6
|
|
|
use Amp\Deferred; |
|
7
|
|
|
use Amp\Promise; |
|
8
|
|
|
use ExceptionalJSON\DecodeErrorException; |
|
9
|
|
|
use PeeHaa\AsyncTwitter\Api\Client\Exception\StreamFailureException; |
|
10
|
|
|
use PeeHaa\AsyncTwitter\Api\Client\Exception\StreamOpenFailureException; |
|
11
|
|
|
use PeeHaa\AsyncTwitter\Api\StatusStream; |
|
12
|
|
|
|
|
13
|
|
|
class StreamReader |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @uses processResponseHeaders |
|
17
|
|
|
* @uses processBodyData |
|
18
|
|
|
* @uses processResponseComplete |
|
19
|
|
|
* @uses processError |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $progressHandlers = [ |
|
22
|
|
|
Notify::RESPONSE_HEADERS => 'processResponseHeaders', |
|
23
|
|
|
Notify::RESPONSE_BODY_DATA => 'processBodyData', |
|
24
|
|
|
Notify::RESPONSE => 'processResponseComplete', |
|
25
|
|
|
Notify::ERROR => 'processError', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var StatusStream |
|
30
|
|
|
*/ |
|
31
|
|
|
private $stream; |
|
32
|
|
|
|
|
33
|
|
|
private $openDeferred; |
|
34
|
|
|
private $inflateCtx; |
|
35
|
|
|
private $rawDataBuffer = ''; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->openDeferred = new Deferred; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function processBodyData(string $data) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->stream === null) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @noinspection PhpUndefinedFunctionInspection */ |
|
49
|
|
|
$this->rawDataBuffer .= $this->inflateCtx !== null |
|
50
|
|
|
? \inflate_add($this->inflateCtx, $data) |
|
51
|
|
|
: $data; |
|
52
|
|
|
|
|
53
|
|
|
while (false !== $pos = \strpos($this->rawDataBuffer, "\r\n")) { |
|
54
|
|
|
if ($pos > 0) { |
|
55
|
|
|
try { |
|
56
|
|
|
$this->stream->update(\json_try_decode(\substr($this->rawDataBuffer, 0, $pos), true)); |
|
57
|
|
|
} catch (DecodeErrorException $e) { |
|
58
|
|
|
$this->stream->fail(new StreamFailureException('Error decoding stream data', 0, $e)); |
|
|
|
|
|
|
59
|
|
|
$this->inflateCtx = $this->stream = null; |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->rawDataBuffer = (string)\substr($this->rawDataBuffer, $pos + 2); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function processResponseHeaders(array $info) |
|
69
|
|
|
{ |
|
70
|
|
|
static $zlibEncodingMap = [ |
|
71
|
|
|
'gzip' => ZLIB_ENCODING_GZIP, |
|
72
|
|
|
'x-gzip' => ZLIB_ENCODING_GZIP, // recommended for compatibility by MDN |
|
73
|
|
|
'deflate' => ZLIB_ENCODING_DEFLATE, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$status = (int)($info['status'] ?? -1); |
|
|
|
|
|
|
77
|
|
|
$headers = (array)($info['headers'] ?? []); |
|
78
|
|
|
|
|
79
|
|
|
if ($status !== 200) { |
|
80
|
|
|
$this->openDeferred->fail(new StreamOpenFailureException("Server responded with HTTP status {$status}")); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$encoding = 'identity'; |
|
84
|
|
|
|
|
85
|
|
|
foreach ($headers as $name => $values) { |
|
86
|
|
|
if (\strtolower($name) !== 'content-encoding') { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$encoding = \strtolower($values[0] ?? 'identity'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($encoding !== 'identity') { |
|
94
|
|
|
if (!isset($zlibEncodingMap[$encoding])) { |
|
95
|
|
|
$this->openDeferred->fail( |
|
96
|
|
|
new StreamOpenFailureException("Server is using an encoding format I don't understand: {$encoding}") |
|
|
|
|
|
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** @noinspection PhpUndefinedFunctionInspection */ |
|
101
|
|
|
$this->inflateCtx = \inflate_init($zlibEncodingMap[$encoding]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->stream = new StatusStream; |
|
105
|
|
|
|
|
106
|
|
|
$this->openDeferred->succeed($this->stream); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function processError(\Throwable $error) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->stream === null) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->stream->fail(new StreamFailureException("HTTP request error", 0, $error)); |
|
|
|
|
|
|
116
|
|
|
$this->inflateCtx = $this->stream = null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function processResponseComplete() |
|
120
|
|
|
{ |
|
121
|
|
|
if ($this->stream === null) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->stream->end(); |
|
126
|
|
|
$this->inflateCtx = $this->stream = null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function awaitStreamOpen(): Promise |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->openDeferred->promise(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function onProgress(array $data) |
|
135
|
|
|
{ |
|
136
|
|
|
if (!isset(self::$progressHandlers[$data[0]])) { |
|
137
|
|
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
\call_user_func([$this, self::$progressHandlers[$data[0]]], $data[1]); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: