From c69381c33e28571497bd4143493b024dae144146 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 23 Jan 2013 15:02:30 +0100 Subject: [PATCH] API Remove Content-Length setting from HTTPResponse (fixes #8010) This reverts commit 356a367eb5d05bea3dfa2edaeabc52a2496b93b2. We can't use headers_sent() to determine an accurate content length, since PHP defaults to buffering a couple of bytes even without ob_start() (see "output_buffering" setting). This makes the patch harmful, since it breaks any responses relying on more structure data, like removing closing brackets from JSON. Which in turn breaks the CMS in horrible ways (see #8010). See #7574 for context. --- control/Director.php | 11 ----------- control/HTTPResponse.php | 11 +++-------- tests/control/HTTPResponseTest.php | 2 -- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/control/Director.php b/control/Director.php index 86684bc67..27b68230c 100644 --- a/control/Director.php +++ b/control/Director.php @@ -148,17 +148,6 @@ class Director implements TemplateGlobalProvider { number_format(memory_get_peak_usage(),0) )); } else { - // Set content length (according to RFC2616) - if( - !headers_sent() - && $response->getBody() - && $req->httpMethod() != 'HEAD' - && $response->getStatusCode() >= 200 - && !in_array($response->getStatusCode(), array(204, 304)) - ) { - $response->fixContentLength(); - } - $response->output(); } } else { diff --git a/control/HTTPResponse.php b/control/HTTPResponse.php index 16a801838..4cc2da3bf 100644 --- a/control/HTTPResponse.php +++ b/control/HTTPResponse.php @@ -150,6 +150,9 @@ class SS_HTTPResponse { public function setBody($body) { $this->body = $body; + + // Set content-length in bytes. Use mbstring to avoid problems with mb_internal_encoding() and mbstring.func_overload + $this->headers['Content-Length'] = mb_strlen($this->body,'8bit'); } public function getBody() { @@ -246,14 +249,6 @@ class SS_HTTPResponse { public function isFinished() { return in_array($this->statusCode, array(301, 302, 401, 403)); } - - /** - * Set content-length in bytes. Should be called right before {@link output()}. - */ - public function fixContentLength() { - // Use mbstring to avoid problems with mb_internal_encoding() and mbstring.func_overload - $this->headers['Content-Length'] = mb_strlen($this->body,'8bit'); - } } diff --git a/tests/control/HTTPResponseTest.php b/tests/control/HTTPResponseTest.php index 69675c19d..f2f37c693 100644 --- a/tests/control/HTTPResponseTest.php +++ b/tests/control/HTTPResponseTest.php @@ -15,7 +15,6 @@ class HTTPResponseTest extends SapphireTest { public function testContentLengthHeader() { $r = new SS_HTTPResponse('123ü'); - $r->fixContentLength(); $this->assertNotNull($r->getHeader('Content-Length'), 'Content-length header is added'); $this->assertEquals( 5, @@ -24,7 +23,6 @@ class HTTPResponseTest extends SapphireTest { ); $r->setBody('1234ü'); - $r->fixContentLength(); $this->assertEquals( 6, $r->getHeader('Content-Length'),