diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index 64761d728..51ff7bd23 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -79,9 +79,19 @@ func InjectResponseContext(ctx context.Context) (context.Context, *ResponseConte return context.WithValue(ctx, responseContextKey{}, value), value } +func setRequest(ctx context.Context, request *http.Request) { + if value, ok := ctx.Value(responseContextKey{}).(*ResponseContext); ok { + value.Request = request + } +} + func setResponseStatus(ctx context.Context, request *http.Request, response *http.Response) { if value, ok := ctx.Value(responseContextKey{}).(*ResponseContext); ok { - value.StatusCode = response.StatusCode + if response != nil { + value.StatusCode = response.StatusCode + } else { + value.StatusCode = 0 + } value.Request = request value.Response = response } @@ -189,6 +199,8 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head request.Header.Set(AcceptEncodingHeader, EncodingGzip) request.Header.Add(AcceptEncodingHeader, EncodingDeflate) + setRequest(ctx, request) + response, err := client.Do(request) if err != nil { return err diff --git a/v2/pkg/engine/resolve/loader.go b/v2/pkg/engine/resolve/loader.go index f2545db97..9ca6f332e 100644 --- a/v2/pkg/engine/resolve/loader.go +++ b/v2/pkg/engine/resolve/loader.go @@ -62,9 +62,16 @@ func newResponseInfo(res *result, subgraphError error) *ResponseInfo { // We're using the response.Request here, because the body will be nil (since the response was read) and won't // cause a memory leak. if res.httpResponseContext.Response != nil { - responseInfo.Request = res.httpResponseContext.Response.Request - responseInfo.ResponseHeaders = res.httpResponseContext.Response.Header.Clone() - } else { + if res.httpResponseContext.Response.Request != nil { + responseInfo.Request = res.httpResponseContext.Response.Request + } + + if res.httpResponseContext.Response.Header != nil { + responseInfo.ResponseHeaders = res.httpResponseContext.Response.Header.Clone() + } + } + + if responseInfo.Request == nil { // In cases where the request errors, the response will be nil, and so we need to get the original request responseInfo.Request = res.httpResponseContext.Request }