Skip to content

Fix 500 Internal Server Error on empty files. - #189

Merged
patrickdappollonio merged 5 commits into
masterfrom
fix-500-empty-file
May 20, 2025
Merged

patrickdappollonio merged 5 commits into
masterfrom
fix-500-empty-file

Conversation

@patrickdappollonio

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.Read to 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 serveFile no longer returns a 500 error and sets appropriate headers for zero-length files.
n, err := f.Read(data[:])

Comment thread internal/server/handlers.go Resolved
Comment thread internal/server/handlers.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.EOF when 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

Comment thread internal/server/handlers.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.HasPrefix and utf8.Valid, but neither strings nor unicode/utf8 are imported. Add "strings" and "unicode/utf8" to the import block to ensure compilation.
"io"

internal/middlewares/etag.go:67

  • Requiring rw.size > 0 skips 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 {

@patrickdappollonio
patrickdappollonio merged commit 6123581 into master May 20, 2025
@patrickdappollonio
patrickdappollonio deleted the fix-500-empty-file branch May 20, 2025 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants