From e2322c799ae0a05ed2b7eb63bffebbe0b54b292f Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 4 May 2020 15:31:46 +0200 Subject: Unify connection handling --- internal/feed/parse.go | 47 ++++++++++------------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) (limited to 'internal/feed/parse.go') diff --git a/internal/feed/parse.go b/internal/feed/parse.go index 1ba90fd..a8f705a 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -1,57 +1,30 @@ package feed import ( - ctxt "context" - "crypto/tls" "fmt" - "net/http" - "time" "github.com/google/uuid" "github.com/mmcdole/gofeed" + "github.com/Necoro/feed2imap-go/internal/http" "github.com/Necoro/feed2imap-go/pkg/log" ) -// share HTTP clients -var ( - stdHTTPClient *http.Client - unsafeHTTPClient *http.Client -) - -func init() { - // std - stdHTTPClient = &http.Client{Transport: http.DefaultTransport} - - // unsafe - tlsConfig := &tls.Config{InsecureSkipVerify: true} - transport := http.DefaultTransport.(*http.Transport).Clone() - transport.TLSClientConfig = tlsConfig - unsafeHTTPClient = &http.Client{Transport: transport} -} - -func context(timeout int) (ctxt.Context, ctxt.CancelFunc) { - return ctxt.WithTimeout(ctxt.Background(), time.Duration(timeout)*time.Second) -} - -func httpClient(disableTLS bool) *http.Client { - if disableTLS { - return unsafeHTTPClient - } - return stdHTTPClient -} - func (feed *Feed) parse() error { - ctx, cancel := context(feed.Global.Timeout) - defer cancel() - fp := gofeed.NewParser() - fp.Client = httpClient(feed.NoTLS) - parsedFeed, err := fp.ParseURLWithContext(feed.Url, ctx) + // we do not use the http support in gofeed, so that we can control the behavior of http requests + // and ensure it to be the same in all places + resp, cancel, err := http.Get(feed.Url, feed.Global.Timeout, feed.NoTLS) if err != nil { return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err) } + defer cancel() // includes resp.Body.Close + + parsedFeed, err := fp.Parse(resp.Body) + if err != nil { + return fmt.Errorf("parsing feed '%s': %w", feed.Name, err) + } feed.feed = parsedFeed feed.items = make([]item, len(parsedFeed.Items)) -- cgit v1.2.3-70-g09d2