Skip to content

Commit de1483d

Browse files
authored
fix(http1): use append for repeat trailer values in encoder (#4118)
The chunked trailer encoder built allowed_trailers with HeaderMap::insert, which overwrites prior values for the same name. When a user sets the same trailer field multiple times, only the last value was written to the wire. This mirrors the decode-side fix (#4107): use append so duplicate trailer values are preserved on encode, matching how regular headers are emitted.
1 parent 08c3416 commit de1483d

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/proto/h1/encode.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl Encoder {
181181

182182
if allowed_set.contains(name) {
183183
if is_valid_trailer_field(name) {
184-
allowed_trailers.insert(name, value);
184+
allowed_trailers.append(name, value);
185185
} else {
186186
debug!("trailer field is not valid: {}", &name);
187187
}
@@ -567,6 +567,32 @@ mod tests {
567567
);
568568
}
569569

570+
#[test]
571+
fn chunked_with_duplicate_trailer_values() {
572+
let encoder = Encoder::chunked();
573+
let trailers = vec![HeaderName::from_static("chunky-trailer")];
574+
let encoder = encoder.into_chunked_with_trailing_fields(trailers);
575+
576+
let mut headers = HeaderMap::new();
577+
headers.append(
578+
HeaderName::from_static("chunky-trailer"),
579+
HeaderValue::from_static("first"),
580+
);
581+
headers.append(
582+
HeaderName::from_static("chunky-trailer"),
583+
HeaderValue::from_static("second"),
584+
);
585+
586+
let buf1 = encoder.encode_trailers::<&[u8]>(headers, false).unwrap();
587+
588+
let mut dst = Vec::new();
589+
dst.put(buf1);
590+
assert_eq!(
591+
dst,
592+
b"0\r\nchunky-trailer: first\r\nchunky-trailer: second\r\n\r\n"
593+
);
594+
}
595+
570596
#[test]
571597
fn chunked_with_no_trailer_header() {
572598
let encoder = Encoder::chunked();

0 commit comments

Comments
 (0)