Skip to content

Commit b2d760b

Browse files
author
Julien Pivotto
committed
http: Add the ability to not follow redirects
Currently, when we unmarshal, we get the default value, which is to follow redirects. Signed-off-by: Julien Pivotto <[email protected]>
1 parent 517c52c commit b2d760b

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

config/http_config.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ type HTTPClientConfig struct {
111111
ProxyURL URL `yaml:"proxy_url,omitempty"`
112112
// TLSConfig to use to connect to the targets.
113113
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
114+
// FollowRedirects specifies wheter the client should follow the HTTP 3xx redirects.
115+
// The omitempty flag is not set, because it would be hidden from the
116+
// marshalled configuration when set to false.
117+
FollowRedirects bool `yaml:"follow_redirects"`
114118
}
115119

116120
// SetDirectory joins any relative file paths with dir.
@@ -172,6 +176,9 @@ func (c *HTTPClientConfig) Validate() error {
172176
// UnmarshalYAML implements the yaml.Unmarshaler interface
173177
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
174178
type plain HTTPClientConfig
179+
*c = HTTPClientConfig{
180+
FollowRedirects: true,
181+
}
175182
if err := unmarshal((*plain)(c)); err != nil {
176183
return err
177184
}
@@ -196,7 +203,13 @@ func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, e
196203
if err != nil {
197204
return nil, err
198205
}
199-
return newClient(rt), nil
206+
client := newClient(rt)
207+
if !cfg.FollowRedirects {
208+
client.CheckRedirect = func(*http.Request, []*http.Request) error {
209+
return http.ErrUseLastResponse
210+
}
211+
}
212+
return client, nil
200213
}
201214

202215
// NewRoundTripperFromConfig returns a new HTTP RoundTripper configured for the

config/http_config_test.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,44 @@ func TestNewClientFromConfig(t *testing.T) {
296296
fmt.Fprint(w, ExpectedMessage)
297297
}
298298
},
299+
}, {
300+
clientConfig: HTTPClientConfig{
301+
FollowRedirects: true,
302+
TLSConfig: TLSConfig{
303+
CAFile: TLSCAChainPath,
304+
CertFile: ClientCertificatePath,
305+
KeyFile: ClientKeyNoPassPath,
306+
ServerName: "",
307+
InsecureSkipVerify: false},
308+
},
309+
handler: func(w http.ResponseWriter, r *http.Request) {
310+
switch r.URL.Path {
311+
case "/redirected":
312+
fmt.Fprintf(w, ExpectedMessage)
313+
default:
314+
http.Redirect(w, r, "/redirected", http.StatusFound)
315+
}
316+
},
317+
}, {
318+
clientConfig: HTTPClientConfig{
319+
FollowRedirects: false,
320+
TLSConfig: TLSConfig{
321+
CAFile: TLSCAChainPath,
322+
CertFile: ClientCertificatePath,
323+
KeyFile: ClientKeyNoPassPath,
324+
ServerName: "",
325+
InsecureSkipVerify: false},
326+
},
327+
handler: func(w http.ResponseWriter, r *http.Request) {
328+
switch r.URL.Path {
329+
case "/redirected":
330+
fmt.Fprint(w, "The redirection was followed.")
331+
default:
332+
w.Header()["Content-Type"] = nil
333+
http.Redirect(w, r, "/redirected", http.StatusFound)
334+
fmt.Fprint(w, ExpectedMessage)
335+
}
336+
},
299337
},
300338
}
301339

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

@@ -932,6 +970,16 @@ func TestHideHTTPClientConfigSecrets(t *testing.T) {
932970
}
933971
}
934972

973+
func TestDefaultFollowRedirect(t *testing.T) {
974+
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
975+
if err != nil {
976+
t.Errorf("Error loading HTTP client config: %v", err)
977+
}
978+
if !cfg.FollowRedirects {
979+
t.Errorf("follow_redirects should be true")
980+
}
981+
}
982+
935983
func TestValidateHTTPConfig(t *testing.T) {
936984
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
937985
if err != nil {

0 commit comments

Comments
 (0)