diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-04 15:31:46 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-05-04 15:31:46 +0200 |
commit | e2322c799ae0a05ed2b7eb63bffebbe0b54b292f (patch) | |
tree | 9f72634c0f1525a514caa7d51e33c4dc8b5a137f /internal/feed | |
parent | 44f53f5956216cdd52ad58d6e666013a8f202967 (diff) | |
download | feed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.tar.gz feed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.tar.bz2 feed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.zip |
Unify connection handling
Diffstat (limited to 'internal/feed')
-rw-r--r-- | internal/feed/mail.go | 12 | ||||
-rw-r--r-- | internal/feed/parse.go | 47 |
2 files changed, 16 insertions, 43 deletions
diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 3cb442e..0360e10 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -7,7 +7,6 @@ import ( "io" "io/ioutil" "mime" - "net/http" "net/url" "path" "strings" @@ -19,6 +18,7 @@ import ( "github.com/gabriel-vasile/mimetype" "github.com/Necoro/feed2imap-go/internal/feed/template" + "github.com/Necoro/feed2imap-go/internal/http" "github.com/Necoro/feed2imap-go/internal/msg" "github.com/Necoro/feed2imap-go/pkg/config" "github.com/Necoro/feed2imap-go/pkg/log" @@ -193,12 +193,12 @@ func (feed *Feed) Messages() (msg.Messages, error) { return mails, nil } -func getImage(src string, client *http.Client) ([]byte, string, error) { - resp, err := client.Get(src) +func getImage(src string, timeout int, disableTLS bool) ([]byte, string, error) { + resp, cancel, err := http.Get(src, timeout, disableTLS) if err != nil { return nil, "", fmt.Errorf("fetching from '%s': %w", src, err) } - defer resp.Body.Close() + defer cancel() img, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -270,7 +270,7 @@ func (item *item) buildBody() { return } - srcUrl,err := url.Parse(src) + srcUrl, err := url.Parse(src) if err != nil { log.Errorf("Feed %s: Item %s: Error parsing URL '%s' embedded in item: %s", feed.Name, item.Item.Link, src, err) @@ -278,7 +278,7 @@ func (item *item) buildBody() { } imgUrl := feedUrl.ResolveReference(srcUrl) - img, mime, err := getImage(imgUrl.String(), httpClient(feed.NoTLS)) + img, mime, err := getImage(imgUrl.String(), feed.Global.Timeout, feed.NoTLS) if err != nil { log.Errorf("Feed %s: Item %s: Error fetching image: %s", feed.Name, item.Item.Link, err) 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)) |