Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support --custom-header flags #5127

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading