Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit a35a00f

Browse files
author
Cesar Blum Silveira
committed
If request aborted and no response sent, log status code '0' when disposing context.
1 parent 246f306 commit a35a00f

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/Frame.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7-
using System.Globalization;
87
using System.IO;
98
using System.Linq;
109
using System.Net;

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/FrameOfT.cs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,33 @@ public override async Task RequestProcessingAsync()
9292
var context = _application.CreateContext(this);
9393
try
9494
{
95-
await _application.ProcessRequestAsync(context).ConfigureAwait(false);
96-
VerifyResponseContentLength();
97-
}
98-
catch (Exception ex)
99-
{
100-
ReportApplicationError(ex);
101-
}
102-
finally
103-
{
104-
// Trigger OnStarting if it hasn't been called yet and the app hasn't
105-
// already failed. If an OnStarting callback throws we can go through
106-
// our normal error handling in ProduceEnd.
107-
// https://github.com/aspnet/KestrelHttpServer/issues/43
108-
if (!HasResponseStarted && _applicationException == null && _onStarting != null)
95+
try
10996
{
110-
await FireOnStarting();
97+
await _application.ProcessRequestAsync(context).ConfigureAwait(false);
98+
VerifyResponseContentLength();
11199
}
112-
113-
PauseStreams();
114-
115-
if (_onCompleted != null)
100+
catch (Exception ex)
116101
{
117-
await FireOnCompleted();
102+
ReportApplicationError(ex);
118103
}
119-
120-
try
104+
finally
121105
{
106+
// Trigger OnStarting if it hasn't been called yet and the app hasn't
107+
// already failed. If an OnStarting callback throws we can go through
108+
// our normal error handling in ProduceEnd.
109+
// https://github.com/aspnet/KestrelHttpServer/issues/43
110+
if (!HasResponseStarted && _applicationException == null && _onStarting != null)
111+
{
112+
await FireOnStarting();
113+
}
114+
115+
PauseStreams();
116+
117+
if (_onCompleted != null)
118+
{
119+
await FireOnCompleted();
120+
}
121+
122122
// If _requestAbort is set, the connection has already been closed.
123123
if (Volatile.Read(ref _requestAborted) == 0)
124124
{
@@ -129,28 +129,34 @@ public override async Task RequestProcessingAsync()
129129
// Finish reading the request body in case the app did not.
130130
await messageBody.Consume();
131131
}
132-
}
133132

134-
// ProduceEnd() must be called before _application.DisposeContext(), to ensure
135-
// HttpContext.Response.StatusCode is correctly set when
136-
// IHttpContextFactory.Dispose(HttpContext) is called.
137-
await ProduceEnd();
138-
}
139-
finally
140-
{
141-
_application.DisposeContext(context, _applicationException);
133+
// ProduceEnd() must be called before _application.DisposeContext(), to ensure
134+
// HttpContext.Response.StatusCode is correctly set when
135+
// IHttpContextFactory.Dispose(HttpContext) is called.
136+
await ProduceEnd();
137+
}
138+
else if (!HasResponseStarted)
139+
{
140+
// If the request was aborted and no response was sent, there's no
141+
// meaningful status code to log.
142+
StatusCode = 0;
143+
}
142144
}
143145
}
144-
145-
StopStreams();
146-
147-
if (!_keepAlive)
146+
finally
148147
{
149-
// End the connection for non keep alive as data incoming may have been thrown off
150-
return;
148+
_application.DisposeContext(context, _applicationException);
151149
}
152150
}
153151

152+
StopStreams();
153+
154+
if (!_keepAlive)
155+
{
156+
// End the connection for non keep alive as data incoming may have been thrown off
157+
return;
158+
}
159+
154160
// Don't reset frame state if we're exiting the loop. This avoids losing request rejection
155161
// information (for 4xx response), and prevents ObjectDisposedException on HTTPS (ODEs
156162
// will be thrown if PrepareRequest is not null and references objects disposed on connection

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/ResponseTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public Task ResponseStatusCodeSetBeforeHttpContextDisposeRequestAborted()
210210
return TaskCache.CompletedTask;
211211
},
212212
expectedClientStatusCode: null,
213-
expectedServerStatusCode: HttpStatusCode.OK);
213+
expectedServerStatusCode: 0);
214214
}
215215

216216
[Fact]
@@ -223,7 +223,7 @@ public Task ResponseStatusCodeSetBeforeHttpContextDisposeRequestAbortedAppExcept
223223
throw new Exception();
224224
},
225225
expectedClientStatusCode: null,
226-
expectedServerStatusCode: HttpStatusCode.InternalServerError);
226+
expectedServerStatusCode: 0);
227227
}
228228

229229
[Fact]

0 commit comments

Comments
 (0)