Skip to content

Commit 29abc50

Browse files
committed
Fix getting content-length header
1 parent a7858e9 commit 29abc50

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/Driver/Http1Driver.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ private function send(?Future $lastWrite, Response $response, ?Request $request
863863
$status = $response->getStatus();
864864
$reason = $response->getReason();
865865

866-
[$headers, $shouldClose] = $this->filter($response, $request, $protocol);
866+
[$headers, $need, $shouldClose] = $this->filter($response, $request, $protocol);
867867

868868
$trailers = $response->getTrailers();
869869

@@ -882,8 +882,8 @@ private function send(?Future $lastWrite, Response $response, ?Request $request
882882

883883
$chunk = "HTTP/$protocol $status $reason\r\n" . Rfc7230::formatHeaders($headers) . "\r\n";
884884

885-
$need = $headers["content-length"] ?? null;
886885
$wrote = 0;
886+
887887
try {
888888
$this->writableStream->write($chunk);
889889

@@ -939,7 +939,7 @@ private function send(?Future $lastWrite, Response $response, ?Request $request
939939
return; // Client will be closed in finally.
940940
} finally {
941941
/** @psalm-suppress TypeDoesNotContainType */
942-
if ($chunk !== null || ($need !== null && $wrote !== (int) $need)) {
942+
if ($chunk !== null || ($need !== null && $wrote !== $need)) {
943943
$this->client->close();
944944
}
945945
}
@@ -950,8 +950,8 @@ private function send(?Future $lastWrite, Response $response, ?Request $request
950950
*
951951
* @param string $protocol Request protocol.
952952
*
953-
* @return array{array<non-empty-string, list<string>>, bool} Response headers to be written and flag if the
954-
* connection should be closed.
953+
* @return array{array<non-empty-string, list<string>>, int|null, bool} Response headers to be written,
954+
* content length, and if the connection should be closed.
955955
*/
956956
private function filter(Response $response, ?Request $request, string $protocol = "1.0"): array
957957
{
@@ -963,7 +963,7 @@ private function filter(Response $response, ?Request $request, string $protocol
963963

964964
if ($response->getStatus() < HttpStatus::OK) {
965965
unset($headers['content-length']); // 1xx responses do not have a body.
966-
return [$headers, false];
966+
return [$headers, null, false];
967967
}
968968

969969
foreach ($response->getPushes() as $push) {
@@ -974,6 +974,10 @@ private function filter(Response $response, ?Request $request, string $protocol
974974
$responseConnectionHeaders = $headers["connection"] ?? [];
975975

976976
$contentLength = $headers["content-length"][0] ?? null;
977+
if ($contentLength !== null) {
978+
$contentLength = (int) $contentLength;
979+
}
980+
977981
$shouldClose = $request === null
978982
|| \array_reduce($requestConnectionHeaders, $closeReduce, false)
979983
|| \array_reduce($responseConnectionHeaders, $closeReduce, false)
@@ -996,7 +1000,7 @@ private function filter(Response $response, ?Request $request, string $protocol
9961000

9971001
$headers["date"] = [formatDateHeader()];
9981002

999-
return [$headers, $shouldClose];
1003+
return [$headers, $contentLength, $shouldClose];
10001004
}
10011005

10021006
/**

0 commit comments

Comments
 (0)