public static function responseInitFilter(InternalRequest $ireq)
{
$headers = yield;
$status = $headers[":status"];
$options = $ireq->client->options;
if ($options->sendServerToken) {
$headers["server"] = [SERVER_TOKEN];
}
if (!empty($headers[":aerys-push"])) {
foreach ($headers[":aerys-push"] as $url => $pushHeaders) {
$headers["link"][] = "<{$url}>; rel=preload";
}
unset($headers[":aerys-push"]);
}
$contentLength = $headers[":aerys-entity-length"];
unset($headers[":aerys-entity-length"]);
if ($contentLength === "@") {
$hasContent = false;
$shouldClose = $ireq->protocol === "1.0";
if ($status >= 200 && $status != 204 && $status != 304) {
$headers["content-length"] = ["0"];
}
} elseif ($contentLength !== "*") {
$hasContent = true;
$shouldClose = $ireq->protocol === "1.0";
$headers["content-length"] = [$contentLength];
unset($headers["transfer-encoding"]);
} elseif ($ireq->protocol === "1.1") {
$hasContent = true;
$shouldClose = false;
$headers["transfer-encoding"] = ["chunked"];
unset($headers["content-length"]);
} else {
$hasContent = true;
$shouldClose = true;
}
if ($hasContent) {
$type = $headers["content-type"][0] ?? $options->defaultContentType;
if (\stripos($type, "text/") === 0 && \stripos($type, "charset=") === false) {
$type .= "; charset={$options->defaultTextCharset}";
}
$headers["content-type"] = [$type];
}
$remainingKeepAlives = $ireq->client->remainingKeepAlives;
if ($shouldClose || $remainingKeepAlives <= 0) {
$headers["connection"] = ["close"];
} elseif ($remainingKeepAlives < PHP_INT_MAX) {
$keepAlive = "timeout={$options->keepAliveTimeout}, max={$remainingKeepAlives}";
$headers["keep-alive"] = [$keepAlive];
} else {
$keepAlive = "timeout={$options->keepAliveTimeout}";
$headers["keep-alive"] = [$keepAlive];
}
$headers["date"] = [$ireq->httpDate];
return $headers;
}