Skip to content

Commit 47ee79a

Browse files
authored
Merge pull request #278 from roidelapluie/redirect
http: Add the ability to not follow redirects
2 parents 90d71d7 + bdce8c0 commit 47ee79a

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

Diff for: config/http_config.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import (
3333
"gopkg.in/yaml.v2"
3434
)
3535

36+
// DefaultHTTPClientConfig is the default HTTP client configuration.
37+
var DefaultHTTPClientConfig = HTTPClientConfig{
38+
FollowRedirects: true,
39+
}
40+
3641
type closeIdler interface {
3742
CloseIdleConnections()
3843
}
@@ -111,6 +116,10 @@ type HTTPClientConfig struct {
111116
ProxyURL URL `yaml:"proxy_url,omitempty"`
112117
// TLSConfig to use to connect to the targets.
113118
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
119+
// FollowRedirects specifies whether the client should follow HTTP 3xx redirects.
120+
// The omitempty flag is not set, because it would be hidden from the
121+
// marshalled configuration when set to false.
122+
FollowRedirects bool `yaml:"follow_redirects"`
114123
}
115124

116125
// SetDirectory joins any relative file paths with dir.
@@ -172,6 +181,7 @@ func (c *HTTPClientConfig) Validate() error {
172181
// UnmarshalYAML implements the yaml.Unmarshaler interface
173182
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
174183
type plain HTTPClientConfig
184+
*c = DefaultHTTPClientConfig
175185
if err := unmarshal((*plain)(c)); err != nil {
176186
return err
177187
}
@@ -196,7 +206,13 @@ func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, e
196206
if err != nil {
197207
return nil, err
198208
}
199-
return newClient(rt), nil
209+
client := newClient(rt)
210+
if !cfg.FollowRedirects {
211+
client.CheckRedirect = func(*http.Request, []*http.Request) error {
212+
return http.ErrUseLastResponse
213+
}
214+
}
215+
return client, nil
200216
}
201217

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

Diff for: config/http_config_test.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,46 @@ 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+
w.Header().Set("Location", "/redirected")
315+
w.WriteHeader(http.StatusFound)
316+
fmt.Fprintf(w, "It should follow the redirect.")
317+
}
318+
},
319+
}, {
320+
clientConfig: HTTPClientConfig{
321+
FollowRedirects: false,
322+
TLSConfig: TLSConfig{
323+
CAFile: TLSCAChainPath,
324+
CertFile: ClientCertificatePath,
325+
KeyFile: ClientKeyNoPassPath,
326+
ServerName: "",
327+
InsecureSkipVerify: false},
328+
},
329+
handler: func(w http.ResponseWriter, r *http.Request) {
330+
switch r.URL.Path {
331+
case "/redirected":
332+
fmt.Fprint(w, "The redirection was followed.")
333+
default:
334+
w.Header().Set("Location", "/redirected")
335+
w.WriteHeader(http.StatusFound)
336+
fmt.Fprintf(w, ExpectedMessage)
337+
}
338+
},
299339
},
300340
}
301341

@@ -317,7 +357,7 @@ func TestNewClientFromConfig(t *testing.T) {
317357
}
318358
response, err := client.Get(testServer.URL)
319359
if err != nil {
320-
t.Errorf("Can't connect to the test server using this config: %+v", validConfig.clientConfig)
360+
t.Errorf("Can't connect to the test server using this config: %+v: %v", validConfig.clientConfig, err)
321361
continue
322362
}
323363

@@ -941,6 +981,16 @@ func TestHideHTTPClientConfigSecrets(t *testing.T) {
941981
}
942982
}
943983

984+
func TestDefaultFollowRedirect(t *testing.T) {
985+
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
986+
if err != nil {
987+
t.Errorf("Error loading HTTP client config: %v", err)
988+
}
989+
if !cfg.FollowRedirects {
990+
t.Errorf("follow_redirects should be true")
991+
}
992+
}
993+
944994
func TestValidateHTTPConfig(t *testing.T) {
945995
cfg, _, err := LoadHTTPConfigFile("testdata/http.conf.good.yml")
946996
if err != nil {

0 commit comments

Comments
 (0)