/
CmsPageHttpHandler.cs
159 lines (129 loc) · 5.83 KB
/
CmsPageHttpHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System.Web;
using System.Xml.Linq;
using Composite.AspNet.Caching;
using Composite.Core.Configuration;
using Composite.Core.Instrumentation;
using Composite.Core.PageTemplates;
using Composite.Core.WebClient.Renderings;
using Composite.Core.WebClient.Renderings.Page;
using Composite.Core.Xml;
namespace Composite.AspNet
{
/// <summary>
/// Renders page templates without building a Web Form's control tree.
/// Contains a custom implementation of "donut caching".
/// </summary>
internal class CmsPageHttpHandler: IHttpHandler
{
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);
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);
}
}
public bool IsReusable => true;
}
}