aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/parse.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-04 15:31:46 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-04 15:31:46 +0200
commite2322c799ae0a05ed2b7eb63bffebbe0b54b292f (patch)
tree9f72634c0f1525a514caa7d51e33c4dc8b5a137f /internal/feed/parse.go
parent44f53f5956216cdd52ad58d6e666013a8f202967 (diff)
downloadfeed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.tar.gz
feed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.tar.bz2
feed2imap-go-e2322c799ae0a05ed2b7eb63bffebbe0b54b292f.zip
Unify connection handling
Diffstat (limited to 'internal/feed/parse.go')
-rw-r--r--internal/feed/parse.go47
1 files changed, 10 insertions, 37 deletions
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))