Replacing NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-09-12 12:39:25 -07:00
parent c2b9fc541e
commit dca5829b29
11 changed files with 153 additions and 59 deletions

View File

@ -3,8 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Cors.Core namespace Microsoft.AspNet.Cors.Core
{ {
@ -22,10 +20,13 @@ namespace Microsoft.AspNet.Cors.Core
{ {
return _defaultPolicyName; return _defaultPolicyName;
} }
[param: NotNull]
set set
{ {
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_defaultPolicyName = value; _defaultPolicyName = value;
} }
} }
@ -35,8 +36,18 @@ namespace Microsoft.AspNet.Cors.Core
/// </summary> /// </summary>
/// <param name="name">The name of the policy.</param> /// <param name="name">The name of the policy.</param>
/// <param name="policy">The <see cref="CorsPolicy"/> policy to be added.</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; PolicyMap[name] = policy;
} }
@ -45,8 +56,18 @@ namespace Microsoft.AspNet.Cors.Core
/// </summary> /// </summary>
/// <param name="name">The name of the policy.</param> /// <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> /// <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(); var policyBuilder = new CorsPolicyBuilder();
configurePolicy(policyBuilder); configurePolicy(policyBuilder);
PolicyMap[name] = policyBuilder.Build(); PolicyMap[name] = policyBuilder.Build();
@ -57,8 +78,13 @@ namespace Microsoft.AspNet.Cors.Core
/// </summary> /// </summary>
/// <param name="name">The name of the policy to lookup.</param> /// <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> /// <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; return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null;
} }
} }

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Cors.Core; using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Cors namespace Microsoft.AspNet.Cors
{ {
@ -171,8 +170,13 @@ namespace Microsoft.AspNet.Cors
/// </summary> /// </summary>
/// <param name="policy">The policy which needs to be combined.</param> /// <param name="policy">The policy which needs to be combined.</param>
/// <returns>The current policy builder</returns> /// <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()); WithOrigins(policy.Origins.ToArray());
WithHeaders(policy.Headers.ToArray()); WithHeaders(policy.Headers.ToArray());
WithExposedHeaders(policy.ExposedHeaders.ToArray()); WithExposedHeaders(policy.ExposedHeaders.ToArray());

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.Primitives; using Microsoft.Framework.Primitives;
@ -23,8 +22,13 @@ namespace Microsoft.AspNet.Cors.Core
/// Creates a new instance of the <see cref="CorsService"/>. /// Creates a new instance of the <see cref="CorsService"/>.
/// </summary> /// </summary>
/// <param name="options">The option model representing <see cref="CorsOptions"/>.</param> /// <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; _options = options.Value;
} }
@ -36,15 +40,30 @@ namespace Microsoft.AspNet.Cors.Core
/// <param name="policyName"></param> /// <param name="policyName"></param>
/// <returns>A <see cref="CorsResult"/> which contains the result of policy evaluation and can be /// <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> /// 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); var policy = _options.GetPolicy(policyName);
return EvaluatePolicy(context, policy); return EvaluatePolicy(context, policy);
} }
/// <inheritdoc /> /// <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 corsResult = new CorsResult();
var accessControlRequestMethod = context.Request.Headers[CorsConstants.AccessControlRequestMethod]; var accessControlRequestMethod = context.Request.Headers[CorsConstants.AccessControlRequestMethod];
if (string.Equals(context.Request.Method, CorsConstants.PreflightHttpMethod, StringComparison.Ordinal) && if (string.Equals(context.Request.Method, CorsConstants.PreflightHttpMethod, StringComparison.Ordinal) &&
@ -113,6 +132,16 @@ namespace Microsoft.AspNet.Cors.Core
/// <inheritdoc /> /// <inheritdoc />
public virtual void ApplyResult(CorsResult result, HttpResponse response) 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; var headers = response.Headers;
if (result.AllowedOrigin != null) if (result.AllowedOrigin != null)

View File

@ -2,11 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.Cors;
using Microsoft.AspNet.Cors.Core; using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection.Extensions; using Microsoft.Framework.DependencyInjection.Extensions;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection 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> /// <param name="configure">A delegate which is run to configure the services.</param>
/// <returns></returns> /// <returns></returns>
public static IServiceCollection ConfigureCors( public static IServiceCollection ConfigureCors(
[NotNull] this IServiceCollection serviceCollection, this IServiceCollection serviceCollection,
[NotNull] Action<CorsOptions> configure) Action<CorsOptions> configure)
{ {
if (serviceCollection == null)
{
throw new ArgumentNullException(nameof(serviceCollection));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
return serviceCollection.Configure(configure); return serviceCollection.Configure(configure);
} }

View File

@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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 System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Cors.Core namespace Microsoft.AspNet.Cors.Core
@ -25,6 +25,11 @@ namespace Microsoft.AspNet.Cors.Core
/// <inheritdoc /> /// <inheritdoc />
public Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName) public Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName)
{ {
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return Task.FromResult(_options.GetPolicy(policyName ?? _options.DefaultPolicyName)); return Task.FromResult(_options.GetPolicy(policyName ?? _options.DefaultPolicyName));
} }
} }

View File

