mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-03 03:27:10 +00:00
fix(replay): a truncated index is a 500, not a partial 200
readAll reimplemented io.ReadAll and swallowed every error but fs.ErrInvalid, returning the bytes it had managed to read with a nil error, so a half-written index.html was indistinguishable from a whole one. io.ReadAll surfaces the directory-handle case fs.ErrInvalid covered as well.
This commit is contained in:
1 parent
dbd57ef989
commit
30febb78b8
2 files changed
+51
-20
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
@@ -233,7 +234,7 @@ func serveIndex(responseWriter http.ResponseWriter, assets fs.FS) {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
body, err := readAll(file)
|
||||
body, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -242,25 +243,6 @@ func serveIndex(responseWriter http.ResponseWriter, assets fs.FS) {
|
||||
_, _ = responseWriter.Write(body)
|
||||
}
|
||||
|
||||
func readAll(file fs.File) ([]byte, error) {
|
||||
const initialCapacity = 4 * 1024
|
||||
buffer := make([]byte, 0, initialCapacity)
|
||||
chunk := make([]byte, 4*1024)
|
||||
for {
|
||||
read, err := file.Read(chunk)
|
||||
if read > 0 {
|
||||
buffer = append(buffer, chunk[:read]...)
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrInvalid) {
|
||||
return nil, err
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return buffer, nil
|
||||
}
|
||||
|
||||
func writeJSON(responseWriter http.ResponseWriter, status int, payload any) {
|
||||
responseWriter.Header().Set("Content-Type", "application/json")
|
||||
responseWriter.WriteHeader(status)
|
||||
|
||||
Reference in new issue
Block a user