-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add request_parse_body() function #11472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc13a4b
Add request_parse_body() function
iluuu1994 72a9f18
Print throwable class in tests
iluuu1994 09689f5
Move function to http.c
iluuu1994 1ca28cd
Improve error messages
iluuu1994 a562903
Add RequestParseBodyException
iluuu1994 856b509
More exception stuff
iluuu1994 dd14fd0
Retain RFC1867 error message format
iluuu1994 bac3f6d
Fix more tests
iluuu1994 e659334
Review fixes
iluuu1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and invalid boundary | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary="foobar | ||
empty | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Invalid boundary in multipart/form-data POST data | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--TEST-- | ||
request_parse_body() with multipart | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="post_field_name" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file_name"; filename="original_file_name.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
[$_POST, $_FILES] = request_parse_body(); | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
$file_path = __DIR__ . '/put_multipart_uploaded_file.txt'; | ||
move_uploaded_file($_FILES['file_name']['tmp_name'], $file_path); | ||
var_dump(file_get_contents($file_path)); | ||
|
||
?> | ||
--CLEAN-- | ||
<?php | ||
$file_path = __DIR__ . '/put_multipart_uploaded_file.txt'; | ||
@unlink($file_path); | ||
?> | ||
--EXPECTF-- | ||
array(1) { | ||
["post_field_name"]=> | ||
string(15) "post field data" | ||
} | ||
array(1) { | ||
["file_name"]=> | ||
array(6) { | ||
["name"]=> | ||
string(22) "original_file_name.txt" | ||
["full_path"]=> | ||
string(22) "original_file_name.txt" | ||
["type"]=> | ||
string(10) "text/plain" | ||
["tmp_name"]=> | ||
string(%d) "%s" | ||
["error"]=> | ||
int(0) | ||
["size"]=> | ||
int(9) | ||
} | ||
} | ||
string(9) "file data" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and garbled field | ||
--INI-- | ||
max_file_uploads=1 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; | ||
Content-Type: text/plain | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: File Upload Mime headers garbled | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and exceeding max files | ||
--INI-- | ||
max_file_uploads=1 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file1"; filename="file1.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file2"; filename="file2.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Maximum number of allowable file uploads has been exceeded | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
35 changes: 35 additions & 0 deletions
35
Zend/tests/request_parse_body/multipart_max_input_vars.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and exceeding max input vars | ||
--INI-- | ||
max_input_vars=1 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="field1" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="field2" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Input variables exceeded 1. To increase the limit change max_input_vars in php.ini. | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and exceeding max parts | ||
--INI-- | ||
max_multipart_body_parts=1 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="post_field_name" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file_name"; filename="original_file_name.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Multipart body parts limit exceeded 1. To increase the limit change max_multipart_body_parts in php.ini. | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
27 changes: 27 additions & 0 deletions
27
Zend/tests/request_parse_body/multipart_missing_boundary.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
request_parse_body() with multipart and missing boundary | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data | ||
empty | ||
--FILE-- | ||
<?php | ||
|
||
$stream = fopen('php://memory','r+'); | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body(); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Missing boundary in multipart/form-data POST data | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
21 changes: 21 additions & 0 deletions
21
Zend/tests/request_parse_body/multipart_options_invalid_key.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
request_parse_body() invalid key | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
request_parse_body(options: ['foo' => 1]); | ||
} catch (Error $e) { | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
request_parse_body(options: [42 => 1]); | ||
} catch (Error $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
ValueError: Invalid key "foo" in $options argument | ||
ValueError: Invalid integer key in $options argument |
17 changes: 17 additions & 0 deletions
17
Zend/tests/request_parse_body/multipart_options_invalid_quantity.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
request_parse_body() invalid quantity | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
request_parse_body(options: [ | ||
'upload_max_filesize' => '1GB', | ||
]); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: Invalid quantity "1GB": unknown multiplier "B", interpreting as "1" for backwards compatibility in %s on line %d | ||
RequestParseBodyException: Request does not provide a content type |
16 changes: 16 additions & 0 deletions
16
Zend/tests/request_parse_body/multipart_options_invalid_value_type.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
request_parse_body() invalid value type | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
request_parse_body(options: [ | ||
'max_input_vars' => [], | ||
]); | ||
} catch (Error $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
ValueError: Invalid array value in $options argument |
39 changes: 39 additions & 0 deletions
39
Zend/tests/request_parse_body/multipart_options_max_files.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--TEST-- | ||
request_parse_body() max_file_uploads option | ||
--INI-- | ||
max_file_uploads=10 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file1"; filename="file1.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="file2"; filename="file2.txt" | ||
Content-Type: text/plain | ||
|
||
file data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body([ | ||
'max_file_uploads' => 1, | ||
]); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Maximum number of allowable file uploads has been exceeded | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
37 changes: 37 additions & 0 deletions
37
Zend/tests/request_parse_body/multipart_options_max_input_vars.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
request_parse_body() max_input_vars option | ||
--INI-- | ||
max_input_vars=10 | ||
--ENV-- | ||
REQUEST_METHOD=PUT | ||
--POST_RAW-- | ||
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332 | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="field1" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332 | ||
Content-Disposition: form-data; name="field2" | ||
|
||
post field data | ||
-----------------------------84000087610663814162942123332-- | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
[$_POST, $_FILES] = request_parse_body([ | ||
'max_input_vars' => 1, | ||
]); | ||
} catch (Throwable $e) { | ||
echo get_class($e), ': ', $e->getMessage(), "\n"; | ||
} | ||
|
||
var_dump($_POST, $_FILES); | ||
|
||
?> | ||
--EXPECT-- | ||
RequestParseBodyException: Input variables exceeded 1. To increase the limit change max_input_vars in php.ini. | ||
array(0) { | ||
} | ||
array(0) { | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.