/
ResponseStreamWrapper.cs
238 lines (195 loc) · 7.97 KB
/
ResponseStreamWrapper.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
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Westwind.AspNetCore.LiveReload;
namespace Westwind.AspNetCore.LiveReload
{
/// <summary>
/// Wraps the Response Stream to inject the WebSocket HTML into
/// an HTML Page.
/// </summary>
public class ResponseStreamWrapper : Stream
{
private Stream _baseStream;
private HttpContext _context;
private bool _isContentLengthSet = false;
public ResponseStreamWrapper(Stream baseStream, HttpContext context)
{
_baseStream = baseStream;
_context = context;
CanWrite = true;
}
public override void Flush()
{
_baseStream.Flush();
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
// this is called at the beginning of a request in 3.x and so
// we have to set the ContentLength here as the flush/write locks headers
if (!_isContentLengthSet && IsHtmlResponse())
{
_context.Response.Headers.ContentLength = null;
_isContentLengthSet = true;
}
return _baseStream.FlushAsync(cancellationToken);
}
public override int Read(byte[] buffer, int offset, int count)
{
return _baseStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin) => _baseStream.Seek(offset, origin);
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _baseStream.ReadAsync(buffer, offset, count, cancellationToken);
}
public override void SetLength(long value)
{
_baseStream.SetLength(value);
IsHtmlResponse(forceReCheck: true);
}
public override void Write(ReadOnlySpan<byte> buffer)
{
_baseStream.Write(buffer);
}
public override void WriteByte(byte value)
{
_baseStream.WriteByte(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
if (IsHtmlResponse())
{
WebsocketScriptInjectionHelper.InjectLiveReloadScriptAsync(buffer.AsMemory(offset, count), _context, _baseStream)
.GetAwaiter()
.GetResult();
}
else
{
_baseStream?.Write(buffer, offset, count);
}
}
public override Task WriteAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken)
{
return WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
}
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (IsHtmlResponse())
{
await WebsocketScriptInjectionHelper.InjectLiveReloadScriptAsync(buffer, _context, _baseStream);
}
else
{
if (_baseStream != null)
await _baseStream.WriteAsync(buffer, cancellationToken);
}
}
private bool? _isHtmlResponse = null;
private bool IsHtmlResponse(bool forceReCheck = false)
{
if (!forceReCheck && _isHtmlResponse != null)
return _isHtmlResponse.Value;
// we need to check if the active request is still valid
// this can fail if we're in the middle of an error response
// or url rewrite in which case we can't intercept
if (_context?.Response == null)
return false;
// Requirements for script injection:
// * has to have result body
// * 200 or 500 response
// * text/html response
// * UTF-8 formatted (explicit or no charset)
_isHtmlResponse =
_context.Response?.Body != null &&
(_context.Response.StatusCode == 200 || _context.Response.StatusCode == 500) &&
_context.Response.ContentType != null &&
_context.Response.ContentType.Contains("text/html", StringComparison.OrdinalIgnoreCase) &&
(_context.Response.ContentType.Contains("utf-8", StringComparison.OrdinalIgnoreCase) ||
!_context.Response.ContentType.Contains("charset=", StringComparison.OrdinalIgnoreCase));
if (!_isHtmlResponse.Value)
return false;
// Check for refresh exlusion rules
RefreshInclusionModes refreshFile = RefreshInclusionModes.ContinueProcessing;
if (LiveReloadConfiguration.Current.RefreshInclusionFilter != null)
{
refreshFile = LiveReloadConfiguration.Current.RefreshInclusionFilter.Invoke(_context.Request.Path.Value);
if (refreshFile == RefreshInclusionModes.DontRefresh)
return false; // don't embed refresh script
}
// Make sure we force dynamic content type since we're
// rewriting the content - static content will set the header explicitly
// and fail when it doesn't matchif (_isHtmlResponse.Value)
if (!_isContentLengthSet && _context.Response.ContentLength != null)
{
_context.Response.Headers.ContentLength = null;
_isContentLengthSet = true;
}
return _isHtmlResponse.Value;
}
protected override void Dispose(bool disposing)
{
//_baseStream?.Dispose();
_baseStream = null;
_context = null;
base.Dispose(disposing);
}
#region Byte Helpers
/// <summary>
/// Tries to find a
/// </summary>
/// <param name="buffer">byte array to be searched</param>
/// <param name="bufferToFind">byte to find</param>
/// <returns></returns>
public static int IndexOfByteArray(byte[] buffer, byte[] bufferToFind)
{
if (buffer.Length == 0 || bufferToFind.Length == 0)
return -1;
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] == bufferToFind[0])
{
bool innerMatch = true;
for (int j = 1; j < bufferToFind.Length; j++)
{
if (buffer[i + j] != bufferToFind[j])
{
innerMatch = false;
break;
}
}
if (innerMatch)
return i;
}
}
return -1;
}
/// <summary>
/// Returns an index into a byte array to find a string in the byte array.
/// Exact match using the encoding provided or UTF-8 by default.
/// </summary>
/// <param name="buffer"></param>
/// <param name="stringToFind"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static int IndexOfByteArray(byte[] buffer, string stringToFind, Encoding encoding = null)
{
if (encoding == null)
encoding = Encoding.UTF8;
if (buffer.Length == 0 || string.IsNullOrEmpty(stringToFind))
return -1;
var bytes = encoding.GetBytes(stringToFind);
return IndexOfByteArray(buffer, bytes);
}
#endregion
public override bool CanRead { get; }
public override bool CanSeek { get; }
public override bool CanWrite { get; }
public override long Length { get; }
public override long Position { get; set; }
}
}