EmbeddedJavaScriptHandler.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:3k
- /*
- * MS 06-04-05 added oldstyled Object.prototype.extend code, enabled by web.config
- * setting oldStyleobjectExtendPrototype
- *
- *
- *
- */
- using System;
- using System.Reflection;
- using System.Web;
- using System.Web.Caching;
- using System.IO;
- namespace AjaxPro
- {
- /// <summary>
- /// Represents an IHttpHandler for the client-side JavaScript prototype and core methods.
- /// </summary>
- public class EmbeddedJavaScriptHandler : IHttpHandler
- {
- private string fileName;
- public EmbeddedJavaScriptHandler(string fileName)
- {
- this.fileName = fileName;
- }
- #region IHttpHandler Members
- public void ProcessRequest(HttpContext context)
- {
- string etag = context.Request.Headers["If-None-Match"];
- string modSince = context.Request.Headers["If-Modified-Since"];
- if(context.Cache[Constant.AjaxID + "." + fileName] != null)
- {
- CacheInfo ci = (CacheInfo)context.Cache[Constant.AjaxID + "." + fileName];
- if(etag != null)
- {
- if(etag == ci.ETag) // TODO: null check
- {
- context.Response.StatusCode = 304;
- return;
- }
- }
-
- if(modSince != null)
- {
- try
- {
- DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime();
- if(DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0)
- {
- context.Response.StatusCode = 304;
- return;
- }
- }
- catch(Exception)
- {
- if(context.Trace.IsEnabled) context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime.");
- }
- }
- }
- etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes(fileName));
- DateTime now = DateTime.Now;
- DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); //.ToUniversalTime();
- context.Response.AddHeader("Content-Type", "application/x-javascript");
- context.Response.ContentEncoding = System.Text.Encoding.UTF8;
- context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
- context.Response.Cache.SetETag(etag);
- context.Response.Cache.SetLastModified(lastMod);
- // Now, we want to read the JavaScript embedded source
- // from the assembly.
- Assembly assembly = Assembly.GetExecutingAssembly();
- Stream s = assembly.GetManifestResourceStream(Constant.AssemblyName + "." + fileName + ".js");
- if(s != null)
- {
- System.IO.StreamReader sr = new System.IO.StreamReader(s);
-
- context.Response.Write(sr.ReadToEnd());
- context.Response.Write("rn");
- sr.Close();
- if (fileName == "prototype")
- {
- if (AjaxPro.Utility.Settings.OldStyle.Contains("objectExtendPrototype"))
- {
- context.Response.Write(@"
- Object.prototype.extend = function(o, override) {
- return Object.extend.apply(this, [this, o, override != false]);
- }
- ");
- }
- }
- }
- context.Cache.Add(Constant.AjaxID + "." + fileName, new CacheInfo(etag, lastMod), null,
- System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
- System.Web.Caching.CacheItemPriority.Normal, null);
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- #endregion
- }
- }