Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom response body streaming of HTTP Responses without Transfer-Encoding or Content-Length set #59055

Open
1 task done
Danielku15 opened this issue Nov 19, 2024 · 0 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Comments

@Danielku15
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to do a plain HTTP response streaming without chunked HTTP encoding but using an own framing logic. Unfortunately Kestrel automatically switches to chunked encoding based on whether a custom Transfer-Encoding or Content-Length is set.

else if (!hasTransferEncoding && !responseHeaders.ContentLength.HasValue)
{
if ((appCompleted || !_canWriteResponseBody) && !_hasAdvanced) // Avoid setting contentLength of 0 if we wrote data before calling CreateResponseHeaders
{
if (CanAutoSetContentLengthZeroResponseHeader())
{
// Since the app has completed writing or cannot write to the response, we can safely set the Content-Length to 0.
responseHeaders.ContentLength = 0;
}
}
// Note for future reference: never change this to set _autoChunk to true on HTTP/1.0
// connections, even if we were to infer the client supports it because an HTTP/1.0 request
// was received that used chunked encoding. Sending a chunked response to an HTTP/1.0
// client would break compliance with RFC 7230 (section 3.3.1):
//
// A server MUST NOT send a response containing Transfer-Encoding unless the corresponding
// request indicates HTTP/1.1 (or later).
//
// This also covers HTTP/2, which forbids chunked encoding in RFC 7540 (section 8.1:
//
// The chunked transfer encoding defined in Section 4.1 of [RFC7230] MUST NOT be used in HTTP/2.
else if (_httpVersion == Http.HttpVersion.Http11)
{
_autoChunk = true;
responseHeaders.SetRawTransferEncoding("chunked", _bytesTransferEncodingChunked);
}
else
{
DisableKeepAlive(ConnectionEndReason.ResponseNoKeepAlive);
}
}

Setting the Transfer-Encoding to something custom (e.g. the outdated identity) breaks compatibliity with reverse proxies like Traeffik. Hence I want to be able to "stream" an endless (no Content-Length known) raw byte response without setting a Transfer-Encoding header.

Describe the solution you'd like

Kestrel should give some control on whether the automatic switch to chunked encoding is done or not allowing me to disable it. This could happen either via a new IFeature or as extension to an existing feature.

Additional context

While this automatic switching might be OK for most standard browser use-cases, it breaks any usecases where HTTP is used as foundation for alternative communication systems. The HTTP/1.1 spec mentions that Transfer-Encodings need to be registered in the HTTP Transfer Coding Registry
so it is better to not send anything than an wrong/unsupported value.

My main real-world usecase is:

  • Networked Transport of RTCM via Internet Protocol (NTRIP)
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

No branches or pull requests

1 participant