2015-03-11 04:01:49 -04:00
|
|
|
/*
|
2016-07-06 15:50:24 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
2015-03-11 04:01:49 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
package main
|
2015-03-11 04:01:49 -04:00
|
|
|
|
|
|
|
import (
|
2015-03-15 22:40:30 -04:00
|
|
|
"errors"
|
2015-03-11 04:01:49 -04:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-07-06 15:50:24 -04:00
|
|
|
byteRangePrefix = "bytes="
|
2015-03-11 04:01:49 -04:00
|
|
|
)
|
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
// errInvalidRange - returned when given range value is not valid.
|
|
|
|
var errInvalidRange = errors.New("Invalid range")
|
|
|
|
|
|
|
|
// InvalidRange - invalid range typed error.
|
2016-05-26 06:15:01 -04:00
|
|
|
type InvalidRange struct{}
|
2016-04-19 05:42:10 -04:00
|
|
|
|
|
|
|
func (e InvalidRange) Error() string {
|
2016-05-26 06:15:01 -04:00
|
|
|
return "The requested range is not satisfiable"
|
2016-04-19 05:42:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 04:01:49 -04:00
|
|
|
// HttpRange specifies the byte range to be sent to the client.
|
|
|
|
type httpRange struct {
|
2016-07-06 15:50:24 -04:00
|
|
|
firstBytePos int64
|
|
|
|
lastBytePos int64
|
|
|
|
size int64
|
2015-03-11 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-28 22:33:56 -04:00
|
|
|
// String populate range stringer interface
|
2016-07-06 15:50:24 -04:00
|
|
|
func (hrange httpRange) String() string {
|
|
|
|
return fmt.Sprintf("bytes %d-%d/%d", hrange.firstBytePos, hrange.lastBytePos, hrange.size)
|
2015-03-11 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
// getLength - get length from the range.
|
|
|
|
func (hrange httpRange) getLength() int64 {
|
|
|
|
return 1 + hrange.lastBytePos - hrange.firstBytePos
|
2015-03-11 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
func parseRequestRange(rangeString string, size int64) (hrange *httpRange, err error) {
|
|
|
|
// Return error if given range string doesn't start with byte range prefix.
|
|
|
|
if !strings.HasPrefix(rangeString, byteRangePrefix) {
|
|
|
|
return nil, fmt.Errorf("'%s' does not start with '%s'", rangeString, byteRangePrefix)
|
2015-03-11 04:01:49 -04:00
|
|
|
}
|
2015-03-25 18:49:42 -04:00
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
// Trim byte range prefix.
|
|
|
|
byteRangeString := strings.TrimPrefix(rangeString, byteRangePrefix)
|
|
|
|
|
|
|
|
// Check if range string contains delimiter '-', else return error.
|
|
|
|
sepIndex := strings.Index(byteRangeString, "-")
|
|
|
|
if sepIndex == -1 {
|
|
|
|
return nil, fmt.Errorf("invalid range string '%s'", rangeString)
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|
2016-07-06 15:50:24 -04:00
|
|
|
|
|
|
|
firstBytePosString := byteRangeString[:sepIndex]
|
|
|
|
lastBytePosString := byteRangeString[sepIndex+1:]
|
|
|
|
|
|
|
|
firstBytePos := int64(-1)
|
|
|
|
lastBytePos := int64(-1)
|
|
|
|
|
|
|
|
// Convert firstBytePosString only if its not empty.
|
|
|
|
if len(firstBytePosString) > 0 {
|
|
|
|
if firstBytePos, err = strconv.ParseInt(firstBytePosString, 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("'%s' does not have valid first byte position value", rangeString)
|
|
|
|
}
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
// Convert lastBytePosString only if its not empty.
|
|
|
|
if len(lastBytePosString) > 0 {
|
|
|
|
if lastBytePos, err = strconv.ParseInt(lastBytePosString, 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("'%s' does not have valid last byte position value", rangeString)
|
|
|
|
}
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|
2016-07-06 15:50:24 -04:00
|
|
|
|
|
|
|
// Return error if firstBytePosString and lastBytePosString are empty.
|
|
|
|
if firstBytePos == -1 && lastBytePos == -1 {
|
|
|
|
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:50:24 -04:00
|
|
|
if firstBytePos == -1 {
|
|
|
|
// Return error if lastBytePos is zero and firstBytePos is not given eg. "bytes=-0"
|
|
|
|
if lastBytePos == 0 {
|
|
|
|
return nil, errInvalidRange
|
|
|
|
}
|
|
|
|
firstBytePos = size - lastBytePos
|
|
|
|
lastBytePos = size - 1
|
|
|
|
} else if lastBytePos == -1 {
|
|
|
|
lastBytePos = size - 1
|
|
|
|
} else if firstBytePos > lastBytePos {
|
|
|
|
// Return error if firstBytePos is greater than lastBytePos
|
|
|
|
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
|
|
|
|
} else if lastBytePos >= size {
|
|
|
|
// Set lastBytePos is out of size range.
|
|
|
|
lastBytePos = size - 1
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|
2016-07-06 15:50:24 -04:00
|
|
|
|
|
|
|
return &httpRange{firstBytePos, lastBytePos, size}, nil
|
2015-03-25 18:49:42 -04:00
|
|
|
}
|