Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #580 from dshafik/allow-numeric-only-http-status-r…

…esponses

Allow for invalid "HTTP/1.1 <code>" responses
  • Loading branch information...
commit e1289b35641b43ffdc3c4e2db37ee5750952d48a 2 parents 57c9051 + 0e0f065
@nateabele nateabele authored
Showing with 44 additions and 3 deletions.
  1. +8 −2 net/http/Response.php
  2. +36 −1 tests/cases/net/http/ResponseTest.php
View
10 net/http/Response.php
@@ -198,13 +198,19 @@ protected function _parseMessage($body) {
if (array_filter($headers) == array()) {
return trim($body);
}
- preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)\s+(.*)/i', array_shift($headers), $match);
+ preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)(?:\s+(.*))?/i', array_shift($headers), $match);
$this->headers($headers);
if (!$match) {
return trim($body);
}
- list($line, $this->version, $code, $message) = $match;
+ list($line, $this->version, $code) = $match;
+ if (isset($this->_statuses[$code])) {
+ $message = $this->_statuses[$code];
+ }
+ if (isset($match[3])) {
+ $message = $match[3];
+ }
$this->status = compact('code', 'message') + $this->status;
$this->protocol = "HTTP/{$this->version}";
return $body;
View
37 tests/cases/net/http/ResponseTest.php
@@ -106,7 +106,7 @@ public function testConstructionWithBody() {
public function testParseMessage() {
$message = join("\r\n", array(
- 'HTTP/1.1 200 OK',
+ 'HTTP/1.1 404 Not Found',
'Header: Value',
'Connection: close',
'Content-Type: application/json;charset=iso-8859-1',
@@ -118,6 +118,9 @@ public function testParseMessage() {
$this->assertEqual($message, (string) $response);
$this->assertEqual('application/json', $response->type);
$this->assertEqual('ISO-8859-1', $response->encoding);
+ $this->assertEqual('404', $response->status['code']);
+ $this->assertEqual('Not Found', $response->status['message']);
+ $this->assertEqual('HTTP/1.1 404 Not Found', $response->status());
$body = 'Not a Message';
$expected = join("\r\n", array('HTTP/1.1 200 OK', '', '', 'Not a Message'));
@@ -307,6 +310,38 @@ public function testDigestParsing() {
$result = array_filter($response->digest());
$this->assertEqual($expected, $result);
}
+
+ public function testMalformedStatus() {
+ $expected = "HTTP/1.1 304 Not Modified";
+
+ $message = join("\r\n", array(
+ 'HTTP/1.1 304',
+ 'Header: Value',
+ 'Connection: close',
+ 'Content-Type: application/json;charset=iso-8859-1',
+ '',
+ 'Test!'
+ ));
+
+ $response = new Response(compact('message'));
+ $result = $response->status();
+ $this->assertEqual($expected, $result);
+
+ $expected = "HTTP/1.1 500 Internal Server Error";
+
+ $message = join("\r\n", array(
+ 'HTTP/1.1 500',
+ 'Header: Value',
+ 'Connection: close',
+ 'Content-Type: application/json;charset=iso-8859-1',
+ '',
+ 'Test!'
+ ));
+
+ $response = new Response(compact('message'));
+ $result = $response->status();
+ $this->assertEqual($expected, $result);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.