Skip to content

Require Host on 1.1 requests #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ private function parseRequest($headers)
}
}

if ($request->getProtocolVersion() === '1.1' && !$request->hasHeader('Host')) {
throw new \InvalidArgumentException('A client must send a host header field in all HTTP/1.1 request messages');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why \InvalidArgumentException?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter to parseRequest ($headers) is not a valid argument.

Also, all exceptions in this method are InvalidArgumentExceptions so I was just keeping the format.

}

// Optional Host header value MUST be valid (host and optional port)
if ($request->hasHeader('Host')) {
$parts = parse_url('http://' . $request->getHeaderLine('Host'));
Expand Down
16 changes: 16 additions & 0 deletions tests/RequestHeaderParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ public function testInvalidAbsoluteFormSchemeEmitsError()
$this->assertSame('Invalid absolute-form request-target', $error->getMessage());
}

public function testHttp11RequestWithNoHostHeaderEmitsError()
{
$error = null;

$parser = new RequestHeaderParser();
$parser->on('headers', $this->expectCallableNever());
$parser->on('error', function ($message) use (&$error) {
$error = $message;
});

$parser->feed("GET / HTTP/1.1\r\n\r\n");

$this->assertInstanceOf('InvalidArgumentException', $error);
$this->assertSame('A client must send a host header field in all HTTP/1.1 request messages', $error->getMessage());
}

public function testInvalidAbsoluteFormWithFragmentEmitsError()
{
$error = null;
Expand Down
8 changes: 4 additions & 4 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ function ($data) use (&$buffer) {
$server->listen($this->socket);
$this->socket->emit('connection', array($this->connection));

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

$this->assertEquals("HTTP/1.1 200 OK\r\nUpgrade: demo\r\nContent-Length: 3\r\nConnection: close\r\n\r\nfoo", $buffer);
Expand Down Expand Up @@ -919,7 +919,7 @@ function ($data) use (&$buffer) {
$server->listen($this->socket);
$this->socket->emit('connection', array($this->connection));

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

$this->assertEquals("HTTP/1.1 200 OK\r\nContent-Length: 3\r\nConnection: close\r\n\r\nfoo", $buffer);
Expand Down Expand Up @@ -949,7 +949,7 @@ function ($data) use (&$buffer) {
$server->listen($this->socket);
$this->socket->emit('connection', array($this->connection));

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

$this->assertEquals("HTTP/1.1 101 Switching Protocols\r\nUpgrade: demo\r\nConnection: upgrade\r\n\r\nfoo", $buffer);
Expand Down Expand Up @@ -979,7 +979,7 @@ function ($data) use (&$buffer) {
$server->listen($this->socket);
$this->socket->emit('connection', array($this->connection));

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

$stream->write('hello');
Expand Down