Skip to content

Update HttpRequestMessageFactory to correctly set the Content-Length header #3768

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

Conversation

muhammad-othman
Copy link
Member

Description

This PR updates the .NET Standard implementation of the HttpRequestMessageFactory to correctly set the Content-Length header, particularly for non-seekable streams, by taking the current stream position into account.

The issue being addressed was originally fixed in #3631. However, due to the test coverage only verifying behavior when UseChunkEncoding = false, the regression was reintroduced when the position adjustment was moved behind a check that only applies to seekable streams.

This update ensures that the stream position is correctly accounted for when setting Content-Length, regardless of the stream's seekability.

Motivation and Context

#3629

Testing

  • DRY_RUN-05ce4689-5754-4c4b-afd9-b0119f943459.
  • Updated TestResetStreamPosition test to run with and without ChunkEncoding.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

Copy link

@niemyjski niemyjski left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for tracking this down.

@muhammad-othman muhammad-othman requested a review from normj April 23, 2025 17:28
{
position = contentStream.Position;
}
position = contentStream.Position;
Copy link
Member

Choose a reason for hiding this comment

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

nit: Not relying on the CanSeek is due to special circumstances of our ChunkedUploadWrapperStream. You might want to add a comment here so somebody in the future, like myself, don't suggest adding back the CanSeek check to avoid the control flow by exception.

@muhammad-othman muhammad-othman force-pushed the muhamoth/fix-HttpRequestMessageFactory-WriteToRequestBody branch from 1b217fd to 4b88ee3 Compare April 23, 2025 20:56
@dscpinheiro dscpinheiro merged commit cbe0551 into main-staging Apr 24, 2025
1 check passed
@dscpinheiro dscpinheiro deleted the muhamoth/fix-HttpRequestMessageFactory-WriteToRequestBody branch April 24, 2025 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants