Revert "Return HTTP 404 if FileNotFoundException is thrown when attempting to send a file (#232)"
This reverts commit 6d7269dafb.
This commit is contained in:
parent
db4d1bfcae
commit
3cd6add095
|
|
@ -233,7 +233,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
// the Range header field.
|
// the Range header field.
|
||||||
if (ifRangeHeader.LastModified.HasValue)
|
if (ifRangeHeader.LastModified.HasValue)
|
||||||
{
|
{
|
||||||
if (_lastModified != null && _lastModified > ifRangeHeader.LastModified)
|
if (_lastModified !=null && _lastModified > ifRangeHeader.LastModified)
|
||||||
{
|
{
|
||||||
_isRangeRequest = false;
|
_isRangeRequest = false;
|
||||||
}
|
}
|
||||||
|
|
@ -318,11 +318,11 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
||||||
public async Task SendAsync()
|
public async Task SendAsync()
|
||||||
{
|
{
|
||||||
|
ApplyResponseHeaders(Constants.Status200Ok);
|
||||||
string physicalPath = _fileInfo.PhysicalPath;
|
string physicalPath = _fileInfo.PhysicalPath;
|
||||||
var sendFile = _context.Features.Get<IHttpSendFileFeature>();
|
var sendFile = _context.Features.Get<IHttpSendFileFeature>();
|
||||||
if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
|
if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
|
||||||
{
|
{
|
||||||
ApplyResponseHeaders(Constants.Status200Ok);
|
|
||||||
// We don't need to directly cancel this, if the client disconnects it will fail silently.
|
// We don't need to directly cancel this, if the client disconnects it will fail silently.
|
||||||
await sendFile.SendFileAsync(physicalPath, 0, _length, CancellationToken.None);
|
await sendFile.SendFileAsync(physicalPath, 0, _length, CancellationToken.None);
|
||||||
return;
|
return;
|
||||||
|
|
@ -332,9 +332,6 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
{
|
{
|
||||||
using (var readStream = _fileInfo.CreateReadStream())
|
using (var readStream = _fileInfo.CreateReadStream())
|
||||||
{
|
{
|
||||||
// Don't apply headers until we are sure we can open this file.
|
|
||||||
ApplyResponseHeaders(Constants.Status200Ok);
|
|
||||||
|
|
||||||
// Larger StreamCopyBufferSize is required because in case of FileStream readStream isn't going to be buffering
|
// Larger StreamCopyBufferSize is required because in case of FileStream readStream isn't going to be buffering
|
||||||
await StreamCopyOperation.CopyToAsync(readStream, _response.Body, _length, StreamCopyBufferSize, _context.RequestAborted);
|
await StreamCopyOperation.CopyToAsync(readStream, _response.Body, _length, StreamCopyBufferSize, _context.RequestAborted);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
|
@ -68,7 +67,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task Invoke(HttpContext context)
|
public Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);
|
var fileContext = new StaticFileContext(context, _options, _matchUrl, _logger, _fileProvider, _contentTypeProvider);
|
||||||
|
|
||||||
|
|
@ -98,35 +97,21 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
case StaticFileContext.PreconditionState.ShouldProcess:
|
case StaticFileContext.PreconditionState.ShouldProcess:
|
||||||
if (fileContext.IsHeadMethod)
|
if (fileContext.IsHeadMethod)
|
||||||
{
|
{
|
||||||
await fileContext.SendStatusAsync(Constants.Status200Ok);
|
return fileContext.SendStatusAsync(Constants.Status200Ok);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (fileContext.IsRangeRequest)
|
if (fileContext.IsRangeRequest)
|
||||||
{
|
{
|
||||||
await fileContext.SendRangeAsync();
|
return fileContext.SendRangeAsync();
|
||||||
return;
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await fileContext.SendAsync();
|
|
||||||
_logger.LogFileServed(fileContext.SubPath, fileContext.PhysicalPath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (FileNotFoundException)
|
|
||||||
{
|
|
||||||
_logger.LogFileNotFound(fileContext.SubPath);
|
|
||||||
await fileContext.SendStatusAsync(StatusCodes.Status404NotFound);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
_logger.LogFileServed(fileContext.SubPath, fileContext.PhysicalPath);
|
||||||
|
return fileContext.SendAsync();
|
||||||
case StaticFileContext.PreconditionState.NotModified:
|
case StaticFileContext.PreconditionState.NotModified:
|
||||||
_logger.LogPathNotModified(fileContext.SubPath);
|
_logger.LogPathNotModified(fileContext.SubPath);
|
||||||
await fileContext.SendStatusAsync(Constants.Status304NotModified);
|
return fileContext.SendStatusAsync(Constants.Status304NotModified);
|
||||||
return;
|
|
||||||
|
|
||||||
case StaticFileContext.PreconditionState.PreconditionFailed:
|
case StaticFileContext.PreconditionState.PreconditionFailed:
|
||||||
_logger.LogPreconditionFailed(fileContext.SubPath);
|
_logger.LogPreconditionFailed(fileContext.SubPath);
|
||||||
await fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);
|
return fileContext.SendStatusAsync(Constants.Status412PreconditionFailed);
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
|
var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
|
||||||
|
|
@ -135,7 +120,7 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await _next(context);
|
return _next(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
@ -14,7 +13,6 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Microsoft.AspNetCore.Testing.xunit;
|
using Microsoft.AspNetCore.Testing.xunit;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Net.Http.Headers;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.StaticFiles
|
namespace Microsoft.AspNetCore.StaticFiles
|
||||||
|
|
@ -31,27 +29,6 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
var response = await server.CreateClient().GetAsync("/ranges.txt");
|
var response = await server.CreateClient().GetAsync("/ranges.txt");
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
Assert.Null(response.Headers.ETag);
|
|
||||||
}
|
|
||||||
|
|
||||||
[ConditionalFact]
|
|
||||||
[OSSkipCondition(OperatingSystems.Windows, SkipReason = "Symlinks not supported on Windows")]
|
|
||||||
public async Task ReturnsNotFoundForBrokenSymlink()
|
|
||||||
{
|
|
||||||
var badLink = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName() + ".txt");
|
|
||||||
|
|
||||||
Process.Start("ln", $"-s \"/tmp/{Path.GetRandomFileName()}\" \"{badLink}\"").WaitForExit();
|
|
||||||
Assert.True(File.Exists(badLink), "Should have created a symlink");
|
|
||||||
|
|
||||||
var builder = new WebHostBuilder()
|
|
||||||
.Configure(app => app.UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true }))
|
|
||||||
.UseWebRoot(AppContext.BaseDirectory);
|
|
||||||
var server = new TestServer(builder);
|
|
||||||
|
|
||||||
var response = await server.CreateClient().GetAsync(Path.GetFileName(badLink));
|
|
||||||
|
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
||||||
Assert.Null(response.Headers.ETag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -124,7 +101,6 @@ namespace Microsoft.AspNetCore.StaticFiles
|
||||||
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
|
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
|
||||||
Assert.True(response.Content.Headers.ContentLength == fileInfo.Length);
|
Assert.True(response.Content.Headers.ContentLength == fileInfo.Length);
|
||||||
Assert.Equal(response.Content.Headers.ContentLength, responseContent.Length);
|
Assert.Equal(response.Content.Headers.ContentLength, responseContent.Length);
|
||||||
Assert.NotNull(response.Headers.ETag);
|
|
||||||
|
|
||||||
using (var stream = fileInfo.CreateReadStream())
|
using (var stream = fileInfo.CreateReadStream())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue