Skip to content

StatusCodePagesMiddleware: Improving response started verification #46576

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

Closed
Closed
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ public StatusCodePagesOptions()

if (context.HttpContext.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService)
{
await problemDetailsService.WriteAsync(new ()
await problemDetailsService.WriteAsync(new()
{
HttpContext = context.HttpContext,
ProblemDetails = { Status = statusCode }
});
}

Copy link
Member

@Tratcher Tratcher Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the content-length and content-type is weird, just make the original code an else block.

Suggested change
else
{
context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(body);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, just saw your comment on the issue... I'll revisit this next week, but I still don't think we want to rely on checking content-length or content-type to determine the presence of a buffered body.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! I don't like it as well but since we were using it already, I decided to suggest it. I tried a repro to see if I can find something reliable but as I mentioned, in the issue, I was not able to repro.

// TODO: Render with a pre-compiled html razor view.
if (!context.HttpContext.Response.HasStarted)
// Do nothing if a response body has already been started.
if (context.HttpContext.Response.HasStarted
|| context.HttpContext.Response.ContentLength.HasValue
|| !string.IsNullOrEmpty(context.HttpContext.Response.ContentType))
{
var body = BuildResponseBody(statusCode);

context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(body);
return;
}

// TODO: Render with a pre-compiled html razor view.
var body = BuildResponseBody(statusCode);

context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(body);
};
}

Expand Down