diff --git a/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderOptions.cs b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderOptions.cs new file mode 100644 index 0000000000..5cc6097284 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderOptions.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNet.WebUtilities.Encoders +{ + /// + /// Specifies options common to all three encoders (HtmlEncode, JavaScriptStringEncode, UrlEncode). + /// + public sealed class EncoderOptions + { + /// + /// Specifies code point tables which do not require escaping by the encoders. + /// + /// + /// By default, only Basic Latin is allowed. + /// + public ICodePointFilter[] CodePointFilters { get; set; } = new[] { Microsoft.AspNet.WebUtilities.Encoders.CodePointFilters.BasicLatin }; + } +} diff --git a/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServiceCollectionExtensions.cs new file mode 100644 index 0000000000..5e501e27a4 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServiceCollectionExtensions.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.WebUtilities.Encoders; +using Microsoft.Framework.ConfigurationModel; + +namespace Microsoft.Framework.DependencyInjection +{ + public static class EncoderServiceCollectionExtensions + { + public static IServiceCollection AddEncoders([NotNull] this IServiceCollection services) + { + return AddEncoders(services, configuration: null); + } + + public static IServiceCollection AddEncoders([NotNull] this IServiceCollection services, IConfiguration configuration) + { + services.AddOptions(configuration); + services.TryAdd(EncoderServices.GetDefaultServices(configuration)); + return services; + } + } +} diff --git a/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServices.cs b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServices.cs new file mode 100644 index 0000000000..d6590d8731 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/Encoders/EncoderServices.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Open Technologies, Inc. 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.Collections.Generic; +using Microsoft.Framework.ConfigurationModel; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.OptionsModel; + +namespace Microsoft.AspNet.WebUtilities.Encoders +{ + public static class EncoderServices + { + public static IEnumerable GetDefaultServices() + { + return GetDefaultServices(configuration: null); + } + + public static IEnumerable GetDefaultServices(IConfiguration configuration) + { + var describe = new ServiceDescriber(configuration); + + // Register the default encoders + // We want to call the 'Default' property getters lazily since they perform static caching + yield return describe.Singleton(CreateFactory(() => HtmlEncoder.Default, filters => new HtmlEncoder(filters))); + yield return describe.Singleton(CreateFactory(() => JavaScriptStringEncoder.Default, filters => new JavaScriptStringEncoder(filters))); + yield return describe.Singleton(CreateFactory(() => UrlEncoder.Default, filters => new UrlEncoder(filters))); + } + + private static Func CreateFactory(Func parameterlessCtor, Func parameterfulCtor) + { + return serviceProvider => + { + var codePointFilters = serviceProvider?.GetService>()?.Options?.CodePointFilters; + return (codePointFilters != null) ? parameterfulCtor(codePointFilters) : parameterlessCtor(); + }; + } + } +} diff --git a/src/Microsoft.AspNet.Http.Extensions/project.json b/src/Microsoft.AspNet.Http.Extensions/project.json index be234399ec..a5cf55cd04 100644 --- a/src/Microsoft.AspNet.Http.Extensions/project.json +++ b/src/Microsoft.AspNet.Http.Extensions/project.json @@ -3,7 +3,9 @@ "description": "ASP.NET 5 common extension methods for HTTP abstractions and IApplicationBuilder.", "dependencies": { "Microsoft.AspNet.Http": "1.0.0-*", + "Microsoft.AspNet.WebUtilities": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*", + "Microsoft.Framework.OptionsModel": "1.0.0-*", "Microsoft.Net.Http.Headers": "1.0.0-*" }, "frameworks" : {