2021-06-14 13:42:49 +02:00
|
|
|
// Options for Open
|
2016-09-10 12:29:57 +02:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2018-01-27 10:27:50 +01:00
|
|
|
"strings"
|
2018-01-12 17:30:54 +01:00
|
|
|
|
2018-01-27 10:27:50 +01:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2016-09-10 12:29:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// OpenOption is an interface describing options for Open
|
|
|
|
type OpenOption interface {
|
|
|
|
fmt.Stringer
|
|
|
|
|
|
|
|
// Header returns the option as an HTTP header
|
|
|
|
Header() (key string, value string)
|
|
|
|
|
|
|
|
// Mandatory returns whether this option can be ignored or not
|
|
|
|
Mandatory() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeOption defines an HTTP Range option with start and end. If
|
|
|
|
// either start or end are < 0 then they will be omitted.
|
2018-01-21 10:51:28 +01:00
|
|
|
//
|
|
|
|
// End may be bigger than the Size of the object in which case it will
|
|
|
|
// be capped to the size of the object.
|
|
|
|
//
|
|
|
|
// Note that the End is inclusive, so to fetch 100 bytes you would use
|
|
|
|
// RangeOption{Start: 0, End: 99}
|
|
|
|
//
|
2018-02-19 16:49:39 +01:00
|
|
|
// If Start is specified but End is not then it will fetch from Start
|
|
|
|
// to the end of the file.
|
|
|
|
//
|
|
|
|
// If End is specified, but Start is not then it will fetch the last
|
|
|
|
// End bytes.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// RangeOption{Start: 0, End: 99} - fetch the first 100 bytes
|
|
|
|
// RangeOption{Start: 100, End: 199} - fetch the second 100 bytes
|
2020-04-24 12:24:57 +02:00
|
|
|
// RangeOption{Start: 100, End: -1} - fetch bytes from offset 100 to the end
|
|
|
|
// RangeOption{Start: -1, End: 100} - fetch the last 100 bytes
|
2018-02-19 16:49:39 +01:00
|
|
|
//
|
2018-01-21 10:51:28 +01:00
|
|
|
// A RangeOption implements a single byte-range-spec from
|
|
|
|
// https://tools.ietf.org/html/rfc7233#section-2.1
|
2016-09-10 12:29:57 +02:00
|
|
|
type RangeOption struct {
|
|
|
|
Start int64
|
|
|
|
End int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header formats the option as an http header
|
|
|
|
func (o *RangeOption) Header() (key string, value string) {
|
|
|
|
key = "Range"
|
|
|
|
value = "bytes="
|
|
|
|
if o.Start >= 0 {
|
2017-06-19 18:35:37 +02:00
|
|
|
value += strconv.FormatInt(o.Start, 10)
|
2016-09-10 12:29:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
value += "-"
|
|
|
|
if o.End >= 0 {
|
2017-06-19 18:35:37 +02:00
|
|
|
value += strconv.FormatInt(o.End, 10)
|
2016-09-10 12:29:57 +02:00
|
|
|
}
|
|
|
|
return key, value
|
|
|
|
}
|
|
|
|
|
2018-01-27 10:27:50 +01:00
|
|
|
// ParseRangeOption parses a RangeOption from a Range: header.
|
2019-04-30 14:06:24 +02:00
|
|
|
// It only accepts single ranges.
|
2018-01-27 10:27:50 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// String formats the option into human readable form
|
|
|
|
func (o *RangeOption) String() string {
|
|
|
|
return fmt.Sprintf("RangeOption(%d,%d)", o.Start, o.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory returns whether the option must be parsed or can be ignored
|
|
|
|
func (o *RangeOption) Mandatory() bool {
|
2018-02-19 16:49:39 +01:00
|
|
|
return true
|
2016-09-10 12:29:57 +02:00
|
|
|
}
|
|
|
|
|
2018-01-22 20:44:00 +01:00
|
|
|
// Decode interprets the RangeOption into an offset and a limit
|
2018-01-27 11:07:17 +01:00
|
|
|
//
|
|
|
|
// The offset is the start of the stream and the limit is how many
|
|
|
|
// bytes should be read from it. If the limit is -1 then the stream
|
|
|
|
// should be read to the end.
|
2018-01-22 20:44:00 +01:00
|
|
|
func (o *RangeOption) Decode(size int64) (offset, limit int64) {
|
|
|
|
if o.Start >= 0 {
|
|
|
|
offset = o.Start
|
|
|
|
if o.End >= 0 {
|
|
|
|
limit = o.End - o.Start + 1
|
|
|
|
} else {
|
2018-01-27 11:07:17 +01:00
|
|
|
limit = -1
|
2018-01-22 20:44:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-27 11:07:17 +01:00
|
|
|
if o.End >= 0 {
|
|
|
|
offset = size - o.End
|
|
|
|
} else {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
limit = -1
|
2018-01-22 20:44:00 +01:00
|
|
|
}
|
|
|
|
return offset, limit
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:05:00 +01:00
|
|
|
// FixRangeOption looks through the slice of options and adjusts any
|
|
|
|
// RangeOption~s found that request a fetch from the end into an
|
2018-08-07 18:37:21 +02:00
|
|
|
// absolute fetch using the size passed in and makes sure the range does
|
2020-10-13 23:49:58 +02:00
|
|
|
// not exceed filesize. Some remotes (e.g. Onedrive, Box) don't support
|
2018-08-07 18:37:21 +02:00
|
|
|
// range requests which index from the end.
|
2018-01-22 18:05:00 +01:00
|
|
|
func FixRangeOption(options []OpenOption, size int64) {
|
2019-08-03 18:44:44 +02:00
|
|
|
if size == 0 {
|
|
|
|
// if size 0 then remove RangeOption~s
|
2020-05-20 12:39:20 +02:00
|
|
|
// replacing with a NullOptions~s which won't be rendered
|
2019-08-03 18:44:44 +02:00
|
|
|
for i := range options {
|
|
|
|
if _, ok := options[i].(*RangeOption); ok {
|
2020-04-24 12:24:57 +02:00
|
|
|
options[i] = NullOption{}
|
2019-08-03 18:44:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-01-22 18:05:00 +01:00
|
|
|
for i := range options {
|
|
|
|
option := options[i]
|
|
|
|
if x, ok := option.(*RangeOption); ok {
|
|
|
|
// If start is < 0 then fetch from the end
|
|
|
|
if x.Start < 0 {
|
|
|
|
x = &RangeOption{Start: size - x.End, End: -1}
|
|
|
|
options[i] = x
|
|
|
|
}
|
2018-08-07 18:37:21 +02:00
|
|
|
if x.End > size {
|
2019-08-03 18:44:44 +02:00
|
|
|
x = &RangeOption{Start: x.Start, End: size - 1}
|
2018-08-07 18:37:21 +02:00
|
|
|
options[i] = x
|
|
|
|
}
|
2018-01-22 18:05:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// SeekOption defines an HTTP Range option with start only.
|
|
|
|
type SeekOption struct {
|
|
|
|
Offset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header formats the option as an http header
|
|
|
|
func (o *SeekOption) Header() (key string, value string) {
|
|
|
|
key = "Range"
|
|
|
|
value = fmt.Sprintf("bytes=%d-", o.Offset)
|
|
|
|
return key, value
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the option into human readable form
|
|
|
|
func (o *SeekOption) String() string {
|
|
|
|
return fmt.Sprintf("SeekOption(%d)", o.Offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory returns whether the option must be parsed or can be ignored
|
|
|
|
func (o *SeekOption) Mandatory() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPOption defines a general purpose HTTP option
|
|
|
|
type HTTPOption struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header formats the option as an http header
|
|
|
|
func (o *HTTPOption) Header() (key string, value string) {
|
|
|
|
return o.Key, o.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the option into human readable form
|
|
|
|
func (o *HTTPOption) String() string {
|
|
|
|
return fmt.Sprintf("HTTPOption(%q,%q)", o.Key, o.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory returns whether the option must be parsed or can be ignored
|
|
|
|
func (o *HTTPOption) Mandatory() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:44:22 +02:00
|
|
|
// HashesOption defines an option used to tell the local fs to limit
|
|
|
|
// the number of hashes it calculates.
|
|
|
|
type HashesOption struct {
|
2018-01-12 17:30:54 +01:00
|
|
|
Hashes hash.Set
|
2017-05-28 13:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header formats the option as an http header
|
|
|
|
func (o *HashesOption) Header() (key string, value string) {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the option into human readable form
|
|
|
|
func (o *HashesOption) String() string {
|
|
|
|
return fmt.Sprintf("HashesOption(%v)", o.Hashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory returns whether the option must be parsed or can be ignored
|
|
|
|
func (o *HashesOption) Mandatory() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-24 12:24:57 +02:00
|
|
|
// NullOption defines an Option which does nothing
|
|
|
|
type NullOption struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header formats the option as an http header
|
|
|
|
func (o NullOption) Header() (key string, value string) {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formats the option into human readable form
|
|
|
|
func (o NullOption) String() string {
|
|
|
|
return fmt.Sprintf("NullOption()")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory returns whether the option must be parsed or can be ignored
|
|
|
|
func (o NullOption) Mandatory() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// OpenOptionAddHeaders adds each header found in options to the
|
|
|
|
// headers map provided the key was non empty.
|
|
|
|
func OpenOptionAddHeaders(options []OpenOption, headers map[string]string) {
|
|
|
|
for _, option := range options {
|
|
|
|
key, value := option.Header()
|
|
|
|
if key != "" && value != "" {
|
|
|
|
headers[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenOptionHeaders adds each header found in options to the
|
|
|
|
// headers map provided the key was non empty.
|
|
|
|
//
|
|
|
|
// It returns a nil map if options was empty
|
|
|
|
func OpenOptionHeaders(options []OpenOption) (headers map[string]string) {
|
|
|
|
if len(options) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
headers = make(map[string]string, len(options))
|
|
|
|
OpenOptionAddHeaders(options, headers)
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenOptionAddHTTPHeaders Sets each header found in options to the
|
|
|
|
// http.Header map provided the key was non empty.
|
|
|
|
func OpenOptionAddHTTPHeaders(headers http.Header, options []OpenOption) {
|
|
|
|
for _, option := range options {
|
|
|
|
key, value := option.Header()
|
|
|
|
if key != "" && value != "" {
|
|
|
|
headers.Set(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|