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

Commit 0b9baf4

Browse files
committed
Fix Exceptions
1 parent cb1a40e commit 0b9baf4

File tree

7 files changed

+60
-46
lines changed

7 files changed

+60
-46
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/BadHttpResponseException.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ private BadHttpResponseException(string message)
1414

1515
}
1616

17-
internal static BadHttpResponseException GetException(ResponseRejectionReasons reason)
17+
internal static BadHttpResponseException GetException(ResponseRejectionReason reason)
1818
{
1919
BadHttpResponseException ex;
2020
switch (reason)
2121
{
22-
case ResponseRejectionReasons.HeadersReadonlyResponseStarted:
22+
case ResponseRejectionReason.ResponseStartedHeadersReadonly:
2323
ex = new BadHttpResponseException("Headers are read-only, response has already started.");
2424
break;
25+
case ResponseRejectionReason.ResponseStartedOnstarting:
26+
ex = new BadHttpResponseException("OnStarted cannot be executed, response has already started.");
27+
break;
2528
default:
2629
ex = new BadHttpResponseException("Bad response.");
2730
break;
@@ -30,27 +33,39 @@ internal static BadHttpResponseException GetException(ResponseRejectionReasons r
3033
return ex;
3134
}
3235

33-
internal static BadHttpResponseException GetException(ResponseRejectionReasons reason, string value)
36+
internal static BadHttpResponseException GetException(ResponseRejectionReason reason, ResponseRejectionParameter value)
3437
{
3538
BadHttpResponseException ex;
3639
switch (reason)
3740
{
38-
case ResponseRejectionReasons.ValueCannotBeSetResponseStarted:
39-
ex = new BadHttpResponseException(value + " cannot be set, response had already started.");
41+
case ResponseRejectionReason.ResponseStartedValueCannotBeSet:
42+
ex = new BadHttpResponseException(value.ToString() + " cannot be set, response had already started.");
43+
break;
44+
default:
45+
ex = new BadHttpResponseException("Bad response.");
4046
break;
41-
case ResponseRejectionReasons.TransferEncodingSetOnNonBodyResponse:
42-
ex = new BadHttpResponseException($"Transfer-Encoding set on a {value} non-body request.");
47+
}
48+
49+
return ex;
50+
}
51+
52+
internal static BadHttpResponseException GetException(ResponseRejectionReason reason, int statusCode)
53+
{
54+
BadHttpResponseException ex;
55+
switch (reason)
56+
{
57+
case ResponseRejectionReason.NonBodyResponseTransferEncodingSet:
58+
ex = new BadHttpResponseException($"Transfer-Encoding set on a {statusCode} non-body request.");
4359
break;
44-
case ResponseRejectionReasons.WriteToNonBodyResponse:
45-
ex = new BadHttpResponseException($"Write to non-body {value} response.");
60+
case ResponseRejectionReason.NonBodyResponseWriteToBody:
61+
ex = new BadHttpResponseException($"Write to non-body {statusCode} response.");
4662
break;
4763
default:
4864
ex = new BadHttpResponseException("Bad response.");
4965
break;
5066
}
5167

5268
return ex;
53-
5469
}
5570
}
5671
}

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public int StatusCode
132132
{
133133
if (HasResponseStarted)
134134
{
135-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.ValueCannotBeSetResponseStarted, nameof(StatusCode));
135+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedValueCannotBeSet, ResponseRejectionParameter.StatusCode);
136136
}
137137

138138
_statusCode = value;
@@ -150,7 +150,7 @@ public string ReasonPhrase
150150
{
151151
if (HasResponseStarted)
152152
{
153-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.ValueCannotBeSetResponseStarted, nameof(ReasonPhrase));
153+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedValueCannotBeSet, ResponseRejectionParameter.ReasonPhrase);
154154
}
155155

156156
_reasonPhrase = value;
@@ -385,7 +385,7 @@ public void OnStarting(Func<object, Task> callback, object state)
385385
{
386386
if (HasResponseStarted)
387387
{
388-
ThrowResponseAlreadyStartedException(nameof(OnStarting));
388+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedOnstarting);
389389
}
390390

391391
if (_onStarting == null)
@@ -663,12 +663,12 @@ protected Task ProduceEnd()
663663
{
664664
_keepAlive = false;
665665
// 400 Bad Request
666-
ErrorResetHeadersToDefaults(statusCode: 400);
666+
PrepareErrorResponse(statusCode: 400);
667667
}
668668
else
669669
{
670670
// 500 Internal Server Error
671-
ErrorResetHeadersToDefaults(statusCode: 500);
671+
PrepareErrorResponse(statusCode: 500);
672672
}
673673
}
674674

@@ -1238,7 +1238,7 @@ public bool StatusCanHaveBody(int statusCode)
12381238

