Removed UseSendFileFallback

This commit is contained in:
David Fowler 2015-12-02 23:43:36 -08:00
parent 657a5ab26b
commit 7009e68fc3
4 changed files with 0 additions and 188 deletions

View File

@ -99,7 +99,6 @@ namespace Microsoft.AspNet.Builder
}
return builder
.UseSendFileFallback()
.UseStaticFiles(options.StaticFileOptions);
}
}

View File

@ -1,66 +0,0 @@
// 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 System.Collections.Generic;
using Microsoft.AspNet.Builder;
namespace Microsoft.AspNet.StaticFiles
{
/// <summary>
/// Extension methods for the SendFileMiddleware
/// </summary>
public static class SendFileExtensions
{
/// <summary>
/// Provide a SendFile fallback if another component does not.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseSendFileFallback(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
/* TODO builder.GetItem(typeof(ISendFile))
// Check for advertised support
if (IsSendFileSupported(builder.Properties))
{
return builder;
}
// Otherwise, insert a fallback SendFile middleware and advertise support
SetSendFileCapability(builder.Properties);
*/
return builder.UseMiddleware<SendFileMiddleware>();
}
private static bool IsSendFileSupported(IDictionary<string, object> properties)
{
object obj;
if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj))
{
var capabilities = (IDictionary<string, object>)obj;
if (capabilities.TryGetValue(Constants.SendFileVersionKey, out obj)
&& Constants.SendFileVersion.Equals((string)obj, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
private static void SetSendFileCapability(IDictionary<string, object> properties)
{
object obj;
if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj))
{
var capabilities = (IDictionary<string, object>)obj;
capabilities[Constants.SendFileVersionKey] = Constants.SendFileVersion;
}
}
}
}

View File

@ -1,120 +0,0 @@
// 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 System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNet.StaticFiles
{
/// <summary>
/// This middleware provides an efficient fallback mechanism for sending static files
/// when the server does not natively support such a feature.
/// The caller is responsible for setting all headers in advance.
/// The caller is responsible for performing the correct impersonation to give access to the file.
/// </summary>
public class SendFileMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
/// <summary>
/// Creates a new instance of the SendFileMiddleware.
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
public SendFileMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
_next = next;
_logger = loggerFactory.CreateLogger<SendFileMiddleware>();
}
public Task Invoke(HttpContext context)
{
// Check if there is a SendFile feature already present
if (context.Features.Get<IHttpSendFileFeature>() == null)
{
context.Features.Set<IHttpSendFileFeature>(new SendFileWrapper(context.Response.Body, _logger));
}
return _next(context);
}
private class SendFileWrapper : IHttpSendFileFeature
{
private readonly Stream _output;
private readonly ILogger _logger;
internal SendFileWrapper(Stream output, ILogger logger)
{
_output = output;
_logger = logger;
}
// Not safe for overlapped writes.
public async Task SendFileAsync(string fileName, long offset, long? length, CancellationToken cancel)
{
cancel.ThrowIfCancellationRequested();
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentNullException(nameof(fileName));
}
if (!File.Exists(fileName))
{
throw new FileNotFoundException(string.Empty, fileName);
}
var fileInfo = new FileInfo(fileName);
if (offset < 0 || offset > fileInfo.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
}
if (length.HasValue &&
(length.Value < 0 || length.Value > fileInfo.Length - offset))
{
throw new ArgumentOutOfRangeException(nameof(length), length, string.Empty);
}
var fileStream = new FileStream(
fileName,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
bufferSize: 1024 * 64,
options: FileOptions.Asynchronous | FileOptions.SequentialScan);
try
{
fileStream.Seek(offset, SeekOrigin.Begin);
_logger.LogCopyingBytesToResponse(
start: offset,
end: length != null ? (offset + length) : null,
path: fileName);
await StreamCopyOperation.CopyToAsync(fileStream, _output, length, cancel);
}
finally
{
fileStream.Dispose();
}
}
}
}
}

View File

@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Logging;