Skip to content

Commit 99cde91

Browse files
committed
Use ProduceEnd
1 parent f27a233 commit 99cde91

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,15 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
635635
// Run the application code for this request
636636
await application.ProcessRequestAsync(context);
637637

638+
// Trigger OnStarting if it hasn't been called yet and the app hasn't
639+
// already failed. If an OnStarting callback throws we can go through
640+
// our normal error handling in ProduceEnd.
641+
// https://github.com/aspnet/KestrelHttpServer/issues/43
642+
if (!HasResponseStarted && _applicationException == null && _onStarting?.Count > 0)
643+
{
644+
await FireOnStarting();
645+
}
646+
638647
if (!_connectionAborted && !VerifyResponseContentLength(out var lengthException))
639648
{
640649
ReportApplicationError(lengthException);
@@ -655,15 +664,6 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
655664

656665
KestrelEventSource.Log.RequestStop(this);
657666

658-
// Trigger OnStarting if it hasn't been called yet and the app hasn't
659-
// already failed. If an OnStarting callback throws we can go through
660-
// our normal error handling in ProduceEnd.
661-
// https://github.com/aspnet/KestrelHttpServer/issues/43
662-
if (!HasResponseStarted && _applicationException == null && _onStarting?.Count > 0)
663-
{
664-
await FireOnStarting();
665-
}
666-
667667
// At this point all user code that needs use to the request or response streams has completed.
668668
// Using these streams in the OnCompleted callback is not allowed.
669669
StopBodies();
@@ -942,7 +942,7 @@ public void ProduceContinue()
942942
}
943943
}
944944

945-
public Task InitializeResponseAsync(int firstWriteByteCount, bool appCompleted = false)
945+
public Task InitializeResponseAsync(int firstWriteByteCount)
946946
{
947947
var startingTask = FireOnStarting();
948948
// If return is Task.CompletedTask no awaiting is required
@@ -953,7 +953,7 @@ public Task InitializeResponseAsync(int firstWriteByteCount, bool appCompleted =
953953

954954
VerifyInitializeState(firstWriteByteCount);
955955

956-
ProduceStart(appCompleted: appCompleted);
956+
ProduceStart(appCompleted: false);
957957

958958
return Task.CompletedTask;
959959
}
@@ -1050,7 +1050,7 @@ protected Task ProduceEnd()
10501050
return WriteSuffix();
10511051
}
10521052

1053-
protected Task WriteSuffix()
1053+
private Task WriteSuffix()
10541054
{
10551055
if (HasResponseCompleted)
10561056
{

src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.FeatureCollection.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ async Task IHttpResponseCompletionFeature.CompleteAsync()
5959
// Finalize headers
6060
if (!HasResponseStarted)
6161
{
62-
if (!VerifyResponseContentLength(out var lengthException))
63-
{
64-
throw lengthException;
65-
}
66-
67-
await InitializeResponseAsync(0, appCompleted: true);
62+
await FireOnStarting();
6863
}
6964

7065
// Flush headers, body, trailers...
@@ -75,7 +70,7 @@ async Task IHttpResponseCompletionFeature.CompleteAsync()
7570
throw lengthException;
7671
}
7772

78-
await WriteSuffix();
73+
await ProduceEnd();
7974
}
8075
}
8176
}

src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2StreamTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3484,7 +3484,7 @@ await InitializeConnectionAsync(async context =>
34843484
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => completionFeature.CompleteAsync().DefaultTimeout());
34853485
Assert.Equal(CoreStrings.FormatTooFewBytesWritten(0, 25), ex.Message);
34863486

3487-
Assert.False(startingTcs.Task.IsCompleted); // OnStarting did not get called.
3487+
Assert.True(startingTcs.Task.IsCompletedSuccessfully);
34883488
Assert.False(context.Response.Headers.IsReadOnly);
34893489
Assert.False(context.Features.Get<IHttpResponseTrailersFeature>().Trailers.IsReadOnly);
34903490

0 commit comments

Comments
 (0)