fileserver: Better handling of HTTP status override#4132
Merged
francislavoie merged 2 commits intocaddyserver:masterfrom Apr 29, 2021
Merged
fileserver: Better handling of HTTP status override#4132francislavoie merged 2 commits intocaddyserver:masterfrom
francislavoie merged 2 commits intocaddyserver:masterfrom
Conversation
Just something I noticed when reading through the code; I think the `io.Copy` was a remnant of an older version of that code. I don't see a reason why we don't just let it fall through and serve the file with `http.ServeContent` normally. More consistent that way. Also, I hoisted the `status_code` override logic above the error handler detection bit, because then it allows for overriding the status code during error routes. Not necessarily that likely to be useful, but it's more correct. (Reminder that the first `w.WriteHeader()` wins, subsequent ones have no effect)
mholt
reviewed
Apr 27, 2021
This makes sure additional header fields can be set by `http.ServeContent`, by only intercepting the `WriteHeader` call when `http.ServeContent` actually calls it.
mholt
approved these changes
Apr 29, 2021
Member
mholt
left a comment
There was a problem hiding this comment.
Thanks, this LGTM.
You did test this, right? 😄
Member
Author
Member
|
Perfect, thanks so much! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Just something I noticed when reading through the code; I think the
io.Copywas a remnant of an older version of that code. I don't see a reason why we don't just let it fall through and serve the file withhttp.ServeContentnormally. More consistent that way.Also, I hoisted thestatus_codeoverride logic above the error handler detection bit, because then it allows for overriding the status code during error routes. Not necessarily that likely to be useful, but it's more correct. (Reminder that the firstw.WriteHeader()wins, subsequent ones have no effect). Marking this as a bug just for this reason, but it's barely worth discussing as a bug 😅.Edit: So calling
w.WriteHeader()ourselves makes thew.Header().Set()operations inhttp.ServeContentnot actually work correctly, which isn't ideal.After a bit of googling, I found a great solution, by wrapping the
ResponseWriterto intercept thew.WriteHeader()call thathttp.ServeContentdoes to write the header we want instead.