Fix webseed requests for non-trivial path components

This commit is contained in:
Matt Joiner 2020-10-07 10:22:55 +11:00
parent 014cb3986b
commit d3daaaf75a
2 changed files with 15 additions and 5 deletions

View 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

View 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
}