File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
Filter options
src/Symfony/Component/Form Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
Original file line number Diff line number Diff line change 12
12
namespace Symfony \Component \Form \Extension \HttpFoundation ;
13
13
14
14
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
15
+ use Symfony \Component \Form \Extension \Validator \Util \ServerParams ;
16
+ use Symfony \Component \Form \FormError ;
15
17
use Symfony \Component \Form \FormInterface ;
16
18
use Symfony \Component \Form \RequestHandlerInterface ;
17
19
use Symfony \Component \HttpFoundation \Request ;
24
26
*/
25
27
class HttpFoundationRequestHandler implements RequestHandlerInterface
26
28
{
29
+ /**
30
+ * @var ServerParams
31
+ */
32
+ private $ serverParams ;
33
+
34
+ /**
35
+ * {@inheritdoc}
36
+ */
37
+ public function __construct (ServerParams $ params = null )
38
+ {
39
+ $ this ->serverParams = $ params ?: new ServerParams ();
40
+ }
41
+
27
42
/**
28
43
* {@inheritdoc}
29
44
*/
@@ -61,6 +76,10 @@ public function handleRequest(FormInterface $form, $request = null)
61
76
$ params = $ request ->request ->get ($ name , $ default );
62
77
$ files = $ request ->files ->get ($ name , $ default );
63
78
} else {
79
+ if ($ this ->serverParams ->getContentLength () > $ this ->serverParams ->getPostMaxSize ()) {
80
+ $ form ->addError (new FormError ('Max post size exceeded. ' ));
81
+ }
82
+
64
83
// Don't submit the form if it is not present in the request
65
84
return ;
66
85
}
Original file line number Diff line number Diff line change 12
12
namespace Symfony \Component \Form ;
13
13
14
14
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
15
+ use Symfony \Component \Form \Extension \Validator \Util \ServerParams ;
15
16
16
17
/**
17
18
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
20
21
*/
21
22
class NativeRequestHandler implements RequestHandlerInterface
22
23
{
24
+ /**
25
+ * @var ServerParams
26
+ */
27
+ private $ serverParams ;
28
+
29
+ /**
30
+ * {@inheritdoc}
31
+ */
32
+ public function __construct (ServerParams $ params = null )
33
+ {
34
+ $ this ->serverParams = $ params ?: new ServerParams ();
35
+ }
36
+
23
37
/**
24
38
* The allowed keys of the $_FILES array.
25
39
*
@@ -75,6 +89,10 @@ public function handleRequest(FormInterface $form, $request = null)
75
89
$ params = array_key_exists ($ name , $ _POST ) ? $ _POST [$ name ] : $ default ;
76
90
$ files = array_key_exists ($ name , $ fixedFiles ) ? $ fixedFiles [$ name ] : $ default ;
77
91
} else {
92
+ if ($ this ->serverParams ->getContentLength () > $ this ->serverParams ->getPostMaxSize ()) {
93
+ $ form ->addError (new FormError ('Max post size exceeded. ' ));
94
+ }
95
+
78
96
// Don't submit the form if it is not present in the request
79
97
return ;
80
98
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Tests ;
13
13
14
+ use Symfony \Component \Form \Forms ;
15
+
14
16
/**
15
17
* @author Bernhard Schussek <bschussek@gmail.com>
16
18
*/
@@ -21,11 +23,17 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
21
23
*/
22
24
protected $ requestHandler ;
23
25
26
+ /**
27
+ * @var \Symfony\Component\Form\FormFactory
28
+ */
29
+ protected $ factory ;
30
+
24
31
protected $ request ;
25
32
26
33
protected function setUp ()
27
34
{
28
35
$ this ->requestHandler = $ this ->getRequestHandler ();
36
+ $ this ->factory = Forms::createFormFactoryBuilder ()->getFormFactory ();
29
37
$ this ->request = null ;
30
38
}
31
39
@@ -249,6 +257,19 @@ public function testSubmitFileIfNoParam($method)
249
257
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
250
258
}
251
259
260
+ public function testAddFormErrorIfPostMaxSizeExceeded ()
261
+ {
262
+ $ form = $ this ->factory ->createNamed ('name ' , 'text ' );
263
+ $ this ->setRequestData ('POST ' , array (), array ());
264
+ $ _SERVER ['CONTENT_LENGTH ' ] = 1000000000 ;
265
+
266
+ $ this ->requestHandler ->handleRequest ($ form , $ this ->request );
267
+
268
+ $ this ->assertEquals ("ERROR: Max post size exceeded. \n" , $ form ->getErrorsAsString ());
269
+
270
+ unset($ _SERVER ['CONTENT_LENGTH ' ]);
271
+ }
272
+
252
273
abstract protected function setRequestData ($ method , $ data , $ files = array ());
253
274
254
275
abstract protected function getRequestHandler ();
You can’t perform that action at this time.
0 commit comments