Files
OpenList/server/s3/range.go
T
ShenLin d9d8aa24e6 fix(s3): default upload content types and return partial content (#3053)
- Default missing upload MIME types to application/octet-stream before passing streams to storage drivers.
- Return HTTP 206 for successful ranged GET responses while preserving error statuses.
- Add isolated response-status regression tests without database initialization.

Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com>
Co-authored-by: Codex <267193182+codex@users.noreply.github.com>
2026-09-07 12:01:15 +08:00

48 lines
1.1 KiB
Go

package s3
import "net/http"
// gofakes3 sets Content-Range without selecting the partial-content status.
func rangeStatusHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.Header.Get("Range") == "" {
next.ServeHTTP(w, r)
return
}
rw := &rangeResponseWriter{ResponseWriter: w}
next.ServeHTTP(rw, r)
if !rw.wroteHeader {
rw.WriteHeader(http.StatusOK)
}
})
}
type rangeResponseWriter struct {
http.ResponseWriter
wroteHeader bool
}
func (w *rangeResponseWriter) WriteHeader(status int) {
if w.wroteHeader {
return
}
if status >= 200 {
w.wroteHeader = true
}
if status == http.StatusOK && w.Header().Get("Content-Range") != "" {
status = http.StatusPartialContent
}
w.ResponseWriter.WriteHeader(status)
}
func (w *rangeResponseWriter) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
return w.ResponseWriter.Write(p)
}
func (w *rangeResponseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}