/// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PostResolveRequestCache(object sender, EventArgs e)
        {
            HttpContext Context = HttpContext.Current;
            HttpRequest Request = Context.Request;

            // Skip over anything we don't care about immediately
            if (!Request.Url.LocalPath.ToLower().Contains("wwsc.axd"))
                return;

            HttpResponse Response = Context.Response;
            string acceptEncoding = Request.Headers["Accept-Encoding"];

            // Start by checking whether GZip is supported by client
            bool useGZip = false;
            if (!string.IsNullOrEmpty(acceptEncoding) &&
                acceptEncoding.ToLower().Contains("gzip"))
                useGZip = true;

            string resource = Request.QueryString["r"] ?? "";
            if (string.IsNullOrEmpty(resource))
            {
                SendErrorResponse("Invalid Resource");
                return;
            }
            //resource = Encoding.ASCII.GetString(Convert.FromBase64String(resource));


            //string contentType = STR_JavaScript_ContentType;
            //if (resource.ToLower().EndsWith(".css"))
            //    contentType = STR_Css_ContentType;

            // Create a cachekey and check whether it exists
            string cacheKey = Request.QueryString.ToString() + useGZip.ToString();

            WebResourceCacheItem cacheItem = Context.Cache[cacheKey] as WebResourceCacheItem;
            if (cacheItem != null)
            {
                // Yup - read cache and send to client
                SendTextOutput(cacheItem.Content, cacheItem.IsCompressed, cacheItem.ContentType);
                return;
            }
            cacheItem = new WebResourceCacheItem();

            // Retrieve information about resource embedded
            // Values are base64 encoded
            string resourceTypeName = Request.QueryString["t"];

            // Try to locate the assembly that houses the Resource
            Assembly resourceAssembly = null;

            // If no type is passed use the current assembly - otherwise
            // run through the loaded assemblies and try to find assembly
            if (string.IsNullOrEmpty(resourceTypeName))
                resourceAssembly = GetType().Assembly;
            else
            {
                Type t = ReflectionUtils.GetTypeFromName(resourceTypeName);
                if (t != null)
                {
                    resourceAssembly = t.Assembly;
                    if (resourceAssembly == null)
                    {
                        SendErrorResponse("Invalid Type Information");
                        return;
                    }
                }
            }

            // Look up the WebResource Attribute and ContentType
            WebResourceAttribute[] attr = resourceAssembly.GetCustomAttributes(typeof(WebResourceAttribute), false) as WebResourceAttribute[];
            if (attr != null && attr.Length > 0)
            {
                var res = attr.Where(at => at.WebResource.ToLower() == resource).FirstOrDefault();
                if (res != null)
                    cacheItem.ContentType = res.ContentType;
            }

            // otherwise default to javascript - primary use case
            if (string.IsNullOrEmpty(cacheItem.ContentType))
                cacheItem.ContentType = STR_JavaScript_ContentType;

            // Load the script file as a string from Resources
            string script = "";
            using (Stream st = resourceAssembly.GetManifestResourceStream(resource))
            {
                StreamReader sr = new StreamReader(st, Encoding.Default);
                script = sr.ReadToEnd();
            }

            // Optimize the script by removing comment lines and stripping spaces
            // Only applies to JavaScript
            if (cacheItem.ContentType == STR_JavaScript_ContentType && !Context.IsDebuggingEnabled)
                script = OptimizeScript(script);

            // Now we're ready to create out output
            // Don't GZip unless at least 4k
            if (useGZip && script.Length > 4096)
            {
                cacheItem.Content = GZipMemory(script);
                cacheItem.IsCompressed = true;
            }
            else
            {
                cacheItem.Content = Encoding.UTF8.GetBytes(script);
                cacheItem.IsCompressed = false;
            }

            // Add into the cache
            Context.Cache.Add(cacheKey, cacheItem, null, DateTime.UtcNow.AddDays(1), TimeSpan.Zero, CacheItemPriority.High, null);

            // Write out to Response object with appropriate Client Cache settings
            SendTextOutput(cacheItem.Content, cacheItem.IsCompressed, cacheItem.ContentType);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PostResolveRequestCache(object sender, EventArgs e)
        {
            HttpContext Context = HttpContext.Current;
            HttpRequest Request = Context.Request;

            // Skip over anything we don't care about immediately
            if (!Request.Url.LocalPath.ToLower().Contains("wwsc.axd"))
            {
                return;
            }

            HttpResponse Response       = Context.Response;
            string       acceptEncoding = Request.Headers["Accept-Encoding"];

            // Start by checking whether GZip is supported by client
            bool useGZip = false;

            if (!string.IsNullOrEmpty(acceptEncoding) &&
                acceptEncoding.ToLower().Contains("gzip"))
            {
                useGZip = true;
            }

            string resource = Request.QueryString["r"] ?? "";

            if (string.IsNullOrEmpty(resource))
            {
                SendErrorResponse("Invalid Resource");
                return;
            }
            //resource = Encoding.ASCII.GetString(Convert.FromBase64String(resource));


            //string contentType = STR_JavaScript_ContentType;
            //if (resource.ToLower().EndsWith(".css"))
            //    contentType = STR_Css_ContentType;

            // Create a cachekey and check whether it exists
            string cacheKey = Request.QueryString.ToString() + useGZip.ToString();

            WebResourceCacheItem cacheItem = Context.Cache[cacheKey] as WebResourceCacheItem;

            if (cacheItem != null)
            {
                // Yup - read cache and send to client
                SendTextOutput(cacheItem.Content, cacheItem.IsCompressed, cacheItem.ContentType);
                return;
            }
            cacheItem = new WebResourceCacheItem();

            // Retrieve information about resource embedded
            // Values are base64 encoded
            string resourceTypeName = Request.QueryString["t"];

            // Try to locate the assembly that houses the Resource
            Assembly resourceAssembly = null;

            // If no type is passed use the current assembly - otherwise
            // run through the loaded assemblies and try to find assembly
            if (string.IsNullOrEmpty(resourceTypeName))
            {
                resourceAssembly = GetType().Assembly;
            }
            else
            {
                Type t = ReflectionUtils.GetTypeFromName(resourceTypeName);
                if (t != null)
                {
                    resourceAssembly = t.Assembly;
                    if (resourceAssembly == null)
                    {
                        SendErrorResponse("Invalid Type Information");
                        return;
                    }
                }
            }

            // Look up the WebResource Attribute and ContentType
            WebResourceAttribute[] attr = resourceAssembly.GetCustomAttributes(typeof(WebResourceAttribute), false) as WebResourceAttribute[];
            if (attr != null && attr.Length > 0)
            {
                var res = attr.Where(at => at.WebResource.ToLower() == resource).FirstOrDefault();
                if (res != null)
                {
                    cacheItem.ContentType = res.ContentType;
                }
            }

            // otherwise default to javascript - primary use case
            if (string.IsNullOrEmpty(cacheItem.ContentType))
            {
                cacheItem.ContentType = STR_JavaScript_ContentType;
            }

            // Load the script file as a string from Resources
            string script = "";

            using (Stream st = resourceAssembly.GetManifestResourceStream(resource))
            {
                StreamReader sr = new StreamReader(st, Encoding.Default);
                script = sr.ReadToEnd();
            }

            // Optimize the script by removing comment lines and stripping spaces
            // Only applies to JavaScript
            if (cacheItem.ContentType == STR_JavaScript_ContentType && !Context.IsDebuggingEnabled)
            {
                script = OptimizeScript(script);
            }

            // Now we're ready to create out output
            // Don't GZip unless at least 4k
            if (useGZip && script.Length > 4096)
            {
                cacheItem.Content      = GZipMemory(script);
                cacheItem.IsCompressed = true;
            }
            else
            {
                cacheItem.Content      = Encoding.UTF8.GetBytes(script);
                cacheItem.IsCompressed = false;
            }

            // Add into the cache
            Context.Cache.Add(cacheKey, cacheItem, null, DateTime.UtcNow.AddDays(1), TimeSpan.Zero, CacheItemPriority.High, null);

            // Write out to Response object with appropriate Client Cache settings
            SendTextOutput(cacheItem.Content, cacheItem.IsCompressed, cacheItem.ContentType);
        }