示例#1
0
        /// <summary>
        /// Generates a value for HTTP header "ETag" based on
        /// information about processed asset
        /// </summary>
        /// <param name="assetContent">Text content of asset</param>
        /// <returns>ETag value</returns>
        private static string GenerateAssetETag(string assetContent)
        {
            string str;

            using (SHA256 hashAlgorithm = AssetHandlerBase.CreateHashAlgorithm())
                str = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(assetContent)));
            return(string.Format("\"{0}\"", (object)str));
        }
示例#2
0
        public void ProcessRequest(HttpContextBase context)
        {
            this._context = context;
            HttpRequestBase  request  = context.Request;
            HttpResponseBase response = context.Response;
            Uri    url = request.Url;
            string str = !(url == (Uri)null) ? url.LocalPath : throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsNull);

            if (string.IsNullOrWhiteSpace(str))
            {
                throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsEmpty);
            }
            if (!this._virtualFileSystemWrapper.FileExists(str))
            {
                throw new HttpException(404, string.Format(BundleTransformer.Core.Resources.Strings.Common_FileNotExist, (object)str));
            }
            string bundleVirtualPath = request.QueryString[Common.BundleVirtualPathQueryStringParameterName];

            if (string.IsNullOrWhiteSpace(bundleVirtualPath) && this.IsStaticAsset)
            {
                AssetHandlerBase.ProcessStaticAssetRequest(context);
            }
            else
            {
                string processedAssetContent;
                try
                {
                    processedAssetContent = this.GetProcessedAssetContent(str, bundleVirtualPath);
                }
                catch (HttpException ex)
                {
                    throw;
                }
                catch (AssetTranslationException ex)
                {
                    throw new HttpException(500, ex.Message, (Exception)ex);
                }
                catch (AssetPostProcessingException ex)
                {
                    throw new HttpException(500, ex.Message, (Exception)ex);
                }
                catch (FileNotFoundException ex)
                {
                    throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_DependencyNotFound, (object)ex.Message, (object)ex));
                }
                catch (Exception ex)
                {
                    throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_UnknownError, (object)ex.Message, (object)ex));
                }
                HttpCachePolicyBase cache = response.Cache;
                if (this._assetHandlerConfig.DisableClientCache)
                {
                    response.StatusCode        = 200;
                    response.StatusDescription = "OK";
                    cache.SetCacheability(HttpCacheability.NoCache);
                    cache.SetExpires(DateTime.UtcNow.AddYears(-1));
                    cache.SetValidUntilExpires(false);
                    cache.SetNoStore();
                    cache.SetNoServerCaching();
                    response.ContentType = this.ContentType;
                    response.Write(processedAssetContent);
                }
                else
                {
                    string assetEtag = AssetHandlerBase.GenerateAssetETag(processedAssetContent);
                    int    num       = AssetHandlerBase.IsETagHeaderChanged(request, assetEtag) ? 1 : 0;
                    if (num != 0)
                    {
                        response.StatusCode        = 200;
                        response.StatusDescription = "OK";
                    }
                    else
                    {
                        response.StatusCode        = 304;
                        response.StatusDescription = "Not Modified";
                        response.AddHeader("Content-Length", "0");
                    }
                    response.AddHeader("X-Asset-Transformation-Powered-By", "Bundle Transformer");
                    cache.SetCacheability(HttpCacheability.Public);
                    cache.SetExpires(DateTime.UtcNow.AddYears(-1));
                    cache.SetValidUntilExpires(true);
                    cache.AppendCacheExtension("must-revalidate");
                    cache.SetNoServerCaching();
                    cache.VaryByHeaders["If-None-Match"] = true;
                    cache.SetETag(assetEtag);
                    if (num != 0)
                    {
                        response.ContentType = this.ContentType;
                        response.Write(processedAssetContent);
                    }
                }
                context.ApplicationInstance.CompleteRequest();
            }
        }