private void AttachRendererToAspNetPage(HttpContext context)
        {
            Verify.IsNotNull(context.Handler, "HttpHandler isn't defined");
            var aspnetPage = context.Handler as System.Web.UI.Page;

            if (aspnetPage == null)
            {
                return;
            }

            var pageRenderer = PageTemplateFacade.BuildPageRenderer(Page.TemplateId);

            pageRenderer.AttachToPage(aspnetPage, PageContentToRender);
        }
Ejemplo n.º 2
0
 private bool IsSlimPageRenderer(Guid pageTemplate)
 {
     return(_pageRendererTypCache.GetOrAdd(pageTemplate,
                                           templateId => PageTemplateFacade.BuildPageRenderer(templateId) is ISlimPageRenderer));
 }
Ejemplo n.º 3
0
        private void InitializeFromHttpContextInternal()
        {
            HttpContext httpContext = HttpContext.Current;
            var         request     = httpContext.Request;
            var         response    = httpContext.Response;

            ProfilingEnabled = request.Url.OriginalString.Contains("c1mode=perf");
            if (ProfilingEnabled)
            {
                if (!UserValidationFacade.IsLoggedIn())
                {
                    string loginUrl = GetLoginRedirectUrl(request.RawUrl);
                    response.Write(@"You must be logged into <a href=""" + loginUrl + @""">C1 console</a> to have the performance view enabled");
                    response.End(); // throws ThreadAbortException
                    return;
                }

                Profiler.BeginProfiling();
                _pagePerfMeasuring = Profiler.Measure("C1 Page");
            }

            _previewKey = request.QueryString["previewKey"];
            PreviewMode = !_previewKey.IsNullOrEmpty();

            if (PreviewMode)
            {
                Page = (IPage)HttpRuntime.Cache.Get(_previewKey + "_SelectedPage");
                C1PageRoute.PageUrlData = new PageUrlData(Page);

                PageRenderer.RenderingReason = (RenderingReason)HttpRuntime.Cache.Get(_previewKey + "_RenderingReason");
            }
            else
            {
                PageUrlData pageUrl = C1PageRoute.PageUrlData ?? PageUrls.UrlProvider.ParseInternalUrl(request.Url.OriginalString);
                Page = pageUrl.GetPage();

                _cachedUrl = request.Url.PathAndQuery;

                PageRenderer.RenderingReason = new UrlSpace(httpContext).ForceRelativeUrls
                    ? RenderingReason.C1ConsoleBrowserPageView
                    : RenderingReason.PageView;
            }

            ValidateViewUnpublishedRequest(httpContext);

            if (Page == null)
            {
                throw new HttpException(404, "Page not found - either this page has not been published yet or it has been deleted.");
            }

            if (Page.DataSourceId.PublicationScope != PublicationScope.Published)
            {
                response.Cache.SetCacheability(HttpCacheability.NoCache);
                CachingDisabled = true;
            }

            PageRenderer.CurrentPage = Page;

            _dataScope = new DataScope(Page.DataSourceId.PublicationScope, Page.DataSourceId.LocaleScope);

            var pagePlaceholderContents = GetPagePlaceholderContents();
            var pageRenderingJob        = new PageContentToRender(Page, pagePlaceholderContents, PreviewMode);

            Verify.IsNotNull(httpContext.Handler, "HttpHandler isn't defined");

            var aspnetPage = (System.Web.UI.Page)httpContext.Handler;

            var pageRenderer = PageTemplateFacade.BuildPageRenderer(Page.TemplateId);

            pageRenderer.AttachToPage(aspnetPage, pageRenderingJob);
        }
Ejemplo n.º 4
0
        public void ProcessRequest(HttpContext context)
        {
            OutputCacheHelper.InitializeFullPageCaching(context);

            using (var renderingContext = RenderingContext.InitializeFromHttpContext())
            {
                bool            cachingEnabled = false;
                string          cacheKey       = null;
                DonutCacheEntry cacheEntry     = null;

                bool consoleUserLoggedIn = Composite.C1Console.Security.UserValidationFacade.IsLoggedIn();

                // "Donut caching" is enabled for logged in users, only if profiling is enabled as well.
                if (!renderingContext.CachingDisabled &&
                    (!consoleUserLoggedIn || renderingContext.ProfilingEnabled))
                {
                    cachingEnabled = OutputCacheHelper.TryGetCacheKey(context, out cacheKey);
                    if (cachingEnabled)
                    {
                        using (Profiler.Measure("Cache lookup"))
                        {
                            cacheEntry = OutputCacheHelper.GetFromCache(context, cacheKey);
                        }
                    }
                }

                XDocument document;
                var       functionContext = PageRenderer.GetPageRenderFunctionContextContainer();

                bool allFunctionsExecuted   = false;
                bool preventResponseCaching = false;

                if (cacheEntry != null)
                {
                    document = cacheEntry.Document;
                    foreach (var header in cacheEntry.OutputHeaders)
                    {
                        context.Response.Headers[header.Name] = header.Value;
                    }

                    // Making sure this response will not go to the output cache
                    preventResponseCaching = true;
                }
                else
                {
                    if (renderingContext.RunResponseHandlers())
                    {
                        return;
                    }

                    var renderer = PageTemplateFacade.BuildPageRenderer(renderingContext.Page.TemplateId);

                    var slimRenderer = (ISlimPageRenderer)renderer;

                    using (Profiler.Measure($"{nameof(ISlimPageRenderer)}.Render"))
                    {
                        document = slimRenderer.Render(renderingContext.PageContentToRender, functionContext);
                    }

                    allFunctionsExecuted = PageRenderer.ExecuteCacheableFunctions(document.Root, functionContext);

                    if (cachingEnabled && !allFunctionsExecuted && OutputCacheHelper.ResponseCacheable(context))
                    {
                        preventResponseCaching = true;

                        if (!functionContext.ExceptionsSuppressed)
                        {
                            using (Profiler.Measure("Adding to cache"))
                            {
                                OutputCacheHelper.AddToCache(context, cacheKey, new DonutCacheEntry(context, document));
                            }
                        }
                    }
                }

                if (!allFunctionsExecuted)
                {
                    using (Profiler.Measure("Executing embedded functions"))
                    {
                        PageRenderer.ExecuteEmbeddedFunctions(document.Root, functionContext);
                    }
                }

                using (Profiler.Measure("Resolving page fields"))
                {
                    PageRenderer.ResolvePageFields(document, renderingContext.Page);
                }

                string xhtml;
                if (document.Root.Name == RenderingElementNames.Html)
                {
                    var xhtmlDocument = new XhtmlDocument(document);

                    PageRenderer.ProcessXhtmlDocument(xhtmlDocument, renderingContext.Page);
                    PageRenderer.ProcessDocumentHead(xhtmlDocument);

                    xhtml = xhtmlDocument.ToString();
                }
                else
                {
                    xhtml = document.ToString();
                }

                if (renderingContext.PreRenderRedirectCheck())
                {
                    return;
                }

                xhtml = renderingContext.ConvertInternalLinks(xhtml);

                if (GlobalSettingsFacade.PrettifyPublicMarkup)
                {
                    xhtml = renderingContext.FormatXhtml(xhtml);
                }

                var response = context.Response;

                if (preventResponseCaching)
                {
                    context.Response.Cache.SetNoServerCaching();
                }

                // Disabling ASP.NET cache if there's a logged-in user
                if (consoleUserLoggedIn)
                {
                    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                }

                // Inserting performance profiling information
                if (renderingContext.ProfilingEnabled)
                {
                    xhtml = renderingContext.BuildProfilerReport();

                    response.ContentType = "text/xml";
                }

                response.Write(xhtml);
            }
        }