#108 compression code cleanup
This commit is contained in:
parent
03b63e2c2a
commit
511d711620
|
|
@ -18,8 +18,8 @@ namespace ResponseCompressionSample
|
|||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<ICompressionProvider, GzipCompressionProvider>();
|
||||
services.AddTransient<ICompressionProvider, CustomCompressionProvider>();
|
||||
services.AddSingleton<ICompressionProvider, GzipCompressionProvider>();
|
||||
services.AddSingleton<ICompressionProvider, CustomCompressionProvider>();
|
||||
services.AddResponseCompression("text/plain", "text/html");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
// 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 Microsoft.AspNetCore.ResponseCompression;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Microsoft.AspNetCore.Builder
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for the ResponseCompression middleware.
|
||||
/// </summary>
|
||||
public static class ResponseCompressionBuilderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds middleware for dynamically compressing HTTP Responses.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
|
||||
public static IApplicationBuilder UseResponseCompression(this IApplicationBuilder builder)
|
||||
{
|
||||
if (builder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(builder));
|
||||
}
|
||||
|
||||
return builder.UseMiddleware<ResponseCompressionMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.ResponseCompression
|
|||
|
||||
private readonly IResponseCompressionProvider _provider;
|
||||
|
||||
private readonly bool _enableHttps;
|
||||
private readonly bool _enableForHttps;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the Response Compression middleware.
|
||||
|
|
@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.ResponseCompression
|
|||
|
||||
_next = next;
|
||||
_provider = provider;
|
||||
_enableHttps = options.Value.EnableHttps;
|
||||
_enableForHttps = options.Value.EnableForHttps;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.ResponseCompression
|
|||
{
|
||||
ICompressionProvider compressionProvider = null;
|
||||
|
||||
if (!context.Request.IsHttps || _enableHttps)
|
||||
if (!context.Request.IsHttps || _enableForHttps)
|
||||
{
|
||||
compressionProvider = _provider.GetCompressionProvider(context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,6 @@ namespace Microsoft.AspNetCore.ResponseCompression
|
|||
/// Indicates if responses over HTTPS connections should be compressed. The default is 'false'.
|
||||
/// Enable compression on HTTPS connections may expose security problems.
|
||||
/// </summary>
|
||||
public bool EnableHttps { get; set; } = false;
|
||||
public bool EnableForHttps { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.ResponseCompression
|
|||
|
||||
if (options.Value.MimeTypes == null || !options.Value.MimeTypes.Any())
|
||||
{
|
||||
throw new InvalidOperationException("No mime types specified");
|
||||
throw new InvalidOperationException("No MIME types specified");
|
||||
}
|
||||
_mimeTypes = new HashSet<string>(options.Value.MimeTypes, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Builder
|
|||
/// <summary>
|
||||
/// Extension methods for the ResponseCompression middleware.
|
||||
/// </summary>
|
||||
public static class ResponseCompressionExtensions
|
||||
public static class ResponseCompressionServicesExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Add response compression services and enable compression for responses with the given MIME types.
|
||||
|
|
@ -45,22 +45,8 @@ namespace Microsoft.AspNetCore.Builder
|
|||
}
|
||||
|
||||
services.Configure(configureOptions);
|
||||
services.TryAddTransient<IResponseCompressionProvider, ResponseCompressionProvider>();
|
||||
services.TryAddSingleton<IResponseCompressionProvider, ResponseCompressionProvider>();
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds middleware for dynamically compressing HTTP Responses.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
|
||||
public static IApplicationBuilder UseResponseCompression(this IApplicationBuilder builder)
|
||||
{
|
||||
if (builder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(builder));
|
||||
}
|
||||
|
||||
return builder.UseMiddleware<ResponseCompressionMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests
|
|||
{
|
||||
var options = new ResponseCompressionOptions();
|
||||
|
||||
Assert.False(options.EnableHttps);
|
||||
Assert.False(options.EnableForHttps);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -256,7 +256,7 @@ namespace Microsoft.AspNetCore.ResponseCompression.Tests
|
|||
{
|
||||
services.AddResponseCompression(options =>
|
||||
{
|
||||
options.EnableHttps = enableHttps;
|
||||
options.EnableForHttps = enableHttps;
|
||||
options.MimeTypes = new[] { TextPlain };
|
||||
});
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue