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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
<?php
namespace React\HttpClient;
use Evenement\EventEmitter;
use React\Stream\ReadableStreamInterface;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;
/**
* @event data ($bodyChunk)
* @event error
* @event end
*/
class Response extends EventEmitter implements ReadableStreamInterface
{
private $stream;
private $protocol;
private $version;
private $code;
private $reasonPhrase;
private $headers;
private $readable = true;
public function __construct(ReadableStreamInterface $stream, $protocol, $version, $code, $reasonPhrase, $headers)
{
$this->stream = $stream;
$this->protocol = $protocol;
$this->version = $version;
$this->code = $code;
$this->reasonPhrase = $reasonPhrase;
$this->headers = $headers;
if (strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
$this->stream = new ChunkedStreamDecoder($stream);
$this->removeHeader('Transfer-Encoding');
}
$this->stream->on('data', array($this, 'handleData'));
$this->stream->on('error', array($this, 'handleError'));
$this->stream->on('end', array($this, 'handleEnd'));
$this->stream->on('close', array($this, 'handleClose'));
}
public function getProtocol()
{
return $this->protocol;
}
public function getVersion()
{
return $this->version;
}
public function getCode()
{
return $this->code;
}
public function getReasonPhrase()
{
return $this->reasonPhrase;
}
public function getHeaders()
{
return $this->headers;
}
private function removeHeader($name)
{
foreach ($this->headers as $key => $value) {
if (strcasecmp($name, $key) === 0) {
unset($this->headers[$key]);
break;
}
}
}
private function getHeader($name)
{
$name = strtolower($name);
$normalized = array_change_key_case($this->headers, CASE_LOWER);
return isset($normalized[$name]) ? (array)$normalized[$name] : array();
}
private function getHeaderLine($name)
{
return implode(', ' , $this->getHeader($name));
}
/** @internal */
public function handleData($data)
{
if ($this->readable) {
$this->emit('data', array($data));
}
}
/** @internal */
public function handleEnd()
{
if (!$this->readable) {
return;
}
$this->emit('end');
$this->close();
}
/** @internal */
public function handleError(\Exception $error)
{
if (!$this->readable) {
return;
}
$this->emit('error', array(new \RuntimeException(
"An error occurred in the underlying stream",
0,
$error
)));
$this->close();
}
/** @internal */
public function handleClose()
{
$this->close();
}
public function close()
{
if (!$this->readable) {
return;
}
$this->readable = false;
$this->stream->close();
$this->emit('close');
$this->removeAllListeners();
}
public function isReadable()
{
return $this->readable;
}
public function pause()
{
if (!$this->readable) {
return;
}
$this->stream->pause();
}
public function resume()
{
if (!$this->readable) {
return;
}
$this->stream->resume();
}
public function pipe(WritableStreamInterface $dest, array $options = array())
{
Util::pipe($this, $dest, $options);
return $dest;
}
}
|