Skip to content

Commit 83d5629

Browse files
author
Trofimov Ivan Andreevich
committed
passing rethrow flag through ctor, not throught CorsPolicy
1 parent 1b5e6a4 commit 83d5629

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/System.Web.Cors/CorsPolicy.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ public long? PreflightMaxAge
8585
/// </summary>
8686
public bool SupportsCredentials { get; set; }
8787

88-
/// <summary>
89-
/// Gets or sets a value indicating whether upstream exceptions should be rethrown.
90-
/// </summary>
91-
public bool RethrowExceptions { get; set; }
92-
9388
/// <summary>
9489
/// Returns a <see cref="System.String" /> that represents this instance.
9590
/// </summary>

src/System.Web.Http.Cors/CorsHttpConfigurationExtensions.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ public static class CorsHttpConfigurationExtensions
2424
/// Enables the support for CORS.
2525
/// </summary>
2626
/// <param name="httpConfiguration">The <see cref="HttpConfiguration"/>.</param>
27-
public static void EnableCors(this HttpConfiguration httpConfiguration)
27+
/// <param name="rethrowExceptions">Indicates whether upstream exceptions should be rethrown</param>
28+
public static void EnableCors(this HttpConfiguration httpConfiguration, bool rethrowExceptions = false)
2829
{
29-
EnableCors(httpConfiguration, null);
30+
EnableCors(httpConfiguration, null, rethrowExceptions);
3031
}
3132

3233
/// <summary>
3334
/// Enables the support for CORS.
3435
/// </summary>
3536
/// <param name="httpConfiguration">The <see cref="HttpConfiguration"/>.</param>
3637
/// <param name="defaultPolicyProvider">The default <see cref="ICorsPolicyProvider"/>.</param>
38+
/// <param name="rethrowExceptions">Indicates whether upstream exceptions should be rethrown</param>
3739
/// <exception cref="System.ArgumentNullException">httpConfiguration</exception>
38-
public static void EnableCors(this HttpConfiguration httpConfiguration, ICorsPolicyProvider defaultPolicyProvider)
40+
public static void EnableCors(this HttpConfiguration httpConfiguration, ICorsPolicyProvider defaultPolicyProvider,
41+
bool rethrowExceptions = false)
3942
{
4043
if (httpConfiguration == null)
4144
{
@@ -49,11 +52,11 @@ public static void EnableCors(this HttpConfiguration httpConfiguration, ICorsPol
4952
httpConfiguration.SetCorsPolicyProviderFactory(policyProviderFactory);
5053
}
5154

52-
AddCorsMessageHandler(httpConfiguration);
55+
AddCorsMessageHandler(httpConfiguration, rethrowExceptions);
5356
}
5457

5558
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Caller owns the disposable object")]
56-
private static void AddCorsMessageHandler(this HttpConfiguration httpConfiguration)
59+
private static void AddCorsMessageHandler(this HttpConfiguration httpConfiguration, bool rethrowExceptions)
5760
{
5861
object corsEnabled;
5962
if (!httpConfiguration.Properties.TryGetValue(CorsEnabledKey, out corsEnabled))
@@ -64,7 +67,7 @@ private static void AddCorsMessageHandler(this HttpConfiguration httpConfigurati
6467
if (!config.Properties.TryGetValue(CorsEnabledKey, out corsEnabled))
6568
{
6669
// Execute this in the Initializer to ensure that the CorsMessageHandler is added last.
67-
config.MessageHandlers.Add(new CorsMessageHandler(config));
70+
config.MessageHandlers.Add(new CorsMessageHandler(config, rethrowExceptions));
6871

6972
ITraceWriter traceWriter = config.Services.GetTraceWriter();
7073

src/System.Web.Http.Cors/CorsMessageHandler.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ namespace System.Web.Http.Cors
1818
public class CorsMessageHandler : DelegatingHandler
1919
{
2020
private HttpConfiguration _httpConfiguration;
21+
private bool _rethrowExceptions;
2122

2223
/// <summary>
2324
/// Initializes a new instance of the <see cref="CorsMessageHandler"/> class.
2425
/// </summary>
2526
/// <param name="httpConfiguration">The <see cref="HttpConfiguration"/>.</param>
27+
/// <param name="rethrowExceptions">Indicates whether upstream exceptions should be rethrown</param>
2628
/// <exception cref="System.ArgumentNullException">httpConfiguration</exception>
27-
public CorsMessageHandler(HttpConfiguration httpConfiguration)
29+
public CorsMessageHandler(HttpConfiguration httpConfiguration, bool rethrowExceptions = false)
2830
{
2931
if (httpConfiguration == null)
3032
{
3133
throw new ArgumentNullException("httpConfiguration");
3234
}
3335

3436
_httpConfiguration = httpConfiguration;
37+
_rethrowExceptions = rethrowExceptions;
3538
}
3639

3740
/// <summary>
@@ -47,25 +50,20 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
4750
CorsRequestContext corsRequestContext = request.GetCorsRequestContext();
4851
if (corsRequestContext != null)
4952
{
50-
CorsPolicy corsPolicy = null;
5153
try
5254
{
53-
HttpResponseMessage responseMessage;
5455
if (corsRequestContext.IsPreflight)
5556
{
56-
responseMessage = await HandleCorsPreflightRequestAsync(request, corsRequestContext, cancellationToken);
57+
return await HandleCorsPreflightRequestAsync(request, corsRequestContext, cancellationToken);
5758
}
5859
else
5960
{
60-
responseMessage = await HandleCorsRequestAsync(request, corsRequestContext, cancellationToken);
61+
return await HandleCorsRequestAsync(request, corsRequestContext, cancellationToken);
6162
}
62-
63-
corsPolicy = await GetCorsPolicyAsync(request, cancellationToken);
64-
return responseMessage;
6563
}
6664
catch (Exception exception)
6765
{
68-
if (corsPolicy != null && corsPolicy.RethrowExceptions)
66+
if (_rethrowExceptions)
6967
throw;
7068

7169
return HandleException(request, exception);

0 commit comments

Comments
 (0)