|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfe |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// ContextWithResponseHeaderHook returns a context that will, if passed to |
| 13 | +// [ClientRequest.Do] or to any of the wrapper methods that call it, arrange |
| 14 | +// for the given callback to be called with the headers from the raw HTTP |
| 15 | +// response. |
| 16 | +// |
| 17 | +// This is intended for allowing callers to respond to out-of-band metadata |
| 18 | +// such as cache-control-related headers, rate limiting headers, etc. Hooks |
| 19 | +// must not modify the given [http.Header] or otherwise attempt to change how |
| 20 | +// the response is handled by [ClientRequest.Do]. |
| 21 | +// |
| 22 | +// If the given context already has a response header hook then the returned |
| 23 | +// context will call both the existing hook and the newly-provided one, with |
| 24 | +// the newer being called first. |
| 25 | +func ContextWithResponseHeaderHook(parentCtx context.Context, cb func(status int, header http.Header)) context.Context { |
| 26 | + // If the given context already has a notification callback then we'll |
| 27 | + // arrange to notify both the previous and the new one. This is not |
| 28 | + // a super efficient way to achieve that but we expect it to be rare |
| 29 | + // for there to be more than one or two hooks associated with a particular |
| 30 | + // request, so it's not warranted to optimize this further. |
| 31 | + existingI := parentCtx.Value(contextResponseHeaderHookKey) |
| 32 | + finalCb := cb |
| 33 | + if existingI != nil { |
| 34 | + existing, ok := existingI.(func(int, http.Header)) |
| 35 | + // This explicit check-and-panic is redundant but required by our linter. |
| 36 | + if !ok { |
| 37 | + panic(fmt.Sprintf("context has response header hook of invalid type %T", existingI)) |
| 38 | + } |
| 39 | + finalCb = func(status int, header http.Header) { |
| 40 | + cb(status, header) |
| 41 | + existing(status, header) |
| 42 | + } |
| 43 | + } |
| 44 | + return context.WithValue(parentCtx, contextResponseHeaderHookKey, finalCb) |
| 45 | +} |
| 46 | + |
| 47 | +func contextResponseHeaderHook(ctx context.Context) func(int, http.Header) { |
| 48 | + cbI := ctx.Value(contextResponseHeaderHookKey) |
| 49 | + if cbI == nil { |
| 50 | + // Stub callback that does absolutely nothing, then. |
| 51 | + return func(int, http.Header) {} |
| 52 | + } |
| 53 | + return cbI.(func(int, http.Header)) |
| 54 | +} |
| 55 | + |
| 56 | +// contextResponseHeaderHookKey is the type of the internal key used to store |
| 57 | +// the callback for [ContextWithResponseHeaderHook] inside a [context.Context] |
| 58 | +// object. |
| 59 | +type contextResponseHeaderHookKeyType struct{} |
| 60 | + |
| 61 | +// contextResponseHeaderHookKey is the internal key used to store the callback |
| 62 | +// for [ContextWithResponseHeaderHook] inside a [context.Context] object. |
| 63 | +var contextResponseHeaderHookKey contextResponseHeaderHookKeyType |
0 commit comments