Skip to content

Commit 129df64

Browse files
committed
Simplify parameter parsing by removing duplicate logic
1 parent 5da1a87 commit 129df64

1 file changed

Lines changed: 36 additions & 48 deletions

File tree

src/Io/MultipartParser.php

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,81 +95,81 @@ private function parseChunk($chunk)
9595
return;
9696
}
9797

98-
if (!$this->headerContainsParameter($headers['content-disposition'], 'name')) {
98+
$name = $this->getParameterFromHeader($headers['content-disposition'], 'name');
99+
if ($name === null) {
99100
return;
100101
}
101102

102-
if ($this->headerContainsParameter($headers['content-disposition'], 'filename')) {
103-
$this->parseFile($headers, $body);
103+
$filename = $this->getParameterFromHeader($headers['content-disposition'], 'filename');
104+
if ($filename !== null) {
105+
$this->parseFile(
106+
$name,
107+
$filename,
108+
isset($headers['content-type'][0]) ? $headers['content-type'][0] : null,
109+
$body
110+
);
104111
} else {
105-
$this->parsePost($headers, $body);
112+
$this->parsePost($name, $body);
106113
}
107114
}
108115

109-
private function parseFile($headers, $body)
116+
private function parseFile($name, $filename, $contentType, $contents)
110117
{
111118
$this->request = $this->request->withUploadedFiles($this->extractPost(
112119
$this->request->getUploadedFiles(),
113-
$this->getParameterFromHeader($headers['content-disposition'], 'name'),
114-
$this->parseUploadedFile($headers, $body)
120+
$name,
121+
$this->parseUploadedFile($filename, $contentType, $contents)
115122
));
116123
}
117124

118-
private function parseUploadedFile($headers, $body)
125+
private function parseUploadedFile($filename, $contentType, $contents)
119126
{
120-
$filename = $this->getParameterFromHeader($headers['content-disposition'], 'filename');
121-
$bodyLength = strlen($body);
122-
$contentType = isset($headers['content-type'][0]) ? $headers['content-type'][0] : null;
127+
$size = strlen($contents);
123128

124129
// no file selected (zero size and empty filename)
125-
if ($bodyLength === 0 && $filename === '') {
130+
if ($size === 0 && $filename === '') {
126131
return new UploadedFile(
127132
Psr7\stream_for(''),
128-
$bodyLength,
133+
$size,
129134
UPLOAD_ERR_NO_FILE,
130135
$filename,
131136
$contentType
132137
);
133138
}
134139

135140
// file exceeds MAX_FILE_SIZE value
136-
if ($this->maxFileSize !== null && $bodyLength > $this->maxFileSize) {
141+
if ($this->maxFileSize !== null && $size > $this->maxFileSize) {
137142
return new UploadedFile(
138143
Psr7\stream_for(''),
139-
$bodyLength,
144+
$size,
140145
UPLOAD_ERR_FORM_SIZE,
141146
$filename,
142147
$contentType
143148
);
144149
}
145150

146151
return new UploadedFile(
147-
Psr7\stream_for($body),
148-
$bodyLength,
152+
Psr7\stream_for($contents),
153+
$size,
149154
UPLOAD_ERR_OK,
150155
$filename,
151156
$contentType
152157
);
153158
}
154159

155-
private function parsePost($headers, $body)
160+
private function parsePost($name, $value)
156161
{
157-
foreach ($headers['content-disposition'] as $part) {
158-
if (strpos($part, 'name') === 0) {
159-
preg_match('/name="?(.*)"$/', $part, $matches);
160-
$this->request = $this->request->withParsedBody($this->extractPost(
161-
$this->request->getParsedBody(),
162-
$matches[1],
163-
$body
164-
));
165-
166-
if (strtoupper($matches[1]) === 'MAX_FILE_SIZE') {
167-
$this->maxFileSize = (int)$body;
168-
169-
if ($this->maxFileSize === 0) {
170-
$this->maxFileSize = null;
171-
}
172-
}
162+
$this->request = $this->request->withParsedBody($this->extractPost(
163+
$this->request->getParsedBody(),
164+
$name,
165+
$value
166+
));
167+
168+
if (strtoupper($name) === 'MAX_FILE_SIZE') {
169+
$this->maxFileSize = (int)$value;
170+
171+
if ($this->maxFileSize === 0) {
172+
$this->maxFileSize = null;
173173
}
174174
}
175175
}
@@ -190,27 +190,15 @@ private function parseHeaders($header)
190190
return $headers;
191191
}
192192

193-
private function headerContainsParameter(array $header, $parameter)
194-
{
195-
foreach ($header as $part) {
196-
if (strpos($part, $parameter . '=') === 0) {
197-
return true;
198-
}
199-
}
200-
201-
return false;
202-
}
203-
204193
private function getParameterFromHeader(array $header, $parameter)
205194
{
206195
foreach ($header as $part) {
207-
if (strpos($part, $parameter) === 0) {
208-
preg_match('/' . $parameter . '="?(.*)"$/', $part, $matches);
196+
if (preg_match('/' . $parameter . '="?(.*)"$/', $part, $matches)) {
209197
return $matches[1];
210198
}
211199
}
212200

213-
return '';
201+
return null;
214202
}
215203

216204
private function stripTrailingEOL($chunk)

0 commit comments

Comments
 (0)