Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,5 @@ pip-log.txt

# Mac crap
.DS_Store
Code/packages/
Code/NTTData.SitecoreCDN.sln
102 changes: 50 additions & 52 deletions Code/App_Config/Include/SitecoreCDN.config
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<settings>
<!-- SitecoreCDN FILE VERSION CACHE SIZE
<sitecore>
<!--This will replace sitecore media provider with custom one to generate right CDN url's for media-->
<mediaLibrary>
<mediaProvider>
<patch:attribute name="type">NTTData.SitecoreCDN.Providers.CDNMediaProvider, NTTData.SitecoreCDN</patch:attribute>
</mediaProvider>
</mediaLibrary>

<settings>
<!-- SitecoreCDN FILE VERSION CACHE SIZE
When using filename versioning, the results of the version lookup will be cached here for performance.
-->
<setting name="SitecoreCDN.FileVersionCacheSize" value ="5MB"/>
<setting name="SitecoreCDN.FileVersionCacheSize" value ="5MB"/>

<!-- SitecoreCDN URL VERSION CACHE TIME
<!-- SitecoreCDN URL VERSION CACHE TIME
When affixing version information for unique urls, the versions lookups are cached for this amount of time before updating
-->
<setting name="SitecoreCDN.UrlVersionCacheTime" value="00:05:00" />
</settings>
<customHandlers>
<handler trigger="~/minify" handler="aws_minify.ashx" type="NTTData.SitecoreCDN.Handlers.MinifyHandler,NTTData.SitecoreCDN"/>
</customHandlers>
<pipelines>
<initialize>
<!-- injects the Url Replacement MediaProvider as the current MediaPovider-->
<processor type="NTTData.SitecoreCDN.Pipelines.ReplaceMediaProvider,NTTData.SitecoreCDN" />
</initialize>
<preprocessRequest>
<!-- this processor converts /path/to/file.ext!cf!a=1!b=2 to /path/to/file.ext?a=1&b=2 -->
<processor type="NTTData.SitecoreCDN.Pipelines.CDNInterceptPipeline,NTTData.SitecoreCDN" patch:before="processor[@type='Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel']" />
</preprocessRequest>
<httpRequestProcessed>
<!-- this processor replaces the Response.Filter to replace media urls in the output -->
<processor type="NTTData.SitecoreCDN.Pipelines.CDNAttachFilter,NTTData.SitecoreCDN" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']" />
</httpRequestProcessed>
</pipelines>
<!-- CDN
<setting name="SitecoreCDN.UrlVersionCacheTime" value="00:00:00" />
</settings>
<customHandlers>
<handler trigger="~/minify" handler="aws_minify.ashx" type="NTTData.SitecoreCDN.Handlers.MinifyHandler,NTTData.SitecoreCDN"/>
</customHandlers>
<pipelines>
<httpRequestProcessed>
<!-- this processor replaces the Response.Filter to replace media urls in the output -->
<processor type="NTTData.SitecoreCDN.Pipelines.CDNAttachFilter,NTTData.SitecoreCDN" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']" />
</httpRequestProcessed>
</pipelines>
<!-- CDN
enabled: if true, html will be processed so that media urls will be replaced by cdn urls, if false media urls are unaffected. Default value: false

filenameVersioningEnabled: If true, versioning hashes are appended to media urls to prevent overcaching by CDN.
Expand All @@ -45,44 +44,43 @@

matchProtocol: If true, the cdnHostname will usa a protocol (http/https) matching the request, otherwise default to http. Default value: false;
-->
<cdn enabled="true" filenameVersioningEnabled="true" minifyEnabled="false" fastLoadJsEnabled="false" processCss="false" debugParser="false" matchProtocol="true">
<provider type="NTTData.SitecoreCDN.Providers.CDNProvider,NTTData.SitecoreCDN" />
<cdn enabled="true" filenameVersioningEnabled="false" minifyEnabled="false" fastLoadJsEnabled="false" processCss="false" debugParser="false" matchProtocol="true">
<provider type="NTTData.SitecoreCDN.Providers.CDNProvider,NTTData.SitecoreCDN" />

<!-- Incoming requests matching these urls will be processed
<!-- Incoming requests matching these urls will be processed
.aspx is processed when Sitecore.Context.Item is resolved
-->
<processRequests>
<regex pattern = "\.asmx" />
<!-- matches any .asmx -->
</processRequests>

<!-- Incoming requests matching these urls will not be processed -->
<excludeRequests>
<!--<regex pattern = "Default\.aspx" />-->
<processRequests>
<regex pattern = "\.asmx" />
<!-- matches any .asmx -->
</processRequests>

</excludeRequests>
<!-- Incoming requests matching these urls will not be processed -->
<excludeRequests>
<!--<regex pattern = "Default\.aspx" />-->
</excludeRequests>

<!-- These regex patterns will prevent matching urls from being replaced in the outgoing html, doesn't affect Incoming request processing -->
<excludeUrls>
<regex pattern = "\.axd" />
<!-- this keeps ScriptResource.axd and WebResource.axd from being CDN'd-->
<regex pattern = "VisitorIdentification.aspx" />
<!-- this keeps the Sitecore Analytics request from being CDN'd -->
</excludeUrls>
</cdn>
<!-- These regex patterns will prevent matching urls from being replaced in the outgoing html, doesn't affect Incoming request processing -->
<excludeUrls>
<regex pattern = "\.axd" />
<!-- this keeps ScriptResource.axd and WebResource.axd from being CDN'd-->
<regex pattern = "VisitorIdentification.aspx" />
<!-- this keeps the Sitecore Analytics request from being CDN'd -->
</excludeUrls>
</cdn>

