@@ -57,65 +57,69 @@ export interface MhtmlHeaders extends Iterable<[string, string]> {
5757}
5858
5959export class Headers implements MhtmlHeaders {
60- #raw = new Map < string , string [ ] > ( ) ;
60+ // keyed by lowercased name; the original name casing (first seen) is
61+ // preserved for iteration while lookups are case-insensitive, per RFC 5322
62+ #raw = new Map < string , { key : string ; values : string [ ] } > ( ) ;
6163
6264 [ Symbol . iterator ] ( ) : Iterator < [ string , string ] > {
6365 return this . entries ( ) ;
6466 }
6567
6668 append ( key : string , value : string ) : void {
67- const list = this . #raw. get ( key ) ;
68- if ( list === undefined ) {
69- this . #raw. set ( key , [ value ] ) ;
69+ const entry = this . #raw. get ( key . toLowerCase ( ) ) ;
70+ if ( entry === undefined ) {
71+ this . #raw. set ( key . toLowerCase ( ) , { key , values : [ value ] } ) ;
7072 } else {
71- list . push ( value ) ;
73+ entry . values . push ( value ) ;
7274 }
7375 }
7476
7577 * entries ( delim : string = ", " ) : IterableIterator < [ string , string ] > {
76- for ( const [ key , values ] of this . #raw. entries ( ) ) {
78+ for ( const { key, values } of this . #raw. values ( ) ) {
7779 yield [ key , values . join ( delim ) ] ;
7880 }
7981 }
8082
8183 * entriesAll ( ) : IterableIterator < [ string , string ] > {
82- for ( const [ key , values ] of this . #raw. entries ( ) ) {
84+ for ( const { key, values } of this . #raw. values ( ) ) {
8385 for ( const val of values ) {
8486 yield [ key , val ] ;
8587 }
8688 }
8789 }
8890
8991 get ( key : string , delim : string = ", " ) : string | null {
90- const vals = this . #raw. get ( key ) ;
91- if ( vals === undefined ) {
92+ const entry = this . #raw. get ( key . toLowerCase ( ) ) ;
93+ if ( entry === undefined ) {
9294 return null ;
9395 } else {
94- return vals . join ( delim ) ;
96+ return entry . values . join ( delim ) ;
9597 }
9698 }
9799
98100 getAll ( key : string ) : string [ ] {
99- return this . #raw. get ( key ) ?? [ ] ;
101+ return this . #raw. get ( key . toLowerCase ( ) ) ?. values ?? [ ] ;
100102 }
101103
102104 has ( key : string ) : boolean {
103- return this . #raw. has ( key ) ;
105+ return this . #raw. has ( key . toLowerCase ( ) ) ;
104106 }
105107
106- keys ( ) : Iterable < string > {
107- return this . #raw. keys ( ) ;
108+ * keys ( ) : IterableIterator < string > {
109+ for ( const { key } of this . #raw. values ( ) ) {
110+ yield key ;
111+ }
108112 }
109113
110114 * values ( delim : string = ", " ) : IterableIterator < string > {
111- for ( const vals of this . #raw. values ( ) ) {
112- yield vals . join ( delim ) ;
115+ for ( const { values } of this . #raw. values ( ) ) {
116+ yield values . join ( delim ) ;
113117 }
114118 }
115119
116120 * valuesAll ( ) : IterableIterator < string > {
117- for ( const vals of this . #raw. values ( ) ) {
118- yield * vals ;
121+ for ( const { values } of this . #raw. values ( ) ) {
122+ yield * values ;
119123 }
120124 }
121125}
0 commit comments