@@ -181,6 +181,7 @@ type writeResHeaders struct {
181
181
httpResCode int // 0 means no ":status" line
182
182
h http.Header // may be nil
183
183
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
184
185
endStream bool
185
186
186
187
date string
@@ -214,7 +215,12 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
214
215
encKV (enc , ":status" , httpCodeString (w .httpResCode ))
215
216
}
216
217
217
- encodeHeaders (enc , w .h , w .trailers )
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 )
218
224
219
225
if w .contentType != "" {
220
226
encKV (enc , "content-type" , w .contentType )
@@ -273,7 +279,7 @@ func (w *writePushPromise) writeFrame(ctx writeContext) error {
273
279
encKV (enc , ":scheme" , w .url .Scheme )
274
280
encKV (enc , ":authority" , w .url .Host )
275
281
encKV (enc , ":path" , w .url .RequestURI ())
276
- encodeHeaders (enc , w .h , nil )
282
+ encodeHeaders (enc , w .h , nil , nil )
277
283
278
284
headerBlock := buf .Bytes ()
279
285
if len (headerBlock ) == 0 {
@@ -329,8 +335,9 @@ func (wu writeWindowUpdate) writeFrame(ctx writeContext) error {
329
335
}
330
336
331
337
// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
332
- // is encoded only if k is in keys.
333
- func encodeHeaders (enc * hpack.Encoder , h http.Header , keys []string ) {
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 ) {
334
341
if keys == nil {
335
342
sorter := sorterPool .Get ().(* sorter )
336
343
// Using defer here, since the returned keys from the
@@ -340,6 +347,10 @@ func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
340
347
keys = sorter .Keys (h )
341
348
}
342
349
for _ , k := range keys {
350
+ if excludeKeys [k ] {
351
+ continue
352
+ }
353
+
343
354
vv := h [k ]
344
355
k , ascii := lowerHeader (k )
345
356
if ! ascii {
0 commit comments