aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/feed/feed.go8
-rw-r--r--internal/feed/mail.go6
-rw-r--r--internal/feed/parse.go4
-rw-r--r--internal/http/client.go17
4 files changed, 24 insertions, 11 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go
index 0741e8c..ffe831f 100644
--- a/internal/feed/feed.go
+++ b/internal/feed/feed.go
@@ -9,6 +9,7 @@ import (
"github.com/Necoro/gofeed"
"github.com/Necoro/feed2imap-go/internal/feed/filter"
+ "github.com/Necoro/feed2imap-go/internal/http"
"github.com/Necoro/feed2imap-go/pkg/config"
"github.com/Necoro/feed2imap-go/pkg/log"
)
@@ -33,6 +34,13 @@ type Descriptor struct {
Url string
}
+func (feed *Feed) Context() http.Context {
+ return http.Context{
+ Timeout: feed.Global.Timeout,
+ DisableTLS: feed.NoTLS,
+ }
+}
+
func (feed *Feed) Descriptor() Descriptor {
var url string
if feed.Url != "" {
diff --git a/internal/feed/mail.go b/internal/feed/mail.go
index 6914b76..444f76a 100644
--- a/internal/feed/mail.go
+++ b/internal/feed/mail.go
@@ -226,8 +226,8 @@ func (feed *Feed) Messages() (msg.Messages, error) {
return mails, nil
}
-func getImage(src string, timeout int, disableTLS bool) ([]byte, string, error) {
- resp, cancel, err := http.Get(src, timeout, disableTLS)
+func getImage(src string, ctx http.Context) ([]byte, string, error) {
+ resp, cancel, err := http.Get(src, ctx)
if err != nil {
return nil, "", fmt.Errorf("fetching from '%s': %w", src, err)
}
@@ -294,7 +294,7 @@ func (item *Item) downloadImage(src string) string {
imgUrl := item.resolveUrl(src)
- img, mime, err := getImage(imgUrl, feed.Global.Timeout, feed.NoTLS)
+ img, mime, err := getImage(imgUrl, feed.Context())
if err != nil {
log.Errorf("Feed %s: Item %s: Error fetching image: %s",
feed.Name, item.Link, err)
diff --git a/internal/feed/parse.go b/internal/feed/parse.go
index 520def8..a471b2d 100644
--- a/internal/feed/parse.go
+++ b/internal/feed/parse.go
@@ -20,7 +20,7 @@ func (feed *Feed) Parse() error {
if feed.Url != "" {
// 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)
+ resp, cancel, err := http.Get(feed.Url, feed.Context())
if err != nil {
return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
}
@@ -30,7 +30,7 @@ func (feed *Feed) Parse() error {
cleanup = func() error { return nil }
} else { // exec
// we use the same context as for HTTP
- ctx, cancel := http.Context(feed.Global.Timeout)
+ ctx, cancel := feed.Context().StdContext()
cmd := exec.CommandContext(ctx, feed.Exec[0], feed.Exec[1:]...)
defer func() {
cancel()
diff --git a/internal/http/client.go b/internal/http/client.go
index 230c333..17eb0cc 100644
--- a/internal/http/client.go
+++ b/internal/http/client.go
@@ -20,6 +20,11 @@ type Error struct {
Status string
}
+type Context struct {
+ Timeout int
+ DisableTLS bool
+}
+
func (err Error) Error() string {
return fmt.Sprintf("http error: %s", err.Status)
}
@@ -35,8 +40,8 @@ func init() {
unsafeClient = &http.Client{Transport: transport}
}
-func Context(timeout int) (ctxt.Context, ctxt.CancelFunc) {
- return ctxt.WithTimeout(ctxt.Background(), time.Duration(timeout)*time.Second)
+func (ctx Context) StdContext() (ctxt.Context, ctxt.CancelFunc) {
+ return ctxt.WithTimeout(ctxt.Background(), time.Duration(ctx.Timeout)*time.Second)
}
func client(disableTLS bool) *http.Client {
@@ -48,9 +53,9 @@ func client(disableTLS bool) *http.Client {
var noop ctxt.CancelFunc = func() {}
-func Get(url string, timeout int, disableTLS bool) (resp *http.Response, cancel ctxt.CancelFunc, err error) {
+func Get(url string, ctx Context) (resp *http.Response, cancel ctxt.CancelFunc, err error) {
prematureExit := true
- ctx, ctxCancel := Context(timeout)
+ stdCtx, ctxCancel := ctx.StdContext()
cancel = func() {
if resp != nil {
@@ -65,13 +70,13 @@ func Get(url string, timeout int, disableTLS bool) (resp *http.Response, cancel
}
}()
- req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
+ req, err := http.NewRequestWithContext(stdCtx, "GET", url, nil)
if err != nil {
return nil, noop, err
}
req.Header.Set("User-Agent", "Feed2Imap-Go/1.0")
- resp, err = client(disableTLS).Do(req)
+ resp, err = client(ctx.DisableTLS).Do(req)
if err != nil {
return nil, noop, err
}