diff --git a/hijack.go b/hijack.go index 477dd992..1a2181df 100644 --- a/hijack.go +++ b/hijack.go @@ -356,11 +356,24 @@ func (ctx *HijackResponse) Headers() http.Header { // SetHeader of the payload via key-value pairs. func (ctx *HijackResponse) SetHeader(pairs ...string) *HijackResponse { + headerIndex := make(map[string]int, len(ctx.payload.ResponseHeaders)) + for i, header := range ctx.payload.ResponseHeaders { + headerIndex[header.Name] = i + } + for i := 0; i < len(pairs); i += 2 { - ctx.payload.ResponseHeaders = append(ctx.payload.ResponseHeaders, &proto.FetchHeaderEntry{ - Name: pairs[i], - Value: pairs[i+1], - }) + name := pairs[i] + value := pairs[i+1] + + if idx, exists := headerIndex[name]; exists { + ctx.payload.ResponseHeaders[idx].Value = value + } else { + ctx.payload.ResponseHeaders = append(ctx.payload.ResponseHeaders, &proto.FetchHeaderEntry{ + Name: name, + Value: value, + }) + headerIndex[name] = len(ctx.payload.ResponseHeaders) - 1 + } } return ctx }