@@ -92,6 +92,8 @@ type Decoder struct {
92
92
// saveBuf is previous data passed to Write which we weren't able
93
93
// to fully parse before. Unlike buf, we own this data.
94
94
saveBuf bytes.Buffer
95
+
96
+ firstField bool // processing the first field of the header block
95
97
}
96
98
97
99
// NewDecoder returns a new decoder with the provided maximum dynamic
@@ -101,6 +103,7 @@ func NewDecoder(maxDynamicTableSize uint32, emitFunc func(f HeaderField)) *Decod
101
103
d := & Decoder {
102
104
emit : emitFunc ,
103
105
emitEnabled : true ,
106
+ firstField : true ,
104
107
}
105
108
d .dynTab .table .init ()
106
109
d .dynTab .allowedMaxSize = maxDynamicTableSize
@@ -226,11 +229,15 @@ func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) {
226
229
return hf , nil
227
230
}
228
231
232
+ // Close declares that the decoding is complete and resets the Decoder
233
+ // to be reused again for a new header block. If there is any remaining
234
+ // data in the decoder's buffer, Close returns an error.
229
235
func (d * Decoder ) Close () error {
230
236
if d .saveBuf .Len () > 0 {
231
237
d .saveBuf .Reset ()
232
238
return DecodingError {errors .New ("truncated headers" )}
233
239
}
240
+ d .firstField = true
234
241
return nil
235
242
}
236
243
@@ -266,6 +273,7 @@ func (d *Decoder) Write(p []byte) (n int, err error) {
266
273
d .saveBuf .Write (d .buf )
267
274
return len (p ), nil
268
275
}
276
+ d .firstField = false
269
277
if err != nil {
270
278
break
271
279
}
@@ -391,7 +399,7 @@ func (d *Decoder) callEmit(hf HeaderField) error {
391
399
func (d * Decoder ) parseDynamicTableSizeUpdate () error {
392
400
// RFC 7541, sec 4.2: This dynamic table size update MUST occur at the
393
401
// beginning of the first header block following the change to the dynamic table size.
394
- if d .dynTab .size > 0 {
402
+ if ! d . firstField && d .dynTab .size > 0 {
395
403
return DecodingError {errors .New ("dynamic table size update MUST occur at the beginning of a header block" )}
396
404
}
397
405
0 commit comments