client: set hard limit on redirect checks

release-2.1
Brian Waldon 2015-02-02 17:09:51 -08:00 committed by Yicheng Qin
parent 50a9b2d9c8
commit b41d6bc416
2 changed files with 18 additions and 3 deletions

View File

@ -28,8 +28,9 @@ import (
)
var (
ErrNoEndpoints = errors.New("client: no endpoints available")
ErrTooManyRedirects = errors.New("client: too many redirects")
ErrNoEndpoints = errors.New("client: no endpoints available")
ErrTooManyRedirects = errors.New("client: too many redirects")
errTooManyRedirectChecks = errors.New("client: too many redirect checks")
)
var DefaultRequestTimeout = 5 * time.Second
@ -297,7 +298,7 @@ type redirectFollowingHTTPClient struct {
}
func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
for i := 0; ; i++ {
for i := 0; i < 100; i++ {
if i > 0 {
if err := r.checkRedirect(i); err != nil {
return nil, nil, err
@ -324,6 +325,8 @@ func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (*
}
return resp, body, nil
}
return nil, nil, errTooManyRedirectChecks
}
type redirectedHTTPAction struct {

View File

@ -522,6 +522,18 @@ func TestRedirectFollowingHTTPClient(t *testing.T) {
},
wantErr: errors.New("Location header not valid URL: :"),
},
// fail if redirects checked way too many times
{
checkRedirect: func(int) error { return nil },
client: &staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusTemporaryRedirect,
Header: http.Header{"Location": []string{"http://example.com"}},
},
},
wantErr: errTooManyRedirectChecks,
},
}
for i, tt := range tests {