Skip to content

Commit 089f833

Browse files
authored
feat: support --custom-header flags (#5127)
1 parent 42b78de commit 089f833

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

cmd/client.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,16 @@ func (config *Config) initTransport(withS3v2 bool) {
372372
// return nil, probe.NewError(e)
373373
// }
374374
}
375-
transport = tr
375+
376+
if len(globalCustomHeader) > 0 {
377+
transport = &headerTransport{
378+
RoundTripper: tr,
379+
customHeader: globalCustomHeader.Clone(),
380+
}
381+
} else {
382+
transport = tr
383+
}
384+
376385
}
377386

378387
transport = limiter.New(config.UploadLimit, config.DownloadLimit, transport)
@@ -399,3 +408,15 @@ type SelectObjectOpts struct {
399408
OutputSerOpts map[string]map[string]string
400409
CompressionType minio.SelectCompressionType
401410
}
411+
412+
type headerTransport struct {
413+
http.RoundTripper
414+
customHeader http.Header
415+
}
416+
417+
func (h *headerTransport) RoundTrip(request *http.Request) (*http.Response, error) {
418+
for k, v := range h.customHeader {
419+
request.Header[k] = v
420+
}
421+
return h.RoundTripper.RoundTrip(request)
422+
}

cmd/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ var globalFlags = []cli.Flag{
9696
Hidden: true,
9797
Value: 10 * time.Minute,
9898
},
99+
cli.StringSliceFlag{
100+
Name: "custom-header,H",
101+
Usage: "add custom HTTP header to the request. 'key:value' format.",
102+
},
99103
}
100104

101105
// bundled encryption flags

cmd/globals.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"crypto/x509"
2424
"fmt"
2525
"net"
26+
"net/http"
2627
"net/netip"
2728
"net/url"
2829
"os"
@@ -36,6 +37,7 @@ import (
3637
"github.com/minio/madmin-go/v3"
3738
"github.com/minio/pkg/v3/console"
3839
"github.com/muesli/termenv"
40+
"golang.org/x/net/http/httpguts"
3941
)
4042

4143
const (
@@ -90,6 +92,8 @@ var (
9092
globalLimitDownload uint64
9193

9294
globalContext, globalCancel = context.WithCancel(context.Background())
95+
96+
globalCustomHeader http.Header
9397
)
9498

9599
var (
@@ -200,5 +204,23 @@ func setGlobalsFromContext(ctx *cli.Context) error {
200204
globalResolvers[host] = addr
201205
}
202206
}
207+
208+
customHeaders := ctx.StringSlice("custom-header")
209+
if len(customHeaders) > 0 {
210+
globalCustomHeader = make(http.Header)
211+
for _, header := range customHeaders {
212+
i := strings.IndexByte(header, ':')
213+
if i <= 0 {
214+
return fmt.Errorf("invalid custom header entry %s", header)
215+
}
216+
h := strings.TrimSpace(header[:i])
217+
hv := strings.TrimSpace(header[i+1:])
218+
if !httpguts.ValidHeaderFieldName(h) || !httpguts.ValidHeaderFieldValue(hv) {
219+
return fmt.Errorf("invalid custom header entry %s", header)
220+
}
221+
globalCustomHeader.Add(h, hv)
222+
}
223+
}
224+
203225
return nil
204226
}

0 commit comments

Comments
 (0)