WORK IN PROGRESS, DO NOT DOWNLOAD

ClientDependency work. Resolved old 20914 bug. Initial fix to remove Umbraco.aspx page popup.

[TFS Changeset #56361]
This commit is contained in:
Shandem
2009-07-13 14:51:26 +00:00
parent 172ea69064
commit 198c2d2d06
14 changed files with 479 additions and 97 deletions

View File

@@ -15,6 +15,7 @@ namespace umbraco.presentation.ClientDependency
public ClientDependencyAttribute()
{
Priority = DefaultPriority;
DoNotOptimize = false;
}
/// <summary>
@@ -26,6 +27,16 @@ namespace umbraco.presentation.ClientDependency
/// </remarks>
protected const int DefaultPriority = 100;
/// <summary>
/// If set to true, this file will not be compressed, combined, etc...
/// it will be rendered out as is.
/// </summary>
/// <remarks>
/// Useful for debugging dodgy scripts.
/// Default is false.
/// </remarks>
public bool DoNotOptimize { get; set; }
/// <summary>
/// If dependencies have a composite group name specified, the system will combine all dependency
/// file contents under the one group name and GZIP the output to output cache.

View File

@@ -15,6 +15,7 @@ namespace umbraco.presentation.ClientDependency
private static ClientDependencyProvider m_Provider = null;
private static ClientDependencyProviderCollection m_Providers = null;
private static List<string> m_Extensions;
private static object m_Lock = new object();
public static ClientDependencyProvider DefaultProvider
@@ -34,6 +35,26 @@ namespace umbraco.presentation.ClientDependency
}
}
/// <summary>
/// The file extensions of Client Dependencies that are file based as opposed to request based.
/// Any file that doesn't have the extensions listed here will be request based, request based is
/// more overhead for the server to process.
/// </summary>
/// <example>
/// A request based JavaScript file may be a .ashx that dynamically creates JavaScript server side.
/// </example>
/// <remarks>
/// If this is not explicitly set, then the extensions 'js' and 'css' are the defaults.
/// </remarks>
public static List<string> FileBasedDependencyExtensionList
{
get
{
LoadProviders();
return m_Extensions;
}
}
private static void LoadProviders()
{
if (m_Provider == null)
@@ -43,7 +64,7 @@ namespace umbraco.presentation.ClientDependency
// Do this again to make sure _provider is still null
if (m_Provider == null)
{
ClientDependencySection section = (ClientDependencySection)WebConfigurationManager.GetSection("system.web/imageService");
ClientDependencySection section = (ClientDependencySection)WebConfigurationManager.GetSection("system.web/clientDependency");
m_Providers = new ClientDependencyProviderCollection();
@@ -55,17 +76,23 @@ namespace umbraco.presentation.ClientDependency
ProvidersHelper.InstantiateProviders(section.Providers, m_Providers, typeof(ClientDependencyProvider));
m_Provider = m_Providers[section.DefaultProvider];
if (m_Provider == null)
throw new ProviderException("Unable to load default ImageProvider");
throw new ProviderException("Unable to load default ClientDependency provider");
}
else
{
//get the default settings
section = new ClientDependencySection();
m_Extensions = section.FileBasedDependencyExtensionList;
PageHeaderProvider php = new PageHeaderProvider();
php.Initialize(PageHeaderProvider.DefaultName, null);
m_Providers.Add(php);
ClientSideRegistrationProvider csrp = new ClientSideRegistrationProvider();
csrp.Initialize(ClientSideRegistrationProvider.DefaultName, null);
m_Providers.Add(csrp);
m_Provider = m_Providers[PageHeaderProvider.DefaultName];
//set the default
m_Provider = m_Providers[section.DefaultProvider];
}
}
}

View File

@@ -12,6 +12,7 @@ namespace umbraco.presentation.ClientDependency
{
DependencyType = ClientDependencyType.Javascript;
Priority = DefaultPriority;
DoNotOptimize = false;
}
/// <summary>
@@ -23,6 +24,16 @@ namespace umbraco.presentation.ClientDependency
/// </remarks>
protected const int DefaultPriority = 100;
/// <summary>
/// If set to true, this file will not be compressed, combined, etc...
/// it will be rendered out as is.
/// </summary>
/// <remarks>
/// Useful for debugging dodgy scripts.
/// Default is false.
/// </remarks>
public bool DoNotOptimize { get; set; }
public ClientDependencyType DependencyType { get; set; }
public string FilePath { get; set; }
public string PathNameAlias { get; set; }

