Replacing NotNullAttribute with thrown exceptions
This commit is contained in:
parent
c2b9fc541e
commit
dca5829b29
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Cors.Core
|
||||
{
|
||||
|
|
@ -22,10 +20,13 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
{
|
||||
return _defaultPolicyName;
|
||||
}
|
||||
|
||||
[param: NotNull]
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
_defaultPolicyName = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -35,8 +36,18 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// </summary>
|
||||
/// <param name="name">The name of the policy.</param>
|
||||
/// <param name="policy">The <see cref="CorsPolicy"/> policy to be added.</param>
|
||||
public void AddPolicy([NotNull] string name, [NotNull] CorsPolicy policy)
|
||||
public void AddPolicy(string name, CorsPolicy policy)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(name));
|
||||
}
|
||||
|
||||
if (policy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policy));
|
||||
}
|
||||
|
||||
PolicyMap[name] = policy;
|
||||
}
|
||||
|
||||
|
|
@ -45,8 +56,18 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// </summary>
|
||||
/// <param name="name">The name of the policy.</param>
|
||||
/// <param name="configurePolicy">A delegate which can use a policy builder to build a policy.</param>
|
||||
public void AddPolicy([NotNull] string name, [NotNull] Action<CorsPolicyBuilder> configurePolicy)
|
||||
public void AddPolicy(string name, Action<CorsPolicyBuilder> configurePolicy)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(name));
|
||||
}
|
||||
|
||||
if (configurePolicy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configurePolicy));
|
||||
}
|
||||
|
||||
var policyBuilder = new CorsPolicyBuilder();
|
||||
configurePolicy(policyBuilder);
|
||||
PolicyMap[name] = policyBuilder.Build();
|
||||
|
|
@ -57,8 +78,13 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// </summary>
|
||||
/// <param name="name">The name of the policy to lookup.</param>
|
||||
/// <returns>The <see cref="CorsPolicy"/> if the policy was added.<c>null</c> otherwise.</returns>
|
||||
public CorsPolicy GetPolicy([NotNull] string name)
|
||||
public CorsPolicy GetPolicy(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(name));
|
||||
}
|
||||
|
||||
return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Cors
|
||||
{
|
||||
|
|
@ -171,8 +170,13 @@ namespace Microsoft.AspNet.Cors
|
|||
/// </summary>
|
||||
/// <param name="policy">The policy which needs to be combined.</param>
|
||||
/// <returns>The current policy builder</returns>
|
||||
private CorsPolicyBuilder Combine([NotNull] CorsPolicy policy)
|
||||
private CorsPolicyBuilder Combine(CorsPolicy policy)
|
||||
{
|
||||
if (policy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policy));
|
||||
}
|
||||
|
||||
WithOrigins(policy.Origins.ToArray());
|
||||
WithHeaders(policy.Headers.ToArray());
|
||||
WithExposedHeaders(policy.ExposedHeaders.ToArray());
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Microsoft.Framework.Primitives;
|
||||
|
||||
|
|
@ -23,8 +22,13 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// Creates a new instance of the <see cref="CorsService"/>.
|
||||
/// </summary>
|
||||
/// <param name="options">The option model representing <see cref="CorsOptions"/>.</param>
|
||||
public CorsService([NotNull] IOptions<CorsOptions> options)
|
||||
public CorsService(IOptions<CorsOptions> options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
|
|
@ -36,15 +40,30 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// <param name="policyName"></param>
|
||||
/// <returns>A <see cref="CorsResult"/> which contains the result of policy evaluation and can be
|
||||
/// used by the caller to set appropriate response headers.</returns>
|
||||
public CorsResult EvaluatePolicy([NotNull] HttpContext context, string policyName)
|
||||
public CorsResult EvaluatePolicy(HttpContext context, string policyName)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var policy = _options.GetPolicy(policyName);
|
||||
return EvaluatePolicy(context, policy);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public CorsResult EvaluatePolicy([NotNull] HttpContext context, [NotNull] CorsPolicy policy)
|
||||
public CorsResult EvaluatePolicy(HttpContext context, CorsPolicy policy)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
if (policy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policy));
|
||||
}
|
||||
|
||||
var corsResult = new CorsResult();
|
||||
var accessControlRequestMethod = context.Request.Headers[CorsConstants.AccessControlRequestMethod];
|
||||
if (string.Equals(context.Request.Method, CorsConstants.PreflightHttpMethod, StringComparison.Ordinal) &&
|
||||
|
|
@ -113,6 +132,16 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// <inheritdoc />
|
||||
public virtual void ApplyResult(CorsResult result, HttpResponse response)
|
||||
{
|
||||
if (result == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(result));
|
||||
}
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(response));
|
||||
}
|
||||
|
||||
var headers = response.Headers;
|
||||
|
||||
if (result.AllowedOrigin != null)
|
||||
|
|
|
|||
|
|
@ -2,11 +2,8 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Cors;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.Framework.Configuration;
|
||||
using Microsoft.Framework.DependencyInjection.Extensions;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.Framework.DependencyInjection
|
||||
{
|
||||
|
|
@ -22,9 +19,19 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
/// <param name="configure">A delegate which is run to configure the services.</param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection ConfigureCors(
|
||||
[NotNull] this IServiceCollection serviceCollection,
|
||||
[NotNull] Action<CorsOptions> configure)
|
||||
this IServiceCollection serviceCollection,
|
||||
Action<CorsOptions> configure)
|
||||
{
|
||||
if (serviceCollection == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serviceCollection));
|
||||
}
|
||||
|
||||
if (configure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
|
||||
return serviceCollection.Configure(configure);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Cors.Core
|
||||
|
|
@ -25,6 +25,11 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// <inheritdoc />
|
||||
public Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
return Task.FromResult(_options.GetPolicy(policyName ?? _options.DefaultPolicyName));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Cors.Core
|
||||
{
|
||||
|
|
@ -18,6 +17,6 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// <param name="context">The <see cref="HttpContext"/> associated with this call.</param>
|
||||
/// <param name="policyName">An optional policy name to look for.</param>
|
||||
/// <returns>A <see cref="CorsPolicy"/></returns>
|
||||
Task<CorsPolicy> GetPolicyAsync([NotNull] HttpContext context, string policyName);
|
||||
Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Cors.Core
|
||||
{
|
||||
|
|
@ -18,7 +17,7 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// <param name="policy">The <see cref="CorsPolicy"/> which needs to be evaluated.</param>
|
||||
/// <returns>A <see cref="CorsResult"/> which contains the result of policy evaluation and can be
|
||||
/// used by the caller to set apporpriate response headers.</returns>
|
||||
CorsResult EvaluatePolicy([NotNull] HttpContext context, [NotNull] CorsPolicy policy);
|
||||
CorsResult EvaluatePolicy(HttpContext context, CorsPolicy policy);
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -26,7 +25,7 @@ namespace Microsoft.AspNet.Cors.Core
|
|||
/// </summary>
|
||||
/// <param name="result">The <see cref="CorsResult"/> used to read the allowed values.</param>
|
||||
/// <param name="response">The <see cref="HttpResponse"/> associated with the current call.</param>
|
||||
void ApplyResult([NotNull] CorsResult result, [NotNull] HttpResponse response);
|
||||
void ApplyResult(CorsResult result, HttpResponse response);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -8,19 +8,11 @@
|
|||
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
|
||||
"Microsoft.Framework.Configuration.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.NotNullAttribute.Sources": { "version": "1.0.0-*", "type": "build" },
|
||||
"Microsoft.Framework.OptionsModel": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks" : {
|
||||
"dnx451" : {
|
||||
"dependencies": {
|
||||
}
|
||||
},
|
||||
"dnxcore50" : {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.0.21-beta-*"
|
||||
}
|
||||
}
|
||||
"dnx451" : { },
|
||||
"dnxcore50" : { }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.Primitives;
|
||||
|
||||
namespace Microsoft.AspNet.Cors
|
||||
|
|
@ -30,11 +29,26 @@ namespace Microsoft.AspNet.Cors
|
|||
/// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
|
||||
/// <param name="policyName">An optional name of the policy to be fetched.</param>
|
||||
public CorsMiddleware(
|
||||
[NotNull] RequestDelegate next,
|
||||
[NotNull] ICorsService corsService,
|
||||
[NotNull] ICorsPolicyProvider policyProvider,
|
||||
RequestDelegate next,
|
||||
ICorsService corsService,
|
||||
ICorsPolicyProvider policyProvider,
|
||||
string policyName)
|
||||
{
|
||||
if (next == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(next));
|
||||
}
|
||||
|
||||
if (corsService == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(corsService));
|
||||
}
|
||||
|
||||
if (policyProvider == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policyProvider));
|
||||
}
|
||||
|
||||
_next = next;
|
||||
_corsService = corsService;
|
||||
_corsPolicyProvider = policyProvider;
|
||||
|
|
@ -48,10 +62,25 @@ namespace Microsoft.AspNet.Cors
|
|||
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
|
||||
/// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
|
||||
public CorsMiddleware(
|
||||
[NotNull] RequestDelegate next,
|
||||
[NotNull] ICorsService corsService,
|
||||
[NotNull] CorsPolicy policy)
|
||||
RequestDelegate next,
|
||||
ICorsService corsService,
|
||||
CorsPolicy policy)
|
||||
{
|
||||
if (next == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(next));
|
||||
}
|
||||
|
||||
if (corsService == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(corsService));
|
||||
}
|
||||
|
||||
if (policy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policy));
|
||||
}
|
||||
|
||||
_next = next;
|
||||
_corsService = corsService;
|
||||
_policy = policy;
|
||||
|
|
@ -68,7 +97,7 @@ namespace Microsoft.AspNet.Cors
|
|||
var corsResult = _corsService.EvaluatePolicy(context, corsPolicy);
|
||||
_corsService.ApplyResult(corsResult, context.Response);
|
||||
|
||||
var accessControlRequestMethod =
|
||||
var accessControlRequestMethod =
|
||||
context.Request.Headers[CorsConstants.AccessControlRequestMethod];
|
||||
if (string.Equals(
|
||||
context.Request.Method,
|
||||
|
|
|
|||
|
|
@ -2,10 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Cors;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.Framework.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
|
|
@ -20,8 +17,13 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="app">The IApplicationBuilder passed to your Configure method</param>
|
||||
/// <param name="policyName">The policy name of a configured policy.</param>
|
||||
/// <returns>The original app parameter</returns>
|
||||
public static IApplicationBuilder UseCors([NotNull]this IApplicationBuilder app, string policyName)
|
||||
public static IApplicationBuilder UseCors(this IApplicationBuilder app, string policyName)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
return app.UseMiddleware<CorsMiddleware>(policyName);
|
||||
}
|
||||
|
||||
|
|
@ -32,9 +34,19 @@ namespace Microsoft.AspNet.Builder
|
|||
/// <param name="configurePolicy">A delegate which can use a policy builder to build a policy.</param>
|
||||
/// <returns>The original app parameter</returns>
|
||||
public static IApplicationBuilder UseCors(
|
||||
[NotNull] this IApplicationBuilder app,
|
||||
[NotNull] Action<CorsPolicyBuilder> configurePolicy)
|
||||
this IApplicationBuilder app,
|
||||
Action<CorsPolicyBuilder> configurePolicy)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
if (configurePolicy == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configurePolicy));
|
||||
}
|
||||
|
||||
var policyBuilder = new CorsPolicyBuilder();
|
||||
configurePolicy(policyBuilder);
|
||||
return app.UseMiddleware<CorsMiddleware>(policyBuilder.Build());
|
||||
|
|
|
|||
|
|
@ -5,19 +5,11 @@
|
|||
"url": "https://github.com/aspnet/cors"
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Cors.Core": "1.0.0-*",
|
||||
"Microsoft.Framework.NotNullAttribute.Sources": { "version": "1.0.0-*", "type": "build" }
|
||||
"Microsoft.AspNet.Cors.Core": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks" : {
|
||||
"dnx451" : {
|
||||
"dependencies": {
|
||||
}
|
||||
},
|
||||
"dnxcore50" : {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.0.21-beta-*"
|
||||
}
|
||||
}
|
||||
"dnx451" : { },
|
||||
"dnxcore50" : { }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue