File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments