@@ -87,6 +87,12 @@ final class StreamingServer extends EventEmitter
8787 /** @var Clock */
8888 private $ clock ;
8989
90+ /** @var LoopInterface */
91+ private $ loop ;
92+
93+ /** @var int */
94+ private $ idleConnectionTimeout ;
95+
9096 /**
9197 * Creates an HTTP server that invokes the given callback for each incoming HTTP request
9298 *
@@ -95,19 +101,21 @@ final class StreamingServer extends EventEmitter
95101 * connections in order to then parse incoming data as HTTP.
96102 * See also [listen()](#listen) for more details.
97103 *
98- * @param LoopInterface $loop
99104 * @param callable $requestHandler
105+ * @param int $idleConnectionTimeout
100106 * @see self::listen()
101107 */
102- public function __construct (LoopInterface $ loop , $ requestHandler )
108+ public function __construct (LoopInterface $ loop , $ requestHandler, $ idleConnectionTimeout )
103109 {
104110 if (!\is_callable ($ requestHandler )) {
105111 throw new \InvalidArgumentException ('Invalid request handler given ' );
106112 }
107113
114+ $ this ->loop = $ loop ;
108115 $ this ->callback = $ requestHandler ;
109116 $ this ->clock = new Clock ($ loop );
110117 $ this ->parser = new RequestHeaderParser ($ this ->clock );
118+ $ this ->idleConnectionTimeout = $ idleConnectionTimeout ;
111119
112120 $ that = $ this ;
113121 $ this ->parser ->on ('headers ' , function (ServerRequestInterface $ request , ConnectionInterface $ conn ) use ($ that ) {
@@ -134,7 +142,32 @@ public function __construct(LoopInterface $loop, $requestHandler)
134142 */
135143 public function listen (ServerInterface $ socket )
136144 {
137- $ socket ->on ('connection ' , array ($ this ->parser , 'handle ' ));
145+ $ socket ->on ('connection ' , array ($ this , 'handleConnection ' ));
146+ }
147+
148+ /** @internal */
149+ public function handleConnection (ConnectionInterface $ connection )
150+ {
151+ $ idleConnectionTimeout = $ this ->idleConnectionTimeout ;
152+ $ loop = $ this ->loop ;
153+ $ idleConnectionTimeoutHandler = function () use ($ connection ) {
154+ $ connection ->close ();
155+ };
156+ $ timer = $ this ->loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
157+ $ closeTimerHandler = function () use ($ connection , &$ closeTimerHandler , &$ dataTimerHandler , $ loop , &$ timer ) {
158+ $ connection ->removeListener ('close ' , $ closeTimerHandler );
159+ $ connection ->removeListener ('data ' , $ dataTimerHandler );
160+
161+ $ loop ->cancelTimer ($ timer );
162+ };
163+ $ dataTimerHandler = function () use ($ loop , $ idleConnectionTimeout , $ idleConnectionTimeoutHandler , &$ timer ) {
164+ $ loop ->cancelTimer ($ timer );
165+ $ timer = $ loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
166+ };
167+ $ connection ->on ('close ' , $ closeTimerHandler );
168+ $ connection ->on ('data ' , $ dataTimerHandler );
169+
170+ $ this ->parseRequest ($ connection );
138171 }
139172
140173 /** @internal */
@@ -359,7 +392,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
359392
360393 // either wait for next request over persistent connection or end connection
361394 if ($ persist ) {
362- $ this ->parser -> handle ($ connection );
395+ $ this ->parseRequest ($ connection );
363396 } else {
364397 $ connection ->end ();
365398 }
@@ -380,13 +413,40 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
380413 // write streaming body and then wait for next request over persistent connection
381414 if ($ persist ) {
382415 $ body ->pipe ($ connection , array ('end ' => false ));
383- $ parser = $ this -> parser ;
384- $ body ->on ('end ' , function () use ($ connection , $ parser , $ body ) {
416+ $ that = $ this ;
417+ $ body ->on ('end ' , function () use ($ connection , $ body , & $ that ) {
385418 $ connection ->removeListener ('close ' , array ($ body , 'close ' ));
386- $ parser -> handle ($ connection );
419+ $ that -> parseRequest ($ connection );
387420 });
388421 } else {
389422 $ body ->pipe ($ connection );
390423 }
391424 }
425+
426+ /**
427+ * @internal
428+ */
429+ public function parseRequest (ConnectionInterface $ connection )
430+ {
431+ $ idleConnectionTimeout = $ this ->idleConnectionTimeout ;
432+ $ loop = $ this ->loop ;
433+ $ parser = $ this ->parser ;
434+ $ idleConnectionTimeoutHandler = function () use ($ connection ) {
435+ $ connection ->close ();
436+ };
437+ $ timer = $ this ->loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
438+ $ removeTimerHandler = function ($ _ , $ conn ) use ($ loop , $ timer , $ parser , $ connection , &$ removeTimerHandler ) {
439+ if (\spl_object_hash ($ conn ) !== \spl_object_hash ($ connection )) {
440+ return ;
441+ }
442+
443+ $ loop ->cancelTimer ($ timer );
444+ $ parser ->removeListener ('headers ' , $ removeTimerHandler );
445+ $ parser ->removeListener ('error ' , $ removeTimerHandler );
446+ };
447+ $ this ->parser ->on ('headers ' , $ removeTimerHandler );
448+ $ this ->parser ->on ('error ' , $ removeTimerHandler );
449+
450+ $ this ->parser ->handle ($ connection );
451+ }
392452}
0 commit comments