Skip to content

Commit

Permalink
http: Add the ability to not follow redirects
Browse files Browse the repository at this point in the history
Currently, when we unmarshal, we get the default value, which is to
follow redirects.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie committed Feb 24, 2021
1 parent 517c52c commit b2d760b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
15 changes: 14 additions & 1 deletion config/http_config.go
Expand Up @@ -111,6 +111,10 @@ type HTTPClientConfig struct {
ProxyURL URL `yaml:"proxy_url,omitempty"`
// TLSConfig to use to connect to the targets.
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
// FollowRedirects specifies wheter the client should follow the HTTP 3xx redirects.
// The omitempty flag is not set, because it would be hidden from the
// marshalled configuration when set to false.
FollowRedirects bool `yaml:"follow_redirects"`
}

// SetDirectory joins any relative file paths with dir.
Expand Down Expand Up @@ -172,6 +176,9 @@ func (c *HTTPClientConfig) Validate() error {
// UnmarshalYAML implements the yaml.Unmarshaler interface
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain HTTPClientConfig
*c = HTTPClientConfig{
FollowRedirects: true,
}
if err := unmarshal((*plain)(c)); err != nil {
return err
}
Expand All @@ -196,7 +203,13 @@ func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, e
if err != nil {
return nil, err
}
return newClient(rt), nil
client := newClient(rt)
if !cfg.FollowRedirects {
client.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
}
return client, nil
}

// NewRoundTripperFromConfig returns a new HTTP RoundTripper configured for the
Expand Down
50 changes: 49 additions & 1 deletion config/http_config_test.go
Expand Up @@ -296,6 +296,44 @@ func TestNewClientFromConfig(t *testing.T) {
fmt.Fprint(w, ExpectedMessage)
}
},
}, {
clientConfig: HTTPClientConfig{
FollowRedirects: true,
TLSConfig: TLSConfig{
CAFile: TLSCAChainPath,
CertFile: ClientCertificatePath,
KeyFile: ClientKeyNoPassPath,
ServerName: "",
InsecureSkipVerify: false},
},
handler: func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/redirected":
fmt.Fprintf(w, ExpectedMessage)
default:
http.Redirect(w, r, "/redirected", http.StatusFound)
}
},
}, {
clientConfig: HTTPClientConfig{
FollowRedirects: false,
TLSConfig: TLSConfig{
CAFile: TLSCAChainPath,
CertFile: ClientCertificatePath,
KeyFile: ClientKeyNoPassPath,
ServerName: "",
InsecureSkipVerify: false},
},
handler: func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/redirected":
fmt.Fprint(w, "The redirection was followed.")
default:
w.Header()["Content-Type"] = nil
http.Redirect(w, r, "/redirected", http.StatusFound)
fmt.Fprint(w, ExpectedMessage)
}
},
},
}

Expand All @@ -317,7 +355,7 @@ func TestNewClientFromConfig(t *testing.T) {
}
response, err := client.Get(testServer.URL)
if err != nil {
t.Errorf("Can't connect to the test server using this config: %+v", validConfig.clientConfig)
t.Errorf("Can't connect to the test server using this config: %+v %v", validConfig.clientConfig, err)
continue
}

Expand Down Expand Up @@ -932,6 +970,16 @@ func TestHideHTTPClientConfigSecrets(t *testing.T) {
}
}

func TestDefaultFollowRedirect(t *testing.T) {
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
if err != nil {
t.Errorf("Error loading HTTP client config: %v", err)
}
if !cfg.FollowRedirects {
t.Errorf("follow_redirects should be true")
}
}

func TestValidateHTTPConfig(t *testing.T) {
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
if err != nil {
Expand Down

0 comments on commit b2d760b

Please sign in to comment.