1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-16 00:04:40 +00:00

backend/http: support content-range response header

This commit is contained in:
Arnie97
2022-03-20 18:12:48 +08:00
committed by Nick Craig-Wood
parent ebac854512
commit 6a5b7664f7
5 changed files with 146 additions and 58 deletions

View File

@@ -13,7 +13,6 @@ import (
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync"
"time"
@@ -317,15 +316,6 @@ func (f *Fs) url(remote string) string {
return f.endpointURL + rest.URLPathEscape(remote)
}
// parse s into an int64, on failure return def
func parseInt64(s string, def int64) int64 {
n, e := strconv.ParseInt(s, 10, 64)
if e != nil {
return def
}
return n
}
// Errors returned by parseName
var (
errURLJoinFailed = errors.New("URLJoin failed")
@@ -601,23 +591,18 @@ func (o *Object) head(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to stat: %w", err)
}
return o.stat(ctx, res, true)
return o.stat(ctx, res)
}
// stat updates info fields in the Object according to HTTP response headers
func (o *Object) stat(ctx context.Context, res *http.Response, isRangeRequest bool) error {
func (o *Object) stat(ctx context.Context, res *http.Response) error {
t, err := http.ParseTime(res.Header.Get("Last-Modified"))
if err != nil {
t = timeUnset
}
o.modTime = t
// 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")
}
o.contentType = res.Header.Get("Content-Type")
o.size = rest.ParseSizeFromHeaders(res.Header)
// If NoSlash is set then check ContentType to see if it is a directory
if o.fs.opt.NoSlash {
@@ -666,8 +651,7 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
}
if o.fs.opt.NoHead {
isRangeRequest := len(req.Header.Get("Range")) > 0
if err = o.stat(ctx, res, isRangeRequest); err != nil {
if err = o.stat(ctx, res); err != nil {
return nil, fmt.Errorf("Stat failed: %w", err)
}
}