fs: Add ParseRangeOption to parse incoming Range: requests

This commit is contained in:
Nick Craig-Wood 2018-01-27 09:27:50 +00:00
parent bc3ee977f4
commit 9a73688e3a
2 changed files with 73 additions and 0 deletions

View file

@ -6,8 +6,10 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/ncw/rclone/fs/hash"
"github.com/pkg/errors"
)
// OpenOption is an interface describing options for Open
@ -52,6 +54,38 @@ func (o *RangeOption) Header() (key string, value string) {
return key, value
}
// ParseRangeOption parses a RangeOption from a Range: header.
// It only appects single ranges.
func ParseRangeOption(s string) (po *RangeOption, err error) {
const preamble = "bytes="
if !strings.HasPrefix(s, preamble) {
return nil, errors.New("Range: header invalid: doesn't start with " + preamble)
}
s = s[len(preamble):]
if strings.IndexRune(s, ',') >= 0 {
return nil, errors.New("Range: header invalid: contains multiple ranges which isn't supported")
}
dash := strings.IndexRune(s, '-')
if dash < 0 {
return nil, errors.New("Range: header invalid: contains no '-'")
}
start, end := strings.TrimSpace(s[:dash]), strings.TrimSpace(s[dash+1:])
o := RangeOption{Start: -1, End: -1}
if start != "" {
o.Start, err = strconv.ParseInt(start, 10, 64)
if err != nil || o.Start < 0 {
return nil, errors.New("Range: header invalid: bad start")
}
}
if end != "" {
o.End, err = strconv.ParseInt(end, 10, 64)
if err != nil || o.End < 0 {
return nil, errors.New("Range: header invalid: bad end")
}
}
return &o, nil
}
// String formats the option into human readable form
func (o *RangeOption) String() string {
return fmt.Sprintf("RangeOption(%d,%d)", o.Start, o.End)

39
fs/options_test.go Normal file
View file

@ -0,0 +1,39 @@
package fs
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseRangeOption(t *testing.T) {
for _, test := range []struct {
in string
want RangeOption
err string
}{
{in: "", err: "doesn't start with bytes="},
{in: "bytes=1-2,3-4", err: "contains multiple ranges"},
{in: "bytes=100", err: "contains no '-'"},
{in: "bytes=x-8", err: "bad start"},
{in: "bytes=8-x", err: "bad end"},
{in: "bytes=1-2", want: RangeOption{Start: 1, End: 2}},
{in: "bytes=-123456789123456789", want: RangeOption{Start: -1, End: 123456789123456789}},
{in: "bytes=123456789123456789-", want: RangeOption{Start: 123456789123456789, End: -1}},
{in: "bytes= 1 - 2 ", want: RangeOption{Start: 1, End: 2}},
{in: "bytes=-", want: RangeOption{Start: -1, End: -1}},
{in: "bytes= - ", want: RangeOption{Start: -1, End: -1}},
} {
got, err := ParseRangeOption(test.in)
what := fmt.Sprintf("parsing %q", test.in)
if test.err != "" {
require.Contains(t, err.Error(), test.err)
require.Nil(t, got, what)
} else {
require.NoError(t, err, what)
assert.Equal(t, test.want, *got, what)
}
}
}