fileserver: Better handling of HTTP status override #4132
Merged
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.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 withhttp.ServeContent
normally. More consistent that way.Also, I hoisted thestatus_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 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.ServeContent
not actually work correctly, which isn't ideal.After a bit of googling, I found a great solution, by wrapping the
ResponseWriter
to intercept thew.WriteHeader()
call thathttp.ServeContent
does to write the header we want instead.