10 "github.com/anacrolix/torrent/metainfo"
13 func trailingPath(infoName string, pathComps []string) string {
15 func() (ret []string) {
16 for _, comp := range append([]string{infoName}, pathComps...) {
17 ret = append(ret, url.QueryEscape(comp))
24 // Creates a request per BEP 19.
25 func NewRequest(url_ string, fileIndex int, info *metainfo.Info, offset, length int64) (*http.Request, error) {
26 fileInfo := info.UpvertedFiles()[fileIndex]
27 if strings.HasSuffix(url_, "/") {
28 // BEP specifies that we append the file path. We need to escape each component of the path
29 // for things like spaces and '#'.
30 url_ += trailingPath(info.Name, fileInfo.Path)
32 req, err := http.NewRequest(http.MethodGet, url_, nil)
36 if offset != 0 || length != fileInfo.Length {
37 req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))