filer, volume: ignore Range when If-Range no longer matches (#12052)

* filer, volume: ignore Range when If-Range no longer matches

A GET with Range and a stale If-Range validator got 206 with the requested
bytes of the current file. RFC 9110 13.1.5: send the whole content instead,
so a resumed download restarts rather than mixing two versions.

* volume(rs): drop Range when If-Range no longer matches

Mirror the Go fix for the Rust volume server: evaluate If-Range against the
response ETag (strong comparison) or Last-Modified once the needle's
validators are known, then drop the range on a stale validator so the whole
current representation is sent. A dropped range on the RangeFromSource
strategy falls back to a full stream of the data-file region; buffered and
chunk-manifest paths already take Option<&str>.

* volume(rs): fail If-Range when the header value is not readable

* volume(rs): accept all HTTP-date formats in If-Range like Go

* server: cover alternate HTTP-date formats in If-Range test

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
Jay HemnaniandChris Lu authored and GitHub committed 2026-10-25 06:36:40 -07:00
1 parent da1d7444b8
commit 0c9872f842
5 files changed
+159 -8

No files matched your search

+25
View File
@@ -313,8 +313,33 @@ func (t *responseBodyTracker) Write(p []byte) (int, error) {
return n, err
}
// ifRangeMatches reports whether an If-Range validator still names the
// representation described by header (RFC 9110 13.1.5): an entity tag by
// strong comparison, or a date equal to Last-Modified.
func ifRangeMatches(ifRange string, header http.Header) bool {
ifRange = strings.TrimSpace(ifRange)
if ifRange == "" {
return true
}
if strings.HasPrefix(ifRange, "\"") || strings.HasPrefix(ifRange, "W/") {
etag := header.Get("ETag")
return etag != "" && !strings.HasPrefix(etag, "W/") && ifRange == etag
}
date, err := http.ParseTime(ifRange)
if err != nil {
return false
}
lastModified, err := http.ParseTime(header.Get("Last-Modified"))
return err == nil && date.Equal(lastModified)
}
func ProcessRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, prepareWriteFn func(offset int64, size int64) (filer.DoStreamContent, error)) error {
rangeReq := r.Header.Get("Range")
// A Range whose If-Range no longer matches is ignored and the whole content
// is sent, so a resumed download restarts instead of mixing two versions.
if rangeReq != "" && !ifRangeMatches(r.Header.Get("If-Range"), w.Header()) {
rangeReq = ""
}
sink := &responseBodyTracker{ResponseWriter: w}
bufferedWriter := writePool.Get().(*bufio.Writer)
bufferedWriter.Reset(sink)
+42
View File
@@ -343,6 +343,48 @@ func TestProcessRangeRequestRanges(t *testing.T) {
}
}
func TestProcessRangeRequestIfRange(t *testing.T) {
data := []byte("0123456789")
const etag = `"v2"`
const lastModified = "Sat, 24 Oct 2026 22:01:04 GMT"
tests := []struct {
ifRange string
wantCode int
wantBody string
}{
{"", http.StatusPartialContent, "01"},
{etag, http.StatusPartialContent, "01"},
{lastModified, http.StatusPartialContent, "01"},
{"Saturday, 24-Oct-26 22:01:04 GMT", http.StatusPartialContent, "01"},
{"Sat Oct 24 22:01:04 2026", http.StatusPartialContent, "01"},
{`"v1"`, http.StatusOK, "0123456789"},
{`W/"v2"`, http.StatusOK, "0123456789"},
{"Sat, 24 Oct 2026 22:01:03 GMT", http.StatusOK, "0123456789"},
{"not a date", http.StatusOK, "0123456789"},
}
for _, tt := range tests {
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/test.txt", nil)
r.Header.Set("Range", "bytes=0-1")
if tt.ifRange != "" {
r.Header.Set("If-Range", tt.ifRange)
}
w := httptest.NewRecorder()
w.Header().Set("ETag", etag)
w.Header().Set("Last-Modified", lastModified)
if err := ProcessRangeRequest(r, w, int64(len(data)), "text/plain", func(offset int64, size int64) (filer.DoStreamContent, error) {
return func(writer io.Writer) error {
_, err := writer.Write(data[offset : offset+size])
return err
}, nil
}); err != nil {
t.Errorf("If-Range %q: %v", tt.ifRange, err)
}
if w.Code != tt.wantCode || w.Body.String() != tt.wantBody {
t.Errorf("If-Range %q: got %d %q, want %d %q", tt.ifRange, w.Code, w.Body.String(), tt.wantCode, tt.wantBody)
}
}
}
// /submit must reject at the limit its master was given, not at a hardcoded
// 256MB: weed server hands -volume.fileSizeLimitMB down to the master, and an
// upload the volume server would store must not be turned away here (#6748).