ConverterJavaScriptHandler.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:3k
- 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 converter methods.
- /// </summary>
- public class ConverterJavaScriptHandler : IHttpHandler
- {
- #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 + ".converter"] != null)
- {
- CacheInfo ci = (CacheInfo)context.Cache[Constant.AjaxID + ".converter"];
- 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("converter"));
- 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);
- if(Utility.Settings != null && Utility.Settings.Encryption != null)
- {
- context.Response.Write(Utility.Settings.Encryption.CryptProvider.ClientScript);
- context.Response.Write("rn");
- context.Response.Write(Utility.Settings.Encryption.KeyProvider.ClientScript);
- context.Response.Write("rn");
- }
- foreach(IJavaScriptConverter c in Utility.Settings.JavaScriptConverters)
- {
- context.Response.Write(c.GetClientScript());
- context.Response.Write("rn");
- }
- context.Cache.Add(Constant.AjaxID + ".converter", 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
- }
- }