MCPcopy Index your code
hub / github.com/docker/cli / withCustomHeadersFromEnv

Function withCustomHeadersFromEnv

cli/command/cli_options.go:171–226  ·  view source on GitHub ↗

withCustomHeadersFromEnv overriding custom HTTP headers to be sent by the client through the [envOverrideHTTPHeaders] environment-variable. This environment variable is the equivalent to the HttpHeaders field in the configuration file. WARNING: If both config and environment-variable are set, the e

()

Source from the content-addressed store, hash-verified

169//
170// TODO(thaJeztah): this is a client Option, and should be moved to the client. It is non-exported for that reason.
171func withCustomHeadersFromEnv() (client.Opt, error) {
172 value := os.Getenv(envOverrideHTTPHeaders)
173 if value == "" {
174 return nil, nil
175 }
176 csvReader := csv.NewReader(strings.NewReader(value))
177 fields, err := csvReader.Read()
178 if err != nil {
179 return nil, invalidParameter(fmt.Errorf(
180 "failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs",
181 envOverrideHTTPHeaders,
182 ))
183 }
184 if len(fields) == 0 {
185 return nil, nil
186 }
187
188 env := map[string]string{}
189 for _, kv := range fields {
190 k, v, hasValue := strings.Cut(kv, "=")
191
192 // Only strip whitespace in keys; preserve whitespace in values.
193 k = strings.TrimSpace(k)
194
195 if k == "" {
196 return nil, invalidParameter(fmt.Errorf(
197 `failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'`,
198 envOverrideHTTPHeaders, kv,
199 ))
200 }
201
202 // We don't currently allow empty key=value pairs, and produce an error.
203 // This is something we could allow in future (e.g. to read value
204 // from an environment variable with the same name). In the meantime,
205 // produce an error to prevent users from depending on this.
206 if !hasValue {
207 return nil, invalidParameter(fmt.Errorf(
208 `failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'`,
209 envOverrideHTTPHeaders, kv,
210 ))
211 }
212
213 env[http.CanonicalHeaderKey(k)] = v
214 }
215
216 if len(env) == 0 {
217 // We should probably not hit this case, as we don't skip values
218 // (only return errors), but we don't want to discard existing
219 // headers with an empty set.
220 return nil, nil
221 }
222
223 // TODO(thaJeztah): add a client.WithExtraHTTPHeaders() function to allow these headers to be _added_ to existing ones, instead of _replacing_
224 // see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
225 return client.WithHTTPHeaders(env), nil
226}
227
228// WithUserAgent configures the User-Agent string for cli HTTP requests.

Callers 1

newAPIClientFromEndpointFunction · 0.85

Calls 2

invalidParameterFunction · 0.70
ReadMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…