Skip to content

Commit f336dcc

Browse files
committed
added extension point in the BodyListener to customize if to decode or not
1 parent 7cb532f commit f336dcc

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

EventListener/BodyListener.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ public function onKernelRequest(GetResponseEvent $event)
8888
$request = $event->getRequest();
8989
$method = $request->getMethod();
9090
$contentType = $request->headers->get('Content-Type');
91-
$isFormRequest = $this->isFormRequest($request);
92-
$normalizeRequest = $this->normalizeForms && $isFormRequest;
91+
$normalizeRequest = $this->normalizeForms && $this->isFormRequest($request);
9392

94-
if (!$isFormRequest && in_array($method, array('POST', 'PUT', 'PATCH', 'DELETE'))) {
93+
if ($this->isDecodeable($request)) {
9594
$format = null === $contentType
9695
? $request->getRequestFormat()
9796
: $request->getFormat($contentType);
@@ -101,9 +100,8 @@ public function onKernelRequest(GetResponseEvent $event)
101100
$content = $request->getContent();
102101

103102
if (!$this->decoderProvider->supports($format)) {
104-
if (
105-
$this->throwExceptionOnUnsupportedContentType &&
106-
$this->isNotAnEmptyDeleteRequestWithNoSetContentType($method, $content, $contentType)
103+
if ($this->throwExceptionOnUnsupportedContentType
104+
&& $this->isNotAnEmptyDeleteRequestWithNoSetContentType($method, $content, $contentType)
107105
) {
108106
throw new UnsupportedMediaTypeHttpException("Request body format '$format' not supported");
109107
}
@@ -136,17 +134,42 @@ public function onKernelRequest(GetResponseEvent $event)
136134
}
137135
}
138136

137+
/**
138+
* Check if the Request is a not a DELETE with no content and no Content-Type
139+
*
140+
* @param $method
141+
* @param $content
142+
* @param $contentType
143+
* @return bool
144+
*/
139145
private function isNotAnEmptyDeleteRequestWithNoSetContentType($method, $content, $contentType)
140146
{
141147
return false === ('DELETE' === $method && empty($content) && null === $contentType);
142148
}
143149

144-
private function isFormRequest(Request $request)
150+
/**
151+
* Check if we should try to decode the body
152+
*
153+
* @param Request $request
154+
* @return bool
155+
*/
156+
protected function isDecodeable(Request $request)
145157
{
146158
if (!in_array($request->getMethod(), array('POST', 'PUT', 'PATCH', 'DELETE'))) {
147159
return false;
148160
}
149161

162+
return !$this->isFormRequest($request);
163+
}
164+
165+
/**
166+
* Check if the content type indicates a form submission
167+
*
168+
* @param Request $request
169+
* @return bool
170+
*/
171+
protected function isFormRequest(Request $request)
172+
{
150173
$contentTypeParts = explode(';', $request->headers->get('Content-Type'));
151174

152175
if (isset($contentTypeParts[0])) {

0 commit comments

Comments
 (0)