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 \PromiseInterface ;
1412use React \Socket \ConnectionInterface ;
2927 * object in return:
3028 *
3129 * ```php
32- * $server = new StreamingServer($loop, function (ServerRequestInterface $request) {
30+ * $server = new StreamingServer(function (ServerRequestInterface $request) {
3331 * return new Response(
3432 * Response::STATUS_OK,
3533 * array(
5452 * in order to start a plaintext HTTP server like this:
5553 *
5654 * ```php
57- * $server = new StreamingServer($loop, $ handler);
55+ * $server = new StreamingServer($handler);
5856 *
5957 * $socket = new React\Socket\SocketServer('0.0.0.0:8080', array(), $loop);
6058 * $server->listen($socket);
@@ -98,23 +96,19 @@ final class StreamingServer extends EventEmitter
9896 * connections in order to then parse incoming data as HTTP.
9997 * See also [listen()](#listen) for more details.
10098 *
101- * @param LoopInterface $loop
10299 * @param callable $requestHandler
103100 * @param float $idleConnectTimeout
104101 * @see self::listen()
105102 */
106- public function __construct (LoopInterface $ loop , $ requestHandler , $ idleConnectTimeout = InactiveConnectionTimeoutMiddleware:: DEFAULT_TIMEOUT )
103+ public function __construct ($ requestHandler , RequestHeaderParser $ parser , Clock $ clock )
107104 {
108105 if (!\is_callable ($ requestHandler )) {
109106 throw new \InvalidArgumentException ('Invalid request handler given ' );
110107 }
111108
112- $ this ->loop = $ loop ;
113- $ this ->idleConnectionTimeout = $ idleConnectTimeout ;
114-
115109 $ this ->callback = $ requestHandler ;
116- $ this ->clock = new Clock ( $ loop ) ;
117- $ this ->parser = new RequestHeaderParser ( $ this -> clock ) ;
110+ $ this ->clock = $ clock ;
111+ $ this ->parser = $ parser ;
118112
119113 $ that = $ this ;
120114 $ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -141,27 +135,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
141135 */
142136 public function listen (ServerInterface $ socket )
143137 {
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 ' ));
165139 }
166140
167141 /** @internal */
@@ -379,7 +353,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
379353
380354 // either wait for next request over persistent connection or end connection
381355 if ($ persist ) {
382- $ this ->handle ($ connection );
356+ $ this ->parser -> handle ($ connection );
383357 } else {
384358 $ connection ->end ();
385359 }
@@ -400,10 +374,10 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
400374 // write streaming body and then wait for next request over persistent connection
401375 if ($ persist ) {
402376 $ 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 ) {
405379 $ connection ->removeListener ('close ' , array ($ body , 'close ' ));
406- $ that ->handle ($ connection );
380+ $ parser ->handle ($ connection );
407381 });
408382 } else {
409383 $ body ->pipe ($ connection );
0 commit comments