Skip to content

Commit

Permalink
Factor out makeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Oct 19, 2023
1 parent cd25e28 commit 22629fb
Showing 1 changed file with 26 additions and 42 deletions.
68 changes: 26 additions & 42 deletions http_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

}

0 comments on commit 22629fb

Please sign in to comment.