Fix incorrect status line parsing in HttpSocketResponse.

Allow for multi-word status reasons.

Closes #3545
This commit is contained in:
mark_story 2014-05-21 21:53:18 -04:00
parent b1fe9134c6
commit 270e8774e4
2 changed files with 12 additions and 8 deletions

View file

@ -159,11 +159,13 @@ class HttpSocketResponse implements ArrayAccess {
$this->raw = $message; $this->raw = $message;
$this->body = (string)substr($message, strlen($match[0])); $this->body = (string)substr($message, strlen($match[0]));
if (preg_match("/(.+) ([0-9]{3})\s*([^ ]*)\r\n/DU", $statusLine, $match)) { if (preg_match("/(.+) ([0-9]{3})(?:\s+(\w.+))?\s*\r\n/DU", $statusLine, $match)) {
$this->httpVersion = $match[1]; $this->httpVersion = $match[1];
$this->code = $match[2]; $this->code = $match[2];
if (isset($match[3])) {
$this->reasonPhrase = $match[3]; $this->reasonPhrase = $match[3];
} }
}
$this->headers = $this->_parseHeader($header); $this->headers = $this->_parseHeader($header);
$transferEncoding = $this->getHeader('Transfer-Encoding'); $transferEncoding = $this->getHeader('Transfer-Encoding');

View file

@ -1762,10 +1762,12 @@ class HttpSocketTest extends CakeTestCase {
*/ */
public function statusProvider() { public function statusProvider() {
return array( return array(
array('HTTP/1.1 200 '), array('HTTP/1.1 200 ', '200'),
array('HTTP/1.1 200 '), array('HTTP/1.1 200 ', '200'),
array('HTTP/1.1 200'), array('HTTP/1.1 200', '200'),
array('HTTP/1.1 200 OK', 'OK'), array('HTTP/1.1 200 OK', '200', 'OK'),
array('HTTP/1.1 404 Not Found', '404', 'Not Found'),
array('HTTP/1.1 404 Not Found', '404', 'Not Found'),
); );
} }
@ -1775,7 +1777,7 @@ class HttpSocketTest extends CakeTestCase {
* @dataProvider statusProvider * @dataProvider statusProvider
* @return void * @return void
*/ */
public function testResponseStatusParsing($status, $msg = '') { public function testResponseStatusParsing($status, $code, $msg = '') {
$this->Socket->connected = true; $this->Socket->connected = true;
$serverResponse = $status . "\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n\r\n<h1>This is a test!</h1>"; $serverResponse = $status . "\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse)); $this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
@ -1785,7 +1787,7 @@ class HttpSocketTest extends CakeTestCase {
$this->assertInstanceOf('HttpSocketResponse', $response); $this->assertInstanceOf('HttpSocketResponse', $response);
$expected = array( $expected = array(
'http-version' => 'HTTP/1.1', 'http-version' => 'HTTP/1.1',
'code' => '200', 'code' => $code,
'reason-phrase' => $msg 'reason-phrase' => $msg
); );
$this->assertEquals($expected, $response['status']); $this->assertEquals($expected, $response['status']);