From 22629fb969704a0b34a60e349fc51cdf958b3ef1 Mon Sep 17 00:00:00 2001 From: Edward McFarlane Date: Thu, 19 Oct 2023 14:54:20 -0400 Subject: [PATCH] Factor out makeRequest --- http_call.go | 68 ++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/http_call.go b/http_call.go index da586147..7cd30a51 100644 --- a/http_call.go +++ b/http_call.go @@ -40,27 +40,7 @@ func newUnaryHTTPCall( url *url.URL, header http.Header, ) *unaryHTTPCall { - // ensure we make a copy of the url before we pass along to the - // Request. This ensures if a transport out of our control wants - // to mutate the req.URL, we don't feel the effects of it. - url = cloneURL(url) - - // This is mirroring what http.NewRequestContext did, but - // using an already parsed url.URL object, rather than a string - // and parsing it again. This is a bit funny with HTTP/1.1 - // explicitly, but this is logic copied over from - // NewRequestContext and doesn't effect the actual version - // being transmitted. - request := (&http.Request{ - Method: http.MethodPost, - URL: url, - Header: header, - Proto: "HTTP/1.1", - ProtoMajor: 1, - ProtoMinor: 1, - Body: nil, // Set in Do. - Host: url.Host, - }).WithContext(ctx) + request := makeRequest(ctx, url, header, nil) return &unaryHTTPCall{ httpClient: httpClient, request: request, @@ -155,28 +135,8 @@ func newDuplexHTTPCall( spec Spec, header http.Header, ) *duplexHTTPCall { - // ensure we make a copy of the url before we pass along to the - // Request. This ensures if a transport out of our control wants - // to mutate the req.URL, we don't feel the effects of it. - url = cloneURL(url) pipeReader, pipeWriter := io.Pipe() - - // This is mirroring what http.NewRequestContext did, but - // using an already parsed url.URL object, rather than a string - // and parsing it again. This is a bit funny with HTTP/1.1 - // explicitly, but this is logic copied over from - // NewRequestContext and doesn't effect the actual version - // being transmitted. - request := (&http.Request{ - Method: http.MethodPost, - URL: url, - Header: header, - Proto: "HTTP/1.1", - ProtoMajor: 1, - ProtoMinor: 1, - Body: pipeReader, - Host: url.Host, - }).WithContext(ctx) + request := makeRequest(ctx, url, header, pipeReader) return &duplexHTTPCall{ ctx: ctx, httpClient: httpClient, @@ -413,3 +373,27 @@ func cloneURL(oldURL *url.URL) *url.URL { } return newURL } + +func makeRequest(ctx context.Context, url *url.URL, header http.Header, body io.ReadCloser) *http.Request { + // ensure we make a copy of the url before we pass along to the + // Request. This ensures if a transport out of our control wants + // to mutate the req.URL, we don't feel the effects of it. + url = cloneURL(url) + // This is mirroring what http.NewRequestContext did, but + // using an already parsed url.URL object, rather than a string + // and parsing it again. This is a bit funny with HTTP/1.1 + // explicitly, but this is logic copied over from + // NewRequestContext and doesn't effect the actual version + // being transmitted. + return (&http.Request{ + Method: http.MethodPost, + URL: url, + Header: header, + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + Body: nil, // Set in Do. + Host: url.Host, + }).WithContext(ctx) + +}