Skip to content

Commit 794b0c7

Browse files
committed
Add deprecated HTTPFallback for package compatibility
Signed-off-by: Derek McGowan <[email protected]>
1 parent 51c649d commit 794b0c7

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

remotes/docker/resolver.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,3 +763,37 @@ func isTLSError(err error) bool {
763763

764764
return false
765765
}
766+
767+
// HTTPFallback is an http.RoundTripper which allows fallback from https to http
768+
// for registry endpoints with configurations for both http and TLS, such as
769+
// defaulted localhost endpoints.
770+
//
771+
// Deprecated: Use NewHTTPFallback instead.
772+
type HTTPFallback struct {
773+
http.RoundTripper
774+
}
775+
776+
func (f HTTPFallback) RoundTrip(r *http.Request) (*http.Response, error) {
777+
resp, err := f.RoundTripper.RoundTrip(r)
778+
var tlsErr tls.RecordHeaderError
779+
if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
780+
// server gave HTTP response to HTTPS client
781+
plainHTTPUrl := *r.URL
782+
plainHTTPUrl.Scheme = "http"
783+
784+
plainHTTPRequest := *r
785+
plainHTTPRequest.URL = &plainHTTPUrl
786+
787+
if r.Body != nil && r.GetBody != nil {
788+
body, err := r.GetBody()
789+
if err != nil {
790+
return nil, err
791+
}
792+
plainHTTPRequest.Body = body
793+
}
794+
795+
return f.RoundTripper.RoundTrip(&plainHTTPRequest)
796+
}
797+
798+
return resp, err
799+
}

0 commit comments

Comments
 (0)