Change SendFileAsync to use a fallback implementation instead of throwing
- If the user wants to use the SendFile API directly then they can access the feature explicitly. - Removed SupportsSendFile - Don't check for existence, let FileStream throw - Updated Doc comments - Pass the buffer into StreamCopyOperation - Using a real using instead of try finally.
This commit is contained in:
parent
a0a1c38e44
commit
5b175beea8
|
|
@ -36,6 +36,7 @@
|
|||
"System.Net.Primitives": "4.0.11-*",
|
||||
"System.Net.WebSockets": "4.0.0-*",
|
||||
"System.Reflection.TypeExtensions": "4.0.1-*",
|
||||
"System.IO": "4.0.11-*",
|
||||
"System.Runtime": "4.0.21-*",
|
||||
"System.Runtime.InteropServices": "4.0.21-*",
|
||||
"System.Security.Claims": "4.0.1-*",
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
// <auto-generated />
|
||||
namespace Microsoft.AspNet.Http.Extensions
|
||||
{
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
|
||||
internal static class Resources
|
||||
{
|
||||
private static readonly ResourceManager _resourceManager
|
||||
= new ResourceManager("Microsoft.AspNet.Http.Extensions.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||
|
||||
/// <summary>
|
||||
/// This server does not support the sendfile.SendAsync extension.
|
||||
/// </summary>
|
||||
internal static string Exception_SendFileNotSupported
|
||||
{
|
||||
get { return GetString("Exception_SendFileNotSupported"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This server does not support the sendfile.SendAsync extension.
|
||||
/// </summary>
|
||||
internal static string FormatException_SendFileNotSupported()
|
||||
{
|
||||
return GetString("Exception_SendFileNotSupported");
|
||||
}
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
||||
System.Diagnostics.Debug.Assert(value != null);
|
||||
|
||||
if (formatterNames != null)
|
||||
{
|
||||
for (var i = 0; i < formatterNames.Length; i++)
|
||||
{
|
||||
value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="Exception_SendFileNotSupported" xml:space="preserve">
|
||||
<value>This server does not support the sendfile.SendAsync extension.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -2,9 +2,9 @@
|
|||
// 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.Http.Extensions;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
|
||||
namespace Microsoft.AspNet.Http
|
||||
|
|
@ -14,26 +14,11 @@ namespace Microsoft.AspNet.Http
|
|||
/// </summary>
|
||||
public static class SendFileResponseExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if the SendFile extension is supported.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <returns>True if sendfile feature exists in the response.</returns>
|
||||
public static bool SupportsSendFile(this HttpResponse response)
|
||||
{
|
||||
if (response == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(response));
|
||||
}
|
||||
|
||||
return response.HttpContext.Features.Get<IHttpSendFileFeature>() != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the given file using the SendFile extension.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="fileName">The full to the file.</param>
|
||||
/// <returns></returns>
|
||||
public static Task SendFileAsync(this HttpResponse response, string fileName)
|
||||
{
|
||||
|
|
@ -54,7 +39,7 @@ namespace Microsoft.AspNet.Http
|
|||
/// Sends the given file using the SendFile extension.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="fileName">The full or relative path to the file.</param>
|
||||
/// <param name="fileName">The full to the file.</param>
|
||||
/// <param name="offset">The offset in the file.</param>
|
||||
/// <param name="count">The number of types to send, or null to send the remainder of the file.</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
|
|
@ -74,10 +59,48 @@ namespace Microsoft.AspNet.Http
|
|||
var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
|
||||
if (sendFile == null)
|
||||
{
|
||||
throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
|
||||
return SendFileAsync(response.Body, fileName, offset, count, cancellationToken);
|
||||
}
|
||||
|
||||
return sendFile.SendFileAsync(fileName, offset, count, cancellationToken);
|
||||
}
|
||||
|
||||
// Not safe for overlapped writes.
|
||||
private static async Task SendFileAsync(Stream outputStream, string fileName, long offset, long? length, CancellationToken cancel)
|
||||
{
|
||||
cancel.ThrowIfCancellationRequested();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int bufferSize = 1024 * 16;
|
||||
|
||||
var fileStream = new FileStream(
|
||||
fileName,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.ReadWrite,
|
||||
bufferSize: bufferSize,
|
||||
options: FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
|
||||
using (fileStream)
|
||||
{
|
||||
fileStream.Seek(offset, SeekOrigin.Begin);
|
||||
|
||||
// TODO: Use buffer pool
|
||||
var buffer = new byte[bufferSize];
|
||||
|
||||
await StreamCopyOperation.CopyToAsync(fileStream, buffer, outputStream, length, cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// 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.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.Http
|
||||
{
|
||||
// FYI: In most cases the source will be a FileStream and the destination will be to the network.
|
||||
internal static class StreamCopyOperation
|
||||
{
|
||||
internal static async Task CopyToAsync(Stream source, byte[] buffer, Stream destination, long? length, CancellationToken cancel)
|
||||
{
|
||||
long? bytesRemaining = length;
|
||||
Debug.Assert(source != null);
|
||||
Debug.Assert(destination != null);
|
||||
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0);
|
||||
Debug.Assert(buffer != null);
|
||||
|
||||
while (true)
|
||||
{
|
||||
// The natural end of the range.
|
||||
if (bytesRemaining.HasValue && bytesRemaining.Value <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cancel.ThrowIfCancellationRequested();
|
||||
|
||||
int readLength = buffer.Length;
|
||||
if (bytesRemaining.HasValue)
|
||||
{
|
||||
readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength);
|
||||
}
|
||||
int count = await source.ReadAsync(buffer, 0, readLength, cancel);
|
||||
|
||||
if (bytesRemaining.HasValue)
|
||||
{
|
||||
bytesRemaining -= count;
|
||||
}
|
||||
|
||||
// End of the source stream.
|
||||
if (count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cancel.ThrowIfCancellationRequested();
|
||||
|
||||
await destination.WriteAsync(buffer, 0, count, cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved. 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.Http.Features;
|
||||
|
|
@ -12,20 +13,10 @@ namespace Microsoft.AspNet.Http.Extensions.Tests
|
|||
public class SendFileResponseExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void SendFileSupport()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
var response = context.Response;
|
||||
Assert.False(response.SupportsSendFile());
|
||||
context.Features.Set<IHttpSendFileFeature>(new FakeSendFileFeature());
|
||||
Assert.True(response.SupportsSendFile());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task SendFileWhenNotSupported()
|
||||
public Task SendFileWhenFileNotFoundThrows()
|
||||
{
|
||||
var response = new DefaultHttpContext().Response;
|
||||
return Assert.ThrowsAsync<NotSupportedException>(() => response.SendFileAsync("foo"));
|
||||
return Assert.ThrowsAsync<FileNotFoundException>(() => response.SendFileAsync("foo"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in New Issue