diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2024-01-11 15:23:52 -0800 |
|---|---|---|
| committer | Joe Tsai <joetsai@digital-static.net> | 2024-01-11 15:30:43 -0800 |
| commit | 2e20bd2ffe2098877ad44d5eed93a1820956d1e9 (patch) | |
| tree | 7472ed2a5ca5442b870a03ea41d8fd4b835a4154 /util/httpio/context.go | |
| parent | b89c11336514d541ad25de564dcf0561c0b24e58 (diff) | |
| download | tailscale-dsnet/httpio.tar.xz tailscale-dsnet/httpio.zip | |
util/httpio: prototype design for handling I/O in HTTPdsnet/httpio
Signed-off-by: Joe Tsai <joetsai@digital-static.net>
Diffstat (limited to 'util/httpio/context.go')
| -rw-r--r-- | util/httpio/context.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/util/httpio/context.go b/util/httpio/context.go new file mode 100644 index 000000000..ddc5a9b51 --- /dev/null +++ b/util/httpio/context.go @@ -0,0 +1,43 @@ +// Copyright (c) Tailscale Inc & AUTHORS +// SPDX-License-Identifier: BSD-3-Clause + +package httpio + +import ( + "context" + "net/http" + + "tailscale.com/util/httphdr" +) + +type headerKey struct{} + +// WithHeader specifies the HTTP header to use with a client request. +// It only affects [Do], [Get], [Post], [Put], and [Delete]. +// +// Example usage: +// +// ctx = httpio.WithHeader(ctx, http.Header{"DD-API-KEY": ...}) +func WithHeader(ctx context.Context, hdr http.Header) context.Context { + return context.WithValue(ctx, headerKey{}, hdr) +} + +type authKey struct{} + +// WithAuth specifies an "Authorization" header to use with a client request. +// This takes precedence over any "Authorization" header that may be present +// in the [http.Header] provided to [WithHeader]. +// It only affects [Do], [Get], [Post], [Put], and [Delete]. +// +// Example usage: +// +// ctx = httpio.WithAuth(ctx, httphdr.BasicAuth{ +// Username: "admin", +// Password: "password", +// }) +func WithAuth(ctx context.Context, auth httphdr.AuthScheme) context.Context { + return context.WithValue(ctx, authKey{}, auth) +} + +// TODO: Add extraction functionality to retrieve the original +// *http.Request and http.ResponseWriter for use with [Handler]. |
