From ebac8545127f074f0a0f0b4d8d511b3466e908c1 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Sun, 20 Mar 2022 16:30:46 +0800 Subject: [PATCH] backend/http: do not update object size based on range requests --- backend/http/http.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/http/http.go b/backend/http/http.go index 59a23916e..498d92e21 100644 --- a/backend/http/http.go +++ b/backend/http/http.go @@ -601,18 +601,24 @@ func (o *Object) head(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to stat: %w", err) } - return o.stat(ctx, res) + return o.stat(ctx, res, true) } // stat updates info fields in the Object according to HTTP response headers -func (o *Object) stat(ctx context.Context, res *http.Response) error { +func (o *Object) stat(ctx context.Context, res *http.Response, isRangeRequest bool) error { t, err := http.ParseTime(res.Header.Get("Last-Modified")) if err != nil { t = timeUnset } - o.size = parseInt64(res.Header.Get("Content-Length"), -1) o.modTime = t - o.contentType = res.Header.Get("Content-Type") + + // TODO: parse Content-Range for total size + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests + if !isRangeRequest { + o.size = parseInt64(res.Header.Get("Content-Length"), -1) + o.contentType = res.Header.Get("Content-Type") + } + // If NoSlash is set then check ContentType to see if it is a directory if o.fs.opt.NoSlash { mediaType, _, err := mime.ParseMediaType(o.contentType) @@ -660,7 +666,8 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read } if o.fs.opt.NoHead { - if err = o.stat(ctx, res); err != nil { + isRangeRequest := len(req.Header.Get("Range")) > 0 + if err = o.stat(ctx, res, isRangeRequest); err != nil { return nil, fmt.Errorf("Stat failed: %w", err) } }