On redirect, only copy headers when they don't already exist in the redirected request
A changeset under consideration for Go 1.7 would automatically copy headers on redirect. This change future-proofs our code so we won't make duplicate copies of the headers if net/http does it automatically in the future. Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
parent
c149d45cc8
commit
4354f0a107
@ -36,8 +36,21 @@ func checkHTTPRedirect(req *http.Request, via []*http.Request) error {
|
|||||||
|
|
||||||
if len(via) > 0 {
|
if len(via) > 0 {
|
||||||
for headerName, headerVals := range via[0].Header {
|
for headerName, headerVals := range via[0].Header {
|
||||||
if headerName == "Accept" || headerName == "Range" {
|
if headerName != "Accept" && headerName != "Range" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
for _, val := range headerVals {
|
for _, val := range headerVals {
|
||||||
|
// Don't add to redirected request if redirected
|
||||||
|
// request already has a header with the same
|
||||||
|
// name and value.
|
||||||
|
hasValue := false
|
||||||
|
for _, existingVal := range req.Header[headerName] {
|
||||||
|
if existingVal == val {
|
||||||
|
hasValue = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasValue {
|
||||||
req.Header.Add(headerName, val)
|
req.Header.Add(headerName, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user