]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix webseed requests for non-trivial path components v1.17.1
authorMatt Joiner <anacrolix@gmail.com>
Tue, 6 Oct 2020 23:22:55 +0000 (10:22 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 6 Oct 2020 23:22:55 +0000 (10:22 +1100)
webseed/client.go
webseed/misc.go

index 0b98b931d647abfffd21e1c49b6d7043589ac2b1..19153dc773c7331584aca3f0e1e16143b6e2cd45 100644 (file)
@@ -109,7 +109,7 @@ func readRequestPartResponses(parts []requestPart) ([]byte, error) {
        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
index 140bc23b42a148fc55a6761c317e2025d2b7bef7..466c664add2499f87994c114f6c5af41a2c63b37 100644 (file)
@@ -3,6 +3,7 @@ package webseed
 import (
        "fmt"
        "net/http"
+       "net/url"
        "path"
        "strings"
 
@@ -10,12 +11,21 @@ import (
 )
 
 // 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
        }