fix: start with zero half-open interval range (#3431)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2024-08-13 19:34:58 +08:00 committed by GitHub
parent 5547307445
commit 584b67d571
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
"time" "time"
"github.com/go-http-utils/headers" "github.com/go-http-utils/headers"
@ -226,6 +227,10 @@ func (ptm *peerTaskManager) storePartialFile(ctx context.Context, request *FileT
return nil return nil
} }
func noRangeEnd(rg string) bool {
return strings.HasSuffix(rg, "-")
}
func (ptm *peerTaskManager) tryReuseStreamPeerTask(ctx context.Context, taskID string, func (ptm *peerTaskManager) tryReuseStreamPeerTask(ctx context.Context, taskID string,
request *StreamTaskRequest) (io.ReadCloser, map[string]string, bool) { request *StreamTaskRequest) (io.ReadCloser, map[string]string, bool) {
var ( var (
@ -329,12 +334,13 @@ func (ptm *peerTaskManager) tryReuseStreamPeerTask(ctx context.Context, taskID s
reuseRange.Start+reuseRange.Length-1, reuse.ContentLength) reuseRange.Start+reuseRange.Length-1, reuse.ContentLength)
} else if request.Range != nil { } else if request.Range != nil {
// the length is from reuse task, ensure it equal with request // the length is from reuse task, ensure it equal with request
if length != request.Range.Length { // skip check no range end case
if length != request.Range.Length && noRangeEnd(request.URLMeta.Range) {
log.Errorf("target task length %d did not match range length %d", length, request.Range.Length) log.Errorf("target task length %d did not match range length %d", length, request.Range.Length)
return nil, nil, false return nil, nil, false
} }
attr[headers.ContentRange] = fmt.Sprintf("bytes %d-%d/*", request.Range.Start, attr[headers.ContentRange] = fmt.Sprintf("bytes %d-%d/*", request.Range.Start,
request.Range.Start+request.Range.Length-1) request.Range.Start+length-1)
} }
// TODO record time when file closed, need add a type to implement Close and WriteTo // TODO record time when file closed, need add a type to implement Close and WriteTo

View File

@ -269,6 +269,10 @@ func NeedUseDragonfly(req *http.Request) bool {
return req.Method == http.MethodGet && layerReg.MatchString(req.URL.Path) return req.Method == http.MethodGet && layerReg.MatchString(req.URL.Path)
} }
func isRedundantRangeHeader(header string) bool {
return header == "bytes=0-"
}
// download uses dragonfly to download. // download uses dragonfly to download.
// the ctx has span info from transport, did not use the ctx from request // the ctx has span info from transport, did not use the ctx from request
func (rt *transport) download(ctx context.Context, req *http.Request) (*http.Response, error) { func (rt *transport) download(ctx context.Context, req *http.Request) (*http.Response, error) {
@ -289,7 +293,7 @@ func (rt *transport) download(ctx context.Context, req *http.Request) (*http.Res
var rg *nethttp.Range var rg *nethttp.Range
// Set meta range's value // Set meta range's value
if rangeHeader := req.Header.Get("Range"); len(rangeHeader) > 0 { if rangeHeader := req.Header.Get("Range"); !isRedundantRangeHeader(rangeHeader) && len(rangeHeader) > 0 {
rgs, err := nethttp.ParseRange(rangeHeader, math.MaxInt64) rgs, err := nethttp.ParseRange(rangeHeader, math.MaxInt64)
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)