mirror of
https://github.com/versia-pub/versia-go.git
synced 2025-12-06 06:28:18 +01:00
fix(protoretry): ignore tls record headers and downgrade to http
This commit is contained in:
parent
0f44cfe95e
commit
0e25d7af91
|
|
@ -3,6 +3,7 @@ package protoretry
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
@ -18,14 +19,39 @@ func New(base *http.Client) *Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||||
if res, err := c.base.Do(req); err == nil {
|
l := log.With().Str("url", req.URL.String()).Logger()
|
||||||
return res, nil
|
|
||||||
} else if !errors.Is(err, syscall.ECONNREFUSED) {
|
l.Debug().Msg("fetch")
|
||||||
return nil, err
|
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"
|
req.URL.Scheme = "http"
|
||||||
return c.base.Do(req)
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Debug().Msg("fetched")
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GET(ctx context.Context, u *url.URL) ([]byte, *http.Response, error) {
|
func (c *Client) GET(ctx context.Context, u *url.URL) ([]byte, *http.Response, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue