Skip to content

Commit

Permalink
Allow Host request header to override the http request host (#440)
Browse files Browse the repository at this point in the history
* Add `allow_host_override` attribute that allows the `Host` request header to override the request host.

* Remove duplicate host override

* Add changelog entry

* Update provider documentation

* Remove feature flag for host overrides

* Update documentation

* Apply suggestions from code review

Co-authored-by: Austin Valle <austinvalle@gmail.com>

* Set `request.Host` in the same loop setting the request headers.

* Add note changelog describing functionality change.

---------

Co-authored-by: Austin Valle <austinvalle@gmail.com>
  • Loading branch information
SBGoods and austinvalle authored Jul 23, 2024
1 parent ec7c3b8 commit fb5fd5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240719-143606.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'data-source/http: Allow `Host` header in `request_headers` to be set on HTTP request'
time: 2024-07-19T14:36:06.795798-04:00
custom:
Issue: "440"
17 changes: 17 additions & 0 deletions .changes/unreleased/NOTES-20240723-162543.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: NOTES
body: |+
data-source/http: Previous versions of this provider ignored any `Host` headers specified in the `request_headers` attribute when setting the HTTP request. Any specified `Host` request headers will now override the `url` attribute host.
For example, in the following configuration:
```hcl
data "http" "example" {
url = "https://www.example.com"
request_headers = {
Host = "www.differentexample.com"
}
}
```
The HTTP request host will now be `www.differentexample.com` instead of `www.example.com`.
time: 2024-07-23T16:25:43.160519-04:00
custom:
Issue: "440"
3 changes: 3 additions & 0 deletions internal/provider/data_source_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
}

request.Header.Set(name, header)
if name == "Host" {
request.Host = header
}
}

response, err := retryClient.Do(request)
Expand Down
43 changes: 43 additions & 0 deletions internal/provider/data_source_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,49 @@ func TestDataSource_UnsupportedInsecureCaCert(t *testing.T) {
})
}

func TestDataSource_HostRequestHeaderOverride_200(t *testing.T) {
altHost := "alt-test-host"

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != altHost {
w.WriteHeader(400)
return
}

w.Header().Set("Content-Type", "text/plain")
w.Header().Set("X-Single", "foobar")
w.Header().Add("X-Double", "1")
w.Header().Add("X-Double", "2")
_, err := w.Write([]byte("1.0.0"))
if err != nil {
t.Errorf("error writing body: %s", err)
}
}))
defer testServer.Close()

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "http" "http_test" {
url = "%s"
request_headers = {
"Host" = "%s"
}
}`, testServer.URL, altHost),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.http.http_test", "status_code", "200"),
resource.TestCheckResourceAttr("data.http.http_test", "response_body", "1.0.0"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.Content-Type", "text/plain"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.X-Single", "foobar"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.X-Double", "1, 2"),
),
},
},
})
}

// testProxiedURL is a hardcoded URL used in acceptance testing where it is
// expected that a locally started HTTP proxy will handle the request.
//
Expand Down

0 comments on commit fb5fd5a

Please sign in to comment.