Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #68 from solcre/hotfix-array-data
Browse files Browse the repository at this point in the history
Hotfix array data
  • Loading branch information
weierophinney committed May 26, 2016
2 parents 783f5cd + b169073 commit 88d879a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/MultipartContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function parse()
*/
protected function parseFromStream($stream)
{
$buffer = '';
$data = new Parameters();
$files = new Parameters();
$partInProgress = false;
Expand All @@ -111,7 +112,8 @@ protected function parseFromStream($stream)
// Time to handle the data we've already parsed!
// Data
if (! $filename) {
$data->set($name, rtrim($content, "\r\n"));
$content = rtrim($content, "\r\n");
$buffer .= "$name=".rawurlencode($content)."&";
}

// File (successful upload so far)
Expand Down Expand Up @@ -263,6 +265,8 @@ protected function parseFromStream($stream)
$this->request->setFiles($files);
}

$data->fromString(rtrim($buffer, '&'));

return $data->toArray();
}

Expand Down
30 changes: 30 additions & 0 deletions test/ContentTypeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,34 @@ public function testReturns400ResponseWhenBodyPartIsMissingName()
$details = $result->getApiProblem()->toArray();
$this->assertContains('does not contain a "name" field', $details['detail']);
}

public function testReturnsArrayWhenFieldNamesHaveArraySyntax()
{
$request = new Request();
$request->setMethod('PUT');
$request->getHeaders()->addHeaderLine(
'Content-Type',
'multipart/form-data; boundary=6603ddd555b044dc9a022f3ad9281c20'
);
$request->setContent(file_get_contents(__DIR__ . '/TestAsset/multipart-form-data-array.txt'));
$event = new MvcEvent();
$event->setRequest($request);
$event->setRouteMatch(new RouteMatch([]));
$listener = $this->listener;
$result = $listener($event);
$parameterData = $event->getParam('ZFContentNegotiationParameterData');
$params = $parameterData->getBodyParams();
$this->assertEquals([
'string_value' => 'string_value_with&ersand',
'array_name' => [
'array_name[0]',
'array_name[1]',
'a' => 'array_name[a]',
'b' => [
0 => 'array_name[b][0]',
'b' => 'array_name[b][b]',
],
],
], $params);
}
}
37 changes: 37 additions & 0 deletions test/TestAsset/multipart-form-data-array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="string_value"
Content-Type: text/plain

string_value_with&ersand
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[]"
Content-Type: text/plain

array_name[0]
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[]"
Content-Type: text/plain

array_name[1]
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[a]"
Content-Type: text/plain

array_name[a]
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[b]"
Content-Type: text/plain

array_name[b]
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[b][]"
Content-Type: text/plain

array_name[b][0]
--6603ddd555b044dc9a022f3ad9281c20
Content-Disposition: form-data; name="array_name[b][b]"
Content-Type: text/plain

array_name[b][b]

--6603ddd555b044dc9a022f3ad9281c20--

0 comments on commit 88d879a

Please sign in to comment.