-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathWebsocketScriptInjectionHelper.cs
More file actions
157 lines (127 loc) · 5.76 KB
/
Copy pathWebsocketScriptInjectionHelper.cs
File metadata and controls
157 lines (127 loc) · 5.76 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
154
155
156
157
using System;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Westwind.AspNetCore.LiveReload;
namespace Westwind.AspNetCore.LiveReload
{
/// <summary>
/// Helper class that handles the HTML injection into
/// a string or byte array.
/// </summary>
public static class WebsocketScriptInjectionHelper
{
private const string STR_WestWindMarker = "<!-- West Wind Live Reload -->";
private const string STR_BodyMarker = "</body>";
private static readonly byte[] _bodyBytes = Encoding.UTF8.GetBytes(STR_BodyMarker);
private static readonly byte[] _markerBytes = Encoding.UTF8.GetBytes(STR_WestWindMarker);
/// <summary>
/// Injects WebSocket Refresh code into JavaScript document
/// just above the `</body>` tag.
/// </summary>
/// <param name="html"></param>
/// <param name="context"></param>
/// <returns></returns>
public static string InjectLiveReloadScript(string html, HttpContext context)
{
if (html.Contains(STR_WestWindMarker))
return html;
string script = GetWebSocketClientJavaScript(context);
html = html.Replace(STR_BodyMarker, script);
return html;
}
/// <summary>
/// Adds Live Reload WebSocket script into the page before the body tag.
/// </summary>
/// <param name="buffer"></param>
/// <param name="context"></param>
/// <param name="baseStream">The raw Response Stream</param>
/// <returns></returns>
public static Task InjectLiveReloadScriptAsync(ReadOnlyMemory<byte> buffer, HttpContext context, Stream baseStream)
{
return InjectLiveReloadScriptAsync(buffer.ToArray(), context, baseStream);
}
/// <summary>
/// Adds Live Reload WebSocket script into the page before the body tag.
/// </summary>
/// <param name="buffer"></param>
/// <param name="context"></param>
/// <param name="baseStream">The raw Response Stream</param>
/// <returns></returns>
public static async Task InjectLiveReloadScriptAsync(byte[] buffer, HttpContext context, Stream baseStream)
{
var index = buffer.LastIndexOf(_markerBytes);
if (index > -1)
{
await baseStream.WriteAsync(buffer, 0, buffer.Length);
return;
}
index = buffer.LastIndexOf(_bodyBytes);
if (index == -1)
{
await baseStream.WriteAsync(buffer, 0, buffer.Length);
return;
}
var endIndex = index + _bodyBytes.Length;
// Write pre-marker buffer
await baseStream.WriteAsync(buffer, 0, index - 1);
// Write the injected script
var scriptBytes = Encoding.UTF8.GetBytes(GetWebSocketClientJavaScript(context));
await baseStream.WriteAsync(scriptBytes, 0, scriptBytes.Length);
// Write the rest of the buffer/HTML doc
await baseStream.WriteAsync(buffer, endIndex, buffer.Length - endIndex);
}
static int LastIndexOf<T>(this T[] array, T[] sought) where T : IEquatable<T> =>
array.AsSpan().LastIndexOf(sought);
private static string _ClientScriptString = string.Empty;
public static string GetWebSocketClientJavaScript(HttpContext context, bool returnScriptOnly = false)
{
var config = LiveReloadConfiguration.Current;
var host = context.Request.Host;
string hostString;
if (!string.IsNullOrEmpty(config.WebSocketHost))
hostString = config.WebSocketHost + config.WebSocketUrl;
else
{
var prefix = context.Request.IsHttps ? "wss" : "ws";
hostString = $"{prefix}://{host.Host}:{host.Port}" + config.WebSocketUrl;
}
if (string.IsNullOrEmpty(_ClientScriptString))
{
// Load `/LiveReloadClientScript.js from resource stream
lock (_ClientScriptString)
{
if (string.IsNullOrEmpty(_ClientScriptString))
{
using (var scriptStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Westwind.AspNetCore.LiveReload.LiveReloadClientScript.js"))
{
if (scriptStream == null)
throw new InvalidDataException("Unable to load LiveReloadClientScript.js Resource");
var buffer = new byte[scriptStream.Length];
scriptStream.Read(buffer, 0, buffer.Length);
_ClientScriptString = System.Text.Encoding.UTF8.GetString(buffer);
}
}
}
}
if (returnScriptOnly)
return _ClientScriptString.Replace("{0}", hostString);
// otherwise return the embeddable script block string that replaces the ending </body> tag
string cscript;
if (string.IsNullOrEmpty(config.LiveReloadScriptUrl))
cscript = $"<script>\n{_ClientScriptString.Replace("{0}", hostString)}\n</script>";
else
cscript = $"<script src=\"{config.LiveReloadScriptUrl}\" integrity=\"sha512-Bs8N/hvPRDuesBW4wst+X1FOsgUzmRiYr7DkT3U865iWDcsoTw01Cf4/tCyUYGdFDhXzvPYqN7ABq1ilxSnd2w==\"></script>";
var script = $@"
<!-- West Wind Live Reload -->
{cscript}
<!-- End Live Reload -->
</body>";
return script;
}
}
}