Skip to content

Commit 75abd1a

Browse files
committed
bug symfony#11244 [HttpFoundation] Remove body-related headers when sending the response, if body is empty (SimonSimCity)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes symfony#11244). Discussion ---------- [HttpFoundation] Remove body-related headers when sending the response, if body is empty | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | I've updated the implementation for informational and 204 or 304 responses. They will now, as they have no content, not return headers like `content-type` or `content-length`. I'm unsure about `content-length` - we could also set it hardcoded to zero ... but I thought, that (because the specs say that it just can't have a response-body) the system should not return anything here. Commits ------- 9dbe89d [HttpFoundation] Remove content-related headers if content is empty
2 parents cc84d95 + 9dbe89d commit 75abd1a

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,42 @@ public function prepare(Request $request)
209209

210210
if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
211211
$this->setContent(null);
212-
}
213-
214-
// Content-type based on the Request
215-
if (!$headers->has('Content-Type')) {
216-
$format = $request->getRequestFormat();
217-
if (null !== $format && $mimeType = $request->getMimeType($format)) {
218-
$headers->set('Content-Type', $mimeType);
212+
$headers->remove('Content-Type');
213+
$headers->remove('Content-Length');
214+
} else {
215+
// Content-type based on the Request
216+
if (!$headers->has('Content-Type')) {
217+
$format = $request->getRequestFormat();
218+
if (null !== $format && $mimeType = $request->getMimeType($format)) {
219+
$headers->set('Content-Type', $mimeType);
220+
}
219221
}
220-
}
221222

222-
// Fix Content-Type
223-
$charset = $this->charset ?: 'UTF-8';
224-
if (!$headers->has('Content-Type')) {
225-
$headers->set('Content-Type', 'text/html; charset='.$charset);
226-
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
227-
// add the charset
228-
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
229-
}
223+
// Fix Content-Type
224+
$charset = $this->charset ?: 'UTF-8';
225+
if (!$headers->has('Content-Type')) {
226+
$headers->set('Content-Type', 'text/html; charset=' . $charset);
227+
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos(
228+
$headers->get('Content-Type'),
229+
'charset'
230+
)
231+
) {
232+
// add the charset
233+
$headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
234+
}
230235

231-
// Fix Content-Length
232-
if ($headers->has('Transfer-Encoding')) {
233-
$headers->remove('Content-Length');
234-
}
236+
// Fix Content-Length
237+
if ($headers->has('Transfer-Encoding')) {
238+
$headers->remove('Content-Length');
239+
}
235240

236-
if ($request->isMethod('HEAD')) {
237-
// cf. RFC2616 14.13
238-
$length = $headers->get('Content-Length');
239-
$this->setContent(null);
240-
if ($length) {
241-
$headers->set('Content-Length', $length);
241+
if ($request->isMethod('HEAD')) {
242+
// cf. RFC2616 14.13
243+
$length = $headers->get('Content-Length');
244+
$this->setContent(null);
245+
if ($length) {
246+
$headers->set('Content-Length', $length);
247+
}
242248
}
243249
}
244250

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ public function testPrepareRemovesContentForHeadRequests()
374374
$response->prepare($request);
375375

376376
$this->assertEquals('', $response->getContent());
377+
$this->assertFalse($response->headers->has('Content-Type'));
378+
$this->assertFalse($response->headers->has('Content-Length'));
377379
}
378380

379381
public function testPrepareSetsPragmaOnHttp10Only()

0 commit comments

Comments
 (0)