Skip to content

Commit

Permalink
Add deprecated HTTPFallback for package compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Derek McGowan <derek@mcg.dev>
  • Loading branch information
dmcgowan committed Apr 23, 2024
1 parent 51c649d commit 794b0c7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions remotes/docker/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,37 @@ func isTLSError(err error) bool {

return false
}

// HTTPFallback is an http.RoundTripper which allows fallback from https to http
// for registry endpoints with configurations for both http and TLS, such as
// defaulted localhost endpoints.
//
// Deprecated: Use NewHTTPFallback instead.
type HTTPFallback struct {
http.RoundTripper
}

func (f HTTPFallback) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := f.RoundTripper.RoundTrip(r)
var tlsErr tls.RecordHeaderError
if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
// server gave HTTP response to HTTPS client
plainHTTPUrl := *r.URL
plainHTTPUrl.Scheme = "http"

plainHTTPRequest := *r
plainHTTPRequest.URL = &plainHTTPUrl

if r.Body != nil && r.GetBody != nil {
body, err := r.GetBody()
if err != nil {
return nil, err
}
plainHTTPRequest.Body = body
}

return f.RoundTripper.RoundTrip(&plainHTTPRequest)
}

return resp, err
}

0 comments on commit 794b0c7

Please sign in to comment.