-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathRender.cs
319 lines (287 loc) · 13.2 KB
/
Render.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
namespace StackExchange.Profiling.Internal
{
/// <summary>
/// Internal MiniProfiler extensions, not meant for consumption.
/// This can and probably will break without warning. Don't use the .Internal namespace directly.
/// </summary>
public static class Render
{
/// <summary>
/// Renders script tag for including MiniProfiler.
/// </summary>
/// <param name="profiler">The profiler to render a tag for.</param>
/// <param name="path">The root path that MiniProfiler is being served from.</param>
/// <param name="isAuthorized">Whether the current user is authorized for MiniProfiler.</param>
/// <param name="renderOptions">The option overrides (if any) to use rendering this MiniProfiler.</param>
/// <param name="serviceProvider">The current request service provider.</param>
/// <param name="requestIDs">The request IDs to fetch for this render.</param>
public static string Includes(
MiniProfiler profiler,
string path,
bool isAuthorized,
RenderOptions? renderOptions,
IServiceProvider? serviceProvider = null,
List<Guid>? requestIDs = null)
{
var sb = StringBuilderCache.Get();
var options = profiler.Options;
sb.Append("<script async id=\"mini-profiler\" src=\"");
sb.Append(path);
sb.Append("includes.min.js?v=");
sb.Append(options.VersionHash);
sb.Append("\" data-version=\"");
sb.Append(options.VersionHash);
sb.Append("\" data-path=\"");
sb.Append(path);
sb.Append("\" data-current-id=\"");
sb.Append(profiler.Id.ToString());
sb.Append("\" data-ids=\"");
if (requestIDs != null)
{
var length = requestIDs.Count;
for (var i = 0; i < length; i++)
{
if (i > 0)
{
sb.Append(',');
}
var id = requestIDs[i];
sb.Append(id.ToString());
}
}
sb.Append("\" data-position=\"");
sb.Append((renderOptions?.Position ?? options.PopupRenderPosition).ToString());
sb.Append("\" data-scheme=\"");
sb.Append((renderOptions?.ColorScheme ?? options.ColorScheme).ToString());
sb.Append("\" data-decimal-places=\"");
sb.Append((renderOptions?.DecimalPlaces ?? options.PopupDecimalPlaces).ToString());
sb.Append('"');
if (isAuthorized)
{
sb.Append(" data-authorized=\"true\"");
}
if (renderOptions?.ShowTrivial ?? options.PopupShowTrivial)
{
sb.Append(" data-trivial=\"true\"");
}
if (renderOptions?.ShowTimeWithChildren ?? options.PopupShowTimeWithChildren)
{
sb.Append(" data-children=\"true\"");
}
if (renderOptions?.ShowControls ?? options.ShowControls)
{
sb.Append(" data-controls=\"true\"");
}
if (renderOptions?.StartHidden ?? options.PopupStartHidden)
{
sb.Append(" data-start-hidden=\"true\"");
}
var nonce = renderOptions?.Nonce ??
(serviceProvider != null ? profiler.Options.NonceProvider?.Invoke(serviceProvider) : null);
if (nonce?.HasValue() ?? false)
{
sb.Append(" nonce=\"").Append(HttpUtility.HtmlAttributeEncode(nonce)).Append("\"");
}
sb.Append(" data-max-traces=\"");
sb.Append((renderOptions?.MaxTracesToShow ?? options.PopupMaxTracesToShow).ToString(CultureInfo.InvariantCulture));
sb.Append("\" data-toggle-shortcut=\"");
sb.Append(renderOptions?.PopupToggleKeyboardShortcut ?? options.PopupToggleKeyboardShortcut);
sb.Append("\" data-trivial-milliseconds=\"");
sb.Append((renderOptions?.TrivialDurationThresholdMilliseconds ?? options.TrivialDurationThresholdMilliseconds).ToString(CultureInfo.InvariantCulture));
if (options.IgnoredDuplicateExecuteTypes.Count > 0)
{
sb.Append("\" data-ignored-duplicate-execute-types=\"");
var i = 0;
foreach (var executeType in options.IgnoredDuplicateExecuteTypes)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(executeType);
i++;
}
}
sb.Append("\"></script>");
return sb.ToStringRecycle();
}
/// <summary>
/// Renders script tag for including MiniProfiler.
/// </summary>
/// <param name="profiler">The profiler to render a tag for.</param>
/// <param name="path">The root path that MiniProfiler is being served from.</param>
/// <param name="isAuthorized">Whether the current user is authorized for MiniProfiler.</param>
/// <param name="requestIDs">The request IDs to fetch for this render.</param>
/// <param name="position">The UI position to render the profiler in (defaults to <see cref="MiniProfilerBaseOptions.PopupRenderPosition"/>).</param>
/// <param name="showTrivial">Whether to show trivial timings column initially or not (defaults to <see cref="MiniProfilerBaseOptions.PopupShowTrivial"/>).</param>
/// <param name="showTimeWithChildren">Whether to show time with children column initially or not (defaults to <see cref="MiniProfilerBaseOptions.PopupShowTimeWithChildren"/>).</param>
/// <param name="maxTracesToShow">The maximum number of profilers to show (before the oldest is removed - defaults to <see cref="MiniProfilerBaseOptions.PopupMaxTracesToShow"/>).</param>
/// <param name="showControls">Whether to show the controls (defaults to <see cref="MiniProfilerBaseOptions.ShowControls"/>).</param>
/// <param name="startHidden">Whether to start hidden (defaults to <see cref="MiniProfilerBaseOptions.PopupStartHidden"/>).</param>
/// <param name="nonce">Content script policy nonce value to use for script and style tags generated.</param>
public static string Includes(
MiniProfiler profiler,
string path,
bool isAuthorized,
List<Guid>? requestIDs = null,
RenderPosition? position = null,
bool? showTrivial = null,
bool? showTimeWithChildren = null,
int? maxTracesToShow = null,
bool? showControls = null,
bool? startHidden = null,
string? nonce = null)
{
var sb = StringBuilderCache.Get();
var options = profiler.Options;
sb.Append("<script async=\"async\" id=\"mini-profiler\" src=\"");
sb.Append(path);
sb.Append("includes.min.js?v=");
sb.Append(options.VersionHash);
if (!string.IsNullOrWhiteSpace(nonce))
{
sb.Append("\" nonce=\"");
sb.Append(HttpUtility.HtmlAttributeEncode(nonce));
}
sb.Append("\" data-version=\"");
sb.Append(options.VersionHash);
sb.Append("\" data-path=\"");
sb.Append(path);
sb.Append("\" data-current-id=\"");
sb.Append(profiler.Id.ToString());
sb.Append("\" data-ids=\"");
if (requestIDs != null)
{
var length = requestIDs.Count;
for (var i = 0; i < length; i++)
{
if (i > 0)
{
sb.Append(',');
}
var id = requestIDs[i];
sb.Append(id.ToString());
}
}
sb.Append("\" data-position=\"");
sb.Append((position ?? options.PopupRenderPosition).ToString());
sb.Append("\" data-scheme=\"");
sb.Append(options.ColorScheme.ToString());
sb.Append("\" data-decimal-places=\"");
sb.Append(options.PopupDecimalPlaces.ToString());
sb.Append('"');
if (isAuthorized)
{
sb.Append(" data-authorized=\"true\"");
}
if (showTrivial ?? options.PopupShowTrivial)
{
sb.Append(" data-trivial=\"true\"");
}
if (showTimeWithChildren ?? options.PopupShowTimeWithChildren)
{
sb.Append(" data-children=\"true\"");
}
if (showControls ?? options.ShowControls)
{
sb.Append(" data-controls=\"true\"");
}
if (startHidden ?? options.PopupStartHidden)
{
sb.Append(" data-start-hidden=\"true\"");
}
sb.Append(" data-max-traces=\"");
sb.Append((maxTracesToShow ?? options.PopupMaxTracesToShow).ToString(CultureInfo.InvariantCulture));
sb.Append("\" data-toggle-shortcut=\"");
sb.Append(options.PopupToggleKeyboardShortcut);
sb.Append("\" data-trivial-milliseconds=\"");
sb.Append(options.TrivialDurationThresholdMilliseconds.ToString(CultureInfo.InvariantCulture));
if (options.IgnoredDuplicateExecuteTypes.Count > 0)
{
sb.Append("\" data-ignored-duplicate-execute-types=\"");
var i = 0;
foreach (var executeType in options.IgnoredDuplicateExecuteTypes)
{
if (i > 0)
{
sb.Append(',');
}
sb.Append(executeType);
i++;
}
}
sb.Append("\"></script>");
return sb.ToStringRecycle();
}
/// <summary>
/// Renders a full HTML page for the share link in MiniProfiler.
/// </summary>
/// <param name="profiler">The profiler to render a tag for.</param>
/// <param name="serviceProvider">The current request service provider.</param>
/// <param name="path">The root path that MiniProfiler is being served from.</param>
/// <returns>A full HTML page for this MiniProfiler.</returns>
public static string SingleResultHtml(MiniProfiler profiler, IServiceProvider serviceProvider, string path)
{
var sb = StringBuilderCache.Get();
sb.Append("<html><head><title>");
sb.Append(profiler.Name);
sb.Append(" (");
sb.Append(profiler.DurationMilliseconds.ToString(CultureInfo.InvariantCulture));
sb.Append(" ms) - Profiling Results</title><script");
var nonce = profiler.Options.NonceProvider?.Invoke(serviceProvider) ?? string.Empty;
if (!string.IsNullOrWhiteSpace(nonce))
{
sb.Append(" nonce=\"");
sb.Append(HttpUtility.HtmlAttributeEncode(nonce));
sb.Append("\"");
}
sb.Append(">var profiler = ");
sb.Append(profiler.ToJson(htmlEscape: true));
sb.Append(";</script>");
sb.Append(Includes(profiler, path: path, isAuthorized: true, nonce: nonce));
sb.Append(@"</head><body><div class=""mp-result-full""></div></body></html>");
return sb.ToString();
}
/// <summary>
/// Renders a full HTML page for the share link in MiniProfiler.
/// </summary>
/// <param name="options">The options to render for.</param>
/// <param name="serviceProvider">The current request service provider.</param>
/// <param name="path">The root path that MiniProfiler is being served from.</param>
/// <returns>A full HTML page for this MiniProfiler.</returns>
public static string ResultListHtml(MiniProfilerBaseOptions options, IServiceProvider serviceProvider, string path)
{
var version = options.VersionHash;
var nonce = options.NonceProvider?.Invoke(serviceProvider) ?? string.Empty;
var nonceAttribute = !string.IsNullOrWhiteSpace(nonce) ? " nonce=\"" + HttpUtility.HtmlAttributeEncode(nonce) + "\"" : null;
return $@"<html>
<head>
<title>List of profiling sessions</title>
<script{nonceAttribute} id=""mini-profiler"" data-ids="""" src=""{path}includes.min.js?v={version}""></script>
<link href=""{path}includes.min.css?v={version}"" rel=""stylesheet"" />
<script{nonceAttribute}>MiniProfiler.listInit({{path: '{path}', version: '{version}', colorScheme: '{options.ColorScheme}'}});</script>
</head>
<body>
<table class=""mp-results-index"">
<thead>
<tr>
<th>Name</th>
<th>Server</th>
<th>Started</th>
<th>Total Duration</th>
<th>Request Start</th>
<th>Response Start</th>
<th>Dom Complete</th>
</tr>
</thead>
<tbody></tbody>
</table>
</body>
</html>";
}
}
}