Skip to content

Commit

Permalink
Only support HTTP/1.1 and HTTP/1.0 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 18, 2017
1 parent 6ac0acb commit 531c466
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ $http->on('request', function (Request $request, Response $response) {

See also [`Request`](#request) and [`Response`](#response) for more details.

If a client sends an invalid request message, it will emit an `error` event,
send an HTTP error response to the client and close the connection:
The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
If a client sends an invalid request message or uses an invalid HTTP protocol
version, it will emit an `error` event, send an HTTP error response to the
client and close the connection:

```php
$http->on('error', function (Exception $e) {
Expand Down
12 changes: 10 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
*
* See also [`Request`](#request) and [`Response`](#response) for more details.
*
* If a client sends an invalid request message, it will emit an `error` event,
* send an HTTP error response to the client and close the connection:
* The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
* If a client sends an invalid request message or uses an invalid HTTP protocol
* version, it will emit an `error` event, send an HTTP error response to the
* client and close the connection:
*
* ```php
* $http->on('error', function (Exception $e) {
Expand Down Expand Up @@ -107,6 +109,12 @@ public function handleConnection(ConnectionInterface $conn)
/** @internal */
public function handleRequest(ConnectionInterface $conn, Request $request)
{
// only support HTTP/1.1 and HTTP/1.0 requests
if ($request->getProtocolVersion() !== '1.1' && $request->getProtocolVersion() !== '1.0') {
$this->emit('error', array(new \InvalidArgumentException('Received request with invalid protocol version')));
return $this->writeError($conn, 505);
}

$response = new Response($conn, $request->getProtocolVersion());
$response->on('close', array($request, 'close'));

Expand Down
32 changes: 32 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,38 @@ function ($data) use (&$buffer) {
$this->assertContains("\r\n\r\nbye", $buffer);
}

public function testRequestInvalidHttpProtocolVersionWillEmitErrorAndSendErrorResponse()
{
$error = null;
$server = new Server($this->socket);
$server->on('error', function ($message) use (&$error) {
$error = $message;
});

$buffer = '';

$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$this->socket->emit('connection', array($this->connection));

$data = "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n";
$this->connection->emit('data', array($data));

$this->assertInstanceOf('InvalidArgumentException', $error);

$this->assertContains("HTTP/1.1 505 HTTP Version Not Supported\r\n", $buffer);
$this->assertContains("\r\n\r\nError 505: HTTP Version Not Supported", $buffer);
}

public function testParserErrorEmitted()
{
$error = null;
Expand Down

0 comments on commit 531c466

Please sign in to comment.