View File

@@ -5,6 +5,7 @@ using System.Web;
using System.Reflection;
using System.IO;
using System.IO.Compression;
using System.Net;
namespace umbraco.presentation.ClientDependency
{
@@ -63,7 +64,7 @@ namespace umbraco.presentation.ClientDependency
if (string.IsNullOrEmpty(fileset))
throw new ArgumentException("Must specify a fileset in the request");
byte[] fileBytes = CombineFiles(fileset, context);
byte[] fileBytes = CombineFiles(fileset, context, type);
byte[] outputBytes = CompressBytes(context, fileBytes);
SetCaching(context);
@@ -77,22 +78,61 @@ namespace umbraco.presentation.ClientDependency
/// <param name="fileList"></param>
/// <param name="context"></param>
/// <returns></returns>
private byte[] CombineFiles(string fileList, HttpContext context)
private byte[] CombineFiles(string fileList, HttpContext context, ClientDependencyType type)
{
MemoryStream ms = new MemoryStream(5000);
//get the file list, and write the contents of each file to the output stream.
string[] strFiles = DecodeFrom64(fileList).Split(';');
StreamWriter sw = new StreamWriter(ms);
StreamWriter sw = new StreamWriter(ms);
foreach (string s in strFiles)
{
if (!string.IsNullOrEmpty(s))
{
FileInfo fi = new FileInfo(context.Server.MapPath(s));
if (!fi.Exists)
throw new NullReferenceException("File could not be found: " + fi.FullName);
string fileContents = File.ReadAllText(fi.FullName);
sw.WriteLine(fileContents);
try
{
FileInfo fi = new FileInfo(context.Server.MapPath(s));
if (ClientDependencyHelper.FileBasedDependencyExtensionList.Contains(fi.Extension.ToLower().Replace(".", "")))
{
//if the file doesn't exist, then we'll assume it is a URI external request
if (!fi.Exists)
{
WriteRequestToStream(ref sw, s);
}
else
{
//if it is a file based dependency then read it
string fileContents = File.ReadAllText(fi.FullName);
sw.WriteLine(fileContents);
}
}
else
{
//if it's not a file based dependency, try to get the request output.
WriteRequestToStream(ref sw, s);
}
}
catch (Exception ex)
{
Type exType = ex.GetType();
if (exType.Equals(typeof(NotSupportedException)) || exType.Equals(typeof(ArgumentException)))
{
//could not parse the string into a fileinfo, so we assume it is a URI
WriteRequestToStream(ref sw, s);
}
else
{
//if this fails, log the exception in trace, but continue
HttpContext.Current.Trace.Warn("ClientDependency", "Could not load file contents from " + s, ex);
System.Diagnostics.Debug.Assert(false, "Could not load file contents from " + s, ex.Message);
}
}
}
if (type == ClientDependencyType.Javascript)
{
sw.Write(";;;"); //write semicolons in case the js isn't formatted correctly. This also helps for debugging.
}
}
sw.Flush();
byte[] outputBytes = ms.ToArray();
@@ -101,6 +141,74 @@ namespace umbraco.presentation.ClientDependency
return outputBytes;
}
/// <summary>
/// Writes the output of an external request to the stream. Returns true/false if succesful or not.
/// </summary>
/// <param name="sw"></param>
/// <param name="url"></param>
/// <returns></returns>
private bool WriteRequestToStream(ref StreamWriter sw, string url)
{
string requestOutput;
bool rVal = false;
rVal = TryReadUri(url, out requestOutput);
if (rVal)
{
//write the contents of the external request.
sw.WriteLine(requestOutput);
}
return rVal;
}
/// <summary>
/// Tries to convert the url to a uri, then read the request into a string and return it.
/// This takes into account relative vs absolute URI's
/// </summary>
/// <param name="url"></param>
/// <param name="requestContents"></param>
/// <returns></returns>
private bool TryReadUri(string url, out string requestContents)
{
Uri uri;
if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri))
{
if (uri.IsAbsoluteUri)
{
WebClient client = new WebClient();
try
{
requestContents = client.DownloadString(uri.AbsoluteUri);
return true;
}
catch (Exception ex)
{
HttpContext.Current.Trace.Warn("ClientDependency", "Could not load file contents from " + url, ex);
System.Diagnostics.Debug.Assert(false, "Could not load file contents from " + url, ex.Message);
}
}
else
{
//its a relative path so use the execute method
StringWriter sw = new StringWriter();
try
{
HttpContext.Current.Server.Execute(url, sw);
requestContents = sw.ToString();
sw.Close();
return true;
}
catch (Exception ex)
{
HttpContext.Current.Trace.Warn("ClientDependency", "Could not load file contents from " + url, ex);
System.Diagnostics.Debug.Assert(false, "Could not load file contents from " + url, ex.Message);
}
}
}
requestContents = "";
return false;
}
/// <summary>
/// Sets the output cache parameters and also the client side caching parameters
/// </summary>
@@ -112,7 +220,7 @@ namespace umbraco.presentation.ClientDependency
TimeSpan duration = TimeSpan.FromDays(10);
HttpCachePolicy cache = context.Response.Cache;
cache.SetCacheability(HttpCacheability.Public);
cache.SetExpires(DateTime.Now.Add(duration));
cache.SetExpires(DateTime.Now.Add(duration));
cache.SetMaxAge(duration);
cache.SetValidUntilExpires(true);
cache.SetLastModified(DateTime.Now);
@@ -133,7 +241,7 @@ namespace umbraco.presentation.ClientDependency
/// Compresses the bytes if the browser supports it
/// </summary>
private byte[] CompressBytes(HttpContext context, byte[] fileBytes)
{
{
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(acceptEncoding))
{
@@ -141,18 +249,21 @@ namespace umbraco.presentation.ClientDependency
Stream compressedStream = null;
acceptEncoding = acceptEncoding.ToLowerInvariant();
bool isCompressed = false;
if (acceptEncoding.Contains("gzip"))
{
context.Response.AddHeader("Content-encoding", "gzip");
compressedStream = new GZipStream(ms, CompressionMode.Compress, true);
isCompressed = true;
}
else if (acceptEncoding.Contains("deflate"))
//deflate is faster in .Net according to Mads Kristensen (blogengine.net)
if (acceptEncoding.Contains("deflate"))
{
context.Response.AddHeader("Content-encoding", "deflate");
compressedStream = new DeflateStream(ms, CompressionMode.Compress, true);
isCompressed = true;
}
else if (acceptEncoding.Contains("gzip"))
{
context.Response.AddHeader("Content-encoding", "gzip");
compressedStream = new GZipStream(ms, CompressionMode.Compress, true);
isCompressed = true;
}
if (isCompressed)
{
//write the bytes to the compressed stream

View File

@@ -12,5 +12,6 @@ namespace umbraco.presentation.ClientDependency
int Priority { get; set; }
//string CompositeGroupName { get; set; }
string PathNameAlias { get; set; }
bool DoNotOptimize { get; set; }
}
}

View File

@@ -4,6 +4,7 @@ using System.Text;
using System.Web.UI;
using System.Configuration.Provider;
using System.Web;
using System.Linq;
namespace umbraco.presentation.ClientDependency.Providers
{
@@ -20,6 +21,8 @@ namespace umbraco.presentation.ClientDependency.Providers
protected abstract void RegisterJsFiles(List<IClientDependencyFile> jsDependencies);
protected abstract void RegisterCssFiles(List<IClientDependencyFile> cssDependencies);
protected abstract void ProcessSingleJsFile(string js);
protected abstract void ProcessSingleCssFile(string css);
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
@@ -98,31 +101,44 @@ namespace umbraco.presentation.ClientDependency.Providers
//}
/// <summary>
/// Returns a full url with the encoded query strings for the handler which will process the composite group.
/// Returns a list of urls. The array will consist of only one entry if
/// none of the dependencies are tagged as DoNotOptimize, otherwise, if any of them are,
/// this will return the path to the file.
///
/// For the optimized files, the full url with the encoded query strings for the handler which will process the composite list
/// of dependencies. The handler will compbine, compress, minify (if JS), and output cache the results
/// based on a hash key of the base64 encoded string.
/// </summary>
/// <param name="dependencies"></param>
/// <param name="groupName"></param>
/// <remarks>
/// If the DoNotOptimize setting has been set for any of the dependencies in the list, then this will ignore them.
/// </remarks>
/// <returns></returns>
//private string ProcessCompositeGroup(List<IClientDependencyFile> dependencies, string groupName, ClientDependencyType type)
//{
// string handler = "{0}?s={1}&t={2}";
// StringBuilder files = new StringBuilder();
// List<IClientDependencyFile> byGroup = dependencies.FindAll(
// delegate(IClientDependencyFile a)
// {
// return a.CompositeGroupName == groupName;
// }
// );
// byGroup.Sort((a, b) => a.Priority.CompareTo(b.Priority));
// foreach (IClientDependencyFile a in byGroup)
// {
// files.Append(a.FilePath + ";");
// }
// string url = string.Format(handler, CompositeDependencyHandler.HandlerFileName, HttpContext.Current.Server.UrlEncode(EncodeTo64(files.ToString())), type.ToString());
// if (url.Length > CompositeDependencyHandler.MaxHandlerUrlLength)
// throw new ArgumentOutOfRangeException("The number of files in the composite group " + groupName + " creates a url handler address that exceeds the CompositeDependencyHandler MaxHandlerUrlLength. Reducing the amount of files in this composite group should fix the issue");
// return url;
//}
protected List<string> ProcessCompositeList(List<IClientDependencyFile> dependencies, ClientDependencyType type)
{
List<string> rVal = new List<string>();
//build the combined composite list url
string handler = "{0}?s={1}&t={2}";
StringBuilder files = new StringBuilder();
foreach (IClientDependencyFile a in dependencies.Where(x => !x.DoNotOptimize))
{
files.Append(a.FilePath + ";");
}
string combinedurl = string.Format(handler, CompositeDependencyHandler.HandlerFileName, HttpContext.Current.Server.UrlEncode(EncodeTo64(files.ToString())), type.ToString());
rVal.Add(combinedurl);
//add any urls that are not to be optimized
foreach (IClientDependencyFile a in dependencies.Where(x => x.DoNotOptimize))
{
rVal.Add(a.FilePath);
}
//if (url.Length > CompositeDependencyHandler.MaxHandlerUrlLength)
// throw new ArgumentOutOfRangeException("The number of files in the composite group " + groupName + " creates a url handler address that exceeds the CompositeDependencyHandler MaxHandlerUrlLength. Reducing the amount of files in this composite group should fix the issue");
return rVal;
}
private string EncodeTo64(string toEncode)
{
@@ -164,21 +180,21 @@ namespace umbraco.presentation.ClientDependency.Providers
}
}
internal class SimpleDependencyFile : IClientDependencyFile
{
public SimpleDependencyFile(string filePath, ClientDependencyType type)
{
FilePath = filePath;
DependencyType = type;
}
//internal class SimpleDependencyFile : IClientDependencyFile
//{
// public SimpleDependencyFile(string filePath, ClientDependencyType type)
// {
// FilePath = filePath;
// DependencyType = type;
// }
public string FilePath{get;set;}
public ClientDependencyType DependencyType { get; set; }
public string InvokeJavascriptMethodOnLoad { get; set; }
public int Priority { get; set; }
//public string CompositeGroupName { get; set; }
public string PathNameAlias { get; set; }
}
// public string FilePath{get;set;}
// public ClientDependencyType DependencyType { get; set; }
// public string InvokeJavascriptMethodOnLoad { get; set; }
// public int Priority { get; set; }
// //public string CompositeGroupName { get; set; }
// public string PathNameAlias { get; set; }
//}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Linq;
namespace umbraco.presentation.ClientDependency.Providers
{
@@ -20,6 +21,41 @@ namespace umbraco.presentation.ClientDependency.Providers
get { return (string)base["defaultProvider"]; }
set { base["defaultProvider"] = value; }
}
/// <summary>
/// The configuration section to set the FileBasedDependencyExtensionList. This is a comma separated list.
/// </summary>
/// <remarks>
/// If this is not explicitly set, then the extensions 'js' and 'css' are the defaults.
/// </remarks>
[ConfigurationProperty("fileDependencyExtensions", DefaultValue = "js,css")]
protected string FileBasedDepdendenyExtensions
{
get { return (string)base["fileDependencyExtensions"]; }
set { base["fileDependencyExtensions"] = value; }
}
/// <summary>
/// The file extensions of Client Dependencies that are file based as opposed to request based.
/// Any file that doesn't have the extensions listed here will be request based, request based is
/// more overhead for the server to process.
/// </summary>
/// <example>
/// A request based JavaScript file may be a .ashx that dynamically creates JavaScript server side.
/// </example>
/// <remarks>
/// If this is not explicitly set, then the extensions 'js' and 'css' are the defaults.
/// </remarks>
public List<string> FileBasedDependencyExtensionList
{
get
{
return FileBasedDepdendenyExtensions.Split(',')
.Select(x => x.Trim().ToLower())
.ToList();
}
}
}
}

