forked from skroonenburg/Rejuicer
/
RejuicerEngine.cs
193 lines (159 loc) · 6.47 KB
/
RejuicerEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.Caching;
using Rejuicer.Model;
using Rejuicer.Configuration;
using Rejuicer.Engine;
namespace Rejuicer
{
internal static class RejuicerEngine
{
private static ReaderWriterLockSlim _configurationLock = new ReaderWriterLockSlim();
internal static Dictionary<string, RejuicerConfigurationSource> _configurations = new Dictionary<string,RejuicerConfigurationSource>();
private static ICacheProvider cacheProvider = new CacheProvider();
private static bool _configurationContainsPlaceholder = false;
internal static IVirtualPathResolver _virtualPathResolver = new VirtualPathResolver();
internal static void AddConfiguration(RejuicerConfigurationSource config)
{
try
{
_configurationLock.EnterWriteLock();
if (_configurations.ContainsKey(config.RequestFor))
{
throw new InvalidOperationException("There is already a configuration for this URL.");
}
Log.WriteLine("Creating configuration for request URL '{0}'", config.RequestFor);
if (config.ContainsPlaceHolder)
{
// Remember that there is a placeholder in one of the configurations.
_configurationContainsPlaceholder = true;
}
_configurations.Add(config.RequestFor, config);
}
finally
{
_configurationLock.ExitWriteLock();
}
// Generate the content so that it is cached.
config.GetContent(cacheProvider);
}
internal static void ClearConfigurations()
{
try
{
_configurationLock.EnterWriteLock();
_configurations.Clear();
_configurationContainsPlaceholder = false;
}
finally
{
_configurationLock.ExitWriteLock();
}
}
public static bool HasConfigurationFor(Uri uri)
{
return HasConfigurationFor(VirtualPathUtility.ToAppRelative(uri.AbsolutePath));
}
public static bool HasConfigurationFor(string virtualPath)
{
return GetConfigFor(virtualPath) != null;
}
internal static IEnumerable<string> GetVirtualPathsFor(string requestedFilename)
{
// Get all dependencies of the matching content type of the parent.
return FileResolver.VirtualPathsFor(GetConfigFor(requestedFilename));
}
public static IEnumerable<FileInfo> GetDependenciesFor(string requestedFilename)
{
return GetConfigFor(requestedFilename).GetDependencies();
}
private static PhysicalFileSource GetPhysicalFileFor(string requestedFilename)
{
requestedFilename = MakeVirtualPathFromRequestUrl(requestedFilename);
return PhysicalFileRegister.For(requestedFilename);
}
public static bool PassThroughDebuggingFor(Uri requestedUrl)
{
return IsPassThroughEnabled && GetPhysicalFileFor(VirtualPathUtility.ToAppRelative(requestedUrl.AbsolutePath)) != null;
}
private static string MakeVirtualPathFromRequestUrl(string requestUrl)
{
return !requestUrl.StartsWith("~") ? string.Format("~{0}", requestUrl) : requestUrl;
}
internal static RejuicerConfigurationSource GetConfigFor(string requestedFilename)
{
try
{
_configurationLock.EnterReadLock();
requestedFilename = MakeVirtualPathFromRequestUrl(requestedFilename);
if (!_configurationContainsPlaceholder)
{
// There are no placeholders in our configurations, so perform a quick dictionary lookup
RejuicerConfigurationSource model = null;
if (_configurations.TryGetValue(requestedFilename, out model))
{
return model;
}
// No matching file configurations were found, so return nothing.
return null;
}
// There are placeholders in the configuration, so iterate over each and look for a match
// remove any placeholders from the incoming requested filenames
var matchUrl = requestedFilename.Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder,
"1234");
foreach (var pair in _configurations)
{
var regexp = pair.Value.RequestFor.Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, "[0-9]+");
if (Regex.IsMatch(matchUrl, regexp))
{
return pair.Value;
}
}
return null;
}
finally
{
_configurationLock.ExitReadLock();
}
}
internal static RejuicerConfiguration Configuration
{
get
{
return RejuicerConfiguration.Current;
}
}
public static OutputContent GetContentFor(Uri uri)
{
return GetContentFor(VirtualPathUtility.ToAppRelative(uri.AbsolutePath));
}
public static OutputContent GetContentFor(string requestedFilename)
{
var config = GetConfigFor(requestedFilename);
if (config == null)
{
if (IsPassThroughEnabled)
{
// Get the non-minified, but transformed content
return GetPhysicalFileFor(requestedFilename).GetContent(cacheProvider, Mode.Combine);
}
return null;
}
return config.GetContent(cacheProvider);
}
public static bool IsPassThroughEnabled
{
get
{
return HttpContext.Current.IsDebuggingEnabled && (Configuration == null || ((!Configuration.PreventPassThroughOnDebug.HasValue || !Configuration.PreventPassThroughOnDebug.Value)));
}
}
}
}