Skip to content

Commit

Permalink
clone headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed May 17, 2022
1 parent 1f20a3a commit 2605919
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
9 changes: 8 additions & 1 deletion http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,11 +2677,18 @@ func (rws *responseWriterState) writeHeader(code int) {
// Per RFC 8297 we must not clear the current header map
h := rws.handlerHeader

_, cl := h["Content-Length"]
_, te := h["Transfer-Encoding"]
if cl || te {
h = h.Clone()
h.Del("Content-Length")
h.Del("Transfer-Encoding")
}

if rws.conn.writeHeaders(rws.stream, &writeResHeaders{
streamID: rws.stream.id,
httpResCode: code,
h: h,
noBody: true,
endStream: rws.handlerDone && !rws.hasTrailers(),
}) != nil {
rws.dirty = true
Expand Down
19 changes: 4 additions & 15 deletions http2/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ type writeResHeaders struct {
httpResCode int // 0 means no ":status" line
h http.Header // may be nil
trailers []string // if non-nil, which keys of h to write. nil means all.
noBody bool // if true, Content-Length and Transfer-Encoding will not be set
endStream bool

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

var excludedKeys map[string]bool
if w.noBody {
excludedKeys = map[string]bool{"Content-Length": true, "Transfer-Encoding": true}
}

encodeHeaders(enc, w.h, w.trailers, excludedKeys)
encodeHeaders(enc, w.h, w.trailers)

if w.contentType != "" {
encKV(enc, "content-type", w.contentType)
Expand Down Expand Up @@ -279,7 +273,7 @@ func (w *writePushPromise) writeFrame(ctx writeContext) error {
encKV(enc, ":scheme", w.url.Scheme)
encKV(enc, ":authority", w.url.Host)
encKV(enc, ":path", w.url.RequestURI())
encodeHeaders(enc, w.h, nil, nil)
encodeHeaders(enc, w.h, nil)

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

// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
// is encoded only if k is in keys. If excludeKeys is not nil, then
// (k, k[h]) is encoded only if k is not true in excludeKeys.
func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string, excludeKeys map[string]bool) {
// is encoded only if k is in keys.
func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
if keys == nil {
sorter := sorterPool.Get().(*sorter)
// Using defer here, since the returned keys from the
Expand All @@ -347,10 +340,6 @@ func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string, excludeKeys
keys = sorter.Keys(h)
}
for _, k := range keys {
if excludeKeys[k] {
continue
}

vv := h[k]
k, ascii := lowerHeader(k)
if !ascii {
Expand Down

0 comments on commit 2605919

Please sign in to comment.