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
Add HeaderEncodingSelector to MultipartContent #39169
Conversation
Tagging subscribers to this area: @dotnet/ncl |
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs
Outdated
Show resolved
Hide resolved
} | ||
} | ||
|
||
private static void WriteLatin1ToStream(Stream stream, string content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally, we used HttpRuleParser.DefaultHttpEncoding
, now we're using hardcoded Latin1
. Is there a reason for the change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that here because the method explicitly calls for Latin1 in the name.
I don't believe we would ever change HttpRuleParser.DefaultHttpEncoding
to something else due to compat, so I can use it here if we prefer it for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I'm asking 😄 Why even introduce WriteLatin1ToStream
?
I'm aware that the default HTTP encoding will hardly ever change, but the constant exists in our code, probably for a reason, and it's used in other places as well (FormUrlEncodedContent
, HeaderDescriptor
, HttpConnection
).
Btw, the code looks the same as in WriteToStream
hot path except the expected array length (IsSingleByte
on encoding could help with better stackalloc allocation)
@@ -219,12 +220,17 @@ private protected async Task SerializeToStreamAsyncCore(Stream stream, Transport | |||
await EncodeStringToStreamAsync(stream, "--" + _boundary + CrLf, cancellationToken).ConfigureAwait(false); | |||
|
|||
// Write each nested content. | |||
var output = new StringBuilder(); | |||
var output = new MemoryStream(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we write directly into stream to prevent copying?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can at the cost of doubling the SerializeHeaders logic
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm once comments addressed.
{ | ||
Debug.Assert(ReferenceEquals(Encoding.Latin1, HttpRuleParser.DefaultHttpEncoding)); | ||
|
||
WriteToStream(stream, content, HttpRuleParser.DefaultHttpEncoding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we use this exact line instead of WriteLatin1ToStream
? Is this helper necessary if it's just a one-liner?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed it to just be an overload of WriteToStream
private static void WriteToStream(Stream stream, string content) =>
WriteToStream(stream, content, HttpRuleParser.DefaultHttpEncoding);
So that code like this doesn't have to be too verbose in passing in the encoding every time
WriteToStream(stream, CrLf + "--"); // const strings
WriteToStream(stream, _boundary);
WriteToStream(stream, CrLf);
6d27bd2
to
edc80ba
Compare
@ManickaP Could you please take another look at the changes here? Changes since the last time:
|
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Http/tests/UnitTests/Headers/MultipartContentTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
* Add HeaderEncodingSelector to MultipartContent * Test cleanup * Avoid WriteLatin1 logic duplication * Move to common HeaderEncodingSelector<TContext> * Fix indentation
Contributes to #38711
The serialization impl could be optimized to avoid allocations, but I tried to keep the change limited to exposing the API.
Blocked on #39468 atm for the
HeaderEncodingSelector<TContext>
API