@@ -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,35 @@ 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 , &$ closeEventHandler , &$ dataEventHandler ) {
154+ $ connection ->removeListener ('close ' , $ closeEventHandler );
155+ $ connection ->removeListener ('data ' , $ dataEventHandler );
156+
157+ $ connection ->close ();
158+ };
159+ $ timer = $ this ->loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
160+ $ closeEventHandler = function () use ($ connection , &$ closeEventHandler , &$ dataEventHandler , $ loop , &$ timer ) {
161+ $ connection ->removeListener ('close ' , $ closeEventHandler );
162+ $ connection ->removeListener ('data ' , $ dataEventHandler );
163+
164+ $ loop ->cancelTimer ($ timer );
165+ };
166+ $ dataEventHandler = function () use ($ loop , $ idleConnectionTimeout , $ idleConnectionTimeoutHandler , &$ timer ) {
167+ $ loop ->cancelTimer ($ timer );
168+ $ timer = $ loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
169+ };
170+ $ connection ->on ('close ' , $ closeEventHandler );
171+ $ connection ->on ('data ' , $ dataEventHandler );
172+
173+ $ this ->parseRequest ($ connection );
138174 }
139175
140176 /** @internal */
@@ -359,7 +395,7 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
359395
360396 // either wait for next request over persistent connection or end connection
361397 if ($ persist ) {
362- $ this ->parser -> handle ($ connection );
398+ $ this ->parseRequest ($ connection );
363399 } else {
364400 $ connection ->end ();
365401 }
@@ -380,13 +416,67 @@ public function handleResponse(ConnectionInterface $connection, ServerRequestInt
380416 // write streaming body and then wait for next request over persistent connection
381417 if ($ persist ) {
382418 $ body ->pipe ($ connection , array ('end ' => false ));
383- $ parser = $ this -> parser ;
384- $ body ->on ('end ' , function () use ($ connection , $ parser , $ body ) {
419+ $ that = $ this ;
420+ $ body ->on ('end ' , function () use ($ connection , $ body , & $ that ) {
385421 $ connection ->removeListener ('close ' , array ($ body , 'close ' ));
386- $ parser -> handle ($ connection );
422+ $ that -> parseRequest ($ connection );
387423 });
388424 } else {
389425 $ body ->pipe ($ connection );
390426 }
391427 }
428+
429+ /**
430+ * @internal
431+ */
432+ public function parseRequest (ConnectionInterface $ connection )
433+ {
434+ $ idleConnectionTimeout = $ this ->idleConnectionTimeout ;
435+ $ loop = $ this ->loop ;
436+ $ parser = $ this ->parser ;
437+ $ idleConnectionTimeoutHandler = function () use ($ connection , $ parser , &$ removeTimerHandler ) {
438+ $ parser ->removeListener ('headers ' , $ removeTimerHandler );
439+ $ parser ->removeListener ('error ' , $ removeTimerHandler );
440+
441+ $ parser ->emit ('error ' , array (
442+ new \RuntimeException ('Request timed out ' , Response::STATUS_REQUEST_TIMEOUT ),
443+ $ connection
444+ ));
445+ };
446+ $ timer = $ this ->loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
447+ $ removeTimerHandler = function ($ requestOrError , $ conn ) use ($ loop , &$ timer , $ parser , $ connection , &$ removeTimerHandler , $ idleConnectionTimeout , $ idleConnectionTimeoutHandler ) {
448+ if ($ conn !== $ connection ) {
449+ return ;
450+ }
451+
452+ $ loop ->cancelTimer ($ timer );
453+ $ parser ->removeListener ('headers ' , $ removeTimerHandler );
454+ $ parser ->removeListener ('error ' , $ removeTimerHandler );
455+
456+ if (!($ requestOrError instanceof ServerRequestInterface)) {
457+ return ;
458+ }
459+
460+ $ requestBody = $ requestOrError ->getBody ();
461+ if (!($ requestBody instanceof HttpBodyStream)) {
462+ return ;
463+ }
464+
465+ $ timer = $ loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
466+ $ requestBody ->on ('data ' , function () use (&$ timer , $ loop , $ idleConnectionTimeout , $ idleConnectionTimeoutHandler ) {
467+ $ loop ->cancelTimer ($ timer );
468+ $ timer = $ loop ->addTimer ($ idleConnectionTimeout , $ idleConnectionTimeoutHandler );
469+ });
470+ $ requestBody ->on ('end ' , function () use (&$ timer , $ loop ) {
471+ $ loop ->cancelTimer ($ timer );
472+ });
473+ $ requestBody ->on ('close ' , function () use (&$ timer , $ loop ) {
474+ $ loop ->cancelTimer ($ timer );
475+ });
476+ };
477+ $ this ->parser ->on ('headers ' , $ removeTimerHandler );
478+ $ this ->parser ->on ('error ' , $ removeTimerHandler );
479+
480+ $ this ->parser ->handle ($ connection );
481+ }
392482}
0 commit comments