perkeep/lib/go/httprange/range.go

75 lines
1.7 KiB
Go
Raw Normal View History

2011-01-28 07:07:18 +00:00
/*
Copyright 2011 Google Inc.
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.
*/
package httprange
2010-08-03 02:30:00 +00:00
import (
"http"
"regexp"
"strconv"
)
// Default is {0, -1} to read all of a file.
type Range struct {
skipBytes int64
limitBytes int64 // or -1 to read all
2010-08-03 02:30:00 +00:00
}
func (rr *Range) SkipBytes() int64 {
return rr.skipBytes
2010-08-03 02:30:00 +00:00
}
// LimitBytes reads the max number of bytes to read, or -1 for no limit.
func (rr *Range) LimitBytes() int64 {
return rr.limitBytes
}
func (rr *Range) IsWholeFile() bool {
return rr.skipBytes == 0 && rr.limitBytes == -1
}
var WholeRange = &Range{0, -1}
2010-08-03 02:30:00 +00:00
var rangePattern = regexp.MustCompile(`bytes=([0-9]+)-([0-9]*)`)
func FromRequest(req *http.Request) *Range {
rrange := req.Header.Get("Range")
if rrange == "" {
return WholeRange
2010-08-03 02:30:00 +00:00
}
return FromString(rrange)
2010-08-03 02:30:00 +00:00
}
func FromString(rrange string) *Range {
matches := rangePattern.FindStringSubmatch(rrange)
2010-08-03 02:30:00 +00:00
if len(matches) == 0 {
return WholeRange
2010-08-03 02:30:00 +00:00
}
2010-08-03 03:02:29 +00:00
skipBytes, _ := strconv.Atoi64(matches[1])
lastByteInclusive := int64(-1)
2010-08-03 02:30:00 +00:00
if len(matches[2]) > 0 {
2010-08-03 03:02:29 +00:00
lastByteInclusive, _ = strconv.Atoi64(matches[2])
}
limitBytes := int64(-1)
if lastByteInclusive != -1 {
limitBytes = lastByteInclusive - skipBytes + 1
if limitBytes < 0 {
limitBytes = 0
}
2010-08-03 02:30:00 +00:00
}
return &Range{skipBytes, limitBytes}
2010-08-03 02:30:00 +00:00
}