Esempio n. 1
0
        public ActionResult Index()
        {
            IOptimizedResult sample  = ResourceHandler.Create <IOptimizedResult>(HomeController.SamplePath);
            IOptimizedResult support = ResourceHandler.Create <IOptimizedResult>(JbstController.SupportScriptPath);

            // populate data to be used in view
            return(this.View(new HomeViewModel(sample.Source, support)));
        }
Esempio n. 2
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="source"></param>
 /// <param name="support"></param>
 public HomeViewModel(string source, IOptimizedResult support)
 {
     this.SampleSource = HttpUtility.HtmlEncode(source).Trim();
     this.CompactSize  = FormatFileSize(support.Compacted.Length);
     this.FullSize     = FormatFileSize(support.PrettyPrinted.Length);
     this.GzipSize     = FormatFileSize(support.Gzipped.Length);
     this.DeflateSize  = FormatFileSize(support.Deflated.Length);
 }
Esempio n. 3
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="source"></param>
 /// <param name="support"></param>
 public HomeViewModel(string source, IOptimizedResult support)
 {
     this.SampleSource = HttpUtility.HtmlEncode(source).Trim();
     this.CompactSize = FormatFileSize(support.Compacted.Length);
     this.FullSize = FormatFileSize(support.PrettyPrinted.Length);
     this.GzipSize = FormatFileSize(support.Gzipped.Length);
     this.DeflateSize = FormatFileSize(support.Deflated.Length);
 }
Esempio n. 4
0
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            HttpResponse response = context.Response;

            try
            {
                response.ClearHeaders();
                response.ClearContent();
            }
            catch { }

            response.TrySkipIisCustomErrors = true;
            response.BufferOutput           = true;

            // specifying "DEBUG" in the query string gets the non-compacted form
            IOptimizedResult info = this.GetResourceInfo(context, this.IsDebug);

            if (info == null)
            {
                // either eTag 304 was sent or no resource found
                return;
            }

            bool isCached = StringComparer.OrdinalIgnoreCase.Equals(info.Hash, this.CacheKey);

            if (isCached)
            {
                // if the content changes then so will the hash
                // so we can effectively cache this forever
                response.ExpiresAbsolute = DateTime.UtcNow.AddYears(1);
            }

            response.ContentType = info.ContentType;

            string filename = Path.GetFileNameWithoutExtension(context.Request.FilePath) + '.' + info.FileExtension;

            // this helps IE determine the Content-Type
            // http://tools.ietf.org/html/rfc2183#section-2.3
            ContentDisposition disposition = new ContentDisposition
            {
                Inline   = true,
                FileName = filename
            };

            response.AddHeader("Content-Disposition", disposition.ToString());

            ResourceHandler.WriteResponse(context, info, this.IsDebug);
        }
