Skip to content

Commit

Permalink
Change "raw" to "pairs"
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Mar 26, 2023
1 parent 90d50ad commit fa2e6f1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
14 changes: 7 additions & 7 deletions src/Http1/Rfc7230.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @link https://tools.ietf.org/html/rfc2616
* @link https://tools.ietf.org/html/rfc5234
*
* @psalm-import-type RawHeaderType from HttpMessage
* @psalm-import-type HeaderPairsType from HttpMessage
* @psalm-import-type HeaderMapType from HttpMessage
*/
final class Rfc7230
Expand Down Expand Up @@ -64,11 +64,11 @@ public static function parseHeaders(string $rawHeaders): array
*
* Allows empty header values, as HTTP/1.0 allows that.
*
* @return RawHeaderType List of [field, value] header pairs.
* @return HeaderPairsType List of [field, value] header pairs.
*
* @throws InvalidHeaderException If invalid headers have been passed.
*/
public static function parseRawHeaders(string $rawHeaders): array
public static function parseHeaderPairs(string $rawHeaders): array
{
$matches = self::matchHeaders($rawHeaders);

Expand Down Expand Up @@ -144,7 +144,7 @@ public static function formatHeaders(array $headers): string
}
}

return self::formatRawHeaders($headerList);
return self::formatHeaderPairs($headerList);
}

