Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Oct 23, 2023
1 parent 7d51345 commit dcde40d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 80 deletions.
5 changes: 1 addition & 4 deletions src/Agent/NewRelic/Agent/Core/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ private string TryGetRUMScriptInternal(string contentType, string requestPath)
{
return null;
}

try
{
var transaction = _transactionService.GetCurrentInternalTransaction();
Expand All @@ -339,10 +340,6 @@ private string TryGetRUMScriptInternal(string contentType, string requestPath)
// Once the transaction name is used for RUM it must be frozen
transaction.CandidateTransactionName.Freeze(TransactionNameFreezeReason.AutoBrowserScriptInjection);
var script = _browserMonitoringScriptMaker.GetScript(transaction, null);
if (script == null)
{
return null;
}

return script;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ResponseStreamWrapper : Stream
private Stream _baseStream;
private HttpContext _context;

private bool _isContentLengthSet = false;

public ResponseStreamWrapper(IAgent agent, Stream baseStream, HttpContext context)
{
Expand All @@ -33,19 +32,7 @@ public ResponseStreamWrapper(IAgent agent, Stream baseStream, HttpContext contex

public override void Flush()
{
//_baseStream.Flush();
}

public override Task FlushAsync(CancellationToken cancellationToken)
{
if (!_isContentLengthSet && IsHtmlResponse())
{
_context.Response.Headers.ContentLength = null;
_isContentLengthSet = true;
}

return base.FlushAsync(cancellationToken);

_baseStream.Flush();
}

public override int Read(byte[] buffer, int offset, int count)
Expand All @@ -58,7 +45,6 @@ public override int Read(byte[] buffer, int offset, int count)
public override void SetLength(long value)
{
_baseStream.SetLength(value);
IsHtmlResponse(forceReCheck: true);
}

public override void Write(ReadOnlySpan<byte> buffer)
Expand All @@ -74,17 +60,9 @@ public override void WriteByte(byte value)

public override void Write(byte[] buffer, int offset, int count)
{
if (IsHtmlResponse())
{
var curBuf = buffer.AsSpan(offset, count).ToArray();
_agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, curBuf, _baseStream, _agent.CurrentTransaction)
.GetAwaiter()
.GetResult();
return;
}

// fallback: just write the stream without modification
_baseStream?.Write(buffer, offset, count);
var curBuf = buffer.AsMemory(offset, count).ToArray();
_agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, curBuf, _baseStream, _agent.CurrentTransaction)
.GetAwaiter().GetResult();
}

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Expand All @@ -94,60 +72,11 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati

public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (IsHtmlResponse())
{
await _agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, buffer.ToArray(), _baseStream, _agent.CurrentTransaction);
return;
}

// if it's not an HTML response, write the buffer without modification
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.StatusCode is 200 or 500 &&
_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;

// 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 match if (_isHtmlResponse.Value)
if (!_isContentLengthSet && _context.Response.ContentLength != null)
{
_context.Response.Headers.ContentLength = null;
_isContentLengthSet = true;
}

return _isHtmlResponse.Value;
await _agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, buffer.ToArray(), _baseStream, _agent.CurrentTransaction);
}

protected override void Dispose(bool disposing)
{
//_baseStream?.Dispose();
_baseStream = null;
_context = null;

Expand Down

0 comments on commit dcde40d

Please sign in to comment.