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

Expose HTTPHost property in HTTPClientConfig #645

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ type HTTPClientConfig struct {
// The omitempty flag is not set, because it would be hidden from the
// marshalled configuration when set to false.
EnableHTTP2 bool `yaml:"enable_http2" json:"enable_http2"`
// HTTPHost optionally overrides the Host header to send.
// If empty, the host from the URL is used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this reconcile with the code that says:

rt = NewHostRoundTripper(opts.host, rt)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If empty, the host from the URL is used.

This is go default behavior. It's similar the the TLSServerName property.

If the property is not set by caller, then the value will be used from URL.

Ref: https://github.com/golang/go/blob/959b3fd4265d7e4efb18af454cd18799ed70b8fe/src/net/http/request.go#L240-L243

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be more correct to mention opts.host ?

HTTPHost string `yaml:"http_host,omitempty" json:"host,omitempty"`
// Proxy configuration.
ProxyConfig `yaml:",inline"`
// HTTPHeaders specify headers to inject in the requests. Those headers
Expand Down Expand Up @@ -664,6 +667,8 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon

if opts.host != "" {
rt = NewHostRoundTripper(opts.host, rt)
} else if cfg.HTTPHost != "" {
rt = NewHostRoundTripper(cfg.HTTPHost, rt)
}

// Return a new configured RoundTripper.
Expand Down
27 changes: 27 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,33 @@ func TestHost(t *testing.T) {
}))
defer ts.Close()

config := DefaultHTTPClientConfig
config.HTTPHost = "localhost.localdomain"

rt, err := NewRoundTripperFromConfig(config, "test_host")
if err != nil {
t.Fatal(err)
}

client := http.Client{
Transport: rt,
}
_, err = client.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
}

func TestHostWithNewRoundTripperFromConfig(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != "localhost.localdomain" {
t.Fatalf("Expected Host header in request to be 'localhost.localdomain', got '%s'", r.Host)
}

w.Header().Add("Content-Type", "application/json")
}))
defer ts.Close()

config := DefaultHTTPClientConfig

rt, err := NewRoundTripperFromConfig(config, "test_host", WithHost("localhost.localdomain"))
Expand Down