Repository navigation
Expand file tree
/
Copy pathHtmlFormat.cs
More file actions
153 lines (127 loc) · 6.01 KB
/
Copy pathHtmlFormat.cs
File metadata and controls
153 lines (127 loc) · 6.01 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ServiceStack.Serialization;
using ServiceStack.Web;
namespace ServiceStack.Formats
{
public class HtmlFormat : IPlugin, Model.IHasStringId
{
public string Id { get; set; } = Plugins.Html;
public static string TitleFormat
= @"{0} Snapshot of {1}";
public static string HtmlTitleFormat
= @"Snapshot of <i>{0}</i> generated by <a href=""https://servicestack.net"">ServiceStack</a> on <b>{1}</b>";
public static bool Humanize = true;
private IAppHost AppHost { get; set; }
public Dictionary<string, string> PathTemplates { get; set; } = new Dictionary<string, string> {
{ "/" + LocalizedStrings.Auth.Localize(), "/Templates/auth.html" }
};
public Func<IRequest, string> ResolveTemplate { get; set; }
public string DefaultResolveTemplate(IRequest req)
{
if (PathTemplates != null && PathTemplates.TryGetValue(req.PathInfo, out var templatePath))
{
var file = HostContext.VirtualFileSources.GetFile(templatePath);
if (file == null)
throw new FileNotFoundException($"Could not load HTML template '{templatePath}'", templatePath);
return file.ReadAllText();
}
return null;
}
public HtmlFormat()
{
ResolveTemplate = DefaultResolveTemplate;
}
public void Register(IAppHost appHost)
{
AppHost = appHost;
//Register this in ServiceStack with the custom formats
appHost.ContentTypes.RegisterAsync(MimeTypes.Html, SerializeToStreamAsync, null);
appHost.ContentTypes.RegisterAsync(MimeTypes.JsonReport, SerializeToStreamAsync, null);
appHost.Config.DefaultContentType = MimeTypes.Html;
appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.Html.ToContentFormat());
appHost.Config.IgnoreFormatsInMetadata.Add(MimeTypes.JsonReport.ToContentFormat());
}
public async Task SerializeToStreamAsync(IRequest req, object response, Stream outputStream)
{
var res = req.Response;
if (req.GetItem("HttpResult") is IHttpResult httpResult && httpResult.Headers.ContainsKey(HttpHeaders.Location)
&& httpResult.StatusCode != System.Net.HttpStatusCode.Created)
return;
try
{
if (res.StatusCode >= 400)
{
var responseStatus = response.GetResponseStatus();
req.Items[Keywords.ErrorStatus] = responseStatus;
}
if (response is CompressedResult)
{
if (res.Dto != null)
response = res.Dto;
else
throw new ArgumentException("Cannot use Cached Result as ViewModel");
}
foreach (var viewEngine in AppHost.ViewEngines)
{
var handled = await viewEngine.ProcessRequestAsync(req, response, outputStream);
if (handled)
return;
}
}
catch (Exception ex)
{
if (res.StatusCode < 400)
throw;
//If there was an exception trying to render a Error with a View,
//It can't handle errors so just write it out here.
response = DtoUtils.CreateErrorResponse(req.Dto, ex);
}
//Handle Exceptions returning string
if (req.ResponseContentType == MimeTypes.PlainText)
{
req.ResponseContentType = MimeTypes.Html;
res.ContentType = MimeTypes.Html;
}
if (req.ResponseContentType != MimeTypes.Html && req.ResponseContentType != MimeTypes.JsonReport)
return;
var dto = response.GetDto();
if (!(dto is string html))
{
// Serialize then escape any potential script tags to avoid XSS when displaying as HTML
var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null";
json = json.HtmlEncodeLite();
var url = req.ResolveAbsoluteUrl()
.Replace("format=html", "")
.Replace("format=shtm", "")
.TrimEnd('?', '&')
.HtmlEncode();
url += url.Contains("?") ? "&" : "?";
var now = DateTime.UtcNow;
var requestName = req.OperationName ?? dto.GetType().GetOperationName();
html = ReplaceTokens(ResolveTemplate?.Invoke(req) ?? Templates.HtmlTemplates.GetHtmlFormatTemplate(), req)
.Replace("${Dto}", json)
.Replace("${Title}", string.Format(TitleFormat, requestName, now))
.Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes().ToString())
.Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now))
.Replace("${ServiceUrl}", url)
.Replace("${Humanize}", Humanize.ToString().ToLower())
;
}
await ((ServiceStackHost)AppHost).WriteAutoHtmlResponseAsync(req, response, html, outputStream);
}
public static string ReplaceTokens(string html, IRequest req)
{
if (string.IsNullOrEmpty(html))
return string.Empty;
html = html
.Replace("${BaseUrl}", req.GetBaseUrl().TrimEnd('/'))
.Replace("${AuthRedirect}", req.ResolveAbsoluteUrl(HostContext.AppHost.GetPlugin<AuthFeature>()?.HtmlRedirect))
.Replace("${AllowOrigins}", HostContext.AppHost.GetPlugin<CorsFeature>()?.AllowOriginWhitelist?.Join(";"))
;
return html;
}
}
}