Skip to content

Commit

Permalink
fix: During browser agent injection, don't set ContentLength if heade…
Browse files Browse the repository at this point in the history
…rs have already been sent. Resolves #2051 (#2059)
  • Loading branch information
tippmar-nr committed Nov 13, 2023
1 parent d0b8d26 commit c191aa6
Showing 1 changed file with 5 additions and 3 deletions.
Expand Up @@ -39,7 +39,8 @@ public override Task FlushAsync(CancellationToken cancellationToken)
{
if (!Disabled && !_isContentLengthSet && IsHtmlResponse())
{
_context.Response.ContentLength = null;
if (!_context.Response.HasStarted) // can't set headers if response has already started
_context.Response.ContentLength = null;
_isContentLengthSet = true;
}

Expand Down Expand Up @@ -154,7 +155,7 @@ private bool IsHtmlResponse(bool forceReCheck = false)
// * text/html response
// * UTF-8 formatted (either explicitly or no charset defined)
_isHtmlResponse =
_context.Response.ContentType != null &&
_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));
Expand All @@ -170,7 +171,8 @@ private bool IsHtmlResponse(bool forceReCheck = false)
// and fail when it doesn't match if (_isHtmlResponse.Value)
if (!_isContentLengthSet && _context.Response.ContentLength != null)
{
_context.Response.ContentLength = null;
if (!_context.Response.HasStarted) // can't set headers if response has already started
_context.Response.ContentLength = null;
_isContentLengthSet = true;
}
}
Expand Down

0 comments on commit c191aa6

Please sign in to comment.