context: /********
   * This file is part of Ext.NET.
   *     
   * Ext.NET is free software: you can redistribute it and/or modify
   * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as 
   * published by the Free Software Foundation, either version 3 of the 
   * License, or (at your option) any later version.
   * 
   * Ext.NET is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   * 
   * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
   * along with Ext.NET.  If not, see <http://www.gnu.org/licenses/>.
   *
   *
   * @version   : 2.0.0.beta - Community Edition (AGPLv3 License)
   * @author    : Ext.NET, Inc. http://www.ext.net/
   * @date      : 2012-03-07
   * @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
   * @license   : GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) 3.0. 
   *              See license.txt and http://www.ext.net/license/.
   *              See AGPL License at http://www.gnu.org/licenses/agpl-3.0.txt
   ********/
  
  using System;
  using System.ComponentModel;
  using System.Configuration;
  using System.IO;
  using System.Reflection;
  using System.Security.Permissions;
  using System.Text;
  using System.Web;
  using System.Web.Configuration;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.Design;
  using System.Xml;
  
  using Ext.Net.Utilities;
  
  namespace Ext.Net
  {
      /// <summary>
      /// 
      /// </summary>
      [Description("")]
      public partial class InitializationScriptNotFoundException : NullReferenceException
      {
          /// <summary>
          /// 
          /// </summary>
          /// <param name="errorMessage"></param>
          [Description("")]
          public InitializationScriptNotFoundException(string errorMessage)
              : base(errorMessage) { }
  
          /// <summary>
          /// 
          /// </summary>
          /// <param name="errorMessage"></param>
          /// <param name="inner"></param>
          [Description("")]
          public InitializationScriptNotFoundException(string errorMessage, Exception inner)
              : base(errorMessage, inner) { }
      }
      
      internal class ResourceHandler : Page, IHttpHandler, IRequiresSessionState
      {
          Stream stream;
          ResourceManager sm;
          StringBuilder sb;
          HttpContext context;
          string webResource;
          byte[] output;
          int length;
          bool compress;
  
          private static long GetAssemblyTime(Assembly assembly)
          {
              AssemblyName assemblyName = assembly.GetName();
  
              return File.GetLastWriteTime(new Uri(assemblyName.CodeBase).LocalPath).Ticks;
          }
  
          private static bool IsSourceModified(HttpRequest request)
          {
              bool dateModified = false;
  
              string requestIfModifiedSinceHeader = request.Headers["If-Modified-Since"] ?? string.Empty;
              DateTime requestIfModifiedSince;
              DateTime.TryParse(requestIfModifiedSinceHeader, out requestIfModifiedSince);
  
              DateTime responseLastModified = new DateTime(ResourceHandler.GetAssemblyTime(typeof(ResourceHandler).Assembly)).ToUniversalTime();
  
              if (requestIfModifiedSince != DateTime.MinValue && responseLastModified != DateTime.MinValue)
              {
                  requestIfModifiedSince = requestIfModifiedSince.ToUniversalTime();
  
                  if (responseLastModified > requestIfModifiedSince)
                  {
                      TimeSpan diff = responseLastModified - requestIfModifiedSince;
                      if (diff > TimeSpan.FromSeconds(1))
                      {
                          dateModified = true;
                      }
                  }
              }
              else
              {
                  dateModified = true;
              }           
  
              return dateModified;
          }
  
          /// <summary>
          /// 
          /// </summary>
          /// <param name="context"></param>
          [Description("")]
          public override void ProcessRequest(HttpContext context)
          {
              this.context = context;
              
              string file = this.context.Request.RawUrl;
  
              bool isInitScript = file.Contains("extnet/extnet-init-js/ext.axd?");
  
              if ((!isInitScript && !ResourceHandler.IsSourceModified(context.Request)) || (isInitScript && !string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])))
              {
                  context.Response.SuppressContent = true;
                  context.Response.StatusCode = 304;
                  context.Response.StatusDescription = "Not Modified";
                  context.Response.AddHeader("Content-Length", "0");
                  return;
              }
              
              this.SetResponseCache(context);
  
              if (isInitScript)
              {
                  string key = file.RightOfRightmostOf('?');
  
                  if (key.IsNotEmpty())
                  {
                      try
                      {
                          string script = this.context.Application[key].ToString();
                          this.context.Session.Remove(key);                        
                          CompressionUtils.GZipAndSend(script);
                      }
                      catch (NullReferenceException)
                      {
                          throw new InitializationScriptNotFoundException("The Ext.NET initialization script was not found.");
                      }
                  }
              }
              else
              {
                  try
                  {
                      this.sm = new ResourceManager();
                      this.compress = CompressionUtils.IsGZipSupported && this.sm.GZip;
  
                      this.SetWebResourceName(file);
  
                      this.stream = this.GetType().Assembly.GetManifestResourceStream(this.webResource);
                      string ext = this.webResource.RightOfRightmostOf('.');
                      this.compress = this.compress && !this.IsImage(ext);
  
                      switch (ext)
                      {
                          case "js":
                              this.WriteFile("text/javascript");
                              break;
                          case "css":
                              this.WriteFile("text/css");
                              break;
  
                          case "gif":
                              this.WriteImage("image/gif");
                              break;
                          case "png":
                              this.WriteImage("image/png");
                              break;
                          case "jpg":
                          case "jpeg":
                              this.WriteImage("image/jpg");
                              break;
                      }
                  }
                  catch (Exception e)
                  {
                      string s = this.IsDebugging ? e.ToString() : e.Message;
                      context.Response.StatusDescription = s.Substring(0, Math.Min(s.Length, 512));
                      this.context.Response.Redirect(Page.ClientScript.GetWebResourceUrl(this.sm.GetType(), this.webResource));
                  }
                  finally
                  {
                      if (this.stream != null)
                      {
                          this.stream.Close();
                      }
                  }
              }
          }
  
          private bool IsImage(string ext)
          {
              return ext == "gif" || ext == "png" || ext == "jpg" || ext == "jpeg";
          }
  
          private bool IsDebugging
          {
              get
              {
                  bool result = false;
  
                  if (HttpContext.Current != null)
                  {
                      result = HttpContext.Current.IsDebuggingEnabled;
                  }
  
                  return result;
              }
          }
  
          private void SetResponseCache(HttpContext context)
          {
              HttpCachePolicy cache = context.Response.Cache;
              DateTime modifiedDate = new DateTime(ResourceHandler.GetAssemblyTime(typeof(ResourceHandler).Assembly)).ToUniversalTime();
              DateTime nowDate = DateTime.Now.ToUniversalTime().AddSeconds(-1);
  
              if (modifiedDate > nowDate)
              {
                  modifiedDate = nowDate;
              }
  
              cache.SetLastModified(modifiedDate);
              cache.SetOmitVaryStar(true);
              cache.SetVaryByCustom("v");
              cache.SetExpires(DateTime.UtcNow.AddDays(365));
              cache.SetMaxAge(TimeSpan.FromDays(365));
              cache.SetValidUntilExpires(true);
              cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
              cache.SetCacheability(HttpCacheability.Public);
              cache.SetLastModifiedFromFileDependencies();
          }
  
          private void WriteFile(string responseType)
          {
              this.output = this.GetCache();
  
              if (this.output != null)
              {
                  this.Send(this.output, responseType);
                  
                  return;
              }
  
              this.sb = new StringBuilder(4096);
  
              using (this.stream)
              {
                  StreamReader reader = new StreamReader(this.stream);
                  this.sb.Append(reader.ReadToEnd());
                  reader.Close();
              }
  
  
gt:             string data = responseType == "text/css"