ConverterJavaScriptHandler.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:3k
源码类别:

Ajax

开发平台:

C#

  1. using System;
  2. using System.Reflection;
  3. using System.Web;
  4. using System.Web.Caching;
  5. using System.IO;
  6. namespace AjaxPro
  7. {
  8. /// <summary>
  9. /// Represents an IHttpHandler for the client-side JavaScript converter methods.
  10. /// </summary>
  11. public class ConverterJavaScriptHandler : IHttpHandler
  12. {
  13. #region IHttpHandler Members
  14. public void ProcessRequest(HttpContext context)
  15. {
  16. string etag = context.Request.Headers["If-None-Match"];
  17. string modSince = context.Request.Headers["If-Modified-Since"];
  18. if(context.Cache[Constant.AjaxID + ".converter"] != null)
  19. {
  20. CacheInfo ci = (CacheInfo)context.Cache[Constant.AjaxID + ".converter"];
  21. if(etag != null)
  22. {
  23. if(etag == ci.ETag) // TODO: null check
  24. {
  25. context.Response.StatusCode = 304;
  26. return;
  27. }
  28. }
  29. if(modSince != null)
  30. {
  31. try
  32. {
  33. DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime();
  34. if(DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0)
  35. {
  36. context.Response.StatusCode = 304;
  37. return;
  38. }
  39. }
  40. catch(Exception)
  41. {
  42. 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.");
  43. }
  44. }
  45. }
  46. etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes("converter"));
  47. DateTime now = DateTime.Now;
  48. DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); //.ToUniversalTime();
  49. context.Response.AddHeader("Content-Type", "application/x-javascript");
  50. context.Response.ContentEncoding = System.Text.Encoding.UTF8;
  51. context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
  52. context.Response.Cache.SetETag(etag);
  53. context.Response.Cache.SetLastModified(lastMod);
  54. if(Utility.Settings != null && Utility.Settings.Encryption != null)
  55. {
  56. context.Response.Write(Utility.Settings.Encryption.CryptProvider.ClientScript);
  57. context.Response.Write("rn");
  58. context.Response.Write(Utility.Settings.Encryption.KeyProvider.ClientScript);
  59. context.Response.Write("rn");
  60. }
  61. foreach(IJavaScriptConverter c in Utility.Settings.JavaScriptConverters)
  62. {
  63. context.Response.Write(c.GetClientScript());
  64. context.Response.Write("rn");
  65. }
  66. context.Cache.Add(Constant.AjaxID + ".converter", new CacheInfo(etag, lastMod), null, 
  67. System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
  68. System.Web.Caching.CacheItemPriority.Normal, null);
  69. }
  70. public bool IsReusable
  71. {
  72. get
  73. {
  74. return false;
  75. }
  76. }
  77. #endregion
  78. }
  79. }