View File

@@ -43,6 +43,11 @@ namespace umbraco.presentation.ClientDependency.Providers
dependencyCalls.ToString(), true);
}
protected override void ProcessSingleJsFile(string js)
{
throw new NotImplementedException();
}
protected override void RegisterCssFiles(List<IClientDependencyFile> cssDependencies)
{
if (cssDependencies.Count == 0)
@@ -62,6 +67,11 @@ namespace umbraco.presentation.ClientDependency.Providers
dependencyCalls.ToString(), true);
}
protected override void ProcessSingleCssFile(string css)
{
throw new NotImplementedException();
}
private void RegisterDependencyLoader()
{
// register loader script

View File

@@ -9,6 +9,8 @@ namespace umbraco.presentation.ClientDependency.Providers
{
public const string DefaultName = "PageHeaderProvider";
public const string ScriptEmbed = "<script type=\"text/javascript\" src=\"{0}\"></script>";
public const string CssEmbed = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
@@ -21,22 +23,55 @@ namespace umbraco.presentation.ClientDependency.Providers
protected override void RegisterJsFiles(List<IClientDependencyFile> jsDependencies)
{
string js = "<script type=\"text/javascript\" src=\"{0}\"></script>";
foreach (IClientDependencyFile dependency in jsDependencies)
if (IsDebugMode)
{
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Registering: {0}", dependency.FilePath));
DependantControl.Page.Header.Controls.Add(new LiteralControl(string.Format(js, dependency.FilePath)));
foreach (IClientDependencyFile dependency in jsDependencies)
{
ProcessSingleJsFile(dependency.FilePath);
}
}
else
{
List<string> jsList = ProcessCompositeList(jsDependencies, ClientDependencyType.Javascript);
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Processed composite list: {0}", jsList[0]));
foreach (string js in jsList)
{
ProcessSingleJsFile(js);
}
}
}
protected override void ProcessSingleJsFile(string js)
{
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Registering: {0}", js));
DependantControl.Page.Header.Controls.Add(new LiteralControl(string.Format(ScriptEmbed, js)));
}
protected override void RegisterCssFiles(List<IClientDependencyFile> cssDependencies)
{
string css = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
foreach (IClientDependencyFile dependency in cssDependencies)
if (IsDebugMode)
{
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Registering: {0}", dependency.FilePath));
DependantControl.Page.Header.Controls.Add(new LiteralControl(string.Format(css, dependency.FilePath)));
foreach (IClientDependencyFile dependency in cssDependencies)
{
ProcessSingleCssFile(dependency.FilePath);
}
}
else
{
List<string> cssList = ProcessCompositeList(cssDependencies, ClientDependencyType.Css);
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Processed composite list: {0}", cssList[0]));
foreach (string css in cssList)
{
ProcessSingleCssFile(css);
}
}
}
protected override void ProcessSingleCssFile(string css)
{
DependantControl.Page.Trace.Write("ClientDependency", string.Format("Registering: {0}", css));
DependantControl.Page.Header.Controls.Add(new LiteralControl(string.Format(CssEmbed, css)));
}
}
}