Skip to content

Correct ValidateStreamForReading(...) #390

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

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ private static HttpContent CreateHeaderFields(HttpHeaders source, HttpHeaders de
}
}

// If we have content headers then create an HttpContent for this Response
// If we have content headers then create an HttpContent for this request or response. Otherwise,
// provide a HttpContent instance to overwrite the null or EmptyContent value.
if (contentHeaders != null)
{
// Need to rewind the input stream to be at the position right after the HTTP header
Expand Down
11 changes: 10 additions & 1 deletion src/System.Net.Http.Formatting/HttpMessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,21 @@ private byte[] SerializeHeader()

private void ValidateStreamForReading(Stream stream)
{
// Stream is null case should be an extreme, incredibly unlikely corner case. Every HttpContent from
// the framework (see dotnet/runtime or .NET Framework reference source) provides a non-null Stream
// in the ReadAsStreamAsync task's return value. Likely need a poorly-designed derived HttpContent
// to hit this. Mostly ignoring the fact this message doesn't make much sense for the case.
if (stream is null || !stream.CanRead)
{
throw Error.NotSupported(Properties.Resources.NotSupported_UnreadableStream);
}

// If the content needs to be written to a target stream a 2nd time, then the stream must support
// seeking (e.g. a FileStream), otherwise the stream can't be copied a second time to a target
// stream (e.g. a NetworkStream).
if (_contentConsumed)
{
if (stream != null && stream.CanRead)
if (stream.CanSeek)
{
stream.Position = 0;
}
Expand Down
Loading