/**
Expand All @@ -153,14 +153,14 @@ public static function formatHeaders(array $headers): string
* Headers are always validated syntactically. This protects against response splitting and header injection
* attacks.
*
* @param RawHeaderType $headers List of headers in [field, value] format as returned by
* {@see HttpMessage::getRawHeaders()}.
* @param HeaderPairsType $headers List of headers in [field, value] format as returned by
* {@see HttpMessage::getHeaderPairs()}.
*
* @return string Formatted headers.
*
* @throws InvalidHeaderException If header names or values are invalid.
*/
public static function formatRawHeaders(array $headers): string
public static function formatHeaderPairs(array $headers): string
{
$lines = \count($headers);
$bytes = \sprintf(
Expand Down
6 changes: 3 additions & 3 deletions src/HttpMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Base class for HTTP request and response messages.
*
* @psalm-type RawHeaderType = list<array{non-empty-string, string}>
* @psalm-type HeaderPairsType = list<array{non-empty-string, string}>
* @psalm-type HeaderParamValueType = string|array<string>
* @psalm-type HeaderParamArrayType = array<non-empty-string, HeaderParamValueType>
* @psalm-type HeaderMapType = array<non-empty-string, list<string>>
Expand All @@ -34,9 +34,9 @@ public function getHeaders(): array
/**
* Returns the headers as list of [field, name] pairs in the original casing provided by the application or server.
*
* @return RawHeaderType
* @return HeaderPairsType
*/
final public function getRawHeaders(): array
final public function getHeaderPairs(): array
{
$headers = [];

Expand Down
12 changes: 6 additions & 6 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Http\Message\UriInterface as PsrUri;

/**
* @psalm-type RawQueryType = list<array{string, string|null}>
* @psalm-type QueryPairsType = list<array{string, string|null}>
* @psalm-type QueryValueType = string|array<string|null>|null
* @psalm-type QueryArrayType = array<string, QueryValueType>
* @psalm-type QueryMapType = array<string, list<string|null>>
Expand All @@ -16,7 +16,7 @@ abstract class HttpRequest extends HttpMessage
/** @var QueryMapType|null */
private ?array $queryMap = null;

/** @var RawQueryType|null */
/** @var QueryPairsType|null */
private ?array $queryPairs = null;

/**
Expand Down Expand Up @@ -101,12 +101,12 @@ public function getQueryParameters(): array
}

/**
* @return RawQueryType
* @return QueryPairsType
* @psalm-suppress PropertyTypeCoercion
*/
public function getRawQueryParameters(): array
public function getQueryParameterPairs(): array
{
/** @var RawQueryType */
/** @var QueryPairsType */
return $this->queryPairs ??= match ($queryString = $this->uri->getQuery()) {
'' => [],
default => QueryString::parse($queryString),
Expand Down Expand Up @@ -184,7 +184,7 @@ private function getInternalQueryParameters(): array
private function buildQueryFromUri(): array
{
$query = [];
foreach ($this->getRawQueryParameters() as [$key, $value]) {
foreach ($this->getQueryParameterPairs() as [$key, $value]) {
$query[$key][] = $value;
}

Expand Down
9 changes: 5 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ function formatDateHeader(?int $timestamp = null): string
}

/**
* Convert the output of {@see Rfc7230::parseRawHeaders()} or {@see HttpMessage::getRawHeaders()} into the structure
* Convert the output of {@see Rfc7230::parseHeaderPairs()} or {@see HttpMessage::getHeaderPairs()} into the structure
* returned by {@see Rfc7230::parseHeaders()} or {@see HttpMessage::getHeaders()}.
*
* @param list<array{non-empty-string, string}> $rawHeaders
* @param list<array{non-empty-string, string}> $pairs
*
* @return array<non-empty-string, list<string>>
*/
function convertRawHeadersToMap(array $rawHeaders): array
function convertHeaderPairsToMap(array $pairs): array
{
$headers = [];

foreach ($rawHeaders as $header) {
foreach ($pairs as $header) {
/** @psalm-suppress RedundantCondition */
\assert(
\count($header) === 2
Expand Down
14 changes: 7 additions & 7 deletions test/Http1/Rfc7230Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

use Amp\Http\InvalidHeaderException;
use PHPUnit\Framework\TestCase;
use function Amp\Http\convertRawHeadersToMap;
use function Amp\Http\convertHeaderPairsToMap;

class Rfc7230Test extends TestCase
{
/** @dataProvider provideValidHeaders */
public function testValidRawHeaderParsing(string $rawHeaders, array $expectedResult): void
public function testValidHeaderPairParsing(string $rawHeaders, array $expectedResult): void
{
$result = Rfc7230::parseRawHeaders($rawHeaders);
$result = Rfc7230::parseHeaderPairs($rawHeaders);
$this->assertSame($result, $expectedResult);
}

/** @dataProvider provideValidHeaders */
public function testValidHeaderParsing(string $rawHeaders, array $expectedResult): void
{
$result = Rfc7230::parseHeaders($rawHeaders);
$headers = convertRawHeadersToMap($expectedResult);
$headers = convertHeaderPairsToMap($expectedResult);
$this->assertSame($result, $headers);
}

/** @dataProvider provideValidHeaders */
public function testValidRawHeaderFormatting(string $rawHeaders /* ignored for this case */, array $expectedResult): void
public function testValidHeaderPairFormatting(string $rawHeaders /* ignored for this case */, array $expectedResult): void
{
$result = Rfc7230::parseRawHeaders(Rfc7230::formatRawHeaders($expectedResult));
$result = Rfc7230::parseHeaderPairs(Rfc7230::formatHeaderPairs($expectedResult));
$this->assertSame($result, $expectedResult);
}

Expand All @@ -40,7 +40,7 @@ public function testValidHeaderFormattingDifferentCasing(string $rawHeaders /* i
$headers[$name][] = $value;
}

$result = Rfc7230::parseRawHeaders(Rfc7230::formatHeaders($headers));
$result = Rfc7230::parseHeaderPairs(Rfc7230::formatHeaders($headers));
$this->assertSame($result, $expectedResult);
}

Expand Down
4 changes: 2 additions & 2 deletions test/HttpMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function removeHeader(string $name): void

class HttpMessageTest extends TestCase
{
public function testGetRawHeaders(): void
public function testGetHeaderPairs(): void
{
$message = new TestHttpMessage([
'X-FooBar' => 'bar',
Expand All @@ -60,7 +60,7 @@ public function testGetRawHeaders(): void
['x-fooBar', 'baz'],
['x-rePlace', 'yes'],
['x-again', 'hello']
], $message->getRawHeaders());
], $message->getHeaderPairs());
}

public function testGetHeader(): void
Expand Down

0 comments on commit fa2e6f1

Please sign in to comment.