Skip to content

Commit 69699c9

Browse files
authored
Merge pull request #304 from dotkernel/issue-303
fix content type, special case for multipart/form-data
2 parents d136bad + 19f746c commit 69699c9

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

config/autoload/content-negotiation.global.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,35 @@
44

55
return [
66
'content-negotiation' => [
7-
'default' => [ // default to any route if not configured above
8-
'Accept' => [
7+
'default' => [ // default to any route if not configured above
8+
'Accept' => [ // the Accept is what format the server can send back
99
'application/json',
1010
'application/hal+json',
1111
],
12-
'Content-Type' => [
12+
'Content-Type' => [ // the Content-Type is what format the server can process
1313
'application/json',
1414
'application/hal+json',
1515
],
1616
],
17-
'your.route.name' => [
17+
'your.route.name' => [
1818
'Accept' => [],
1919
'Content-Type' => [],
2020
],
21+
'user.avatar.create' => [
22+
'Accept' => [
23+
'application/json',
24+
'application/hal+json',
25+
],
26+
'Content-Type' => [
27+
'multipart/form-data',
28+
],
29+
],
30+
'user.my-avatar.create' => [
31+
'Accept' => [
32+
'application/json',
33+
'application/hal+json',
34+
],
35+
'Content-Type' => 'multipart/form-data',
36+
],
2137
],
2238
];

src/App/src/Middleware/ContentNegotiationMiddleware.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function process(
5858
$response = $handler->handle($request);
5959

6060
$responseContentType = $response->getHeaderLine('Content-Type');
61+
6162
if (! $this->validateResponseContentType($responseContentType, $accept)) {
6263
return $this->notAcceptableResponse('Unable to resolve Accept header to a representation');
6364
}
@@ -98,15 +99,17 @@ public function checkContentType(string $routeName, string $contentType): bool
9899
return true;
99100
}
100101

102+
$contentType = explode(';', $contentType);
103+
101104
$acceptList = $this->config['default']['Content-Type'] ?? [];
102105
if (! empty($this->config[$routeName]['Content-Type'])) {
103106
$acceptList = $this->config[$routeName]['Content-Type'] ?? [];
104107
}
105108

106109
if (is_array($acceptList)) {
107-
return in_array($contentType, $acceptList, true);
110+
return ! empty(array_intersect($contentType, $acceptList));
108111
} else {
109-
return $contentType === $acceptList;
112+
return in_array($acceptList, $contentType, true);
110113
}
111114
}
112115

0 commit comments

Comments
 (0)