62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
// 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.Collections.Generic;
|
|
using Microsoft.AspNet.Builder;
|
|
using Microsoft.Framework.Internal;
|
|
|
|
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([NotNull] this IApplicationBuilder 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;
|
|
}
|
|
}
|
|
}
|
|
} |