Skip to content

Commit a577989

Browse files
Maciej Mrozińskiclue
authored andcommitted
changes in Request, Response and tests
1 parent f5eaedb commit a577989

5 files changed

Lines changed: 100 additions & 86 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"guzzlehttp/psr7": "^1.0",
99
"react/socket": "^0.7",
1010
"react/event-loop": "0.4.*",
11-
"react/stream": "0.4.*",
11+
"react/stream": "^0.5|^0.6",
1212
"react/promise": "~2.2",
1313
"evenement/evenement": "~2.0"
1414
},

src/Request.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function ($stream) use ($requestData, &$streamRef, &$stateRef) {
6767
$stream->on('data', array($this, 'handleData'));
6868
$stream->on('end', array($this, 'handleEnd'));
6969
$stream->on('error', array($this, 'handleError'));
70+
$stream->on('close', array($this, 'handleClose'));
7071

7172
$headers = (string) $requestData;
7273

@@ -144,14 +145,15 @@ public function handleData($data)
144145
$this->stream->removeListener('data', array($this, 'handleData'));
145146
$this->stream->removeListener('end', array($this, 'handleEnd'));
146147
$this->stream->removeListener('error', array($this, 'handleError'));
148+
$this->stream->removeListener('close', array($this, 'handleClose'));
147149

148150
if (!isset($response)) {
149151
return;
150152
}
151153

152154
$this->response = $response;
153155

154-
$response->on('end', function () {
156+
$response->on('close', function () {
155157
$this->close();
156158
});
157159
$response->on('error', function (\Exception $error) {
@@ -170,9 +172,7 @@ public function handleData($data)
170172

171173
public function handleEnd()
172174
{
173-
$this->closeError(new \RuntimeException(
174-
"Connection closed before receiving response"
175-
));
175+
$this->handleClose();
176176
}
177177

178178
public function handleError($error)
@@ -184,16 +184,23 @@ public function handleError($error)
184184
));
185185
}
186186

187+
public function handleClose()
188+
{
189+
$this->closeError(new \RuntimeException(
190+
"Connection closed before receiving response"
191+
));
192+
}
193+
187194
public function closeError(\Exception $error)
188195
{
189196
if (self::STATE_END <= $this->state) {
190197
return;
191198
}
192-
$this->emit('error', array($error, $this));
193-
$this->close($error);
199+
$this->emit('error', array($error));
200+
$this->close();
194201
}
195202

196-
public function close(\Exception $error = null)
203+
public function close()
197204
{
198205
if (self::STATE_END <= $this->state) {
199206
return;
@@ -205,7 +212,7 @@ public function close(\Exception $error = null)
205212
$this->stream->close();
206213
}
207214

208-
$this->emit('end', array($error, $this->response, $this));
215+
$this->emit('close', array());
209216
$this->removeAllListeners();
210217
}
211218

src/Response.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
namespace React\HttpClient;
44

5-
use Evenement\EventEmitterTrait;
5+
use Evenement\EventEmitter;
66
use React\Stream\ReadableStreamInterface;
77
use React\Stream\Util;
88
use React\Stream\WritableStreamInterface;
99

1010
/**
11-
* @event data ($bodyChunk, Response $thisResponse)
11+
* @event data ($bodyChunk)
1212
* @event error
1313
* @event end
1414
*/
15-
class Response implements ReadableStreamInterface
15+
class Response extends EventEmitter implements ReadableStreamInterface
1616
{
17-
use EventEmitterTrait;
1817

1918
private $stream;
2019
private $protocol;
@@ -48,6 +47,7 @@ public function __construct(ReadableStreamInterface $stream, $protocol, $version
4847
$this->stream->on('data', array($this, 'handleData'));
4948
$this->stream->on('error', array($this, 'handleError'));
5049
$this->stream->on('end', array($this, 'handleEnd'));
50+
$this->stream->on('close', array($this, 'handleClose'));
5151
}
5252

5353
public function getProtocol()
@@ -77,34 +77,48 @@ public function getHeaders()
7777

7878
public function handleData($data)
7979
{
80-
$this->emit('data', array($data, $this));
80+
if ($this->readable) {
81+
$this->emit('data', array($data));
82+
}
8183
}
8284

8385
public function handleEnd()
8486
{
87+
if (!$this->readable) {
88+
return;
89+
}
90+
$this->emit('end', array());
8591
$this->close();
8692
}
8793

8894
public function handleError(\Exception $error)
8995
{
96+
if (!$this->readable) {
97+
return;
98+
}
9099
$this->emit('error', array(new \RuntimeException(
91100
"An error occurred in the underlying stream",
92101
0,
93102
$error
94-
), $this));
103+
)));
104+
105+
$this->close();
106+
}
95107

96-
$this->close($error);
108+
public function handleClose()
109+
{
110+
$this->close();
97111
}
98112

99-
public function close(\Exception $error = null)
113+
public function close()
100114
{
101115
if (!$this->readable) {
102116
return;
103117
}
104118

105119
$this->readable = false;
106120

107-
$this->emit('end', array($error, $this));
121+
$this->emit('close', array());
108122

109123
$this->removeAllListeners();
110124
$this->stream->close();

0 commit comments

Comments
 (0)