From 0a6daec3d3c5f85cd8d74f746dbd46411ede233f Mon Sep 17 00:00:00 2001 From: DevMiner Date: Wed, 28 Aug 2024 01:29:56 +0200 Subject: [PATCH] fix(protoretry): ignore tls record headers and downgrade to http --- pkg/protoretry/client.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/protoretry/client.go b/pkg/protoretry/client.go index 43fbb61..80be70c 100644 --- a/pkg/protoretry/client.go +++ b/pkg/protoretry/client.go @@ -3,6 +3,7 @@ package protoretry import ( "context" "errors" + "github.com/rs/zerolog/log" "io" "net/http" "net/url" @@ -18,14 +19,39 @@ func New(base *http.Client) *Client { } func (c *Client) Do(req *http.Request) (*http.Response, error) { - if res, err := c.base.Do(req); err == nil { - return res, nil - } else if !errors.Is(err, syscall.ECONNREFUSED) { + l := log.With().Str("url", req.URL.String()).Logger() + + l.Debug().Msg("fetch") + res, err := c.base.Do(req) + if err != nil { + var urlErr *url.Error + + if errors.Is(err, syscall.ECONNREFUSED) { + goto onTlsError + } + + if errors.As(err, &urlErr) && errors.Is(urlErr.Err, http.ErrSchemeMismatch) { + goto onTlsError + } + + l.Error().Err(err).Msg("failed") return nil, err + + onTlsError: + l.Debug().Msg("downgrading to http") + req.URL.Scheme = "http" + + l.Debug().Msg("fetch") + if res, err := c.base.Do(req); err == nil { + return res, nil + } else { + l.Error().Err(err).Msg("failed") + return nil, err + } } - req.URL.Scheme = "http" - return c.base.Do(req) + l.Debug().Msg("fetched") + return res, nil } func (c *Client) GET(ctx context.Context, u *url.URL) ([]byte, *http.Response, error) {