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

[release/6.0] Support zero-byte reads on raw response streams in SocketsHttpHandler #72804

Merged
merged 2 commits into from
Aug 11, 2022
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 @@ -1749,7 +1749,7 @@ private ValueTask<int> ReadBufferedAsync(Memory<byte> destination)
// If the caller provided buffer, and thus the amount of data desired to be read,
// is larger than the internal buffer, there's no point going through the internal
// buffer, so just do an unbuffered read.
return destination.Length >= _readBuffer.Length ?
return destination.Length == 0 || destination.Length >= _readBuffer.Length ?
ReadAsync(destination) :
ReadBufferedAsyncCore(destination);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);

HttpConnection? connection = _connection;
if (connection == null || buffer.Length == 0)
if (connection == null)
{
// Response body fully consumed or the caller didn't ask for any data
// Response body fully consumed
return 0;
}

Expand All @@ -74,7 +74,7 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
}
}

if (bytesRead == 0)
if (bytesRead == 0 && buffer.Length != 0)
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
{
// A cancellation request may have caused the EOF.
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,39 @@ await server.AcceptConnectionAsync(async (LoopbackServer.Connection connection)
}
});
}

[Fact]
public async Task UpgradeConnection_ResponseStreamSupportsZeroByteReads()
{
var zeroByteReadIssued = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
using HttpClient client = CreateHttpClient();

using HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
using Stream responseStream = await response.Content.ReadAsStreamAsync();

ValueTask<int> zeroByteReadTask = responseStream.ReadAsync(Memory<byte>.Empty);

Assert.False(zeroByteReadTask.IsCompleted);
await Task.Delay(10);
Assert.False(zeroByteReadTask.IsCompleted);

zeroByteReadIssued.SetResult();

Assert.Equal(0, await zeroByteReadTask);
Assert.Equal(1, await responseStream.ReadAsync(new byte[2]));
},
server => server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestHeaderAndSendCustomResponseAsync($"HTTP/1.1 101 Switching Protocols\r\nDate: {DateTimeOffset.UtcNow:R}\r\n\r\n");

await zeroByteReadIssued.Task;

await connection.Stream.WriteAsync(new byte[] { (byte)'A' });
}));
}
}

[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
Expand Down