From 0d89e73c9f2ff6a42884464257c8de4b76f89f1d Mon Sep 17 00:00:00 2001 From: Dave Freilich Date: Sun, 15 Dec 2024 09:24:59 +0200 Subject: [PATCH 1/5] fix: set request in hook context before send request, in case of error --- v2/pkg/engine/datasource/httpclient/nethttpclient.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index 64761d728..b6e6d06e4 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -79,6 +79,12 @@ 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 @@ -189,6 +195,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 From f4504e9001b0b50c80a726fa9ac98ef306045a8a Mon Sep 17 00:00:00 2001 From: Dave Freilich Date: Sun, 15 Dec 2024 09:34:52 +0200 Subject: [PATCH 2/5] fix: change loader process --- .../engine/datasource/httpclient/nethttpclient.go | 6 ++++-- v2/pkg/engine/resolve/loader.go | 13 ++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index b6e6d06e4..074536bfe 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -198,13 +198,15 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head setRequest(ctx, request) response, err := client.Do(request) + + // Set response, even before error check, to ensure that it's in the context + setResponseStatus(ctx, request, response) + if err != nil { return err } defer response.Body.Close() - setResponseStatus(ctx, request, response) - respReader, err := respBodyReader(response) 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 } From 04824691c0473aaee79c20f1af8df0a5f127439c Mon Sep 17 00:00:00 2001 From: Dave Freilich Date: Sun, 15 Dec 2024 15:46:25 +0200 Subject: [PATCH 3/5] fix null pointer issue --- v2/pkg/engine/datasource/httpclient/nethttpclient.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index 074536bfe..d0aa73f59 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -87,7 +87,11 @@ func setRequest(ctx context.Context, request *http.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 } From 4b857df1bd23c8b2d09662c1771910dcf738abff Mon Sep 17 00:00:00 2001 From: Dave Freilich Date: Sun, 15 Dec 2024 17:38:26 +0200 Subject: [PATCH 4/5] move responseStatus back --- v2/pkg/engine/datasource/httpclient/nethttpclient.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index d0aa73f59..518643c8a 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -203,15 +203,15 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head response, err := client.Do(request) - // Set response, even before error check, to ensure that it's in the context - setResponseStatus(ctx, request, response) - if err != nil { return err } defer response.Body.Close() + setResponseStatus(ctx, request, response) + respReader, err := respBodyReader(response) + if err != nil { return err } From 1ca3e12ddef61f871fd890b42c1db8fefd10009d Mon Sep 17 00:00:00 2001 From: Dave Freilich Date: Sun, 15 Dec 2024 17:38:59 +0200 Subject: [PATCH 5/5] remove spaces --- v2/pkg/engine/datasource/httpclient/nethttpclient.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/v2/pkg/engine/datasource/httpclient/nethttpclient.go b/v2/pkg/engine/datasource/httpclient/nethttpclient.go index 518643c8a..51ff7bd23 100644 --- a/v2/pkg/engine/datasource/httpclient/nethttpclient.go +++ b/v2/pkg/engine/datasource/httpclient/nethttpclient.go @@ -202,7 +202,6 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head setRequest(ctx, request) response, err := client.Do(request) - if err != nil { return err } @@ -211,7 +210,6 @@ func makeHTTPRequest(client *http.Client, ctx context.Context, url, method, head setResponseStatus(ctx, request, response) respReader, err := respBodyReader(response) - if err != nil { return err }