Skip to content

Commit

Permalink
Merge pull request jetheredge#156 from AlexCuse/master
Browse files Browse the repository at this point in the history
Changes to Mutex Code for Lower-Trust Environments
  • Loading branch information
jetheredge committed Feb 23, 2012
2 parents fb91998 + fb6dbe6 commit 93a0efc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
16 changes: 1 addition & 15 deletions SquishIt.Framework/Base/BundleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using SquishIt.Framework.Minifiers;
using SquishIt.Framework.Resolvers;
Expand Down Expand Up @@ -41,13 +40,6 @@ protected IMinifier<T> Minifier
set { minifier = value; }
}

private IFilePathMutexProvider mutexProvider;
protected IFilePathMutexProvider MutexProvider
{
get { return mutexProvider ?? (mutexProvider = FilePathMutexProvider.Instance); }
set { mutexProvider = value; }
}

protected string HashKeyName { get; set; }
private bool ShouldRenderOnlyIfOutputFileIsMissing { get; set; }
internal List<string> DependentFiles = new List<string>();
Expand Down Expand Up @@ -404,9 +396,7 @@ private string RenderRelease(string key, string renderTo, IRenderer renderer)
string content;
if (!bundleCache.TryGetValue(key, out content))
{
var renderMutex = MutexProvider.GetMutexForPath(renderTo);
renderMutex.WaitOne();
try
using(new CriticalRenderingSection(renderTo))
{
if (!bundleCache.TryGetValue(key, out content))
{
Expand Down Expand Up @@ -498,10 +488,6 @@ private string RenderRelease(string key, string renderTo, IRenderer renderer)
content += String.Concat(GetFilesForRemote(remoteAssetPaths, bundleState), renderedTag);
}
}
finally
{
renderMutex.ReleaseMutex();
}
bundleCache.Add(key, content, DependentFiles);
}

Expand Down
1 change: 1 addition & 0 deletions SquishIt.Framework/SquishIt.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Base\BundleState.cs" />
<Compile Include="Base\IBundle.cs" />
<Compile Include="Coffee\CoffeescriptCompiler.cs" />
<Compile Include="Utilities\CriticalRenderingSection.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="IBundleCache.cs" />
<Compile Include="Minifiers\IMinifier.cs" />
Expand Down
64 changes: 64 additions & 0 deletions SquishIt.Framework/Utilities/CriticalRenderingSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Web;
namespace SquishIt.Framework.Utilities
{
public class CriticalRenderingSection : IDisposable {
// this feels a bit like IDisposable abuse but allows us to code BundleBase in a more mutex-agnostic fashion
// probably acceptable alternative for try .. finally though
private IFilePathMutexProvider mutexProvider;
protected IFilePathMutexProvider MutexProvider
{
get { return mutexProvider ?? (mutexProvider = FilePathMutexProvider.Instance); }
set { mutexProvider = value; }
}

Mutex mutex;
public CriticalRenderingSection(string path)
{
if (IsHighOrUnrestrictedTrust)
{
mutex = MutexProvider.GetMutexForPath(path);
mutex.WaitOne();
}
}

public void Dispose()
{
if (IsHighOrUnrestrictedTrust)
{
mutex.ReleaseMutex();
}
}

bool IsHighOrUnrestrictedTrust
{
get { return trustLevel == AspNetHostingPermissionLevel.High || trustLevel == AspNetHostingPermissionLevel.Unrestricted; }
}

AspNetHostingPermissionLevel trustLevel = GetCurrentTrustLevel();

static AspNetHostingPermissionLevel GetCurrentTrustLevel()
{
var lastTrustedLevel = AspNetHostingPermissionLevel.None;

foreach (AspNetHostingPermissionLevel level in new[] {
AspNetHostingPermissionLevel.Minimal,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Unrestricted })
{
try {
new AspNetHostingPermission(level).Demand();
lastTrustedLevel = level;
}
catch (System.Security.SecurityException) {
break;
}
}

return lastTrustedLevel;
}
}
}

0 comments on commit 93a0efc

Please sign in to comment.