]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Further fixes to webseed path encoding v1.19.2
authorMatt Joiner <anacrolix@gmail.com>
Mon, 21 Dec 2020 01:24:24 +0000 (12:24 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 21 Dec 2020 01:24:24 +0000 (12:24 +1100)
webseed/request.go [moved from webseed/misc.go with 70% similarity]
webseed/request_test.go [new file with mode: 0644]

similarity index 70%
rename from webseed/misc.go
rename to webseed/request.go
index 466c664add2499f87994c114f6c5af41a2c63b37..6d62ccb61355dc83bd05008ad231a31c472ff97f 100644 (file)
@@ -10,20 +10,24 @@ import (
        "github.com/anacrolix/torrent/metainfo"
 )
 
+func trailingPath(infoName string, pathComps []string) string {
+       return path.Join(
+               func() (ret []string) {
+                       for _, comp := range append([]string{infoName}, pathComps...) {
+                               ret = append(ret, url.QueryEscape(comp))
+                       }
+                       return
+               }()...,
+       )
+}
+
 // 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
-                       }()...,
-               )
+               url_ += trailingPath(info.Name, fileInfo.Path)
        }
        req, err := http.NewRequest(http.MethodGet, url_, nil)
        if err != nil {
diff --git a/webseed/request_test.go b/webseed/request_test.go
new file mode 100644 (file)
index 0000000..b82a005
--- /dev/null
@@ -0,0 +1,23 @@
+package webseed
+
+import (
+       "net/url"
+       "testing"
+
+       qt "github.com/frankban/quicktest"
+)
+
+func TestTrailingPath(t *testing.T) {
+       c := qt.New(t)
+       test := func(parts []string, result string) {
+               unescaped, err := url.QueryUnescape(trailingPath(parts[0], parts[1:]))
+               if !c.Check(err, qt.IsNil) {
+                       return
+               }
+               c.Check(unescaped, qt.Equals, result)
+       }
+       test([]string{"a_b-c", "d + e.f"}, "a_b-c/d + e.f")
+       test([]string{"a_1-b_c2", "d 3. (e, f).g"},
+               "a_1-b_c2/d 3. (e, f).g",
+       )
+}