12391239
private void RejectNonBodyTransferEncodingResponse(bool appCompleted)
12401240
{
1241-
var ex = BadHttpResponseException.GetException(ResponseRejectionReasons.TransferEncodingSetOnNonBodyResponse, StatusCode.ToString());
1241+
var ex = BadHttpResponseException.GetException(ResponseRejectionReason.NonBodyResponseTransferEncodingSet, StatusCode);
12421242
if (!appCompleted)
12431243
{
12441244
// Back out of header creation surface exeception in user code
@@ -1249,11 +1249,11 @@ private void RejectNonBodyTransferEncodingResponse(bool appCompleted)
12491249
{
12501250
ReportApplicationError(ex);
12511251
// 500 Internal Server Error
1252-
ErrorResetHeadersToDefaults(statusCode: 500);
1252+
PrepareErrorResponse(statusCode: 500);
12531253
}
12541254
}
12551255

1256-
private void ErrorResetHeadersToDefaults(int statusCode)
1256+
private void PrepareErrorResponse(int statusCode)
12571257
{
12581258
StatusCode = statusCode;
12591259
ReasonPhrase = null;
@@ -1284,18 +1284,11 @@ public void HandleNonBodyResponseWrite(int count)
12841284
else
12851285
{
12861286
// Throw Exception for 101, 204, 205, 304 responses.
1287-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.WriteToNonBodyResponse, StatusCode.ToString());
1287+
throw BadHttpResponseException.GetException(ResponseRejectionReason.NonBodyResponseWriteToBody, StatusCode);
12881288
}
12891289
}
12901290

12911291
private void ThrowResponseAbortedException()
1292-
{
1293-
throw new ObjectDisposedException(
1294-
"The response has been aborted due to an unhandled application exception.",
1295-
_applicationException);
1296-
}
1297-
1298-
public void RejectRequest(string message)
12991292
{
13001293
throw new ObjectDisposedException(
13011294
"The response has been aborted due to an unhandled application exception.",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ StringValues IHeaderDictionary.this[string key]
2929
{
3030
if (_isReadOnly)
3131
{
32-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.HeadersReadonlyResponseStarted);
32+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedHeadersReadonly);
3333
}
3434
SetValueFast(key, value);
3535
}
@@ -139,7 +139,7 @@ void IDictionary<string, StringValues>.Add(string key, StringValues value)
139139
{
140140
if (_isReadOnly)
141141
{
142-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.HeadersReadonlyResponseStarted);
142+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedHeadersReadonly);
143143
}
144144
AddValueFast(key, value);
145145
}
@@ -148,7 +148,7 @@ void ICollection<KeyValuePair<string, StringValues>>.Clear()
148148
{
149149
if (_isReadOnly)
150150
{
151-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.HeadersReadonlyResponseStarted);
151+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedHeadersReadonly);
152152
}
153153
ClearFast();
154154
}
@@ -195,7 +195,7 @@ bool IDictionary<string, StringValues>.Remove(string key)
195195
{
196196
if (_isReadOnly)
197197
{
198-
throw BadHttpResponseException.GetException(ResponseRejectionReasons.HeadersReadonlyResponseStarted);
198+
throw BadHttpResponseException.GetException(ResponseRejectionReason.ResponseStartedHeadersReadonly);
199199
}
200200
return RemoveFast(key);
201201
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
5+
{
6+
public enum ResponseRejectionReason
7+
{
8+
ResponseStartedOnstarting,
9+
ResponseStartedHeadersReadonly,
10+
ResponseStartedValueCannotBeSet,
11+
NonBodyResponseTransferEncodingSet,
12+
NonBodyResponseWriteToBody
13+
}
14+
15+
public enum ResponseRejectionParameter
16+
{
17+
StatusCode,
18+
ReasonPhrase
19+
}
20+
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/Microsoft.AspNetCore.Server.KestrelTests/FrameTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ public void ManuallySettingTransferEncodingThrowsForHeadResponse()
687687
frame.HttpVersion = "HTTP/1.1";
688688
((IHttpRequestFeature)frame).Method = "HEAD";
689689

690-
//Act
690+
// Act
691691
frame.ResponseHeaders.Add("Transfer-Encoding", "chunked");
692692

693693
// Assert
@@ -712,7 +712,7 @@ public void ManuallySettingTransferEncodingThrowsForNoBodyResponse()
712712
frame.HttpVersion = "HTTP/1.1";
713713
((IHttpResponseFeature)frame).StatusCode = 304;
714714

715-
//Act
715+
// Act
716716
frame.ResponseHeaders.Add("Transfer-Encoding", "chunked");
717717

718718
// Assert

test/Microsoft.AspNetCore.Server.KestrelTests/TestFrameProtectedMembers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
54
using Microsoft.AspNetCore.Hosting.Server;
65
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;
76

0 commit comments

Comments
 (0)