mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
getproviders: account for occasionally missing Host header in errors (#31542)
This commit is contained in:
parent
c62e20c78f
commit
b82eee6c9f
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user