WebSeed PathEscaper API tweaks
This commit is contained in:
parent
a79c3bd5d3
commit
d2d8125eea
|
@ -1326,7 +1326,7 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
|
||||||
defer cl.unlock()
|
defer cl.unlock()
|
||||||
t.initialPieceCheckDisabled = spec.DisableInitialPieceCheck
|
t.initialPieceCheckDisabled = spec.DisableInitialPieceCheck
|
||||||
for _, url := range spec.Webseeds {
|
for _, url := range spec.Webseeds {
|
||||||
t.addWebSeed(url, spec.DefaultWebseedEscapePath)
|
t.addWebSeed(url)
|
||||||
}
|
}
|
||||||
for _, peerAddr := range spec.PeerAddrs {
|
for _, peerAddr := range spec.PeerAddrs {
|
||||||
t.addPeer(PeerInfo{
|
t.addPeer(PeerInfo{
|
||||||
|
|
13
spec.go
13
spec.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/anacrolix/torrent/metainfo"
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
pp "github.com/anacrolix/torrent/peer_protocol"
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
||||||
"github.com/anacrolix/torrent/storage"
|
"github.com/anacrolix/torrent/storage"
|
||||||
"github.com/anacrolix/torrent/webseed"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Specifies a new torrent for adding to a client, or additions to an existing Torrent. There are
|
// Specifies a new torrent for adding to a client, or additions to an existing Torrent. There are
|
||||||
|
@ -20,9 +19,11 @@ type TorrentSpec struct {
|
||||||
InfoBytes []byte
|
InfoBytes []byte
|
||||||
// The name to use if the Name field from the Info isn't available.
|
// The name to use if the Name field from the Info isn't available.
|
||||||
DisplayName string
|
DisplayName string
|
||||||
Webseeds []string
|
// WebSeed URLs. For additional options add the URLs separately with Torrent.AddWebSeeds
|
||||||
DhtNodes []string
|
// instead.
|
||||||
PeerAddrs []string
|
Webseeds []string
|
||||||
|
DhtNodes []string
|
||||||
|
PeerAddrs []string
|
||||||
// The combination of the "xs" and "as" fields in magnet links, for now.
|
// The combination of the "xs" and "as" fields in magnet links, for now.
|
||||||
Sources []string
|
Sources []string
|
||||||
|
|
||||||
|
@ -37,10 +38,6 @@ type TorrentSpec struct {
|
||||||
// Whether to allow data download or upload
|
// Whether to allow data download or upload
|
||||||
DisallowDataUpload bool
|
DisallowDataUpload bool
|
||||||
DisallowDataDownload bool
|
DisallowDataDownload bool
|
||||||
|
|
||||||
// Custom encoder for webseed URLs
|
|
||||||
// Leave nil to use the default (url.QueryEscape)
|
|
||||||
DefaultWebseedEscapePath webseed.PathEscaper
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
|
func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
|
||||||
|
|
38
torrent.go
38
torrent.go
|
@ -2350,30 +2350,24 @@ func (t *Torrent) callbacks() *Callbacks {
|
||||||
return &t.cl.config.Callbacks
|
return &t.cl.config.Callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddWebseedsOpt func() *AddWebseedOpts
|
type AddWebSeedsOpt func(*webseed.Client)
|
||||||
|
|
||||||
type AddWebseedOpts struct {
|
// Sets the WebSeed trailing path escaper for a webseed.Client.
|
||||||
// Custom encoder for webseed URLs
|
func WebSeedPathEscaper(custom webseed.PathEscaper) AddWebSeedsOpt {
|
||||||
// Leave nil to use the default (url.QueryEscape)
|
return func(c *webseed.Client) {
|
||||||
PathEscaper webseed.PathEscaper
|
c.PathEscaper = custom
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Add web seeds to the torrent.
|
|
||||||
// If opt is not nil, only the first one is used.
|
|
||||||
func (t *Torrent) AddWebSeeds(urls []string, opt ...AddWebseedsOpt) {
|
|
||||||
t.cl.lock()
|
|
||||||
defer t.cl.unlock()
|
|
||||||
for _, u := range urls {
|
|
||||||
if opt == nil {
|
|
||||||
t.addWebSeed(u, nil)
|
|
||||||
} else {
|
|
||||||
t.addWebSeed(u, opt[0]().PathEscaper)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) addWebSeed(url string, pathEscaper webseed.PathEscaper) {
|
func (t *Torrent) AddWebSeeds(urls []string, opts ...AddWebSeedsOpt) {
|
||||||
|
t.cl.lock()
|
||||||
|
defer t.cl.unlock()
|
||||||
|
for _, u := range urls {
|
||||||
|
t.addWebSeed(u, opts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) {
|
||||||
if t.cl.config.DisableWebseeds {
|
if t.cl.config.DisableWebseeds {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2410,11 +2404,13 @@ func (t *Torrent) addWebSeed(url string, pathEscaper webseed.PathEscaper) {
|
||||||
r: r,
|
r: r,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
PathEscaper: pathEscaper,
|
|
||||||
},
|
},
|
||||||
activeRequests: make(map[Request]webseed.Request, maxRequests),
|
activeRequests: make(map[Request]webseed.Request, maxRequests),
|
||||||
maxRequests: maxRequests,
|
maxRequests: maxRequests,
|
||||||
}
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&ws.client)
|
||||||
|
}
|
||||||
ws.peer.initUpdateRequestsTimer()
|
ws.peer.initUpdateRequestsTimer()
|
||||||
ws.requesterCond.L = t.cl.locker()
|
ws.requesterCond.L = t.cl.locker()
|
||||||
for i := 0; i < maxRequests; i += 1 {
|
for i := 0; i < maxRequests; i += 1 {
|
||||||
|
|
|
@ -41,11 +41,6 @@ func (r Request) Cancel() {
|
||||||
r.cancel()
|
r.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Spec struct {
|
|
||||||
Urls []string
|
|
||||||
EncodeUrl func(string) string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
HttpClient *http.Client
|
HttpClient *http.Client
|
||||||
Url string
|
Url string
|
||||||
|
@ -82,7 +77,7 @@ func (ws *Client) NewRequest(r RequestSpec) Request {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
var requestParts []requestPart
|
var requestParts []requestPart
|
||||||
if !ws.fileIndex.Locate(r, func(i int, e segments.Extent) bool {
|
if !ws.fileIndex.Locate(r, func(i int, e segments.Extent) bool {
|
||||||
req, err := NewRequestWithOpts(
|
req, err := newRequest(
|
||||||
ws.Url, i, ws.info, e.Start, e.Length,
|
ws.Url, i, ws.info, e.Start, e.Length,
|
||||||
ws.PathEscaper,
|
ws.PathEscaper,
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,22 +15,13 @@ type PathEscaper func(pathComps []string) string
|
||||||
// Escapes path name components suitable for appending to a webseed URL. This works for converting
|
// Escapes path name components suitable for appending to a webseed URL. This works for converting
|
||||||
// S3 object keys to URLs too.
|
// S3 object keys to URLs too.
|
||||||
//
|
//
|
||||||
// Contrary to the name, this actually does a QueryEscape, rather than a
|
// Contrary to the name, this actually does a QueryEscape, rather than a PathEscape. This works
|
||||||
// PathEscape. This works better with most S3 providers. You can use
|
// better with most S3 providers.
|
||||||
// EscapePathWithOpts for a custom encoding.
|
|
||||||
func EscapePath(pathComps []string) string {
|
func EscapePath(pathComps []string) string {
|
||||||
return escapePath(pathComps, nil)
|
return defaultPathEscaper(pathComps)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EscapePathWithCustomEscaper(pathComps []string, pathEscaper PathEscaper) string {
|
func defaultPathEscaper(pathComps []string) string {
|
||||||
return escapePath(pathComps, pathEscaper)
|
|
||||||
}
|
|
||||||
|
|
||||||
func escapePath(pathComps []string, pathEscaper PathEscaper) string {
|
|
||||||
if pathEscaper != nil {
|
|
||||||
return pathEscaper(pathComps)
|
|
||||||
}
|
|
||||||
|
|
||||||
var ret []string
|
var ret []string
|
||||||
for _, comp := range pathComps {
|
for _, comp := range pathComps {
|
||||||
ret = append(ret, url.QueryEscape(comp))
|
ret = append(ret, url.QueryEscape(comp))
|
||||||
|
@ -43,26 +34,13 @@ func trailingPath(
|
||||||
fileComps []string,
|
fileComps []string,
|
||||||
pathEscaper PathEscaper,
|
pathEscaper PathEscaper,
|
||||||
) string {
|
) string {
|
||||||
return escapePath(append([]string{infoName}, fileComps...), pathEscaper)
|
if pathEscaper == nil {
|
||||||
|
pathEscaper = defaultPathEscaper
|
||||||
|
}
|
||||||
|
return pathEscaper(append([]string{infoName}, fileComps...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a request per BEP 19.
|
// Creates a request per BEP 19.
|
||||||
func NewRequest(
|
|
||||||
url_ string,
|
|
||||||
fileIndex int, info *metainfo.Info,
|
|
||||||
offset, length int64) (*http.Request, error) {
|
|
||||||
return newRequest(url_, fileIndex, info, offset, length, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRequestWithOpts(
|
|
||||||
url_ string, fileIndex int,
|
|
||||||
info *metainfo.Info,
|
|
||||||
offset, length int64,
|
|
||||||
pathEscaper PathEscaper,
|
|
||||||
) (*http.Request, error) {
|
|
||||||
return newRequest(url_, fileIndex, info, offset, length, pathEscaper)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRequest(
|
func newRequest(
|
||||||
url_ string, fileIndex int,
|
url_ string, fileIndex int,
|
||||||
info *metainfo.Info,
|
info *metainfo.Info,
|
||||||
|
@ -73,7 +51,7 @@ func newRequest(
|
||||||
if strings.HasSuffix(url_, "/") {
|
if strings.HasSuffix(url_, "/") {
|
||||||
// BEP specifies that we append the file path. We need to escape each component of the path
|
// BEP specifies that we append the file path. We need to escape each component of the path
|
||||||
// for things like spaces and '#'.
|
// for things like spaces and '#'.
|
||||||
url_ += escapePath(append([]string{info.Name}, fileInfo.Path...), pathEscaper)
|
url_ += trailingPath(info.Name, fileInfo.Path, pathEscaper)
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(http.MethodGet, url_, nil)
|
req, err := http.NewRequest(http.MethodGet, url_, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -15,7 +15,7 @@ func TestEscapePath(t *testing.T) {
|
||||||
escaper PathEscaper,
|
escaper PathEscaper,
|
||||||
unescaper func(string) (string, error),
|
unescaper func(string) (string, error),
|
||||||
) {
|
) {
|
||||||
unescaped, err := unescaper(escapePath(parts, escaper))
|
unescaped, err := unescaper(escaper(parts))
|
||||||
if !c.Check(err, qt.IsNil) {
|
if !c.Check(err, qt.IsNil) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,13 @@ func TestEscapePath(t *testing.T) {
|
||||||
test(
|
test(
|
||||||
[]string{"a_b-c", "d + e.f"},
|
[]string{"a_b-c", "d + e.f"},
|
||||||
"a_b-c/d + e.f",
|
"a_b-c/d + e.f",
|
||||||
nil,
|
defaultPathEscaper,
|
||||||
url.QueryUnescape,
|
url.QueryUnescape,
|
||||||
)
|
)
|
||||||
test(
|
test(
|
||||||
[]string{"a_1-b_c2", "d 3. (e, f).g"},
|
[]string{"a_1-b_c2", "d 3. (e, f).g"},
|
||||||
"a_1-b_c2/d 3. (e, f).g",
|
"a_1-b_c2/d 3. (e, f).g",
|
||||||
nil,
|
defaultPathEscaper,
|
||||||
url.QueryUnescape,
|
url.QueryUnescape,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ func TestEscapePath(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEscapePathForEmptyInfoName(t *testing.T) {
|
func TestEscapePathForEmptyInfoName(t *testing.T) {
|
||||||
qt.Check(t, escapePath([]string{`ノ┬─┬ノ ︵ ( \o°o)\`}, nil), qt.Equals, "%E3%83%8E%E2%94%AC%E2%94%80%E2%94%AC%E3%83%8E+%EF%B8%B5+%28+%5Co%C2%B0o%29%5C")
|
qt.Check(t, defaultPathEscaper([]string{`ノ┬─┬ノ ︵ ( \o°o)\`}), qt.Equals, "%E3%83%8E%E2%94%AC%E2%94%80%E2%94%AC%E3%83%8E+%EF%B8%B5+%28+%5Co%C2%B0o%29%5C")
|
||||||
qt.Check(t, escapePath([]string{"hello", "world"}, nil), qt.Equals, "hello/world")
|
qt.Check(t, defaultPathEscaper([]string{"hello", "world"}), qt.Equals, "hello/world")
|
||||||
qt.Check(t, escapePath([]string{"war", "and", "peace"}, nil), qt.Equals, "war/and/peace")
|
qt.Check(t, defaultPathEscaper([]string{"war", "and", "peace"}), qt.Equals, "war/and/peace")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue