2020-06-01 16:25:45 +08:00
|
|
|
package webseed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-10-07 07:22:55 +08:00
|
|
|
"net/url"
|
2020-06-01 16:25:45 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
2022-04-22 10:23:43 +08:00
|
|
|
type PathEscaper func(pathComps []string) string
|
|
|
|
|
2022-02-23 15:03:38 +08:00
|
|
|
// Escapes path name components suitable for appending to a webseed URL. This works for converting
|
|
|
|
// S3 object keys to URLs too.
|
2022-04-22 10:23:43 +08:00
|
|
|
//
|
2022-04-26 08:46:01 +08:00
|
|
|
// Contrary to the name, this actually does a QueryEscape, rather than a PathEscape. This works
|
|
|
|
// better with most S3 providers.
|
2022-02-23 15:03:38 +08:00
|
|
|
func EscapePath(pathComps []string) string {
|
2022-04-26 08:46:01 +08:00
|
|
|
return defaultPathEscaper(pathComps)
|
2022-04-21 22:21:29 +08:00
|
|
|
}
|
|
|
|
|
2022-04-26 08:46:01 +08:00
|
|
|
func defaultPathEscaper(pathComps []string) string {
|
2022-04-22 10:23:43 +08:00
|
|
|
var ret []string
|
|
|
|
for _, comp := range pathComps {
|
2022-12-23 08:18:36 +08:00
|
|
|
esc := url.PathEscape(comp)
|
|
|
|
// S3 incorrectly escapes + in paths to spaces, so we add an extra encoding for that. This
|
|
|
|
// seems to be handled correctly regardless of whether an endpoint uses query or path
|
|
|
|
// escaping.
|
|
|
|
esc = strings.ReplaceAll(esc, "+", "%2B")
|
|
|
|
ret = append(ret, esc)
|
2022-04-21 22:21:29 +08:00
|
|
|
}
|
2022-12-25 15:20:42 +08:00
|
|
|
return strings.Join(ret, "/")
|
2020-12-21 09:24:24 +08:00
|
|
|
}
|
|
|
|
|
2022-04-22 10:23:43 +08:00
|
|
|
func trailingPath(
|
|
|
|
infoName string,
|
|
|
|
fileComps []string,
|
|
|
|
pathEscaper PathEscaper,
|
|
|
|
) string {
|
2022-04-26 08:46:01 +08:00
|
|
|
if pathEscaper == nil {
|
|
|
|
pathEscaper = defaultPathEscaper
|
|
|
|
}
|
|
|
|
return pathEscaper(append([]string{infoName}, fileComps...))
|
2022-02-23 15:03:38 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 16:25:45 +08:00
|
|
|
// Creates a request per BEP 19.
|
2022-04-21 22:21:29 +08:00
|
|
|
func newRequest(
|
|
|
|
url_ string, fileIndex int,
|
|
|
|
info *metainfo.Info,
|
|
|
|
offset, length int64,
|
2022-04-22 10:23:43 +08:00
|
|
|
pathEscaper PathEscaper,
|
2022-04-21 22:21:29 +08:00
|
|
|
) (*http.Request, error) {
|
2020-06-01 16:25:45 +08:00
|
|
|
fileInfo := info.UpvertedFiles()[fileIndex]
|
2020-10-07 07:22:55 +08:00
|
|
|
if strings.HasSuffix(url_, "/") {
|
|
|
|
// BEP specifies that we append the file path. We need to escape each component of the path
|
|
|
|
// for things like spaces and '#'.
|
2022-04-26 08:46:01 +08:00
|
|
|
url_ += trailingPath(info.Name, fileInfo.Path, pathEscaper)
|
2020-06-01 16:25:45 +08:00
|
|
|
}
|
2020-10-07 07:22:55 +08:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url_, nil)
|
2020-06-01 16:25:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if offset != 0 || length != fileInfo.Length {
|
|
|
|
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|