diff --git a/internal/getproviders/registry_client.go b/internal/getproviders/registry_client.go index 428d362e2c55..5da2a83ca2b2 100644 --- a/internal/getproviders/registry_client.go +++ b/internal/getproviders/registry_client.go @@ -437,7 +437,7 @@ func (c *registryClient) getFile(url *url.URL) ([]byte, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("%s returned from %s", resp.Status, resp.Request.Host) + return nil, fmt.Errorf("%s returned from %s", resp.Status, HostFromRequest(resp.Request)) } data, err := ioutil.ReadAll(resp.Body) @@ -478,7 +478,7 @@ func maxRetryErrorHandler(resp *http.Response, err error, numTries int) (*http.R // both response and error. var errMsg string if resp != nil { - errMsg = fmt.Sprintf(": %s returned from %s", resp.Status, resp.Request.Host) + errMsg = fmt.Sprintf(": %s returned from %s", resp.Status, HostFromRequest(resp.Request)) } else if err != nil { errMsg = fmt.Sprintf(": %s", err) } @@ -492,6 +492,22 @@ func maxRetryErrorHandler(resp *http.Response, err error, numTries int) (*http.R return resp, fmt.Errorf("the request failed, please try again later%s", errMsg) } +// HostFromRequest extracts host the same way net/http Request.Write would, +// accounting for empty Request.Host +func HostFromRequest(req *http.Request) string { + if req.Host != "" { + return req.Host + } + if req.URL != nil { + return req.URL.Host + } + + // this should never happen and if it does + // it will be handled as part of Request.Write() + // https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/net/http/request.go;l=574 + return "" +} + // configureRequestTimeout configures the registry client request timeout from // environment variables func configureRequestTimeout() {