Using IHtmlEncode to encode content
Fixes: https://github.com/aspnet/StaticFiles/issues/29
This commit is contained in:
parent
bd9ced4b10
commit
62036bf74b
|
|
@ -1,6 +1,6 @@
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.FileProviders;
|
|
||||||
using Microsoft.AspNet.StaticFiles;
|
using Microsoft.AspNet.StaticFiles;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.Logging;
|
using Microsoft.Framework.Logging;
|
||||||
using Microsoft.Framework.Logging.Console;
|
using Microsoft.Framework.Logging.Console;
|
||||||
|
|
||||||
|
|
@ -8,11 +8,18 @@ namespace StaticFilesSample
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddDirectoryBrowser();
|
||||||
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, ILoggerFactory factory)
|
public void Configure(IApplicationBuilder app, ILoggerFactory factory)
|
||||||
{
|
{
|
||||||
// Displays all log levels
|
// Displays all log levels
|
||||||
factory.AddConsole(LogLevel.Verbose);
|
factory.AddConsole(LogLevel.Verbose);
|
||||||
|
|
||||||
|
app.UseRequestServices();
|
||||||
|
|
||||||
app.UseFileServer(new FileServerOptions()
|
app.UseFileServer(new FileServerOptions()
|
||||||
{
|
{
|
||||||
EnableDirectoryBrowsing = true,
|
EnableDirectoryBrowsing = true,
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,15 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Kestrel": "1.0.0-*",
|
"Kestrel": "1.0.0-*",
|
||||||
|
"Microsoft.AspNet.RequestContainer": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
|
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
|
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
|
||||||
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
|
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
|
||||||
"Microsoft.Framework.Logging.Console": "1.0.0-*"
|
"Microsoft.Framework.Logging.Console": "1.0.0-*"
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"aspnet50": { },
|
"aspnet50": {},
|
||||||
"aspnetcore50": { }
|
"aspnetcore50": {}
|
||||||
},
|
},
|
||||||
"webroot": "wwwroot"
|
"webroot": "wwwroot"
|
||||||
}
|
}
|
||||||
|
|
@ -73,4 +73,4 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
return contents.Exists;
|
return contents.Exists;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
// 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 Microsoft.AspNet.StaticFiles;
|
||||||
|
using Microsoft.Framework.ConfigurationModel;
|
||||||
|
|
||||||
|
namespace Microsoft.Framework.DependencyInjection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for adding directory browser services.
|
||||||
|
/// </summary>
|
||||||
|
public static class DirectoryBrowserServiceExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds directory browser middleware services.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IServiceCollection AddDirectoryBrowser([NotNull] this IServiceCollection services)
|
||||||
|
{
|
||||||
|
return services.AddDirectoryBrowser(configuration: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds directory browser middleware services.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IServiceCollection AddDirectoryBrowser([NotNull] this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
return services.AddEncoders(configuration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,11 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.WebUtilities.Encoders;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.StaticFiles
|
namespace Microsoft.AspNet.StaticFiles
|
||||||
{
|
{
|
||||||
|
|
@ -20,6 +20,8 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
{
|
{
|
||||||
private const string TextHtmlUtf8 = "text/html; charset=utf-8";
|
private const string TextHtmlUtf8 = "text/html; charset=utf-8";
|
||||||
|
|
||||||
|
private static IHtmlEncoder _htmlEncoder;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates an HTML view for a directory.
|
/// Generates an HTML view for a directory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -34,6 +36,11 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
throw new ArgumentNullException("contents");
|
throw new ArgumentNullException("contents");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_htmlEncoder == null)
|
||||||
|
{
|
||||||
|
_htmlEncoder = context.ApplicationServices.GetHtmlEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
context.Response.ContentType = TextHtmlUtf8;
|
context.Response.ContentType = TextHtmlUtf8;
|
||||||
|
|
||||||
if (Helpers.IsHeadMethod(context.Request.Method))
|
if (Helpers.IsHeadMethod(context.Request.Method))
|
||||||
|
|
@ -154,7 +161,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
|
|
||||||
private static string HtmlEncode(string body)
|
private static string HtmlEncode(string body)
|
||||||
{
|
{
|
||||||
return WebUtility.HtmlEncode(body);
|
return _htmlEncoder.HtmlEncode(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -20,4 +20,4 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task GenerateContentAsync(HttpContext context, IEnumerable<IFileInfo> contents);
|
Task GenerateContentAsync(HttpContext context, IEnumerable<IFileInfo> contents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,12 +4,12 @@ using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.FileProviders;
|
using Microsoft.AspNet.FileProviders;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.TestHost;
|
using Microsoft.AspNet.TestHost;
|
||||||
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.StaticFiles
|
namespace Microsoft.AspNet.StaticFiles
|
||||||
|
|
@ -19,13 +19,29 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task NullArguments()
|
public async Task NullArguments()
|
||||||
{
|
{
|
||||||
Assert.Throws<ArgumentException>(() => TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { Formatter = null })));
|
Assert.Throws<ArgumentException>(() => TestServer.Create(app =>
|
||||||
|
{
|
||||||
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
|
|
||||||
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions() { Formatter = null });
|
||||||
|
}));
|
||||||
|
|
||||||
// No exception, default provided
|
// No exception, default provided
|
||||||
TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = null }));
|
TestServer.Create(app =>
|
||||||
|
{
|
||||||
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
|
|
||||||
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions() { FileProvider = null });
|
||||||
|
});
|
||||||
|
|
||||||
// PathString(null) is OK.
|
// PathString(null) is OK.
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser((string)null));
|
TestServer server = TestServer.Create(app =>
|
||||||
|
{
|
||||||
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
|
|
||||||
|
app.UseDirectoryBrowser((string)null);
|
||||||
|
});
|
||||||
|
|
||||||
var response = await server.CreateClient().GetAsync("/");
|
var response = await server.CreateClient().GetAsync("/");
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
@ -38,11 +54,16 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[InlineData("", @".\", "/missing.dir")]
|
[InlineData("", @".\", "/missing.dir")]
|
||||||
public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl)
|
public async Task NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl)
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
RequestPath = new PathString(baseUrl),
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
|
||||||
}));
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
||||||
|
{
|
||||||
|
RequestPath = new PathString(baseUrl),
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
||||||
|
});
|
||||||
|
});
|
||||||
HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync();
|
HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync();
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
@ -55,11 +76,16 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
||||||
public async Task FoundDirectory_Served(string baseUrl, string baseDir, string requestUrl)
|
public async Task FoundDirectory_Served(string baseUrl, string baseDir, string requestUrl)
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
RequestPath = new PathString(baseUrl),
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
|
||||||
}));
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
||||||
|
{
|
||||||
|
RequestPath = new PathString(baseUrl),
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
||||||
|
});
|
||||||
|
});
|
||||||
HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync();
|
HttpResponseMessage response = await server.CreateRequest(requestUrl).GetAsync();
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -77,11 +103,17 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[InlineData("/somedir", @".", "/somedir/subfolder", "?a=b")]
|
[InlineData("/somedir", @".", "/somedir/subfolder", "?a=b")]
|
||||||
public async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl, string queryString)
|
public async Task NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl, string queryString)
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
RequestPath = new PathString(baseUrl),
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
|
||||||
}));
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
||||||
|
{
|
||||||
|
RequestPath = new PathString(baseUrl),
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
HttpResponseMessage response = await server.CreateRequest(requestUrl + queryString).GetAsync();
|
HttpResponseMessage response = await server.CreateRequest(requestUrl + queryString).GetAsync();
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
|
Assert.Equal(HttpStatusCode.Moved, response.StatusCode);
|
||||||
|
|
@ -96,11 +128,17 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
||||||
public async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl)
|
public async Task PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl)
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
RequestPath = new PathString(baseUrl),
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
|
||||||
}));
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
||||||
|
{
|
||||||
|
RequestPath = new PathString(baseUrl),
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
HttpResponseMessage response = await server.CreateRequest(requestUrl).PostAsync();
|
HttpResponseMessage response = await server.CreateRequest(requestUrl).PostAsync();
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
@ -112,11 +150,17 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
[InlineData("/somedir", @".", "/somedir/subfolder/")]
|
||||||
public async Task HeadDirectory_HeadersButNotBodyServed(string baseUrl, string baseDir, string requestUrl)
|
public async Task HeadDirectory_HeadersButNotBodyServed(string baseUrl, string baseDir, string requestUrl)
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app => app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
TestServer server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
RequestPath = new PathString(baseUrl),
|
app.UseServices(services => services.AddDirectoryBrowser());
|
||||||
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
|
||||||
}));
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
|
||||||
|
{
|
||||||
|
RequestPath = new PathString(baseUrl),
|
||||||
|
FileProvider = new PhysicalFileProvider(Path.Combine(Environment.CurrentDirectory, baseDir))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
HttpResponseMessage response = await server.CreateRequest(requestUrl).SendAsync("HEAD");
|
HttpResponseMessage response = await server.CreateRequest(requestUrl).SendAsync("HEAD");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.AspNet.Http.Core": "1.0.0-*",
|
"Microsoft.AspNet.Http.Core": "1.0.0-*",
|
||||||
|
"Microsoft.AspNet.RequestContainer": "1.0.0-*",
|
||||||
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
|
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
|
||||||
"Microsoft.AspNet.TestHost": "1.0.0-*",
|
"Microsoft.AspNet.TestHost": "1.0.0-*",
|
||||||
"xunit.runner.kre": "1.0.0-*"
|
"xunit.runner.kre": "1.0.0-*"
|
||||||
|
|
@ -15,4 +16,4 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue