You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.2 KiB
135 lines
4.2 KiB
10 years ago
|
/*
|
||
9 years ago
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
10 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
9 years ago
|
"regexp"
|
||
10 years ago
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
const (
|
||
9 years ago
|
byteRangePrefix = "bytes="
|
||
10 years ago
|
)
|
||
|
|
||
9 years ago
|
// Valid byte position regexp
|
||
|
var validBytePos = regexp.MustCompile(`^[0-9]+$`)
|
||
|
|
||
10 years ago
|
// HttpRange specifies the byte range to be sent to the client.
|
||
|
type httpRange struct {
|
||
8 years ago
|
offsetBegin int64
|
||
|
offsetEnd int64
|
||
|
resourceSize int64
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// String populate range stringer interface
|
||
9 years ago
|
func (hrange httpRange) String() string {
|
||
8 years ago
|
return fmt.Sprintf("bytes %d-%d/%d", hrange.offsetBegin, hrange.offsetEnd, hrange.resourceSize)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// getlength - get length from the range.
|
||
9 years ago
|
func (hrange httpRange) getLength() int64 {
|
||
8 years ago
|
return 1 + hrange.offsetEnd - hrange.offsetBegin
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRange, err error) {
|
||
9 years ago
|
// 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)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
// Trim byte range prefix.
|
||
|
byteRangeString := strings.TrimPrefix(rangeString, byteRangePrefix)
|
||
|
|
||
9 years ago
|
// Check if range string contains delimiter '-', else return error. eg. "bytes=8"
|
||
9 years ago
|
sepIndex := strings.Index(byteRangeString, "-")
|
||
|
if sepIndex == -1 {
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have a valid range value", rangeString)
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
offsetBeginString := byteRangeString[:sepIndex]
|
||
|
offsetBegin := int64(-1)
|
||
|
// Convert offsetBeginString only if its not empty.
|
||
|
if len(offsetBeginString) > 0 {
|
||
|
if !validBytePos.MatchString(offsetBeginString) {
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have a valid first byte position value", rangeString)
|
||
|
}
|
||
|
|
||
8 years ago
|
if offsetBegin, err = strconv.ParseInt(offsetBeginString, 10, 64); err != nil {
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have a valid first byte position value", rangeString)
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
offsetEndString := byteRangeString[sepIndex+1:]
|
||
|
offsetEnd := int64(-1)
|
||
|
// Convert offsetEndString only if its not empty.
|
||
|
if len(offsetEndString) > 0 {
|
||
|
if !validBytePos.MatchString(offsetEndString) {
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have a valid last byte position value", rangeString)
|
||
|
}
|
||
|
|
||
8 years ago
|
if offsetEnd, err = strconv.ParseInt(offsetEndString, 10, 64); err != nil {
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have a valid last byte position value", rangeString)
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// rangeString contains first and last byte positions. eg. "bytes=2-5"
|
||
8 years ago
|
if offsetBegin > -1 && offsetEnd > -1 {
|
||
|
if offsetBegin > offsetEnd {
|
||
9 years ago
|
// Last byte position is not greater than first byte position. eg. "bytes=5-2"
|
||
|
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// First and last byte positions should not be >= resourceSize.
|
||
|
if offsetBegin >= resourceSize {
|
||
9 years ago
|
return nil, errInvalidRange
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if offsetEnd >= resourceSize {
|
||
|
offsetEnd = resourceSize - 1
|
||
9 years ago
|
}
|
||
8 years ago
|
} else if offsetBegin > -1 {
|
||
9 years ago
|
// rangeString contains only first byte position. eg. "bytes=8-"
|
||
8 years ago
|
if offsetBegin >= resourceSize {
|
||
|
// First byte position should not be >= resourceSize.
|
||
9 years ago
|
return nil, errInvalidRange
|
||
|
}
|
||
|
|
||
8 years ago
|
offsetEnd = resourceSize - 1
|
||
|
} else if offsetEnd > -1 {
|
||
9 years ago
|
// rangeString contains only last byte position. eg. "bytes=-3"
|
||
8 years ago
|
if offsetEnd == 0 {
|
||
9 years ago
|
// Last byte position should not be zero eg. "bytes=-0"
|
||
|
return nil, errInvalidRange
|
||
|
}
|
||
|
|
||
8 years ago
|
if offsetEnd >= resourceSize {
|
||
|
offsetBegin = 0
|
||
9 years ago
|
} else {
|
||
8 years ago
|
offsetBegin = resourceSize - offsetEnd
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
offsetEnd = resourceSize - 1
|
||
9 years ago
|
} else {
|
||
|
// rangeString contains first and last byte positions missing. eg. "bytes=-"
|
||
9 years ago
|
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
return &httpRange{offsetBegin, offsetEnd, resourceSize}, nil
|
||
10 years ago
|
}
|