h2non/gentleman

On Go 1.9 redirect plugin produce duplicate of headers.

vooon opened this issue · 3 comments

vooon commented

Same code compiled with go 1.7 and 1.9 produce different requests.

On go 1.7:

DEBU[0001] http request
GET /api-v2.1/ HTTP/0.0
Host: test.sdn:8765
Content-Type: application/json
Referer: http://test.sdn:8765/api-v2.1
User-Agent: gentleman/2.0.2
X-Auth-Token: censored

Go 1.9:

DEBU[0001] http request
GET /api-v2.1/ HTTP/0.0
Host: test.sdn:8765
Content-Type: application/json
Content-Type: application/json
Referer: http://test.sdn:8765/api-v2.1
User-Agent: gentleman/2.0.2
User-Agent: gentleman/2.0.2
X-Auth-Token: censored
X-Auth-Token: censored

I workaround that by making custom plugin:

func NewRedirectPlugin() plugin.Plugin {
	return plugin.NewRequestPlugin(func(ctx *context.Context, h context.Handler) {
		ctx.Client.CheckRedirect = func(req *http.Request, pool []*http.Request) error {
			if len(pool) >= redirect.RedirectLimit {
				return redirect.ErrRedirectLimitExceeded
			}

			// redirect.Config() copy Headers from pool[0].Headers to req.Headers using req.Headers.Add()
			// That works on Go 1.7 (and seems required), but on Go 1.9 that produce header duplicates.
			// E.g. two X-Auth-Token instead of one. That broke auth.
			// Seems that in Go 1.9 net/http do headers copy

			return nil
		}
		h.Next(ctx)
	})
}
h2non commented

Thanks for reporting. I know where the issue is, will fix it asap.

h2non commented

This should be fixed in gentleman@v2.0.3. Try it upgrading the package:

go get -u gopkg.in/h2non/gentleman.v2