@ -3,7 +3,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Cors.Core 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="context">The <see cref="HttpContext"/> associated with this call.</param>
/// <param name="policyName">An optional policy name to look for.</param> /// <param name="policyName">An optional policy name to look for.</param>
/// <returns>A <see cref="CorsPolicy"/></returns> /// <returns>A <see cref="CorsPolicy"/></returns>
Task<CorsPolicy> GetPolicyAsync([NotNull] HttpContext context, string policyName); Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName);
} }
} }

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Cors.Core 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> /// <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 /// <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> /// 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> /// <summary>
@ -26,7 +25,7 @@ namespace Microsoft.AspNet.Cors.Core
/// </summary> /// </summary>
/// <param name="result">The <see cref="CorsResult"/> used to read the allowed values.</param> /// <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> /// <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);
} }
} }

View File

@ -8,19 +8,11 @@
"Microsoft.AspNet.Http.Extensions": "1.0.0-*", "Microsoft.AspNet.Http.Extensions": "1.0.0-*",
"Microsoft.Framework.Configuration.Abstractions": "1.0.0-*", "Microsoft.Framework.Configuration.Abstractions": "1.0.0-*",
"Microsoft.Framework.DependencyInjection.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-*" "Microsoft.Framework.OptionsModel": "1.0.0-*"
}, },
"frameworks" : { "frameworks" : {
"dnx451" : { "dnx451" : { },
"dependencies": { "dnxcore50" : { }
}
},
"dnxcore50" : {
"dependencies": {
"System.Runtime": "4.0.21-beta-*"
}
}
} }
} }

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Cors.Core; using Microsoft.AspNet.Cors.Core;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Primitives; using Microsoft.Framework.Primitives;
namespace Microsoft.AspNet.Cors 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="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> /// <param name="policyName">An optional name of the policy to be fetched.</param>
public CorsMiddleware( public CorsMiddleware(
[NotNull] RequestDelegate next, RequestDelegate next,
[NotNull] ICorsService corsService, ICorsService corsService,
[NotNull] ICorsPolicyProvider policyProvider, ICorsPolicyProvider policyProvider,
string policyName) 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; _next = next;
_corsService = corsService; _corsService = corsService;
_corsPolicyProvider = policyProvider; _corsPolicyProvider = policyProvider;
@ -48,10 +62,25 @@ namespace Microsoft.AspNet.Cors
/// <param name="corsService">An instance of <see cref="ICorsService"/>.</param> /// <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> /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
public CorsMiddleware( public CorsMiddleware(
[NotNull] RequestDelegate next, RequestDelegate next,
[NotNull] ICorsService corsService, ICorsService corsService,
[NotNull] CorsPolicy policy) 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; _next = next;
_corsService = corsService; _corsService = corsService;
_policy = policy; _policy = policy;
@ -68,7 +97,7 @@ namespace Microsoft.AspNet.Cors
var corsResult = _corsService.EvaluatePolicy(context, corsPolicy); var corsResult = _corsService.EvaluatePolicy(context, corsPolicy);
_corsService.ApplyResult(corsResult, context.Response); _corsService.ApplyResult(corsResult, context.Response);
var accessControlRequestMethod = var accessControlRequestMethod =
context.Request.Headers[CorsConstants.AccessControlRequestMethod]; context.Request.Headers[CorsConstants.AccessControlRequestMethod];
if (string.Equals( if (string.Equals(
context.Request.Method, context.Request.Method,

View File

@ -2,10 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Cors; using Microsoft.AspNet.Cors;
using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Builder 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="app">The IApplicationBuilder passed to your Configure method</param>
/// <param name="policyName">The policy name of a configured policy.</param> /// <param name="policyName">The policy name of a configured policy.</param>
/// <returns>The original app parameter</returns> /// <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); 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> /// <param name="configurePolicy">A delegate which can use a policy builder to build a policy.</param>
/// <returns>The original app parameter</returns> /// <returns>The original app parameter</returns>
public static IApplicationBuilder UseCors( public static IApplicationBuilder UseCors(
[NotNull] this IApplicationBuilder app, this IApplicationBuilder app,
[NotNull] Action<CorsPolicyBuilder> configurePolicy) Action<CorsPolicyBuilder> configurePolicy)
{ {
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configurePolicy == null)
{
throw new ArgumentNullException(nameof(configurePolicy));
}
var policyBuilder = new CorsPolicyBuilder(); var policyBuilder = new CorsPolicyBuilder();
configurePolicy(policyBuilder); configurePolicy(policyBuilder);
return app.UseMiddleware<CorsMiddleware>(policyBuilder.Build()); return app.UseMiddleware<CorsMiddleware>(policyBuilder.Build());

View File

@ -5,19 +5,11 @@
"url": "https://github.com/aspnet/cors" "url": "https://github.com/aspnet/cors"
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Cors.Core": "1.0.0-*", "Microsoft.AspNet.Cors.Core": "1.0.0-*"
"Microsoft.Framework.NotNullAttribute.Sources": { "version": "1.0.0-*", "type": "build" }
}, },
"frameworks" : { "frameworks" : {
"dnx451" : { "dnx451" : { },
"dependencies": { "dnxcore50" : { }
}
},
"dnxcore50" : {
"dependencies": {
"System.Runtime": "4.0.21-beta-*"
}
}
} }
} }