Skip to content

Commit

Permalink
Upgrade to amphp/hpack ^3
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Dec 12, 2019
1 parent 5382d0e commit 638a77e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"php": ">=7.2",
"amphp/amp": "^2.4",
"amphp/byte-stream": "^1.6",
"amphp/hpack": "^2",
"amphp/hpack": "^3",
"amphp/http": "^1.5",
"amphp/socket": "^1",
"amphp/sync": "^1.3",
Expand All @@ -42,7 +42,7 @@
"amphp/phpunit-util": "^1.1",
"amphp/php-cs-fixer-config": "dev-master",
"phpunit/phpunit": "^7 || ^8",
"amphp/http-server": "^2-rc3"
"amphp/http-server": "dev-master"
},
"suggest": {
"ext-zlib": "*",
Expand Down
18 changes: 12 additions & 6 deletions src/Connection/Internal/Http2ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1411,12 +1411,18 @@ private function generateHeaders(Request $request): \Generator
$authority .= ':' . $port;
}

$headers = \array_merge([
":authority" => [$authority],
":path" => [$path],
":scheme" => [$uri->getScheme()],
":method" => [$request->getMethod()],
], $request->getHeaders());
$headers = [
[":authority", $authority],
[":path", $path],
[":scheme", $uri->getScheme()],
[":method", $request->getMethod()],
];

foreach ($request->getHeaders() as $field => $values) {
foreach ($values as $value) {
$headers[] = [$field, $value];
}
}

return $headers;
}
Expand Down
60 changes: 14 additions & 46 deletions test/Connection/Http2ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,6 @@ public static function packFrame(string $data, int $type, int $flags, int $strea
return \substr(\pack("NccN", \strlen($data), $type, $flags, $stream), 1) . $data;
}

public static function packHeader(
array $headers,
bool $continue = false,
int $stream = 1,
int $split = \PHP_INT_MAX
): string {
$data = "";
$hpack = new HPack;
$headers = $hpack->encode($headers);
$all = \str_split($headers, $split);
if ($split !== PHP_INT_MAX) {
$flag = Http2Parser::PADDED;
$len = 1;
$all[0] = \chr($len) . $all[0] . \str_repeat("\0", $len);
} else {
$flag = 0;
}

$end = \array_pop($all);
$type = Http2Parser::HEADERS;

foreach ($all as $frame) {
$data .= self::packFrame($frame, $type, $flag, $stream);
$type = Http2Parser::CONTINUATION;
$flag = 0;
}

$flags = ($continue ? $flag : Http2Parser::END_STREAM | $flag) | Http2Parser::END_HEADERS;

return $data . self::packFrame($end, $type, $flags, $stream);
}

public function test100Continue(): \Generator
{
$hpack = new HPack;
Expand All @@ -72,13 +40,13 @@ public function test100Continue(): \Generator
$stream = yield $connection->getStream($request);

$server->write(self::packFrame($hpack->encode([
":status" => Status::CONTINUE,
"date" => [formatDateHeader()],
[":status", Status::CONTINUE],
["date", formatDateHeader()],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS, 1));

$server->write(self::packFrame($hpack->encode([
":status" => Status::NO_CONTENT,
"date" => [formatDateHeader()],
[":status", Status::NO_CONTENT],
["date", formatDateHeader()],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS | Http2Parser::END_STREAM, 1));

/** @var Response $response */
Expand All @@ -105,8 +73,8 @@ public function testSwitchingProtocols(): \Generator
$stream = yield $connection->getStream($request);

$server->write(self::packFrame($hpack->encode([
":status" => Status::SWITCHING_PROTOCOLS,
"date" => [formatDateHeader()],
[":status", Status::SWITCHING_PROTOCOLS],
["date", formatDateHeader()],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS, 1));

$this->expectException(Http2ConnectionException::class);
Expand Down Expand Up @@ -136,10 +104,10 @@ public function testTrailers(): \Generator
yield delay(100);

$server->write(self::packFrame($hpack->encode([
":status" => Status::OK,
"content-length" => ["4"],
"trailers" => ["Foo"],
"date" => [formatDateHeader()],
[":status", Status::OK],
["content-length", "4"],
["trailers", "Foo"],
["date", formatDateHeader()],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS, 1));

yield delay(100);
Expand All @@ -149,7 +117,7 @@ public function testTrailers(): \Generator
yield delay(100);

$server->write(self::packFrame($hpack->encode([
"foo" => ['bar'],
["foo", 'bar'],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS | Http2Parser::END_STREAM, 1));
});

Expand Down Expand Up @@ -182,9 +150,9 @@ public function testTrailersWithoutTrailers(): \Generator
$stream = yield $connection->getStream($request);

$server->write(self::packFrame($hpack->encode([
":status" => Status::OK,
"content-length" => ["4"],
"date" => [formatDateHeader()],
[":status", Status::OK],
["content-length", "4"],
["date", formatDateHeader()],
]), Http2Parser::HEADERS, Http2Parser::END_HEADERS, 1));

$server->write(self::packFrame('test', Http2Parser::DATA, Http2Parser::END_STREAM, 1));
Expand Down

0 comments on commit 638a77e

Please sign in to comment.