BUGFIX Setting "Content-Length" header in HTTPResponse to avoid problems with proxies timing out

This commit is contained in:
Ingo Schommer 2011-07-26 08:25:09 +02:00
parent 5e9ba3c1d5
commit dfb0504d0d
2 changed files with 21 additions and 1 deletions

View File

@ -98,7 +98,7 @@ class SS_HTTPResponse {
* See {@link setStatusCode()} for more information.
*/
function __construct($body = null, $statusCode = null, $statusDescription = null) {
$this->body = $body;
$this->setBody($body);
if($statusCode) $this->setStatusCode($statusCode, $statusDescription);
}
@ -150,6 +150,9 @@ class SS_HTTPResponse {
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'] = (function_exists('mb_strlen') ? mb_strlen($this->body,'8bit') : strlen($this->body));
}
function getBody() {

View File

@ -13,4 +13,21 @@ class HTTPResponseTest extends SapphireTest {
);
}
function testContentLengthHeader() {
$r = new SS_HTTPResponse('123ü');
$this->assertNotNull($r->getHeader('Content-Length'), 'Content-length header is added');
$this->assertEquals(
5,
$r->getHeader('Content-Length'),
'Header matches actual content length in bytes'
);
$r->setBody('1234ü');
$this->assertEquals(
6,
$r->getHeader('Content-Length'),
'Header is updated when body is changed'
);
}
}