Skip to content

Commit 2605919

Browse files
committed
clone headers
1 parent 1f20a3a commit 2605919

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

http2/server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2677,11 +2677,18 @@ func (rws *responseWriterState) writeHeader(code int) {
26772677
// Per RFC 8297 we must not clear the current header map
26782678
h := rws.handlerHeader
26792679

2680+
_, cl := h["Content-Length"]
2681+
_, te := h["Transfer-Encoding"]
2682+
if cl || te {
2683+
h = h.Clone()
2684+
h.Del("Content-Length")
2685+
h.Del("Transfer-Encoding")
2686+
}
2687+
26802688
if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
26812689
streamID: rws.stream.id,
26822690
httpResCode: code,
26832691
h: h,
2684-
noBody: true,
26852692
endStream: rws.handlerDone && !rws.hasTrailers(),
26862693
}) != nil {
26872694
rws.dirty = true

http2/write.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ type writeResHeaders struct {
181181
httpResCode int // 0 means no ":status" line
182182
h http.Header // may be nil
183183
trailers []string // if non-nil, which keys of h to write. nil means all.
184-
noBody bool // if true, Content-Length and Transfer-Encoding will not be set
185184
endStream bool
186185

187186
date string
@@ -215,12 +214,7 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
215214
encKV(enc, ":status", httpCodeString(w.httpResCode))
216215
}
217216

218-
var excludedKeys map[string]bool
219-
if w.noBody {
220-
excludedKeys = map[string]bool{"Content-Length": true, "Transfer-Encoding": true}
221-
}
222-
223-
encodeHeaders(enc, w.h, w.trailers, excludedKeys)
217+
encodeHeaders(enc, w.h, w.trailers)
224218

225219
if w.contentType != "" {
226220
encKV(enc, "content-type", w.contentType)
@@ -279,7 +273,7 @@ func (w *writePushPromise) writeFrame(ctx writeContext) error {
279273
encKV(enc, ":scheme", w.url.Scheme)
280274
encKV(enc, ":authority", w.url.Host)
281275
encKV(enc, ":path", w.url.RequestURI())
282-
encodeHeaders(enc, w.h, nil, nil)
276+
encodeHeaders(enc, w.h, nil)
283277

284278
headerBlock := buf.Bytes()
285279
if len(headerBlock) == 0 {
@@ -335,9 +329,8 @@ func (wu writeWindowUpdate) writeFrame(ctx writeContext) error {
335329
}
336330

337331
// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
338-
// is encoded only if k is in keys. If excludeKeys is not nil, then
339-
// (k, k[h]) is encoded only if k is not true in excludeKeys.
340-
func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string, excludeKeys map[string]bool) {
332+
// is encoded only if k is in keys.
333+
func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
341334
if keys == nil {
342335
sorter := sorterPool.Get().(*sorter)
343336
// Using defer here, since the returned keys from the
@@ -347,10 +340,6 @@ func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string, excludeKeys
347340
keys = sorter.Keys(h)
348341
}
349342
for _, k := range keys {
350-
if excludeKeys[k] {
351-
continue
352-
}
353-
354343
vv := h[k]
355344
k, ascii := lowerHeader(k)
356345
if !ascii {

0 commit comments

Comments
 (0)