Skip to content

Commit

Permalink
Merge 998e544 into 466cd67
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed May 23, 2020
2 parents 466cd67 + 998e544 commit 82593f0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/Connection/DefaultConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,29 @@ public function create(
}

if ($tlsInfo->getApplicationLayerProtocol() === 'h2') {
$connection = new Http2Connection($socket);
yield $connection->initialize();
$http2Connection = new Http2Connection($socket);
yield $http2Connection->initialize();

foreach ($request->getEventListeners() as $eventListener) {
yield $eventListener->completeConnectionCreation($request);
}

return $connection;
return $http2Connection;
}
}

// Treat the presence of only HTTP/2 as prior knowledge, see https://http2.github.io/http2-spec/#known-http
if ($request->getProtocolVersions() === ['2']) {
$http2Connection = new Http2Connection($socket);
yield $http2Connection->initialize();

foreach ($request->getEventListeners() as $eventListener) {
yield $eventListener->completeConnectionCreation($request);
}

return $http2Connection;
}

if (!\array_intersect($request->getProtocolVersions(), ['1.0', '1.1'])) {
$socket->close();

Expand Down
4 changes: 3 additions & 1 deletion src/Connection/Internal/Http2ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,9 @@ private function shutdown(?int $lastId = null, ?HttpException $reason = null): P
if ($this->settings !== null) {
$settings = $this->settings;
$this->settings = null;
$settings->fail($reason ?? new UnprocessedRequestException(new SocketException("Connection closed")));

$message = "Connection closed before HTTP/2 settings could be received";
$settings->fail($reason ?? new UnprocessedRequestException(new SocketException($message)));
}

if ($this->streams) {
Expand Down
43 changes: 43 additions & 0 deletions test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Amp\CancelledException;
use Amp\Http\Client\Body\FileBody;
use Amp\Http\Client\Body\FormBody;
use Amp\Http\Client\Connection\UnprocessedRequestException;
use Amp\Http\Client\Interceptor\DecompressResponse;
use Amp\Http\Client\Interceptor\ModifyRequest;
use Amp\Http\Client\Interceptor\SetRequestHeaderIfUnset;
Expand Down Expand Up @@ -702,6 +703,48 @@ public function testConcurrentSlowNetworkInterceptor(): \Generator
$this->assertSame(1, $json2['http2']);
}

public function testHttp2UpgradeResponse(): \Generator
{
$request = new Request('http://nghttp2.org/');
$request->setHeader('connection', 'upgrade, http2-settings');
$request->setHeader('upgrade', 'h2c');
$request->setHeader('http2-settings', 'AAMAAABkAARAAAAAAAIAAAAA');

$this->expectException(HttpException::class);
$this->expectExceptionMessage('CONNECT or upgrade request made without upgrade handler callback');

/** @var Response $response */
yield $this->executeRequest($request);
}

public function testHttp2PriorKnowledge(): \Generator
{
$request = new Request('http://nghttp2.org/');
$request->setProtocolVersions(['2']);

/** @var Response $response */
$response = yield $this->executeRequest($request);

$this->assertSame(200, $response->getStatus());
$this->assertSame('2', $response->getProtocolVersion());
}

public function testHttp2PriorKnowledgeUnsupported(): \Generator
{
$request = new Request('http://github.com/');
$request->setProtocolVersions(['2']);

$this->expectException(SocketException::class);
$this->expectExceptionMessage('Connection closed before HTTP/2 settings could be received');

try {
/** @var Response $response */
yield $this->executeRequest($request);
} catch (UnprocessedRequestException $e) {
throw $e->getPrevious();
}
}

protected function setUp(): void
{
parent::setUp();
Expand Down

0 comments on commit 82593f0

Please sign in to comment.