Пример #1
0
        public void Index(string resourceName)
        {
            HttpRequest  req  = System.Web.HttpContext.Current.Request;
            HttpResponse resp = System.Web.HttpContext.Current.Response;

            // If an If-Modified-Since header is ever provided, we always say
            // not modified. This is because when there actually is a change,
            // cache busting should occur.
            if (req.Params["If-Modified-Since"] != null &&
                req.Params["v"] != null)
            {
                resp.StatusCode = 304;
                return;
            }

            if (resourceName.EndsWith(".js"))
            {
                // Lop off the suffix for lookup purposes
                resourceName = resourceName.Substring(0, resourceName.Length - ".js".Length);
            }

            HashKey <string> needed = new HashKey <string>();

            if (resourceName.Contains("__"))
            {
                foreach (string item in resourceName.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    needed.Add(item);
                }
            }
            else
            {
                needed.Add(resourceName);
            }

            String debugStr     = req.Params["debug"];
            String container    = req.Params["container"];
            String containerStr = req.Params["c"];

            bool debug = "1".Equals(debugStr);

            if (container == null)
            {
                container = ContainerConfig.DEFAULT_CONTAINER;
            }
            RenderingContext rcontext = "1".Equals(containerStr) ?
                                        RenderingContext.CONTAINER : RenderingContext.GADGET;

            ICollection <GadgetFeature> features = registry.GetFeatures(needed);
            StringBuilder jsData = new StringBuilder();

            foreach (GadgetFeature feature in features)
            {
                foreach (JsLibrary lib in feature.getJsLibraries(rcontext, container))
                {
                    if (!lib._Type.Equals(JsLibrary.Type.URL))
                    {
                        if (debug)
                        {
                            jsData.Append(lib.DebugContent);
                        }
                        else
                        {
                            jsData.Append(lib.Content);
                        }
                        jsData.Append(";\n");
                    }
                }
            }

            if (jsData.Length == 0)
            {
                resp.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            if (req.Params["v"] != null)
            {
                // Versioned files get cached indefinitely
                HttpUtil.SetCachingHeaders(resp);
            }
            else
            {
                // Unversioned files get cached for 1 hour.
                HttpUtil.SetCachingHeaders(resp, 60 * 60);
            }
            resp.ContentType     = "text/javascript; charset=utf-8";
            resp.ContentEncoding = Encoding.UTF8;
            resp.Output.Write(jsData.ToString());
        }
Пример #2
0
        /// <summary>
        /// Injects javascript libraries needed to satisfy feature dependencies.
        /// </summary>
        /// <param name="gadget"></param>
        /// <param name="headTag"></param>
        private void InjectFeatureLibraries(Gadget gadget, Node headTag)
        {
            // TODO: If there isn't any js in the document, we can skip this. Unfortunately, that means
            // both script tags (easy to detect) and event handlers (much more complex).
            GadgetContext    context    = gadget.getContext();
            GadgetSpec       spec       = gadget.getSpec();
            String           forcedLibs = context.getParameter("libs");
            HashKey <String> forced;

            if (string.IsNullOrEmpty(forcedLibs))
            {
                forced = new HashKey <string>();
            }
            else
            {
                forced = new HashKey <string>();
                foreach (var item in forcedLibs.Split(':'))
                {
                    forced.Add(item);
                }
            }


            // Forced libs are always done first.
            if (forced.Count != 0)
            {
                String  jsUrl   = urlGenerator.getBundledJsUrl(forced, context);
                Element libsTag = headTag.getOwnerDocument().createElement("script");
                libsTag.setAttribute("src", jsUrl);
                headTag.appendChild(libsTag);

                // Forced transitive deps need to be added as well so that they don't get pulled in twice.
                // TODO: Figure out a clean way to avoid having to call getFeatures twice.
                foreach (GadgetFeature dep in featureRegistry.GetFeatures(forced))
                {
                    forced.Add(dep.getName());
                }
            }

            // Inline any libs that weren't forced. The ugly context switch between inline and external
            // Js is needed to allow both inline and external scripts declared in feature.xml.
            String container = context.getContainer();
            ICollection <GadgetFeature> features = GetFeatures(spec, forced);

            // Precalculate the maximum length in order to avoid excessive garbage generation.
            int size = 0;

            foreach (GadgetFeature feature in features)
            {
                foreach (JsLibrary library in feature.getJsLibraries(RenderingContext.GADGET, container))
                {
                    if (library._Type == JsLibrary.Type.URL)
                    {
                        size += library.Content.Length;
                    }
                }
            }

            // Really inexact.
            StringBuilder inlineJs = new StringBuilder(size);

            foreach (GadgetFeature feature in features)
            {
                foreach (JsLibrary library in feature.getJsLibraries(RenderingContext.GADGET, container))
                {
                    if (library._Type == JsLibrary.Type.URL)
                    {
                        if (inlineJs.Length > 0)
                        {
                            Element inlineTag = headTag.getOwnerDocument().createElement("script");
                            headTag.appendChild(inlineTag);
                            inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.ToString()));
                            inlineJs.Length = 0;
                        }
                        Element referenceTag = headTag.getOwnerDocument().createElement("script");
                        referenceTag.setAttribute("src", library.Content);
                        headTag.appendChild(referenceTag);
                    }
                    else
                    {
                        if (!forced.Contains(feature.getName()))
                        {
                            // already pulled this file in from the shared contents.
                            if (context.getDebug())
                            {
                                inlineJs.Append(library.DebugContent);
                            }
                            else
                            {
                                inlineJs.Append(library.Content);
                            }
                            inlineJs.Append(";\n");
                        }
                    }
                }
            }

            inlineJs.Append(GetLibraryConfig(gadget, features));

            if (inlineJs.Length > 0)
            {
                Element inlineTag = headTag.getOwnerDocument().createElement("script");
                headTag.appendChild(inlineTag);
                inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.ToString()));
            }
        }