-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Do not serve response body for HEAD requests #7230
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do any of these tests check that content-length is set? I'd expect the same header for GET and HEAD
No these don't. There are unit tests that check that for GET. I can add Content-Length to these tests for HEAD |
Actually we can set it for all requests that get to that line since we short-circuit return for 304 and 412 before we even get to setting Content-Length. I will remove the check if(serveBody) before we set Content-Length here https://github.com/aspnet/Mvc/blame/dev/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileResultExecutorBase.cs#L89 |
@Tratcher Updated |
cdceb59
to
5675822
Compare
In order to avoid any mistakes for HEAD requests I think the safest would be to remove all HEAD checks from the code and place only a single if statement at the very end of the execution just above the line where you'd start writing to the body of the response:
In this case you make sure that the HEAD request will be exactly equivalent to a GET request except it won't ever include a response body. In other words I personally think the variable This would hugely simplify the code which would make it more easy to understand and avoid future errors. EDIT: Sorry forget what I said, because the method also checks for all the caching preconditions. It would have been easier to have the precondition logic extracted and done in a separate step before processing the rest, but that would be too big of a change now I guess. |
Addresses #7208