Skip to content

Commit 5da1a87

Browse files
committed
Rely on required boundary=X parameter and do not try to guess
1 parent 8c281d4 commit 5da1a87

3 files changed

Lines changed: 49 additions & 47 deletions

File tree

src/Io/MultipartParser.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,15 @@ private function parse()
6060
{
6161
$this->buffer = (string)$this->request->getBody();
6262

63-
$this->determineStartMethod();
64-
65-
return $this->request;
66-
}
67-
68-
private function determineStartMethod()
69-
{
70-
if (!$this->request->hasHeader('content-type')) {
71-
$this->findBoundary();
72-
return;
73-
}
74-
7563
$contentType = $this->request->getHeaderLine('content-type');
76-
preg_match('/boundary="?(.*)"?$/', $contentType, $matches);
77-
if (isset($matches[1])) {
78-
$this->boundary = $matches[1];
79-
$this->parseBuffer();
80-
return;
64+
if(!preg_match('/boundary="?(.*)"?$/', $contentType, $matches)) {
65+
return $this->request;
8166
}
8267

83-
$this->findBoundary();
84-
}
68+
$this->boundary = $matches[1];
69+
$this->parseBuffer();
8570

86-
private function findBoundary()
87-
{
88-
if (substr($this->buffer, 0, 3) === '---' && strpos($this->buffer, "\r\n") !== false) {
89-
$boundary = substr($this->buffer, 2, strpos($this->buffer, "\r\n"));
90-
$boundary = substr($boundary, 0, -2);
91-
$this->boundary = $boundary;
92-
$this->parseBuffer();
93-
}
71+
return $this->request;
9472
}
9573

9674
private function parseBuffer()

tests/Io/MultipartParserTest.php

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@
88

99
final class MultipartParserTest extends TestCase
1010
{
11+
public function testDoesNotParseWithoutMultipartFormDataContentType()
12+
{
13+
$boundary = "---------------------------5844729766471062541057622570";
14+
15+
$data = "--$boundary\r\n";
16+
$data .= "Content-Disposition: form-data; name=\"single\"\r\n";
17+
$data .= "\r\n";
18+
$data .= "single\r\n";
19+
$data .= "--$boundary\r\n";
20+
$data .= "Content-Disposition: form-data; name=\"second\"\r\n";
21+
$data .= "\r\n";
22+
$data .= "second\r\n";
23+
$data .= "--$boundary--\r\n";
24+
25+
$request = new ServerRequest('POST', 'http://example.com/', array(
26+
'Content-Type' => 'multipart/form-data',
27+
), $data, 1.1);
28+
29+
$parsedRequest = MultipartParser::parseRequest($request);
30+
31+
$this->assertEmpty($parsedRequest->getUploadedFiles());
32+
$this->assertEmpty($parsedRequest->getParsedBody());
33+
}
34+
1135
public function testPostKey()
1236
{
1337
$boundary = "---------------------------5844729766471062541057622570";
@@ -23,7 +47,7 @@ public function testPostKey()
2347
$data .= "--$boundary--\r\n";
2448

2549
$request = new ServerRequest('POST', 'http://example.com/', array(
26-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
50+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
2751
), $data, 1.1);
2852

2953
$parsedRequest = MultipartParser::parseRequest($request);
@@ -55,7 +79,7 @@ public function testPostStringOverwritesMap()
5579
$data .= "--$boundary--\r\n";
5680

5781
$request = new ServerRequest('POST', 'http://example.com/', array(
58-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
82+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
5983
), $data, 1.1);
6084

6185
$parsedRequest = MultipartParser::parseRequest($request);
@@ -84,7 +108,7 @@ public function testPostMapOverwritesString()
84108
$data .= "--$boundary--\r\n";
85109

86110
$request = new ServerRequest('POST', 'http://example.com/', array(
87-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
111+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
88112
), $data, 1.1);
89113

90114
$parsedRequest = MultipartParser::parseRequest($request);
@@ -115,7 +139,7 @@ public function testPostVectorOverwritesString()
115139
$data .= "--$boundary--\r\n";
116140

117141
$request = new ServerRequest('POST', 'http://example.com/', array(
118-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
142+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
119143
), $data, 1.1);
120144

121145
$parsedRequest = MultipartParser::parseRequest($request);
@@ -146,7 +170,7 @@ public function testPostDeeplyNestedArray()
146170
$data .= "--$boundary--\r\n";
147171

148172
$request = new ServerRequest('POST', 'http://example.com/', array(
149-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
173+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
150174
), $data, 1.1);
151175

152176
$parsedRequest = MultipartParser::parseRequest($request);
@@ -178,7 +202,7 @@ public function testEmptyPostValue()
178202
$data .= "--$boundary--\r\n";
179203

180204
$request = new ServerRequest('POST', 'http://example.com/', array(
181-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
205+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
182206
), $data, 1.1);
183207

184208
$parsedRequest = MultipartParser::parseRequest($request);
@@ -203,7 +227,7 @@ public function testEmptyPostKey()
203227
$data .= "--$boundary--\r\n";
204228

205229
$request = new ServerRequest('POST', 'http://example.com/', array(
206-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
230+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
207231
), $data, 1.1);
208232

209233
$parsedRequest = MultipartParser::parseRequest($request);
@@ -228,7 +252,7 @@ public function testNestedPostKeyAssoc()
228252
$data .= "--$boundary--\r\n";
229253

230254
$request = new ServerRequest('POST', 'http://example.com/', array(
231-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
255+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
232256
), $data, 1.1);
233257

234258
$parsedRequest = MultipartParser::parseRequest($request);
@@ -257,7 +281,7 @@ public function testNestedPostKeyVector()
257281
$data .= "--$boundary--\r\n";
258282

259283
$request = new ServerRequest('POST', 'http://example.com/', array(
260-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
284+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
261285
), $data, 1.1);
262286

263287
$parsedRequest = MultipartParser::parseRequest($request);
@@ -337,7 +361,7 @@ public function testFileUpload()
337361
$data .= "--$boundary--\r\n";
338362

339363
$request = new ServerRequest('POST', 'http://example.com/', array(
340-
'Content-Type' => 'multipart/form-data',
364+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
341365
), $data, 1.1);
342366

343367
$parsedRequest = MultipartParser::parseRequest($request);
@@ -477,7 +501,7 @@ public function testInvalidContentDispositionMissingWillBeIgnored()
477501
$data .= "--$boundary--\r\n";
478502

479503
$request = new ServerRequest('POST', 'http://example.com/', array(
480-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
504+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
481505
), $data, 1.1);
482506

483507
$parsedRequest = MultipartParser::parseRequest($request);
@@ -497,7 +521,7 @@ public function testInvalidContentDispositionWithoutNameWillBeIgnored()
497521
$data .= "--$boundary--\r\n";
498522

499523
$request = new ServerRequest('POST', 'http://example.com/', array(
500-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
524+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
501525
), $data, 1.1);
502526

503527
$parsedRequest = MultipartParser::parseRequest($request);
@@ -516,7 +540,7 @@ public function testInvalidMissingEndBoundaryWillBeIgnored()
516540
$data .= "value\r\n";
517541

518542
$request = new ServerRequest('POST', 'http://example.com/', array(
519-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
543+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
520544
), $data, 1.1);
521545

522546
$parsedRequest = MultipartParser::parseRequest($request);
@@ -539,7 +563,7 @@ public function testInvalidUploadFileWithoutContentTypeUsesNullValue()
539563
$data .= "--$boundary--\r\n";
540564

541565
$request = new ServerRequest('POST', 'http://example.com/', array(
542-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
566+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
543567
), $data, 1.1);
544568

545569
$parsedRequest = MultipartParser::parseRequest($request);
@@ -573,7 +597,7 @@ public function testInvalidUploadFileWithoutMultipleContentTypeUsesLastValue()
573597
$data .= "--$boundary--\r\n";
574598

575599
$request = new ServerRequest('POST', 'http://example.com/', array(
576-
'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
600+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
577601
), $data, 1.1);
578602

579603
$parsedRequest = MultipartParser::parseRequest($request);
@@ -606,7 +630,7 @@ public function testUploadEmptyFile()
606630
$data .= "--$boundary--\r\n";
607631

608632
$request = new ServerRequest('POST', 'http://example.com/', array(
609-
'Content-Type' => 'multipart/form-data',
633+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
610634
), $data, 1.1);
611635

612636
$parsedRequest = MultipartParser::parseRequest($request);
@@ -639,7 +663,7 @@ public function testUploadNoFile()
639663
$data .= "--$boundary--\r\n";
640664

641665
$request = new ServerRequest('POST', 'http://example.com/', array(
642-
'Content-Type' => 'multipart/form-data',
666+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
643667
), $data, 1.1);
644668

645669
$parsedRequest = MultipartParser::parseRequest($request);
@@ -676,7 +700,7 @@ public function testPostMaxFileSize()
676700
$data .= "--$boundary--\r\n";
677701

678702
$request = new ServerRequest('POST', 'http://example.com/', array(
679-
'Content-Type' => 'multipart/form-data',
703+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
680704
), $data, 1.1);
681705

682706
$parsedRequest = MultipartParser::parseRequest($request);
@@ -730,7 +754,7 @@ public function testPostMaxFileSizeIgnoredByFilesComingBeforeIt()
730754
$data .= "--$boundary--\r\n";
731755

732756
$request = new ServerRequest('POST', 'http://example.com/', array(
733-
'Content-Type' => 'multipart/form-data',
757+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
734758
), $data, 1.1);
735759

736760
$parsedRequest = MultipartParser::parseRequest($request);

tests/Middleware/RequestBodyParserMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testMultipartFormDataParsing()
149149

150150

151151
$request = new ServerRequest('POST', 'http://example.com/', array(
152-
'Content-Type' => 'multipart/form-data',
152+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
153153
), $data, 1.1);
154154

155155
/** @var ServerRequestInterface $parsedRequest */

0 commit comments

Comments
 (0)