1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\Artax\Internal; |
4
|
|
|
|
5
|
|
|
use Amp\Artax; |
6
|
|
|
use Amp\ByteStream\InputStream; |
7
|
|
|
use Amp\ByteStream\IteratorStream; |
8
|
|
|
use Amp\CancellationTokenSource; |
9
|
|
|
use Amp\CancelledException; |
10
|
|
|
use Amp\Emitter; |
11
|
|
|
use Amp\Promise; |
12
|
|
|
use Psr\Http\Message\StreamInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PSR-7 stream implementation that converts an `Amp\ByteStream\InputStream` into a PSR-7 compatible stream. |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
class ResponseStream implements StreamInterface |
20
|
|
|
{ |
21
|
|
|
private $buffer = ''; |
22
|
|
|
private $position = 0; |
23
|
|
|
private $eof = false; |
24
|
|
|
|
25
|
|
|
private $body; |
26
|
|
|
private $cancellationTokenSource; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param InputStream $body HTTP response stream to wrap. |
30
|
|
|
* @param CancellationTokenSource $cancellationTokenSource Cancellation source bound to the request to abort it. |
31
|
|
|
*/ |
32
|
60 |
|
public function __construct(InputStream $body, CancellationTokenSource $cancellationTokenSource) |
33
|
|
|
{ |
34
|
60 |
|
$this->body = $body; |
35
|
60 |
|
$this->cancellationTokenSource = $cancellationTokenSource; |
36
|
60 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function __toString() |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
2 |
|
return $this->getContents(); |
42
|
1 |
|
} catch (\Throwable $e) { |
43
|
1 |
|
return ''; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
60 |
|
public function __destruct() |
48
|
|
|
{ |
49
|
60 |
|
$this->close(); |
50
|
60 |
|
} |
51
|
|
|
|
52
|
60 |
|
public function close() |
53
|
|
|
{ |
54
|
60 |
|
$this->cancellationTokenSource->cancel(); |
55
|
|
|
|
56
|
60 |
|
$emitter = new Emitter(); |
57
|
60 |
|
$emitter->fail(new Artax\HttpException('The stream has been closed')); |
58
|
60 |
|
$this->body = new IteratorStream($emitter->iterate()); |
59
|
60 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function detach() |
62
|
|
|
{ |
63
|
1 |
|
$this->close(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
public function getSize() |
67
|
|
|
{ |
68
|
1 |
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function tell() |
72
|
|
|
{ |
73
|
1 |
|
return $this->position; |
74
|
|
|
} |
75
|
|
|
|
76
|
52 |
|
public function eof() |
77
|
|
|
{ |
78
|
52 |
|
return $this->eof; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function isSeekable() |
82
|
|
|
{ |
83
|
1 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function seek($offset, $whence = SEEK_SET) |
87
|
|
|
{ |
88
|
2 |
|
throw new \RuntimeException('Stream is not seekable'); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function rewind() |
92
|
|
|
{ |
93
|
1 |
|
$this->seek(0); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function isWritable() |
97
|
|
|
{ |
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function write($string) |
102
|
|
|
{ |
103
|
1 |
|
throw new \RuntimeException('Stream is not writable'); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function isReadable() |
107
|
|
|
{ |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
55 |
|
public function read($length) |
112
|
|
|
{ |
113
|
55 |
|
if ($this->eof) { |
114
|
1 |
|
return ''; |
115
|
|
|
} |
116
|
|
|
|
117
|
55 |
|
if ($this->buffer === '') { |
118
|
|
|
try { |
119
|
55 |
|
$this->buffer = Promise\wait($this->body->read()); |
120
|
4 |
|
} catch (Artax\HttpException $e) { |
121
|
3 |
|
throw new \RuntimeException('Reading from the stream failed', 0, $e); |
122
|
1 |
|
} catch (CancelledException $e) { |
123
|
1 |
|
throw new \RuntimeException('Reading from the stream failed', 0, $e); |
124
|
|
|
} |
125
|
|
|
|
126
|
51 |
|
if ($this->buffer === null) { |
127
|
51 |
|
$this->eof = true; |
128
|
|
|
|
129
|
51 |
|
return ''; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
47 |
|
$read = \substr($this->buffer, 0, $length); |
134
|
47 |
|
$this->buffer = (string) \substr($this->buffer, $length); |
135
|
47 |
|
$this->position += \strlen($read); |
136
|
|
|
|
137
|
47 |
|
return $read; |
138
|
|
|
} |
139
|
|
|
|
140
|
52 |
|
public function getContents() |
141
|
|
|
{ |
142
|
52 |
|
$buffer = ''; |
143
|
|
|
|
144
|
52 |
|
while (!$this->eof()) { |
145
|
52 |
|
$buffer .= $this->read(8192 * 8); |
146
|
|
|
} |
147
|
|
|
|
148
|
51 |
|
return $buffer; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function getMetadata($key = null) |
152
|
|
|
{ |
153
|
1 |
|
return $key === null ? [] : null; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|