Skip to content

Normalize de-chunked localhost proxy responses before rewriting them #18626

Closed

Description

@kevinushey

Summary

AsyncClient consumes HTTP chunk framing before returning a buffered response. handleLocalhostResponse() removes Transfer-Encoding and sets Content-Length in its ordinary response branch, but responses handled by an earlier branch bypass that normalization.

In particular, a response containing Location or Refresh is copied and written immediately. If the upstream response used final Transfer-Encoding: chunked, the copied response contains a decoded body that is still labeled as chunked. The Spark UI rewriting path can similarly retain Transfer-Encoding while setBody() adds Content-Length.

For an upstream chunked redirect, the downstream protocol mismatch is deterministic: the client receives decoded bytes under a Transfer-Encoding: chunked header and attempts to decode them again.

Suggested change

Normalize a buffered response once, before the redirect, Spark UI, and ordinary-response branches:

  • When parseTransferEncoding(response.headers()).chunkedIsFinal is true, remove Transfer-Encoding.
  • Set Content-Length to the decoded body size.
  • Use that normalized response for all subsequent header and body rewrites.

This would also allow removal of the lower chunked-only response branch.

Add coverage for a chunked localhost response containing Location or Refresh, and ideally for the Spark UI rewrite path as well.

Context

Follow-up from #18541.

Relevant code:

std::string refresh = response.headerValue(kRefresh);
if (!location.empty() || !refresh.empty())
{
// make a copy of the response to rewrite the headers into
http::Response redirectResponse;
redirectResponse.assign(response);
// handle Location
if (!location.empty())
{
rewriteLocalhostAddressHeader(kLocation,
ptrConnection->request(),
port,
baseAddress,
ipv6,
&redirectResponse);
}
// handle Refresh
if (!refresh.empty())
{
rewriteLocalhostAddressHeader(kRefresh,
ptrConnection->request(),
port,
baseAddress,
ipv6,
&redirectResponse);
}
// write the copy
ptrConnection->writeResponse(redirectResponse);
}
else
{
// fixup bad SparkUI URLs in responses (they use paths hard
// coded to the root "/" and we are proxying them behind
// a "/p/<port>/" URL)
if (isSparkUIResponse(response))
{
sendSparkUIResponse(response, ptrConnection);
}
else if (core::http::util::parseTransferEncoding(response.headers()).chunkedIsFinal)
{
// Even if the response from upstream is "Transfer-Encoding: chunked",
// our response to the client is no longer chunked; the AsyncClient
// parses and consumes the chunk lengths. Therefore, remove the
// Transfer-Encoding header and set the content length, to reflect
// that the body will come all at once.
//
// This must use the same parse AsyncClient used to decide to
// de-chunk (it once compared the raw field to "chunked" here): if
// AsyncClient de-chunked a body whose field read "Chunked", a
// string compare would leave the now-decoded body labelled as
// chunked on the way out, and the client would try to de-chunk it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions