Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUGFIX Setting "Content-Length" header in HTTPResponse to avoid probl…

…ems with proxies timing out
  • Loading branch information...
commit dfb0504d0d7359c090cb182e96fbb80aa1608f30 1 parent 5e9ba3c
@chillu chillu authored
Showing with 21 additions and 1 deletion.
  1. +4 −1 control/HTTPResponse.php
  2. +17 −0 tests/control/HTTPResponseTest.php
View
5 control/HTTPResponse.php
@@ -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 @@ function isError() {
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
17 tests/control/HTTPResponseTest.php
@@ -13,4 +13,21 @@ function testStatusDescriptionStripsNewlines() {
);
}
+ 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'
+ );
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.