forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
36 lines (33 loc) · 919 Bytes
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package webseed
import (
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/anacrolix/torrent/metainfo"
)
// Creates a request per BEP 19.
func NewRequest(url_ string, fileIndex int, info *metainfo.Info, offset, length int64) (*http.Request, error) {
fileInfo := info.UpvertedFiles()[fileIndex]
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 '#'.
url_ += path.Join(
func() (ret []string) {
for _, comp := range append([]string{info.Name}, fileInfo.Path...) {
ret = append(ret, url.PathEscape(comp))
}
return
}()...,
)
}
req, err := http.NewRequest(http.MethodGet, url_, nil)
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
}