4
4
5
5
use Evenement \EventEmitter ;
6
6
use Psr \Http \Message \ServerRequestInterface ;
7
+ use React \EventLoop \LoopInterface ;
7
8
use React \Http \Message \Response ;
8
9
use React \Http \Message \ServerRequest ;
9
10
use React \Socket \ConnectionInterface ;
@@ -24,12 +25,45 @@ class RequestHeaderParser extends EventEmitter
24
25
{
25
26
private $ maxSize = 8192 ;
26
27
28
+ /**
29
+ * @var LoopInterface
30
+ */
31
+ private $ loop ;
32
+
33
+ /**
34
+ * @var float
35
+ */
36
+ private $ idleConnectionTimeout ;
37
+
38
+ /**
39
+ * @param LoopInterface $loop
40
+ * @param float $idleConnectionTimeout
41
+ */
42
+ public function __construct (LoopInterface $ loop , $ idleConnectionTimeout )
43
+ {
44
+ $ this ->loop = $ loop ;
45
+ $ this ->idleConnectionTimeout = $ idleConnectionTimeout ;
46
+ }
47
+
27
48
public function handle (ConnectionInterface $ conn )
28
49
{
50
+ $ loop = $ this ->loop ;
51
+ $ idleConnectionTimeout = $ this ->idleConnectionTimeout ;
52
+ $ createTimer = function () use ($ conn , $ loop , $ idleConnectionTimeout ) {
53
+ return $ loop ->addTimer ($ idleConnectionTimeout , function () use ($ conn ) {
54
+ $ conn ->close ();
55
+ });
56
+ };
57
+ $ timer = $ createTimer ();
58
+ $ conn ->on ('close ' , function () use ($ loop , $ timer ) {
59
+ $ loop ->cancelTimer ($ timer );
60
+ });
29
61
$ buffer = '' ;
30
62
$ maxSize = $ this ->maxSize ;
31
63
$ that = $ this ;
32
- $ conn ->on ('data ' , $ fn = function ($ data ) use (&$ buffer , &$ fn , $ conn , $ maxSize , $ that ) {
64
+ $ conn ->on ('data ' , $ fn = function ($ data ) use (&$ buffer , &$ fn , $ conn , $ maxSize , $ that , $ loop , &$ timer , $ createTimer ) {
65
+ $ loop ->cancelTimer ($ timer );
66
+ $ timer = $ createTimer ();
33
67
// append chunk of data to buffer and look for end of request headers
34
68
$ buffer .= $ data ;
35
69
$ endOfHeader = \strpos ($ buffer , "\r\n\r\n" );
@@ -43,6 +77,7 @@ public function handle(ConnectionInterface $conn)
43
77
new \OverflowException ("Maximum header size of {$ maxSize } exceeded. " , Response::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE ),
44
78
$ conn
45
79
));
80
+ $ loop ->cancelTimer ($ timer );
46
81
return ;
47
82
}
48
83
@@ -67,6 +102,7 @@ public function handle(ConnectionInterface $conn)
67
102
$ exception ,
68
103
$ conn
69
104
));
105
+ $ loop ->cancelTimer ($ timer );
70
106
return ;
71
107
}
72
108
@@ -105,6 +141,7 @@ public function handle(ConnectionInterface $conn)
105
141
if ($ contentLength === 0 ) {
106
142
$ stream ->emit ('end ' );
107
143
$ stream ->close ();
144
+ $ loop ->cancelTimer ($ timer );
108
145
}
109
146
});
110
147
}
0 commit comments