Skip to content

Commit

Permalink
feat: support --custom-header flags (#5127)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuker authored Feb 13, 2025
1 parent 42b78de commit 089f833
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,16 @@ func (config *Config) initTransport(withS3v2 bool) {
// return nil, probe.NewError(e)
// }
}
transport = tr

if len(globalCustomHeader) > 0 {
transport = &headerTransport{
RoundTripper: tr,
customHeader: globalCustomHeader.Clone(),
}
} else {
transport = tr
}

}

transport = limiter.New(config.UploadLimit, config.DownloadLimit, transport)
Expand All @@ -399,3 +408,15 @@ type SelectObjectOpts struct {
OutputSerOpts map[string]map[string]string
CompressionType minio.SelectCompressionType
}

type headerTransport struct {
http.RoundTripper
customHeader http.Header
}

func (h *headerTransport) RoundTrip(request *http.Request) (*http.Response, error) {
for k, v := range h.customHeader {
request.Header[k] = v
}
return h.RoundTripper.RoundTrip(request)
}
4 changes: 4 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ var globalFlags = []cli.Flag{
Hidden: true,
Value: 10 * time.Minute,
},
cli.StringSliceFlag{
Name: "custom-header,H",
Usage: "add custom HTTP header to the request. 'key:value' format.",
},
}

// bundled encryption flags
Expand Down
22 changes: 22 additions & 0 deletions cmd/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"crypto/x509"
"fmt"
"net"
"net/http"
"net/netip"
"net/url"
"os"
Expand All @@ -36,6 +37,7 @@ import (
"github.com/minio/madmin-go/v3"
"github.com/minio/pkg/v3/console"
"github.com/muesli/termenv"
"golang.org/x/net/http/httpguts"
)

const (
Expand Down Expand Up @@ -90,6 +92,8 @@ var (
globalLimitDownload uint64

globalContext, globalCancel = context.WithCancel(context.Background())

globalCustomHeader http.Header
)

var (
Expand Down Expand Up @@ -200,5 +204,23 @@ func setGlobalsFromContext(ctx *cli.Context) error {
globalResolvers[host] = addr
}
}

customHeaders := ctx.StringSlice("custom-header")
if len(customHeaders) > 0 {
globalCustomHeader = make(http.Header)
for _, header := range customHeaders {
i := strings.IndexByte(header, ':')
if i <= 0 {
return fmt.Errorf("invalid custom header entry %s", header)
}
h := strings.TrimSpace(header[:i])
hv := strings.TrimSpace(header[i+1:])
if !httpguts.ValidHeaderFieldName(h) || !httpguts.ValidHeaderFieldValue(hv) {
return fmt.Errorf("invalid custom header entry %s", header)
}
globalCustomHeader.Add(h, hv)
}
}

return nil
}

0 comments on commit 089f833

Please sign in to comment.