webseed/client.go | 2 +- webseed/misc.go | 18 ++++++++++++++---- diff --git a/webseed/client.go b/webseed/client.go index 0b98b931d647abfffd21e1c49b6d7043589ac2b1..19153dc773c7331584aca3f0e1e16143b6e2cd45 100644 --- a/webseed/client.go +++ b/webseed/client.go @@ -109,7 +109,7 @@ var buf bytes.Buffer for _, part := range parts { err := recvPartResult(&buf, part) if err != nil { - return buf.Bytes(), err + return buf.Bytes(), fmt.Errorf("reading %q at %q: %w", part.req.URL, part.req.Header.Get("Range"), err) } } return buf.Bytes(), nil diff --git a/webseed/misc.go b/webseed/misc.go index 140bc23b42a148fc55a6761c317e2025d2b7bef7..466c664add2499f87994c114f6c5af41a2c63b37 100644 --- a/webseed/misc.go +++ b/webseed/misc.go @@ -3,6 +3,7 @@ import ( "fmt" "net/http" + "net/url" "path" "strings" @@ -10,12 +11,21 @@ "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) { +func NewRequest(url_ string, fileIndex int, info *metainfo.Info, offset, length int64) (*http.Request, error) { fileInfo := info.UpvertedFiles()[fileIndex] - if strings.HasSuffix(url, "/") { - url += path.Join(append([]string{info.Name}, fileInfo.Path...)...) + 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) + req, err := http.NewRequest(http.MethodGet, url_, nil) if err != nil { return nil, err }