10 "github.com/anacrolix/torrent/metainfo"
13 // Escapes path name components suitable for appending to a webseed URL. This works for converting
14 // S3 object keys to URLs too.
15 func EscapePath(pathComps []string) string {
17 func() (ret []string) {
18 for _, comp := range pathComps {
19 ret = append(ret, url.QueryEscape(comp))
26 func trailingPath(infoName string, fileComps []string) string {
27 return EscapePath(append([]string{infoName}, fileComps...))
30 // Creates a request per BEP 19.
31 func NewRequest(url_ string, fileIndex int, info *metainfo.Info, offset, length int64) (*http.Request, error) {
32 fileInfo := info.UpvertedFiles()[fileIndex]
33 if strings.HasSuffix(url_, "/") {
34 // BEP specifies that we append the file path. We need to escape each component of the path
35 // for things like spaces and '#'.
36 url_ += trailingPath(info.Name, fileInfo.Path)
38 req, err := http.NewRequest(http.MethodGet, url_, nil)
42 if offset != 0 || length != fileInfo.Length {
43 req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))