Skip to content

Commit

Permalink
Changed timestamp for client-side caching to use an MD5 hash of conte…
Browse files Browse the repository at this point in the history
…nt. Removed .NET 3.5 version from nuspec, no longer supported.
  • Loading branch information
skroonenburg committed Nov 14, 2011
1 parent f76477a commit 52dce2f
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 16 deletions.
7 changes: 3 additions & 4 deletions Nuspec/Rejuicer.nuspec
Expand Up @@ -2,9 +2,9 @@
<package>
<metadata>
<id>Rejuicer</id>
<version>1.2.6</version>
<version>1.2.7</version>
<authors>Sam Kroonenburg</authors>
<description>Minify your CSS/JS. Simple fluent configuration. Wildcard file matching. Works on Azure. Easy script debugging.</description>
<description>Minify your CSS/JS. Simple fluent configuration. Wildcard file matching. Browser caching. Works on Azure. Easy script debugging.</description>
<language>en-US</language>
<projectUrl>http://rejuice.me</projectUrl>
<iconUrl>http://rejuice.me/wp-content/uploads/2011/05/rejuicer_logo.png</iconUrl>
Expand All @@ -16,8 +16,7 @@
</dependencies>
</metadata>
<files>
<file src="..\..\NET40\Source\Rejuicer\Rejuicer\bin\Release\Rejuicer.dll" target="lib\40\Rejuicer.dll" />
<file src="..\..\NET35\Source\Rejuicer\Rejuicer\bin\Release\Rejuicer.dll" target="lib\35\Rejuicer.dll" />
<file src="..\Source\Rejuicer\Rejuicer\bin\Release\Rejuicer.dll" target="lib\40\Rejuicer.dll" />
<file src="web.config.transform" target="Content\web.config.transform" />
<file src="Content\App_Start\Rejuicer.cs.pp" target="Content\App_Start" />
</files>
Expand Down
Expand Up @@ -39,6 +39,7 @@ public byte[] Combine(IEnumerable<byte[]> data)
foreach (var value in data)
{
sb.Append(value.ReadString());
sb.AppendLine();
}

return sb.ToString().AsBytes();
Expand Down
34 changes: 34 additions & 0 deletions Source/Rejuicer/Rejuicer/Engine/HashUtilities.cs
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Rejuicer.Engine
{
public static class HashUtilities
{
public static byte[] HashArray(this byte[] bytes)
{
using (MD5 md5 = MD5.Create())
{
return md5.ComputeHash(bytes);
}
}

public static string HashStringValue(this byte[] bytes, bool uppercase = false)
{
return bytes.HashArray().ToDecimalString(uppercase);
}

public static string ToDecimalString(this byte[] bytes, bool upperCase)
{
StringBuilder result = new StringBuilder(bytes.Length * 2);

for (int i = 0; i < bytes.Length; i++)
result.Append(bytes[i].ToString(upperCase ? "D2" : "d2"));

return result.ToString();
}
}
}
1 change: 1 addition & 0 deletions Source/Rejuicer/Rejuicer/Engine/OutputContent.cs
Expand Up @@ -15,6 +15,7 @@ public OutputContent()
}

public byte[] Content { get; set; }
public byte[] ContentHash { get; set; }
public string ContentType { get; set; }
public bool AllowClientCaching { get; set; }
public DateTime LastModifiedDate { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Source/Rejuicer/Rejuicer/Engine/RejuicerEngine.cs
Expand Up @@ -131,7 +131,7 @@ internal static RejuicerConfigurationSource GetConfigFor(string requestedFilenam
// 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,
RejuicerConfigurationSource.GetTimeStampString(DateTime.Now));
"1234");

foreach (var pair in _configurations)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Rejuicer/Rejuicer/HtmlHelpers/IncludesCacheModel.cs
Expand Up @@ -10,11 +10,11 @@ namespace Rejuicer.HtmlHelpers
public class IncludesCacheModel
{
public IHtmlString IncludesHtml { get; set; }
public DateTime Timestamp { get; set; }
public string HashValue { get; set; }

public IHtmlString RenderHtml()
{
return new HtmlString(IncludesHtml.ToString().Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, RejuicerConfigurationSource.GetTimeStampString(Timestamp)));
return new HtmlString(IncludesHtml.ToString().Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, HashValue));
}
}
}
9 changes: 7 additions & 2 deletions Source/Rejuicer/Rejuicer/HtmlHelpers/Rejuicer.cs
Expand Up @@ -55,7 +55,7 @@ public static IHtmlString IncludeRejuicedJsFor(this HtmlHelper instance, string
return script.ToString(TagRenderMode.Normal);
})));

var cachedIncludes = new IncludesCacheModel { IncludesHtml = scripts, Timestamp = config.GetLastModifiedDate(cacheProvider) };
var cachedIncludes = new IncludesCacheModel { IncludesHtml = scripts, HashValue = config.GetHashValue(cacheProvider) };

SetCachedIncludesFor(filename, cachedIncludes, dependencies);

Expand All @@ -72,6 +72,11 @@ public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string

var toInclude = GetIncludesFor(filename);
var config = RejuicerEngine.GetConfigFor(filename);
if (config == null)
{
return new HtmlString("");
}

var dependencies = config.GetDependencies();

var links = new HtmlString(string.Join("\n", toInclude.Select(f =>
Expand All @@ -85,7 +90,7 @@ public static IHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string
return link.ToString(TagRenderMode.SelfClosing);
})));

var cachedIncludes = new IncludesCacheModel { IncludesHtml = links, Timestamp = config.GetLastModifiedDate(cacheProvider) };
var cachedIncludes = new IncludesCacheModel { IncludesHtml = links, HashValue = config.GetHashValue(cacheProvider) };

SetCachedIncludesFor(filename, cachedIncludes, dependencies);

Expand Down
5 changes: 4 additions & 1 deletion Source/Rejuicer/Rejuicer/Model/PhysicalFileSource.cs
Expand Up @@ -96,6 +96,7 @@ public OutputContent GetContent(ICacheProvider cacheProvider, Mode mode)
var combinedValue = new OutputContent
{
Content = rejuicedValue,
ContentHash = rejuicedValue.HashArray(),
AllowClientCaching = false,
ContentType =
ResourceType == ResourceType.Css ? "text/css" : "text/javascript",
Expand All @@ -111,9 +112,11 @@ public OutputContent GetContent(ICacheProvider cacheProvider, Mode mode)
Log.WriteLine("Minifying Content For '{0}'", VirtualPath);

// Minified value
var minifiedContent = minificationProvider.Minify(rejuicedValue);
var minifiedValue = new OutputContent
{
Content = minificationProvider.Minify(rejuicedValue),
Content = minifiedContent,
ContentHash = minifiedContent.HashArray(),
AllowClientCaching = false,
ContentType =
ResourceType == ResourceType.Css
Expand Down
10 changes: 6 additions & 4 deletions Source/Rejuicer/Rejuicer/Model/RejuicerConfigurationSource.cs
Expand Up @@ -101,14 +101,14 @@ public DateTime GetLastModifiedDate(ICacheProvider cacheProvider)
return GetContent(cacheProvider).LastModifiedDate;
}

public static string GetTimeStampString(DateTime dateTime)
public string GetHashValue(ICacheProvider cacheProvider)
{
return (dateTime - DateTime.MinValue).Ticks.ToString();
return GetContent(cacheProvider).ContentHash.HashStringValue();
}

public string GetTimestampedUrl(ICacheProvider cacheProvider)
{
return RequestFor.Replace(FilenameUniquePlaceholder, GetTimeStampString(GetLastModifiedDate(cacheProvider)));
return RequestFor.Replace(FilenameUniquePlaceholder, GetHashValue(cacheProvider));
}

public string GetNonTimestampedUrl(ICacheProvider cacheProvider)
Expand Down Expand Up @@ -138,9 +138,11 @@ public OutputContent GetContent(ICacheProvider cacheProvider)
Log.WriteLine("Combining content for '{0}'", RequestFor);

// Combine all of the files into one string
var minifiedContent = minificationProvider.Combine(content.Select(x => x.Content));
cachedValue = new OutputContent
{
Content = minificationProvider.Combine(content.Select(x => x.Content)),
Content = minifiedContent,
ContentHash = minifiedContent.HashArray(),
AllowClientCaching = ContainsPlaceHolder,
ContentType = minificationProvider.GetContentType(RequestFor),
LastModifiedDate = content.Max(x => x.LastModifiedDate)
Expand Down
4 changes: 2 additions & 2 deletions Source/Rejuicer/Rejuicer/Properties/AssemblyInfo.cs
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.6.0")]
[assembly: AssemblyFileVersion("1.2.6.0")]
[assembly: AssemblyVersion("1.2.7.0")]
[assembly: AssemblyFileVersion("1.2.7.0")]
[assembly: InternalsVisibleTo("Rejuicer-test")]
[assembly: InternalsVisibleTo("RejuicerConfiguration-test")]
1 change: 1 addition & 0 deletions Source/Rejuicer/Rejuicer/Rejuicer.csproj
Expand Up @@ -67,6 +67,7 @@
<ItemGroup>
<Compile Include="Engine\BaseStringMinificationProvider.cs" />
<Compile Include="Engine\CacheProvider.cs" />
<Compile Include="Engine\HashUtilities.cs" />
<Compile Include="Engine\ImageMinificationProvider.cs" />
<Compile Include="Engine\Log.cs" />
<Compile Include="Engine\PhysicalFileRegister.cs" />
Expand Down

0 comments on commit 52dce2f

Please sign in to comment.