Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a middleware for browser refresh. #24574

Merged
merged 8 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changes per PR comments
  • Loading branch information
pranavkm committed Aug 5, 2020
commit 989ecd130e95d3379207ac1e8e16c300453c330c
23 changes: 12 additions & 11 deletions src/Tools/dotnet-watch/BrowserRefresh/src/ResponseStreamWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ public override Task FlushAsync(CancellationToken cancellationToken)
public override void Write(ReadOnlySpan<byte> buffer)
{
OnWrite();

_baseStream.Write(buffer);
if (IsHtmlResponse && !ScriptInjectionPerformed)
{
ScriptInjectionPerformed = WebSocketScriptInjection.Instance.TryInjectLiveReloadScript(_baseStream, buffer);
}
else
{
_baseStream.Write(buffer);
}
}

public override void WriteByte(byte value)
{
OnWrite();
_baseStream.WriteByte(value);
}

Expand All @@ -71,7 +78,7 @@ public override void Write(byte[] buffer, int offset, int count)

if (IsHtmlResponse && !ScriptInjectionPerformed)
{
ScriptInjectionPerformed = WebSocketScriptInjection.Instance.TryInjectLiveReloadScript(_baseStream, buffer, offset, count);
ScriptInjectionPerformed = WebSocketScriptInjection.Instance.TryInjectLiveReloadScript(_baseStream, buffer.AsSpan(offset, count));
}
else
{
Expand All @@ -85,7 +92,7 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc

if (IsHtmlResponse && !ScriptInjectionPerformed)
{
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer, offset, count);
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer.AsMemory(offset, count), cancellationToken);
}
else
{
Expand All @@ -99,13 +106,7 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella

if (IsHtmlResponse && !ScriptInjectionPerformed)
{
var materialized = buffer.ToArray();

ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(
_baseStream,
materialized,
0,
materialized.Length);
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer, cancellationToken);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Watch.BrowserRefresh
Expand All @@ -27,52 +28,49 @@ public WebSocketScriptInjection(string clientScript)
_scriptInjectionBytes = Encoding.UTF8.GetBytes(clientScript);
}

public bool TryInjectLiveReloadScript(Stream baseStream, byte[] buffer, int offset, int count)
public bool TryInjectLiveReloadScript(Stream baseStream, ReadOnlySpan<byte> buffer)
{
var span = buffer.AsSpan(offset, count);
var index = span.LastIndexOf(_bodyBytes);
var index = buffer.LastIndexOf(_bodyBytes);
if (index == -1)
{
baseStream.Write(span);
baseStream.Write(buffer);
return false;
}

if (index > 0)
{
baseStream.Write(span.Slice(0, index));
span = span[index..];
baseStream.Write(buffer.Slice(0, index));
buffer = buffer[index..];
}

// Write the injected script
baseStream.Write(_scriptInjectionBytes);

// Write the rest of the buffer/HTML doc
baseStream.Write(span);
baseStream.Write(buffer);
return true;
}

public async ValueTask<bool> TryInjectLiveReloadScriptAsync(Stream baseStream, byte[] buffer, int offset, int count)
public async ValueTask<bool> TryInjectLiveReloadScriptAsync(Stream baseStream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
var index = buffer.AsSpan(offset, count).LastIndexOf(_bodyBytes);
var index = buffer.Span.LastIndexOf(_bodyBytes);
if (index == -1)
{
await baseStream.WriteAsync(buffer, offset, count);
await baseStream.WriteAsync(buffer, cancellationToken);
return false;
}

var memory = buffer.AsMemory(offset, count);

if (index > 0)
{
await baseStream.WriteAsync(memory.Slice(0, index));
memory = memory[index..];
await baseStream.WriteAsync(buffer.Slice(0, index), cancellationToken);
buffer = buffer[index..];
}

// Write the injected script
await baseStream.WriteAsync(_scriptInjectionBytes);
await baseStream.WriteAsync(_scriptInjectionBytes, cancellationToken);

// Write the rest of the buffer/HTML doc
await baseStream.WriteAsync(memory);
await baseStream.WriteAsync(buffer, cancellationToken);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -21,7 +22,7 @@ public async Task TryInjectLiveReloadScriptAsync_DoesNotInjectMarkup_IfInputDoes
var input = Encoding.UTF8.GetBytes("<div>this is not a real body tag.</div>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 0, input.Length);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input);

// Assert
Assert.False(result);
Expand All @@ -47,7 +48,7 @@ This is the footer
</html>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 0, input.Length);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input);

