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 \CancellablePromiseInterface ;
14
12
use React \Promise \PromiseInterface ;
30
28
* object in return:
31
29
*
32
30
* ```php
33
- * $server = new StreamingServer($loop, function (ServerRequestInterface $request) {
31
+ * $server = new StreamingServer(function (ServerRequestInterface $request) {
34
32
* return new Response(
35
33
* Response::STATUS_OK,
36
34
* array(
55
53
* in order to start a plaintext HTTP server like this:
56
54
*
57
55
* ```php
58
- * $server = new StreamingServer($loop, $ handler);
56
+ * $server = new StreamingServer($handler);
59
57
*
60
58
* $socket = new React\Socket\SocketServer('0.0.0.0:8080', array(), $loop);
61
59
* $server->listen($socket);
@@ -85,8 +83,6 @@ final class StreamingServer extends EventEmitter
85
83
{
86
84
private $ callback ;
87
85
private $ parser ;
88
- private $ loop ;
89
- private $ idleConnectionTimeout ;
90
86
91
87
/**
92
88
* Creates an HTTP server that invokes the given callback for each incoming HTTP request
@@ -96,22 +92,18 @@ final class StreamingServer extends EventEmitter
96
92
* connections in order to then parse incoming data as HTTP.
97
93
* See also [listen()](#listen) for more details.
98
94
*
99
- * @param LoopInterface $loop
100
95
* @param callable $requestHandler
101
96
* @param float $idleConnectTimeout
102
97
* @see self::listen()
103
98
*/
104
- public function __construct (LoopInterface $ loop , $ requestHandler , $ idleConnectTimeout = InactiveConnectionTimeoutMiddleware:: DEFAULT_TIMEOUT )
99
+ public function __construct ($ requestHandler , RequestHeaderParser $ parser )
105
100
{
106
101
if (!\is_callable ($ requestHandler )) {
107
102
throw new \InvalidArgumentException ('Invalid request handler given ' );
108
103
}
109
104
110
- $ this ->loop = $ loop ;
111
- $ this ->idleConnectionTimeout = $ idleConnectTimeout ;
112
-
113
105
$ this ->callback = $ requestHandler ;
114
- $ this ->parser = new RequestHeaderParser () ;
106
+ $ this ->parser = $ parser ;
115
107
116
108
$ that = $ this ;
117
109
$ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -138,27 +130,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
138
130
*/
139
131
public function listen (ServerInterface $ socket )
140
132
{
141
- $ socket ->on ('connection ' , array ($ this , 'handle ' ));
142
- }
143
-
144
- /** @internal */
145
- public function handle (ConnectionInterface $ conn )
146
- {
147
- $ timer = $ this ->loop ->addTimer ($ this ->idleConnectionTimeout , function () use ($ conn ) {
148
- $ conn ->close ();
149
- });
150
- $ loop = $ this ->loop ;
151
- $ conn ->once ('data ' , function () use ($ loop , $ timer ) {
152
- $ loop ->cancelTimer ($ timer );
153
- });
154
- $ conn ->on ('end ' , function () use ($ loop , $ timer ) {
155
- $ loop ->cancelTimer ($ timer );
156
- });
157
- $ conn ->on ('close ' , function () use ($ loop , $ timer ) {
158
- $ loop ->cancelTimer ($ timer );
159
- });
160
-
161
- $ this ->parser ->handle ($ conn );
133
+ $ socket ->on ('connection ' , array ($ this ->parser , 'handle ' ));
162
134
}
163
135
164
136
/** @internal */
@@ -376,7 +348,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
376
348
377
349
// either wait for next request over persistent connection or end connection
378
350
if ($ persist ) {
379
- $ this ->handle ($ connection );
351
+ $ this ->parser -> handle ($ connection );
380
352
} else {
381
353
$ connection ->end ();
382
354
}
@@ -400,7 +372,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
400
372
$ that = $ this ;
401
373
$ body ->on ('end ' , function () use ($ connection , $ that , $ body ) {
402
374
$ connection ->removeListener ('close ' , array ($ body , 'close ' ));
403
- $ that ->handle ($ connection );
375
+ $ that ->parser -> handle ($ connection );
404
376
});
405
377
} else {
406
378
$ body ->pipe ($ connection );
0 commit comments