Provide a facility for registering encoder services

This commit is contained in:
Levi B 2015-02-16 14:54:59 -08:00
parent d0543305f0
commit 3f9423eda9
4 changed files with 87 additions and 0 deletions

View File

@ -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
{
/// <summary>
/// Specifies options common to all three encoders (HtmlEncode, JavaScriptStringEncode, UrlEncode).
/// </summary>
public sealed class EncoderOptions
{
/// <summary>
/// Specifies code point tables which do not require escaping by the encoders.
/// </summary>
/// <remarks>
/// By default, only Basic Latin is allowed.
/// </remarks>
public ICodePointFilter[] CodePointFilters { get; set; } = new[] { Microsoft.AspNet.WebUtilities.Encoders.CodePointFilters.BasicLatin };
}
}

View File

@ -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;
}
}
}

View File

@ -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<IServiceDescriptor> GetDefaultServices()
{
return GetDefaultServices(configuration: null);
}
public static IEnumerable<IServiceDescriptor> 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<IHtmlEncoder>(CreateFactory(() => HtmlEncoder.Default, filters => new HtmlEncoder(filters)));
yield return describe.Singleton<IJavaScriptStringEncoder>(CreateFactory(() => JavaScriptStringEncoder.Default, filters => new JavaScriptStringEncoder(filters)));
yield return describe.Singleton<IUrlEncoder>(CreateFactory(() => UrlEncoder.Default, filters => new UrlEncoder(filters)));
}
private static Func<IServiceProvider, T> CreateFactory<T>(Func<T> parameterlessCtor, Func<ICodePointFilter[], T> parameterfulCtor)
{
return serviceProvider =>
{
var codePointFilters = serviceProvider?.GetService<IOptions<EncoderOptions>>()?.Options?.CodePointFilters;
return (codePointFilters != null) ? parameterfulCtor(codePointFilters) : parameterlessCtor();
};
}
}
}

View File

@ -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" : {