// Assert
Assert.True(result);
Expand All @@ -64,7 +65,7 @@ public async Task TryInjectLiveReloadScriptAsync_WithOffsetBodyTagAppearsInMiddl
var input = Encoding.UTF8.GetBytes("unused</table></body>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 6, input.Length - 6);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input.AsMemory(6));

// Assert
Assert.True(result);
Expand All @@ -81,7 +82,7 @@ public async Task TryInjectLiveReloadScriptAsync_WithOffsetBodyTagAppearsAtStart
var input = Encoding.UTF8.GetBytes("unused</body>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 6, input.Length - 6);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input.AsMemory(6));

// Assert
Assert.True(result);
Expand All @@ -98,7 +99,7 @@ public async Task TryInjectLiveReloadScriptAsync_InjectsMarkupIfBodyTagAppearsAt
var input = Encoding.UTF8.GetBytes("</body></html>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 0, input.Length);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input);

// Assert
Assert.True(result);
Expand All @@ -115,7 +116,7 @@ public async Task TryInjectLiveReloadScriptAsync_InjectsMarkupIfBodyTagAppearsBy
var input = Encoding.UTF8.GetBytes("</body>");

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 0, input.Length);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input);

// Assert
Assert.True(result);
Expand All @@ -129,10 +130,10 @@ public async Task TryInjectLiveReloadScriptAsync_MultipleBodyTags()
// Arrange
var expected = $"<p></body>some text</p>{ClientScript}</body>";
var stream = new MemoryStream();
var input = Encoding.UTF8.GetBytes("abc<p></body>some text</p></body>");
var input = Encoding.UTF8.GetBytes("abc<p></body>some text</p></body>").AsMemory(3);

// Act
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input, 3, input.Length - 3);
var result = await ScriptInjection.TryInjectLiveReloadScriptAsync(stream, input);

// Assert
Assert.True(result);
Expand All @@ -146,10 +147,10 @@ public void TryInjectLiveReloadScript_NoBodyTag()
// Arrange
var expected = "<p>Hello world</p>";
var stream = new MemoryStream();
var input = Encoding.UTF8.GetBytes(expected);
var input = Encoding.UTF8.GetBytes(expected).AsSpan();

// Act
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input, 0, input.Length);
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input);

// Assert
Assert.False(result);
Expand All @@ -163,10 +164,10 @@ public void TryInjectLiveReloadScript_NoOffset()
// Arrange
var expected = $"</table>{ClientScript}</body>";
var stream = new MemoryStream();
var input = Encoding.UTF8.GetBytes("</table></body>");
var input = Encoding.UTF8.GetBytes("</table></body>").AsSpan();

// Act
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input, 0, input.Length);
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input);

// Assert
Assert.True(result);
Expand All @@ -180,10 +181,10 @@ public void TryInjectLiveReloadScript_WithOffset()
// Arrange
var expected = $"</table>{ClientScript}</body>";
var stream = new MemoryStream();
var input = Encoding.UTF8.GetBytes("unused</table></body>");
var input = Encoding.UTF8.GetBytes("unused</table></body>").AsSpan(6);

// Act
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input, 6, input.Length - 6);
var result = ScriptInjection.TryInjectLiveReloadScript(stream, input);

// Assert
Assert.True(result);
Expand Down