10 "github.com/anacrolix/torrent/metainfo"
13 type PathEscaper func(pathComps []string) string
15 // Escapes path name components suitable for appending to a webseed URL. This works for converting
16 // S3 object keys to URLs too.
18 // Contrary to the name, this actually does a QueryEscape, rather than a PathEscape. This works
19 // better with most S3 providers.
20 func EscapePath(pathComps []string) string {
21 return defaultPathEscaper(pathComps)
24 func defaultPathEscaper(pathComps []string) string {
26 for _, comp := range pathComps {
27 ret = append(ret, url.QueryEscape(comp))
29 return path.Join(ret...)
35 pathEscaper PathEscaper,
37 if pathEscaper == nil {
38 pathEscaper = defaultPathEscaper
40 return pathEscaper(append([]string{infoName}, fileComps...))
43 // Creates a request per BEP 19.
45 url_ string, fileIndex int,
48 pathEscaper PathEscaper,
49 ) (*http.Request, error) {
50 fileInfo := info.UpvertedFiles()[fileIndex]
51 if strings.HasSuffix(url_, "/") {
52 // BEP specifies that we append the file path. We need to escape each component of the path
53 // for things like spaces and '#'.
54 url_ += trailingPath(info.Name, fileInfo.Path, pathEscaper)
56 req, err := http.NewRequest(http.MethodGet, url_, nil)
60 if offset != 0 || length != fileInfo.Length {
61 req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))