5
5
use Evenement \EventEmitter ;
6
6
use Psr \Http \Message \ResponseInterface ;
7
7
use Psr \Http \Message \ServerRequestInterface ;
8
- use React \EventLoop \LoopInterface ;
9
8
use React \Http \Message \Response ;
10
9
use React \Http \Message \ServerRequest ;
11
- use React \Http \Middleware \InactiveConnectionTimeoutMiddleware ;
12
10
use React \Promise ;
13
11
use React \Promise \PromiseInterface ;
14
12
use React \Socket \ConnectionInterface ;
29
27
* object in return:
30
28
*
31
29
* ```php
32
- * $server = new StreamingServer($loop, function (ServerRequestInterface $request) {
30
+ * $server = new StreamingServer(function (ServerRequestInterface $request) {
33
31
* return new Response(
34
32
* Response::STATUS_OK,
35
33
* array(
54
52
* in order to start a plaintext HTTP server like this:
55
53
*
56
54
* ```php
57
- * $server = new StreamingServer($loop, $ handler);
55
+ * $server = new StreamingServer($handler);
58
56
*
59
57
* $socket = new React\Socket\SocketServer('0.0.0.0:8080', array(), $loop);
60
58
* $server->listen($socket);
@@ -98,23 +96,19 @@ final class StreamingServer extends EventEmitter
98
96
* connections in order to then parse incoming data as HTTP.
99
97
* See also [listen()](#listen) for more details.
100
98
*
101
- * @param LoopInterface $loop
102
99
* @param callable $requestHandler
103
100
* @param float $idleConnectTimeout
104
101
* @see self::listen()
105
102
*/
106
- public function __construct (LoopInterface $ loop , $ requestHandler , $ idleConnectTimeout = InactiveConnectionTimeoutMiddleware:: DEFAULT_TIMEOUT )
103
+ public function __construct ($ requestHandler , RequestHeaderParser $ parser , Clock $ clock )
107
104
{
108
105
if (!\is_callable ($ requestHandler )) {
109
106
throw new \InvalidArgumentException ('Invalid request handler given ' );
110
107
}
111
108
112
- $ this ->loop = $ loop ;
113
- $ this ->idleConnectionTimeout = $ idleConnectTimeout ;
114
-
115
109
$ this ->callback = $ requestHandler ;
116
- $ this ->clock = new Clock ( $ loop ) ;
117
- $ this ->parser = new RequestHeaderParser ( $ this -> clock ) ;
110
+ $ this ->clock = $ clock ;
111
+ $ this ->parser = $ parser ;
118
112
119
113
$ that = $ this ;
120
114
$ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -141,27 +135,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
141
135
*/
142
136
public function listen (ServerInterface $ socket )
143
137
{
144
- $ socket ->on ('connection ' , array ($ this , 'handle ' ));
145
- }
146
-
147
- /** @internal */
148
- public function handle (ConnectionInterface $ conn )
149
- {
150
- $ timer = $ this ->loop ->addTimer ($ this ->idleConnectionTimeout , function () use ($ conn ) {
151
- $ conn ->close ();
152
- });
153
- $ loop = $ this ->loop ;
154
- $ conn ->once ('data ' , function () use ($ loop , $ timer ) {
155
- $ loop ->cancelTimer ($ timer );
156
- });
157
- $ conn ->on ('end ' , function () use ($ loop , $ timer ) {
158
- $ loop ->cancelTimer ($ timer );
159
- });
160
- $ conn ->on ('close ' , function () use ($ loop , $ timer ) {
161
- $ loop ->cancelTimer ($ timer );
162
- });
163
-
164
- $ this ->parser ->handle ($ conn );
138
+ $ socket ->on ('connection ' , array ($ this ->parser , 'handle ' ));
165
139
}
166
140
167
141
/** @internal */
@@ -379,7 +353,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
379
353
380
354
// either wait for next request over persistent connection or end connection
381
355
if ($ persist ) {
382
- $ this ->handle ($ connection );
356
+ $ this ->parser -> handle ($ connection );
383
357
} else {
384
358
$ connection ->end ();
385
359
}
@@ -400,10 +374,10 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
400
374
// write streaming body and then wait for next request over persistent connection
401
375
if ($ persist ) {
402
376
$ body ->pipe ($ connection , array ('end ' => false ));
403
- $ that = $ this ;
404
- $ body ->on ('end ' , function () use ($ connection , $ that , $ body ) {
377
+ $ parser = $ this -> parser ;
378
+ $ body ->on ('end ' , function () use ($ connection , $ parser , $ body ) {
405
379
$ connection ->removeListener ('close ' , array ($ body , 'close ' ));
406
- $ that ->handle ($ connection );
380
+ $ parser ->handle ($ connection );
407
381
});
408
382
} else {
409
383
$ body ->pipe ($ connection );
0 commit comments