@@ -180,61 +180,59 @@ const envOverrideHTTPHeaders = "DOCKER_CUSTOM_HEADERS"
180180// override headers with the same name).
181181//
182182// TODO(thaJeztah): this is a client Option, and should be moved to the client. It is non-exported for that reason.
183- func withCustomHeadersFromEnv () client.Opt {
184- return func (apiClient * client.Client ) error {
185- value := os .Getenv (envOverrideHTTPHeaders )
186- if value == "" {
187- return nil
188- }
189- csvReader := csv .NewReader (strings .NewReader (value ))
190- fields , err := csvReader .Read ()
191- if err != nil {
192- return invalidParameter (errors .Errorf (
193- "failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs" ,
194- envOverrideHTTPHeaders ,
195- ))
196- }
197- if len (fields ) == 0 {
198- return nil
199- }
200-
201- env := map [string ]string {}
202- for _ , kv := range fields {
203- k , v , hasValue := strings .Cut (kv , "=" )
204-
205- // Only strip whitespace in keys; preserve whitespace in values.
206- k = strings .TrimSpace (k )
183+ func withCustomHeadersFromEnv () (client.Opt , error ) {
184+ value := os .Getenv (envOverrideHTTPHeaders )
185+ if value == "" {
186+ return nil , nil
187+ }
188+ csvReader := csv .NewReader (strings .NewReader (value ))
189+ fields , err := csvReader .Read ()
190+ if err != nil {
191+ return nil , invalidParameter (errors .Errorf (
192+ "failed to parse custom headers from %s environment variable: value must be formatted as comma-separated key=value pairs" ,
193+ envOverrideHTTPHeaders ,
194+ ))
195+ }
196+ if len (fields ) == 0 {
197+ return nil , nil
198+ }
207199
208- if k == "" {
209- return invalidParameter (errors .Errorf (
210- `failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'` ,
211- envOverrideHTTPHeaders , kv ,
212- ))
213- }
200+ env := map [string ]string {}
201+ for _ , kv := range fields {
202+ k , v , hasValue := strings .Cut (kv , "=" )
214203
215- // We don't currently allow empty key=value pairs, and produce an error.
216- // This is something we could allow in future (e.g. to read value
217- // from an environment variable with the same name). In the meantime,
218- // produce an error to prevent users from depending on this.
219- if ! hasValue {
220- return invalidParameter (errors .Errorf (
221- `failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'` ,
222- envOverrideHTTPHeaders , kv ,
223- ))
224- }
204+ // Only strip whitespace in keys; preserve whitespace in values.
205+ k = strings .TrimSpace (k )
225206
226- env [http .CanonicalHeaderKey (k )] = v
207+ if k == "" {
208+ return nil , invalidParameter (errors .Errorf (
209+ `failed to set custom headers from %s environment variable: value contains a key=value pair with an empty key: '%s'` ,
210+ envOverrideHTTPHeaders , kv ,
211+ ))
227212 }
228213
229- if len (env ) == 0 {
230- // We should probably not hit this case, as we don't skip values
231- // (only return errors), but we don't want to discard existing
232- // headers with an empty set.
233- return nil
214+ // We don't currently allow empty key=value pairs, and produce an error.
215+ // This is something we could allow in future (e.g. to read value
216+ // from an environment variable with the same name). In the meantime,
217+ // produce an error to prevent users from depending on this.
218+ if ! hasValue {
219+ return nil , invalidParameter (errors .Errorf (
220+ `failed to set custom headers from %s environment variable: missing "=" in key=value pair: '%s'` ,
221+ envOverrideHTTPHeaders , kv ,
222+ ))
234223 }
235224
236- // TODO(thaJeztah): add a client.WithExtraHTTPHeaders() function to allow these headers to be _added_ to existing ones, instead of _replacing_
237- // see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
238- return client .WithHTTPHeaders (env )(apiClient )
225+ env [http .CanonicalHeaderKey (k )] = v
226+ }
227+
228+ if len (env ) == 0 {
229+ // We should probably not hit this case, as we don't skip values
230+ // (only return errors), but we don't want to discard existing
231+ // headers with an empty set.
232+ return nil , nil
239233 }
234+
235+ // TODO(thaJeztah): add a client.WithExtraHTTPHeaders() function to allow these headers to be _added_ to existing ones, instead of _replacing_
236+ // see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
237+ return client .WithHTTPHeaders (env ), nil
240238}
0 commit comments