55use Evenement \EventEmitter ;
66use Psr \Http \Message \ResponseInterface ;
77use Psr \Http \Message \ServerRequestInterface ;
8- use React \EventLoop \LoopInterface ;
98use React \Http \Message \Response ;
109use React \Http \Message \ServerRequest ;
11- use React \Http \Middleware \InactiveConnectionTimeoutMiddleware ;
1210use React \Promise ;
1311use React \Promise \CancellablePromiseInterface ;
1412use React \Promise \PromiseInterface ;
3028 * object in return:
3129 *
3230 * ```php
33- * $server = new StreamingServer($loop, function (ServerRequestInterface $request) {
31+ * $server = new StreamingServer(function (ServerRequestInterface $request) {
3432 * return new Response(
3533 * Response::STATUS_OK,
3634 * array(
5553 * in order to start a plaintext HTTP server like this:
5654 *
5755 * ```php
58- * $server = new StreamingServer($loop, $ handler);
56+ * $server = new StreamingServer($handler);
5957 *
6058 * $socket = new React\Socket\SocketServer('0.0.0.0:8080', array(), $loop);
6159 * $server->listen($socket);
@@ -85,8 +83,6 @@ final class StreamingServer extends EventEmitter
8583{
8684 private $ callback ;
8785 private $ parser ;
88- private $ loop ;
89- private $ idleConnectionTimeout ;
9086
9187 /**
9288 * Creates an HTTP server that invokes the given callback for each incoming HTTP request
@@ -96,22 +92,18 @@ final class StreamingServer extends EventEmitter
9692 * connections in order to then parse incoming data as HTTP.
9793 * See also [listen()](#listen) for more details.
9894 *
99- * @param LoopInterface $loop
10095 * @param callable $requestHandler
10196 * @param float $idleConnectTimeout
10297 * @see self::listen()
10398 */
104- public function __construct (LoopInterface $ loop , $ requestHandler , $ idleConnectTimeout = InactiveConnectionTimeoutMiddleware:: DEFAULT_TIMEOUT )
99+ public function __construct ($ requestHandler , RequestHeaderParser $ parser )
105100 {
106101 if (!\is_callable ($ requestHandler )) {
107102 throw new \InvalidArgumentException ('Invalid request handler given ' );
108103 }
109104
110- $ this ->loop = $ loop ;
111- $ this ->idleConnectionTimeout = $ idleConnectTimeout ;
112-
113105 $ this ->callback = $ requestHandler ;
114- $ this ->parser = new RequestHeaderParser () ;
106+ $ this ->parser = $ parser ;
115107
116108 $ that = $ this ;
117109 $ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -138,27 +130,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
138130 */
139131 public function listen (ServerInterface $ socket )
140132 {
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 ' ));
162134 }
163135
164136 /** @internal */
@@ -376,7 +348,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
376348
377349 // either wait for next request over persistent connection or end connection
378350 if ($ persist ) {
379- $ this ->handle ($ connection );
351+ $ this ->parser -> handle ($ connection );
380352 } else {
381353 $ connection ->end ();
382354 }
@@ -397,10 +369,10 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
397369 // write streaming body and then wait for next request over persistent connection
398370 if ($ persist ) {
399371 $ body ->pipe ($ connection , array ('end ' => false ));
400- $ that = $ this ;
401- $ body ->on ('end ' , function () use ($ connection , $ that , $ body ) {
372+ $ parser = $ this -> parser ;
373+ $ body ->on ('end ' , function () use ($ connection , $ parser , $ body ) {
402374 $ connection ->removeListener ('close ' , array ($ body , 'close ' ));
403- $ that ->handle ($ connection );
375+ $ parser ->handle ($ connection );
404376 });
405377 } else {
406378 $ body ->pipe ($ connection );
0 commit comments