bug #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 #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
This commit is contained in:
Fabien Potencier 2014-07-09 11:03:38 +02:00
commit 75abd1a451
2 changed files with 36 additions and 28 deletions

View File

@ -209,8 +209,9 @@ class Response
if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
$this->setContent(null);
}
$headers->remove('Content-Type');
$headers->remove('Content-Length');
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getRequestFormat();
@ -222,10 +223,14 @@ class Response
// Fix Content-Type
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
$headers->set('Content-Type', 'text/html; charset=' . $charset);
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos(
$headers->get('Content-Type'),
'charset'
)
) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
$headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
}
// Fix Content-Length
@ -241,6 +246,7 @@ class Response
$headers->set('Content-Length', $length);
}
}
}
// Fix protocol
if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {

View File

@ -374,6 +374,8 @@ class ResponseTest extends ResponseTestCase
$response->prepare($request);
$this->assertEquals('', $response->getContent());
$this->assertFalse($response->headers->has('Content-Type'));
$this->assertFalse($response->headers->has('Content-Length'));
}
public function testPrepareSetsPragmaOnHttp10Only()