Esempio n. 5
0
        private IOptimizedResult ProcessPrecompiled(IResourceBuildHelper helper, string file)
        {
            IOptimizedResult result = ResourceHandler.Create <IOptimizedResult>(file);

            if (result != null)
            {
                if (!this.isMimeSet &&
                    !String.IsNullOrEmpty(result.ContentType) &&
                    !String.IsNullOrEmpty(result.FileExtension))
                {
                    this.contentType   = result.ContentType;
                    this.fileExtension = result.FileExtension;
                    this.isMimeSet     = true;
                }

                if (result is IGlobalizedBuildResult)
                {
                    this.GlobalizationKeys.AddRange(((IGlobalizedBuildResult)result).GlobalizationKeys);
                }

                helper.AddVirtualPathDependency(file);

                ICollection dependencies = BuildManager.GetVirtualPathDependencies(file);
                if (dependencies != null)
                {
                    foreach (string dependency in dependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }

                if (result is IDependentResult)
                {
                    foreach (string dependency in ((IDependentResult)result).VirtualPathDependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }
            }

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Determines the appropriate source for the incomming request
        /// </summary>
        /// <param name="context"></param>
        /// <param name="isDebug"></param>
        /// <returns>CompiledBuildResult</returns>
        protected virtual IOptimizedResult GetResourceInfo(HttpContext context, bool isDebug)
        {
            string           virtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
            IOptimizedResult info        = ResourceHandler.Create <IOptimizedResult>(virtualPath);

            if (info == null)
            {
                throw new HttpException(404, "Resource not found: " + virtualPath);
            }

            // check if client has cached copy
            ETag etag = new HashETag(info.Hash);

            if (etag.HandleETag(context, HttpCacheability.ServerAndPrivate, isDebug))
            {
                return(null);
            }

            return(info);
        }
Esempio n. 7
0
        /// <summary>
        /// Determines the most compact Content-Encoding supported by request.
        /// </summary>
        /// <param name="acceptEncoding"></param>
        /// <param name="isDebug"></param>
        /// <returns>optimal format</returns>
        private static BuildResultType GetOutputEncoding(IOptimizedResult result, HttpContext context, bool isDebug)
        {
            if (isDebug)
            {
                // short cut all debug builds
                return(BuildResultType.PrettyPrint);
            }

            string acceptEncoding = context.Request.Headers[ResourceHandler.HeaderAcceptEncoding];

            if (String.IsNullOrEmpty(acceptEncoding))
            {
                // not compressed but fully compacted
                return(BuildResultType.Compact);
            }

            acceptEncoding = acceptEncoding.ToLowerInvariant();

            if (result.Deflated != null &&
                result.Deflated.Length > 0 &&
                acceptEncoding.Contains(ResourceHandler.DeflateContentEncoding))
            {
                // compressed with Deflate
                return(BuildResultType.Deflate);
            }

            if (result.Gzipped != null &&
                result.Gzipped.Length > 0 &&
                acceptEncoding.Contains(ResourceHandler.GzipContentEncoding))
            {
                // compressed with Gzip
                return(BuildResultType.Gzip);
            }

            // not compressed but fully compacted
            return(BuildResultType.Compact);
        }
Esempio n. 8
0
        /// <summary>
        /// Writes the infor object to the HttpResult stream
        /// </summary>
        /// <param name="context"></param>
        /// <param name="info"></param>
        /// <param name="isDebug"></param>
        public static void WriteResponse(HttpContext context, IOptimizedResult info, bool isDebug)
        {
            HttpResponse response = context.Response;

            switch (ResourceHandler.GetOutputEncoding(info, context, isDebug))
            {
            case BuildResultType.PrettyPrint:
            {
                response.ContentEncoding = Encoding.UTF8;
                response.Output.Write(info.PrettyPrinted);
                break;
            }

            case BuildResultType.Gzip:
            {
                response.AppendHeader(ResourceHandler.HeaderContentEncoding, ResourceHandler.GzipContentEncoding);
                response.OutputStream.Write(info.Gzipped, 0, info.Gzipped.Length);
                break;
            }

            case BuildResultType.Deflate:
            {
                response.AppendHeader(ResourceHandler.HeaderContentEncoding, ResourceHandler.DeflateContentEncoding);
                response.OutputStream.Write(info.Deflated, 0, info.Deflated.Length);
                break;
            }

            case BuildResultType.Compact:
            default:
            {
                response.ContentEncoding = Encoding.UTF8;
                response.Output.Write(info.Compacted);
                break;
            }
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="path"></param>
 public ResourceResult(string resourcePath)
 {
     this.ResourcePath = resourcePath;
     this.Resource     = ResourceHandler.Create <IOptimizedResult>(resourcePath);
     this.IsDebug      = HttpContext.Current.IsDebuggingEnabled;
 }
Esempio n. 10
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="path"></param>
 public ResourceResult(string resourcePath)
 {
     this.ResourcePath = resourcePath;
     this.Resource = ResourceHandler.Create<IOptimizedResult>(resourcePath);
     this.IsDebug = HttpContext.Current.IsDebuggingEnabled;
 }
Esempio n. 11
0
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            if (String.IsNullOrEmpty(sourceText))
            {
                resource  = null;
                compacted = null;
                return;
            }

            StringBuilder resources = new StringBuilder();
            StringBuilder compacts  = new StringBuilder();

            string[] files = sourceText.Split(LineDelims, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < files.Length; i++)
            {
                try
                {
                    string file       = files[i],
                           compactAlt = null;

                    if (file != null)
                    {
                        file = file.Trim();
                    }

                    // skip blank and comment lines
                    if (String.IsNullOrEmpty(file) ||
                        file.StartsWith("//") ||
                        file.StartsWith("#"))
                    {
                        continue;
                    }

                    MergeResourceCodeProvider.SplitAlternates(file, out file, out compactAlt);

                    if (file.IndexOf("://") >= 0)
                    {
                        string preProcessed, compact;

                        this.ProcessExternalResource(helper, file, out preProcessed, out compact, errors);

                        if (!String.IsNullOrEmpty(compactAlt))
                        {
                            this.ProcessExternalResource(helper, compactAlt, out compactAlt, out compact, errors);
                        }

                        compacts.Append(compact);
                        resources.Append(preProcessed);
                        continue;
                    }

                    // process embedded resource
                    if (file.IndexOf(',') >= 0)
                    {
                        string preProcessed, compact;

                        this.ProcessEmbeddedResource(helper, file, out preProcessed, out compact, errors);

                        if (!String.IsNullOrEmpty(compactAlt))
                        {
                            this.ProcessEmbeddedResource(helper, compactAlt, out compactAlt, out compact, errors);
                        }

                        compacts.Append(compact);
                        resources.Append(preProcessed);
                        continue;
                    }

                    file = ResourceHandler.EnsureAppRelative(file);
                    if (!String.IsNullOrEmpty(compactAlt))
                    {
                        compactAlt = ResourceHandler.EnsureAppRelative(compactAlt);
                    }

                    // try to get as a IOptimizedResult
                    IOptimizedResult result = this.ProcessPrecompiled(helper, file);
                    if (result != null)
                    {
                        resources.Append(result.PrettyPrinted);

                        if (String.IsNullOrEmpty(compactAlt))
                        {
                            compacts.Append(result.Compacted);
                        }
                        else
                        {
                            IOptimizedResult result2 = this.ProcessPrecompiled(helper, compactAlt);
                            if (result2 != null)
                            {
                                compacts.Append(result2.Compacted);
                            }
                        }
                        continue;
                    }

                    // ask BuildManager if compiles down to a string
                    string text = BuildManager.GetCompiledCustomString(file);
                    if (String.IsNullOrEmpty(text))
                    {
                        // use the raw contents of the virtual path
                        text = helper.OpenReader(file).ReadToEnd();
                    }

                    if (!String.IsNullOrEmpty(text))
                    {
                        helper.AddVirtualPathDependency(file);

                        resources.Append(text);

                        if (String.IsNullOrEmpty(compactAlt))
                        {
                            compacts.Append(text);
                        }
                        else
                        {
                            helper.AddVirtualPathDependency(compactAlt);

                            string text2 = BuildManager.GetCompiledCustomString(compactAlt);
                            compacts.Append(text2);
                        }
                        continue;
                    }
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, i + 1, 1, ex));
                }
            }

            resources.AppendLine();
            resources.AppendFormat("/* JsonFx v{0} */", JsonFx.About.Fx.Version);

            resource  = resources.ToString();
            compacted = compacts.ToString();
        }