X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=sources.go;h=ed5ecbfa6c0b492884aaa60e55b222ce9d5ecf7d;hb=HEAD;hp=46c6abe9f9d8b3e7c745dd7de73112fdee39dc28;hpb=6f9390a1256dd2f0d5e98fcdb70b23dd50f2ce02;p=btrtrc.git diff --git a/sources.go b/sources.go index 46c6abe9..ed5ecbfa 100644 --- a/sources.go +++ b/sources.go @@ -2,14 +2,19 @@ package torrent import ( "context" + "errors" + "fmt" "net/http" "github.com/anacrolix/log" + "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/metainfo" ) -func (t *Torrent) useSources(sources []string) { +// Add HTTP endpoints that serve the metainfo. They will be used if the torrent info isn't obtained +// yet. The Client HTTP client is used. +func (t *Torrent) UseSources(sources []string) { select { case <-t.Closed(): return @@ -30,7 +35,7 @@ func (t *Torrent) useSources(sources []string) { panic(s) } level := log.Debug - if err != nil { + if err != nil && !errors.Is(err, context.Canceled) { level = log.Warning } t.logger.Levelf(level, "used torrent source %q [err=%v]", s, err) @@ -66,6 +71,10 @@ func getTorrentSource(ctx context.Context, source string, hc *http.Client) (mi m return } defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + err = fmt.Errorf("unexpected response status code: %v", resp.StatusCode) + return + } err = bencode.NewDecoder(resp.Body).Decode(&mi) return }