<!--
<!--

cdnHostName: ex "cd1.mydomain.com" this url will be used in forming the new cdn urls of media
each site that wants to leverage CDN hosting, needs this attribute set (by .config or by MultiSitesManager)

<site .... cdnHostName = "cd1.mydomain.com" />
-->
<sites>
<site name="website">
<patch:attribute name="cdnHostName">yourcdnhostname</patch:attribute>
</site>
</sites>
</sitecore>
<sites>
<site name="website">
<patch:attribute name="cdnHostName">cdnHostName</patch:attribute>
</site>
</sites>
</sitecore>
</configuration>

2 changes: 1 addition & 1 deletion Code/Caching/ExcludeIncludeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ExcludeIncludeCache(string name, long maxSize)

public void SetResult(string path, bool result)
{
this.SetObject(path, result, sizeof(bool));
this.SetObject(path, result);
//this.SetString(path, url, DateTime.UtcNow.Add(_cacheTime));
}

Expand Down
2 changes: 1 addition & 1 deletion Code/Caching/UrlCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UrlCache: CustomCache
public UrlCache(string name, long maxSize)
: base(name, maxSize)
{
this.InnerCache.Scavengable = true;
//this.InnerCache.Scavengable = true;
_cacheTime = Settings.GetTimeSpanSetting("SitecoreCDN.UrlVersionCacheTime", "00:05:00");
}

Expand Down
21 changes: 14 additions & 7 deletions Code/NTTData.SitecoreCDN.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NTTData.SitecoreCDN</RootNamespace>
<AssemblyName>NTTData.SitecoreCDN</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -21,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,13 +31,16 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack">
<HintPath>..\Dependencies\HtmlAgilityPack.dll</HintPath>
<Reference Include="HtmlAgilityPack, Version=1.4.6.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>packages\HtmlAgilityPack.1.4.6\lib\Net40\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sitecore.Kernel">
<HintPath>..\Dependencies\Sitecore.Kernel.dll</HintPath>
<Reference Include="Sitecore.Kernel, Version=10.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>packages\Sitecore-8.1-update-3\Sitecore.Kernel.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
Expand Down Expand Up @@ -66,7 +71,9 @@
<Compile Include="Switchers\CDNUrlSwitcher.cs" />
<Compile Include="Util\TimerReport.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
26 changes: 0 additions & 26 deletions Code/Pipelines/ReplaceMediaProvider.cs

This file was deleted.

56 changes: 30 additions & 26 deletions Code/Providers/CDNMediaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NTTData.SitecoreCDN.Configuration;
using Sitecore.Resources.Media;
using NTTData.SitecoreCDN.Switchers;
using Sitecore.Text;
Expand All @@ -21,38 +22,41 @@ public class CDNMediaProvider : MediaProvider
/// <returns></returns>
public override string GetMediaUrl(Sitecore.Data.Items.MediaItem item, MediaUrlOptions options)
{
string hostname = CDNManager.GetCDNHostName();
string url = base.GetMediaUrl(item, options);
if (CDNSettings.Enabled)
{
string hostname = CDNManager.GetCDNHostName();
string url = base.GetMediaUrl(item, options);

bool shouldReplace = !string.IsNullOrEmpty(hostname) && // cdnHostname exists for site
Sitecore.Context.PageMode.IsNormal; // PageMode is normal
bool shouldReplace = !string.IsNullOrEmpty(hostname) && // cdnHostname exists for site
Sitecore.Context.PageMode.IsNormal; // PageMode is normal

bool dontReplace = !CDNManager.IsMediaPubliclyAccessible(item) || // media is publicly accessible
CDNManager.IsMediaAnalyticsTracked(item); // media is analytics tracked
bool dontReplace = !CDNManager.IsMediaPubliclyAccessible(item) || // media is publicly accessible
CDNManager.IsMediaAnalyticsTracked(item); // media is analytics tracked

CDNUrlState contextState = CDNUrlSwitcher.CurrentValue;
CDNUrlState contextState = CDNUrlSwitcher.CurrentValue;

if (contextState == CDNUrlState.Enabled)
{
shouldReplace = true;
}
else if (contextState == CDNUrlState.Disabled)
{
UrlString url2 = new UrlString(url);
url2[CDNManager.StopToken] = "1";
url = url2.ToString();
shouldReplace = false;
}
if (contextState == CDNUrlState.Enabled)
{
shouldReplace = true;
}
else if (contextState == CDNUrlState.Disabled)
{
UrlString url2 = new UrlString(url);
url2[CDNManager.StopToken] = "1";
url = url2.ToString();
shouldReplace = false;
}


if (shouldReplace && !dontReplace) // media not DMS tracked
{
return CDNManager.ReplaceMediaUrl(url, hostname);
}
else
{
return url;
if (shouldReplace && !dontReplace) // media not DMS tracked
{
return CDNManager.ReplaceMediaUrl(url, hostname);
}
else
{
return url;
}
}
return base.GetMediaUrl(item, options);
}
}
}
2 changes: 1 addition & 1 deletion Code/Providers/CDNProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public virtual bool IsMediaAnalyticsTracked(MediaItem media)
{
try
{
if (!Settings.Analytics.Enabled)
if (!Settings.GetBoolSetting("Xdb.Enabled", true))
return false;

string cacheKey = media.ID.ToString() + "_tracked";
Expand Down
4 changes: 4 additions & 0 deletions Code/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.6" targetFramework="net40" requireReinstallation="true" />
</packages>