mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
File transfer: New API /file/read
This commit is contained in:
94
webapi/HTTP Range.go
Normal file
94
webapi/HTTP Range.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
File Name: HTTP Range.go
|
||||
Copyright: 2021 Peernet Foundation s.r.o.
|
||||
Author: Peter Kleissner
|
||||
*/
|
||||
|
||||
package webapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fork from https://golang.org/src/net/http/fs.go
|
||||
|
||||
// HTTPRange represents an HTTP range
|
||||
type HTTPRange struct {
|
||||
start, length int
|
||||
}
|
||||
|
||||
// ParseRangeHeader parses a Range header string as per RFC 7233.
|
||||
func ParseRangeHeader(s string, size int, noMultiRange bool) ([]HTTPRange, error) {
|
||||
if s == "" {
|
||||
return nil, nil // header not present
|
||||
}
|
||||
const b = "bytes="
|
||||
if !strings.HasPrefix(s, b) {
|
||||
return nil, errors.New("invalid range")
|
||||
}
|
||||
var ranges []HTTPRange
|
||||
for _, ra := range strings.Split(s[len(b):], ",") {
|
||||
ra = textproto.TrimString(ra)
|
||||
if ra == "" {
|
||||
continue
|
||||
}
|
||||
i := strings.Index(ra, "-")
|
||||
if i < 0 {
|
||||
return nil, errors.New("invalid range")
|
||||
}
|
||||
start, end := textproto.TrimString(ra[:i]), textproto.TrimString(ra[i+1:])
|
||||
var r HTTPRange
|
||||
if start == "" {
|
||||
if size < 0 {
|
||||
return nil, errors.New("range start relative to end not supported")
|
||||
}
|
||||
// If no start is specified, end specifies the
|
||||
// range start relative to the end of the file.
|
||||
i, err := strconv.ParseInt(end, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid range")
|
||||
}
|
||||
if int(i) > size {
|
||||
i = int64(size)
|
||||
}
|
||||
r.start = size - int(i)
|
||||
r.length = size - r.start
|
||||
} else {
|
||||
i, err := strconv.ParseInt(start, 10, 64)
|
||||
if err != nil || i < 0 {
|
||||
return nil, errors.New("invalid range")
|
||||
}
|
||||
if size > 0 && int(i) >= size {
|
||||
// If the range begins after the size of the content, then it does not overlap. -> always return error.
|
||||
return nil, errors.New("start out of range")
|
||||
}
|
||||
r.start = int(i)
|
||||
if end == "" {
|
||||
// If no end is specified, range extends to end of the file.
|
||||
if size < 0 {
|
||||
//return nil, errors.New("open range not supported")
|
||||
r.length = -1
|
||||
} else {
|
||||
r.length = size - r.start
|
||||
}
|
||||
} else {
|
||||
i, err := strconv.ParseInt(end, 10, 64)
|
||||
if err != nil || r.start > int(i) {
|
||||
return nil, errors.New("invalid range")
|
||||
}
|
||||
if size > 0 && int(i) >= size {
|
||||
i = int64(size - 1)
|
||||
}
|
||||
r.length = int(i) - r.start + 1
|
||||
}
|
||||
}
|
||||
ranges = append(ranges, r)
|
||||
if noMultiRange && len(ranges) > 1 {
|
||||
return nil, errors.New("multiple ranges not supported")
|
||||
}
|
||||
}
|
||||
return ranges, nil
|
||||
}
|
||||
Reference in New Issue
Block a user