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);
@@ -99,23 +97,19 @@ final class StreamingServer extends EventEmitter
9997 * connections in order to then parse incoming data as HTTP.
10098 * See also [listen()](#listen) for more details.
10199 *
102- * @param LoopInterface $loop
103100 * @param callable $requestHandler
104101 * @param float $idleConnectTimeout
105102 * @see self::listen()
106103 */
107- public function __construct (LoopInterface $ loop , $ requestHandler , $ idleConnectTimeout = InactiveConnectionTimeoutMiddleware:: DEFAULT_TIMEOUT )
104+ public function __construct ($ requestHandler , RequestHeaderParser $ parser , Clock $ clock )
108105 {
109106 if (!\is_callable ($ requestHandler )) {
110107 throw new \InvalidArgumentException ('Invalid request handler given ' );
111108 }
112109
113- $ this ->loop = $ loop ;
114- $ this ->idleConnectionTimeout = $ idleConnectTimeout ;
115-
116110 $ this ->callback = $ requestHandler ;
117- $ this ->clock = new Clock ( $ loop ) ;
118- $ this ->parser = new RequestHeaderParser ( $ this -> clock ) ;
111+ $ this ->clock = $ clock ;
112+ $ this ->parser = $ parser ;
119113
120114 $ that = $ this ;
121115 $ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -142,27 +136,7 @@ public function __construct(LoopInterface $loop, $requestHandler, $idleConnectTi
142136 */
143137 public function listen (ServerInterface $ socket )
144138 {
145- $ socket ->on ('connection ' , array ($ this , 'handle ' ));
146- }
147-
148- /** @internal */
149- public function handle (ConnectionInterface $ conn )
150- {
151- $ timer = $ this ->loop ->addTimer ($ this ->idleConnectionTimeout , function () use ($ conn ) {
152- $ conn ->close ();
153- });
154- $ loop = $ this ->loop ;
155- $ conn ->once ('data ' , function () use ($ loop , $ timer ) {
156- $ loop ->cancelTimer ($ timer );
157- });
158- $ conn ->on ('end ' , function () use ($ loop , $ timer ) {
159- $ loop ->cancelTimer ($ timer );
160- });
161- $ conn ->on ('close ' , function () use ($ loop , $ timer ) {
162- $ loop ->cancelTimer ($ timer );
163- });
164-
165- $ this ->parser ->handle ($ conn );
139+ $ socket ->on ('connection ' , array ($ this ->parser , 'handle ' ));
166140 }
167141
168142 /** @internal */
@@ -380,7 +354,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
380354
381355 // either wait for next request over persistent connection or end connection
382356 if ($ persist ) {
383- $ this ->handle ($ connection );
357+ $ this ->parser -> handle ($ connection );
384358 } else {
385359 $ connection ->end ();
386360 }
@@ -401,10 +375,10 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
401375 // write streaming body and then wait for next request over persistent connection
402376 if ($ persist ) {
403377 $ body ->pipe ($ connection , array ('end ' => false ));
404- $ that = $ this ;
405- $ body ->on ('end ' , function () use ($ connection , $ that , $ body ) {
378+ $ parser = $ this -> parser ;
379+ $ body ->on ('end ' , function () use ($ connection , $ parser , $ body ) {
406380 $ connection ->removeListener ('close ' , array ($ body , 'close ' ));
407- $ that ->handle ($ connection );
381+ $ parser ->handle ($ connection );
408382 });
409383 } else {
410384 $ body ->pipe ($ connection );
0 commit comments