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

TransferEncoding chunked testing only if it is last header value #105647

Closed

Conversation

rokonec
Copy link
Contributor

@rokonec rokonec commented Jul 29, 2024

Fixes #63053

Context

Transfer-Encodings are ordered and so the check should only succeed if "chunked" is the final Transfer-Encoding value
See #63053

Changes made

Test only final value.
During TransferEncodingChunked is set to true, it is ensured chunked will be last value, even when it was already present.

Testing

Few new unit tests.

Note

This could be a breaking change if server behave out of spec. Currently if server send Transfer-Encoding: chunked, gzip, but actually do gzip and then chunk, client would have handled it without error, with changes in this PR it will fail to receive content.

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also changing how we interpret response bodies that contain "chunked" as the non-last value.
By the spec, we should likely be falling back to reading until the connection is closed instead of using the content length value.


We rely on headers.TransferEncodingChunked to decide whether to apply chunking when writing request bodies (e.g. in WinHttpHandler or SocketsHttpHandler's HttpConnection.cs).

Instead of potentially generating garbage, should we tighten request validation to throw if a Transfer-Encoding header was set but chunked wasn't the last value?


Is this part in H3 correct?
https://datatracker.ietf.org/doc/html/rfc9114#section-4.1-11

// H3 does not support Transfer-Encoding: chunked.
if (request.HasHeaders && request.Headers.TransferEncodingChunked == true)
{
request.Headers.TransferEncodingChunked = false;
}

Should we unconditionally remove the header instead?


Our docs currently state:

Gets or sets a value that indicates if the Transfer-Encoding header for an HTTP request contains chunked.

This feels like it has a non-zero risk of breaking someone. Should we push it to .NET 10 just in case?

{
if (_parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked, lastValueOnly: false))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will now do an extra pass over the values in the common case where the value wasn't set yet.

Can we instead do something like

_parent.RemoveParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked);
_parent.AddParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked);

@@ -89,7 +89,7 @@ internal sealed class HttpGeneralHeaders
return true;
}

if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked))
if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked, lastValueOnly:true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked, lastValueOnly:true))
if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodingChunked, lastValueOnly: true))

@rokonec
Copy link
Contributor Author

rokonec commented Aug 5, 2024

I believe this PR shall be abandoned.
Reasoning: #63053 (comment)

@rokonec rokonec marked this pull request as draft August 5, 2024 15:08
@rokonec rokonec closed this Aug 12, 2024
@rokonec rokonec deleted the dev/rokonec/only-last-chuked-count-63053 branch August 12, 2024 18:52
@karelz karelz added this to the 9.0.0 milestone Sep 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SocketsHttpHandler does not check for Transfer-Encoding: chunked on response properly
4 participants