Skip to content

Commit c191aa6

Browse files
authored
fix: During browser agent injection, don't set ContentLength if headers have already been sent. Resolves #2051 (#2059)
1 parent d0b8d26 commit c191aa6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AspNetCore6Plus/BrowserInjectingStreamWrapper.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public override Task FlushAsync(CancellationToken cancellationToken)
3939
{
4040
if (!Disabled && !_isContentLengthSet && IsHtmlResponse())
4141
{
42-
_context.Response.ContentLength = null;
42+
if (!_context.Response.HasStarted) // can't set headers if response has already started
43+
_context.Response.ContentLength = null;
4344
_isContentLengthSet = true;
4445
}
4546

@@ -154,7 +155,7 @@ private bool IsHtmlResponse(bool forceReCheck = false)
154155
// * text/html response
155156
// * UTF-8 formatted (either explicitly or no charset defined)
156157
_isHtmlResponse =
157-
_context.Response.ContentType != null &&
158+
_context.Response.ContentType != null &&
158159
_context.Response.ContentType.Contains("text/html", StringComparison.OrdinalIgnoreCase) &&
159160
(_context.Response.ContentType.Contains("utf-8", StringComparison.OrdinalIgnoreCase) ||
160161
!_context.Response.ContentType.Contains("charset=", StringComparison.OrdinalIgnoreCase));
@@ -170,7 +171,8 @@ private bool IsHtmlResponse(bool forceReCheck = false)
170171
// and fail when it doesn't match if (_isHtmlResponse.Value)
171172
if (!_isContentLengthSet && _context.Response.ContentLength != null)
172173
{
173-
_context.Response.ContentLength = null;
174+
if (!_context.Response.HasStarted) // can't set headers if response has already started
175+
_context.Response.ContentLength = null;
174176
_isContentLengthSet = true;
175177
}
176178
}

0 commit comments

Comments
 (0)