From 99796c2069e071f490e52f969f0909788c0b66eb Mon Sep 17 00:00:00 2001 From: coverste Date: Fri, 21 Jun 2019 10:53:01 -0700 Subject: [PATCH 1/3] fix host header --- full_config.yaml | 1 + web_requester.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/full_config.yaml b/full_config.yaml index d4bd8ba..5414406 100644 --- a/full_config.yaml +++ b/full_config.yaml @@ -60,6 +60,7 @@ Request: Headers: Authorization: Bearer $APIKEY Content-Type: application/json + Host: example.com # POST request body # For binary body see https://yaml.org/type/binary.html diff --git a/web_requester.go b/web_requester.go index 89b3953..dfdebb3 100644 --- a/web_requester.go +++ b/web_requester.go @@ -153,6 +153,19 @@ func (w *webRequester) Request() error { } req.Header = w.headers + + // from https://golang.org/src/net/http/request.go?#L124 + // For client requests, the URL's Host specifies the server to + // connect to, while the Request's Host field optionally + // specifies the Host header value to send in the HTTP + // request. + if host, ok := w.headers["Host"]; ok { + if len(host) != 1 { + return errors.New("Host Header is not a single value.") + } + req.Host = host[0] + } + resp, err := httpClient.Do(req) /* to look at the response body From 6934f235c711fd16639374f5740da06b01c3f051 Mon Sep 17 00:00:00 2001 From: coverste Date: Tue, 30 Jul 2019 12:44:54 -0700 Subject: [PATCH 2/3] code review comments --- web_requester.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/web_requester.go b/web_requester.go index dfdebb3..7f5d806 100644 --- a/web_requester.go +++ b/web_requester.go @@ -159,11 +159,20 @@ func (w *webRequester) Request() error { // connect to, while the Request's Host field optionally // specifies the Host header value to send in the HTTP // request. - if host, ok := w.headers["Host"]; ok { - if len(host) != 1 { - return errors.New("Host Header is not a single value.") + + var hosts []string + + // headers are case-insensitive + for k, v := range w.headers { + if strings.ToLower(k) == "host" { + hosts = append(hosts, v...) } - req.Host = host[0] + } + if len(hosts) > 1 { + return errors.New("multiple host headers are not allowed") + } + if len(hosts) > 0 { + req.Host = hosts[0] } resp, err := httpClient.Do(req) From 3a62319a693741bf67afe15bc2a11af7dbcd435f Mon Sep 17 00:00:00 2001 From: coverste Date: Tue, 30 Jul 2019 14:55:08 -0700 Subject: [PATCH 3/3] code review comments --- web_requester.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/web_requester.go b/web_requester.go index 7f5d806..f4d6dab 100644 --- a/web_requester.go +++ b/web_requester.go @@ -162,17 +162,17 @@ func (w *webRequester) Request() error { var hosts []string - // headers are case-insensitive - for k, v := range w.headers { - if strings.ToLower(k) == "host" { - hosts = append(hosts, v...) + //case insensitive + if host, ok := w.headers["host"]; ok { + if len(hosts) != 1 { + return errors.New("multiple host headers are not allowed") } - } - if len(hosts) > 1 { - return errors.New("multiple host headers are not allowed") - } - if len(hosts) > 0 { - req.Host = hosts[0] + req.Host = host[0] + } else if host, ok = w.headers["Host"]; ok { + if len(hosts) != 1 { + return errors.New("multiple host headers are not allowed") + } + req.Host = host[0] } resp, err := httpClient.Do(req)