File tree Expand file tree Collapse file tree 3 files changed +41
-7
lines changed
Expand file tree Collapse file tree 3 files changed +41
-7
lines changed Original file line number Diff line number Diff line change @@ -17,21 +17,28 @@ class RequestHeaderParser extends EventEmitter
1717
1818 public function feed ($ data )
1919 {
20- if (strlen ($ this ->buffer ) + strlen ($ data ) > $ this ->maxSize ) {
20+ $ this ->buffer .= $ data ;
21+
22+ $ endOfHeader = strpos ($ this ->buffer , "\r\n\r\n" );
23+
24+ if (false !== $ endOfHeader ) {
25+ $ currentHeaderSize = $ endOfHeader ;
26+ } else {
27+ $ currentHeaderSize = strlen ($ this ->buffer );
28+ }
29+
30+ if ($ currentHeaderSize > $ this ->maxSize ) {
2131 $ this ->emit ('error ' , array (new \OverflowException ("Maximum header size of {$ this ->maxSize } exceeded. " ), $ this ));
2232 $ this ->removeAllListeners ();
2333 return ;
2434 }
2535
26- $ this ->buffer .= $ data ;
27-
28- if (false !== strpos ($ this ->buffer , "\r\n\r\n" )) {
36+ if (false !== $ endOfHeader ) {
2937 try {
3038 $ this ->parseAndEmitRequest ();
3139 } catch (Exception $ exception ) {
3240 $ this ->emit ('error ' , [$ exception ]);
3341 }
34-
3542 $ this ->removeAllListeners ();
3643 }
3744 }
Original file line number Diff line number Diff line change @@ -121,6 +121,33 @@ public function testHeaderOverflowShouldEmitError()
121121 $ this ->assertSame (0 , count ($ parser ->listeners ('error ' )));
122122 }
123123
124+ public function testHeaderOverflowShouldNotEmitErrorWhenDataExceedsMaxHeaderSize ()
125+ {
126+ $ request = null ;
127+ $ bodyBuffer = null ;
128+
129+ $ parser = new RequestHeaderParser ();
130+ $ parser ->on ('headers ' , function ($ parsedRequest , $ parsedBodyBuffer ) use (&$ request , &$ bodyBuffer ) {
131+ $ request = $ parsedRequest ;
132+ $ bodyBuffer = $ parsedBodyBuffer ;
133+ });
134+
135+ $ data = $ this ->createAdvancedPostRequest ();
136+ $ body = str_repeat ('A ' , 4097 - strlen ($ data ));
137+ $ data .= $ body ;
138+
139+ $ parser ->feed ($ data );
140+
141+ $ headers = array (
142+ 'Host ' => 'example.com:80 ' ,
143+ 'User-Agent ' => 'react/alpha ' ,
144+ 'Connection ' => 'close ' ,
145+ );
146+ $ this ->assertSame ($ headers , $ request ->getHeaders ());
147+
148+ $ this ->assertSame ($ body , $ bodyBuffer );
149+ }
150+
124151 public function testGuzzleRequestParseException ()
125152 {
126153 $ error = null ;
Original file line number Diff line number Diff line change @@ -81,8 +81,8 @@ public function testParserErrorEmitted()
8181 $ conn = new ConnectionStub ();
8282 $ io ->emit ('connection ' , [$ conn ]);
8383
84- $ data = $ this -> createGetRequest () ;
85- $ data = str_pad ( $ data , 4096 * 4 ) ;
84+ $ data = " GET / HTTP/1.1 \r\n Host: example.com \r\n Connection: close \r\n X-DATA: " ;
85+ $ data .= str_repeat ( ' A ' , 4097 - strlen ( $ data )) . "\r\n\r\n" ;
8686 $ conn ->emit ('data ' , [$ data ]);
8787
8888 $ this ->assertInstanceOf ('OverflowException ' , $ error );
You can’t perform that action at this time.
0 commit comments