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
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
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-21 22:21:29 +08:00
|
|
|
// Contrary to the name, this actually does a QueryEscape, rather than a
|
|
|
|
// PathEscape. This works better with most S3 providers. You can use
|
|
|
|
// EscapePathWithCustomEncoding for a custom encoding.
|
2022-02-23 15:03:38 +08:00
|
|
|
func EscapePath(pathComps []string) string {
|
2022-04-21 22:21:29 +08:00
|
|
|
return escapePath(pathComps, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapePath(pathComps []string, encodeUrl func(string) string) string {
|
|
|
|
if encodeUrl == nil {
|
|
|
|
encodeUrl = url.QueryEscape
|
|
|
|
}
|
2020-12-21 09:24:24 +08:00
|
|
|
return path.Join(
|
|
|
|
func() (ret []string) {
|
2022-02-23 15:03:38 +08:00
|
|
|
for _, comp := range pathComps {
|
2022-04-21 22:21:29 +08:00
|
|
|
ret = append(ret, encodeUrl(comp))
|
2020-12-21 09:24:24 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}()...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:21:29 +08:00
|
|
|
func trailingPath(infoName string, fileComps []string, encodeUrl func(string) string) string {
|
|
|
|
return escapePath(append([]string{infoName}, fileComps...), encodeUrl)
|
2022-02-23 15:03:38 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 16:25:45 +08:00
|
|
|
// Creates a request per BEP 19.
|
2020-10-07 07:22:55 +08:00
|
|
|
func NewRequest(url_ string, fileIndex int, info *metainfo.Info, offset, length int64) (*http.Request, error) {
|
2022-04-21 22:21:29 +08:00
|
|
|
return newRequest(url_, fileIndex, info, offset, length, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequestWithCustomUrlEncoding(
|
|
|
|
url_ string, fileIndex int,
|
|
|
|
info *metainfo.Info,
|
|
|
|
offset, length int64,
|
|
|
|
encodeUrl func(string) string,
|
|
|
|
) (*http.Request, error) {
|
|
|
|
return newRequest(url_, fileIndex, info, offset, length, encodeUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequest(
|
|
|
|
url_ string, fileIndex int,
|
|
|
|
info *metainfo.Info,
|
|
|
|
offset, length int64,
|
|
|
|
encodeUrl func(string) string,
|
|
|
|
) (*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-21 22:21:29 +08:00
|
|
|
url_ += trailingPath(info.Name, fileInfo.Path, encodeUrl)
|
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
|
|
|
|
}
|