Skip to content

Commit

Permalink
Ensure response can be buffered if already buffered (#483)
Browse files Browse the repository at this point in the history
There are times where buffering may be called but it is already buffering and it would throw. Now, if it is already buffering, it will continue to buffer and not throw.
  • Loading branch information
twsouthwick authored Mar 27, 2024
1 parent 6af1a41 commit 4cea060
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ void IHttpResponseBodyFeature.DisableBuffering()

void IHttpResponseBufferingFeature.EnableBuffering(int memoryThreshold, long? bufferLimit)
{
if (_state == StreamState.NotStarted)
if (_state == StreamState.Buffering)
{
return;
}
else if (_state == StreamState.NotStarted)
{
Debug.Assert(_bufferedStream is null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -82,6 +84,24 @@ public async Task InputStreamWithMetadata()
Assert.Equal(ContentValue, result);
}

[Fact]
public async Task BufferMultipleTimes()
{
// Act
var result = await RunAsync(ContentValue, async context =>
{
var feature = context.AsAspNetCore().Features.GetRequired<IHttpRequestInputStreamFeature>();
await feature.BufferInputStreamAsync(default);
await feature.BufferInputStreamAsync(default);
context.Request.InputStream.CopyTo(context.Response.OutputStream);
});

// Assert
Assert.Equal(ContentValue, result);
}

[Fact]
public async Task InputStreamCanSeek()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -212,6 +214,25 @@ public async Task MultipleClearContent()
Assert.Equal("part4", result);
}

[Fact]
public async Task BufferMultipleTimes()
{
const string Result = "text";

// Act
var result = await RunAsync(context =>
{
var feature = context.AsAspNetCore().Features.GetRequired<IHttpResponseBufferingFeature>();
feature.EnableBuffering(1024, default);
feature.EnableBuffering(1024, default);
context.Response.Write(Result);
});

Assert.Equal(Result, result);
}

[Fact]
public async Task BufferOutputIsNotEnabled()
{
Expand Down

0 comments on commit 4cea060

Please sign in to comment.