]> Sergey Matveev's repositories - btrtrc.git/blobdiff - sources.go
Drop support for go 1.20
[btrtrc.git] / sources.go
index b2d5b940106d4575e65959ed3994d0bc34a48b24..ed5ecbfa6c0b492884aaa60e55b222ce9d5ecf7d 100644 (file)
@@ -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,10 +35,10 @@ 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=%q]", s, err)
+                       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
 }