Skip to content

Commit fca70a0

Browse files
committed
Improving the request/response format autodetection
1 parent d97f9ab commit fca70a0

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class Request
140140
*/
141141
protected $acceptableContentTypes;
142142

143+
/**
144+
* @var string|null
145+
*/
146+
protected $preferredFormat;
147+
143148
/**
144149
* @var string
145150
*/
@@ -1559,6 +1564,28 @@ public function isNoCache()
15591564
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
15601565
}
15611566

1567+
/**
1568+
* Returns the preferred format.
1569+
*/
1570+
public function getPreferredFormat(?string $default = 'html'): ?string
1571+
{
1572+
if (null !== $this->preferredFormat) {
1573+
return $this->preferredFormat;
1574+
}
1575+
1576+
$this->preferredFormat = $this->getRequestFormat($this->getContentType());
1577+
1578+
if (null === $this->preferredFormat) {
1579+
foreach ($this->getAcceptableContentTypes() as $contentType) {
1580+
if (null !== $this->preferredFormat = $this->getFormat($contentType)) {
1581+
break;
1582+
}
1583+
}
1584+
}
1585+
1586+
return $this->preferredFormat ?: $default;
1587+
}
1588+
15621589
/**
15631590
* Returns the preferred language.
15641591
*

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function prepare(Request $request)
270270
} else {
271271
// Content-type based on the Request
272272
if (!$headers->has('Content-Type')) {
273-
$format = $request->getRequestFormat();
273+
$format = $request->getPreferredFormat();
274274
if (null !== $format && $mimeType = $request->getMimeType($format)) {
275275
$headers->set('Content-Type', $mimeType);
276276
}

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
174174
$e = $request->attributes->get('exception');
175175

176176
try {
177-
return new Response($this->errorFormatter->render($e, $request->getRequestFormat()), $e->getStatusCode(), $e->getHeaders());
177+
return new Response($this->errorFormatter->render($e, $request->getPreferredFormat()), $e->getStatusCode(), $e->getHeaders());
178178
} catch (ErrorRendererNotFoundException $_) {
179179
return new Response($this->errorFormatter->render($e), $e->getStatusCode(), $e->getHeaders());
180180
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy