diff --git a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs index 4b97459f50..58ebe8f6d6 100644 --- a/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs +++ b/src/Microsoft.AspNet.StaticFiles/FileServerExtensions.cs @@ -99,7 +99,6 @@ namespace Microsoft.AspNet.Builder } return builder - .UseSendFileFallback() .UseStaticFiles(options.StaticFileOptions); } } diff --git a/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs b/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs deleted file mode 100644 index f6a5d287d6..0000000000 --- a/src/Microsoft.AspNet.StaticFiles/SendFileExtensions.cs +++ /dev/null @@ -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 -{ - /// - /// Extension methods for the SendFileMiddleware - /// - public static class SendFileExtensions - { - /// - /// Provide a SendFile fallback if another component does not. - /// - /// - /// - 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(); - } - - private static bool IsSendFileSupported(IDictionary properties) - { - object obj; - if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj)) - { - var capabilities = (IDictionary)obj; - if (capabilities.TryGetValue(Constants.SendFileVersionKey, out obj) - && Constants.SendFileVersion.Equals((string)obj, StringComparison.Ordinal)) - { - return true; - } - } - return false; - } - - private static void SetSendFileCapability(IDictionary properties) - { - object obj; - if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj)) - { - var capabilities = (IDictionary)obj; - capabilities[Constants.SendFileVersionKey] = Constants.SendFileVersion; - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs deleted file mode 100644 index 01c84221ac..0000000000 --- a/src/Microsoft.AspNet.StaticFiles/SendFileMiddleware.cs +++ /dev/null @@ -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 -{ - /// - /// 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. - /// - public class SendFileMiddleware - { - private readonly RequestDelegate _next; - private readonly ILogger _logger; - - /// - /// Creates a new instance of the SendFileMiddleware. - /// - /// The next middleware in the pipeline. - /// An instance used to create loggers. - 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(); - } - - public Task Invoke(HttpContext context) - { - // Check if there is a SendFile feature already present - if (context.Features.Get() == null) - { - context.Features.Set(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(); - } - } - } - } -} diff --git a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs index e5818f1a13..67650e3a01 100644 --- a/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs +++ b/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs @@ -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;