Moving the sendfile httpresponse extensions to Microsoft.AspNet.Http.Extensions package
Addresses: https://github.com/aspnet/HttpAbstractions/issues/221
This commit is contained in:
parent
0821a12268
commit
894a14b8c3
|
|
@ -1,144 +1,158 @@
|
|||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.34006
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated />
|
||||
namespace Microsoft.AspNet.StaticFiles
|
||||
{
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
|
||||
internal static class Resources
|
||||
{
|
||||
private static readonly ResourceManager _resourceManager
|
||||
= new ResourceManager("Microsoft.AspNet.StaticFiles.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||
|
||||
namespace Microsoft.AspNet.StaticFiles {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// No IContentTypeProvider was specified.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.AspNet.StaticFiles.Resources", System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(Resources)).Assembly);
|
||||
resourceMan = temp;
|
||||
internal static string Args_NoContentTypeProvider
|
||||
{
|
||||
get { return GetString("Args_NoContentTypeProvider"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No IContentTypeProvider was specified.
|
||||
/// </summary>
|
||||
internal static string FormatArgs_NoContentTypeProvider()
|
||||
{
|
||||
return GetString("Args_NoContentTypeProvider");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No formatter provided.
|
||||
/// </summary>
|
||||
internal static string Args_NoFormatter
|
||||
{
|
||||
get { return GetString("Args_NoFormatter"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No formatter provided.
|
||||
/// </summary>
|
||||
internal static string FormatArgs_NoFormatter()
|
||||
{
|
||||
return GetString("Args_NoFormatter");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Index of
|
||||
/// </summary>
|
||||
internal static string HtmlDir_IndexOf
|
||||
{
|
||||
get { return GetString("HtmlDir_IndexOf"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Index of
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_IndexOf()
|
||||
{
|
||||
return GetString("HtmlDir_IndexOf");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Last Modified
|
||||
/// </summary>
|
||||
internal static string HtmlDir_LastModified
|
||||
{
|
||||
get { return GetString("HtmlDir_LastModified"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Last Modified
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_LastModified()
|
||||
{
|
||||
return GetString("HtmlDir_LastModified");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modified
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Modified
|
||||
{
|
||||
get { return GetString("HtmlDir_Modified"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modified
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_Modified()
|
||||
{
|
||||
return GetString("HtmlDir_Modified");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Name
|
||||
{
|
||||
get { return GetString("HtmlDir_Name"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_Name()
|
||||
{
|
||||
return GetString("HtmlDir_Name");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Size
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Size
|
||||
{
|
||||
get { return GetString("HtmlDir_Size"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Size
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_Size()
|
||||
{
|
||||
return GetString("HtmlDir_Size");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The list of files in the given directory. Column headers are listed in the first row.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_TableSummary
|
||||
{
|
||||
get { return GetString("HtmlDir_TableSummary"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The list of files in the given directory. Column headers are listed in the first row.
|
||||
/// </summary>
|
||||
internal static string FormatHtmlDir_TableSummary()
|
||||
{
|
||||
return GetString("HtmlDir_TableSummary");
|
||||
}
|
||||
|
||||
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 resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to No IContentTypeProvider was specified..
|
||||
/// </summary>
|
||||
internal static string Args_NoContentTypeProvider {
|
||||
get {
|
||||
return ResourceManager.GetString("Args_NoContentTypeProvider", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to No formatter provided..
|
||||
/// </summary>
|
||||
internal static string Args_NoFormatter {
|
||||
get {
|
||||
return ResourceManager.GetString("Args_NoFormatter", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to This server does not support the sendfile.SendAsync extension..
|
||||
/// </summary>
|
||||
internal static string Exception_SendFileNotSupported {
|
||||
get {
|
||||
return ResourceManager.GetString("Exception_SendFileNotSupported", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Index of.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_IndexOf {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_IndexOf", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Last Modified.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_LastModified {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_LastModified", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Modified.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Modified {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_Modified", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Name.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Name {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_Name", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Size.
|
||||
/// </summary>
|
||||
internal static string HtmlDir_Size {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_Size", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to The list of files in the given directory. Column headers are listed in the first row..
|
||||
/// </summary>
|
||||
internal static string HtmlDir_TableSummary {
|
||||
get {
|
||||
return ResourceManager.GetString("HtmlDir_TableSummary", resourceCulture);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,9 +123,6 @@
|
|||
<data name="Args_NoFormatter" xml:space="preserve">
|
||||
<value>No formatter provided.</value>
|
||||
</data>
|
||||
<data name="Exception_SendFileNotSupported" xml:space="preserve">
|
||||
<value>This server does not support the sendfile.SendAsync extension.</value>
|
||||
</data>
|
||||
<data name="HtmlDir_IndexOf" xml:space="preserve">
|
||||
<value>Index of</value>
|
||||
</data>
|
||||
|
|
|
|||
|
|
@ -1,57 +0,0 @@
|
|||
// 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 System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
|
||||
namespace Microsoft.AspNet.StaticFiles
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extensions for HttpResponse exposing the SendFile extension.
|
||||
/// </summary>
|
||||
public static class SendFileResponseExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if the SendFile extension is supported.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <returns>True if sendfile.SendAsync is defined in the environment.</returns>
|
||||
public static bool SupportsSendFile([NotNull] this HttpResponse response)
|
||||
{
|
||||
return response.HttpContext.GetFeature<IHttpSendFileFeature>() != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the given file using the SendFile extension.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName)
|
||||
{
|
||||
return response.SendFileAsync(fileName, 0, null, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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="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>
|
||||
/// <returns></returns>
|
||||
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName, long offset, long? count, CancellationToken cancellationToken)
|
||||
{
|
||||
var sendFile = response.HttpContext.GetFeature<IHttpSendFileFeature>();
|
||||
if (sendFile == null)
|
||||
{
|
||||
throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
|
||||
}
|
||||
|
||||
return sendFile.SendFileAsync(fileName, offset, count, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.StaticFiles
|
||||
{
|
||||
public class SendFileResponseExtensionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void SendFileSupport()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
var response = context.Response;
|
||||
Assert.False(response.SupportsSendFile());
|
||||
context.SetFeature<IHttpSendFileFeature>(new FakeSendFileFeature());
|
||||
Assert.True(response.SupportsSendFile());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task SendFileWhenNotSupported()
|
||||
{
|
||||
var response = new DefaultHttpContext().Response;
|
||||
return Assert.ThrowsAsync<NotSupportedException>(() => response.SendFileAsync("foo"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendFileWorks()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
var response = context.Response;
|
||||
var fakeFeature = new FakeSendFileFeature();
|
||||
context.SetFeature<IHttpSendFileFeature>(fakeFeature);
|
||||
|
||||
await response.SendFileAsync("bob", 1, 3, CancellationToken.None);
|
||||
|
||||
Assert.Equal("bob", fakeFeature.name);
|
||||
Assert.Equal(1, fakeFeature.offset);
|
||||
Assert.Equal(3, fakeFeature.length);
|
||||
Assert.Equal(CancellationToken.None, fakeFeature.token);
|
||||
}
|
||||
|
||||
private class FakeSendFileFeature : IHttpSendFileFeature
|
||||
{
|
||||
public string name = null;
|
||||
public long offset = 0;
|
||||
public long? length = null;
|
||||
public CancellationToken token;
|
||||
|
||||
public Task SendFileAsync(string path, long offset, long? length, CancellationToken cancellation)
|
||||
{
|
||||
this.name = path;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
this.token = cancellation;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue