Repository navigation
Fix 500 Internal Server Error on empty files. - #189
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a 500 Internal Server Error when serving empty files by treating EOF on zero-length files as non-fatal and only sniffing content/charset when bytes were read.
- Update
f.Readto distinguish EOF from other errors and capture bytes read (n). - Add logic to skip content-type and charset detection if no bytes were read.
- Ensure empty files do not trigger an internal error.
Comments suppressed due to low confidence (1)
internal/server/handlers.go:309
- Consider adding unit tests for the empty-file path to verify that
serveFileno longer returns a 500 error and sets appropriate headers for zero-length files.
n, err := f.Read(data[:])
There was a problem hiding this comment.
Pull Request Overview
This PR updates serveFile to handle empty files without returning a 500 error by treating io.EOF as a non-error, only performing content detection when bytes were read, and resetting the file pointer before serving.
- Ignore
io.EOFwhen probing the file, preventing errors for empty files. - Only detect MIME type and charset if data was actually read.
- Seek back to the start of the file after probing.
Comments suppressed due to low confidence (2)
internal/server/handlers.go:309
- Add a unit test for serving an empty file to verify that it no longer returns a 500 error when
n == 0.
n, err := f.Read(data[:])
internal/server/handlers.go:308
- [nitpick] Extract the buffer size (512) into a named constant (e.g.,
probeBufferSize) to clarify its purpose and make it easier to adjust.
var data [512]byte
There was a problem hiding this comment.
Pull Request Overview
This PR addresses a 500 Internal Server Error when serving empty files by skipping content inspection on zero-byte reads, resetting the file offset after peeking, and adjusting ETag generation for empty responses.
- Guard content-type and charset detection to only run when at least one byte is read.
- Reset the file’s read pointer to the start after the initial detection read.
- Modify the ETag middleware to skip ETag computation on empty bodies.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/server/handlers.go | Skip content detection on empty reads, add Seek to reset the file cursor, import io. |
| internal/middlewares/etag.go | Add rw.size > 0 guard to avoid computing ETags for empty responses. |
Comments suppressed due to low confidence (2)
internal/server/handlers.go:7
- The new charset detection code uses
strings.HasPrefixandutf8.Valid, but neitherstringsnorunicode/utf8are imported. Add"strings"and"unicode/utf8"to the import block to ensure compilation.
"io"
internal/middlewares/etag.go:67
- Requiring
rw.size > 0skips ETag generation for legitimately empty responses. Remove this condition so that zero-length bodies also receive an ETag.
if rw.size > 0 && rw.size <= rw.maxSize && rw.err == nil && rw.statusCode >= 200 && rw.statusCode < 300 {
No description provided.