@@ -32,6 +32,10 @@ const XML_UNESCAPE: Record<string, string> = {
32
32
'#39' : "'"
33
33
} ;
34
34
35
+ const NAME_RE = new RegExp ( '[;\\s]*([^=;, ]+)\\s*' , 'ys' ) ;
36
+ const QUOTED_RE = new RegExp ( '=\\s*("(?:\\\\\\\\|\\\\"|[^"])*")' , 'ys' ) ;
37
+ const UNQUOTED_RE = new RegExp ( '=\\s*([^;, ]*)' , 'ys' ) ;
38
+
35
39
/**
36
40
* Safe string that should not be escaped.
37
41
*/
@@ -161,6 +165,46 @@ export function escapeRegExp(string: string) {
161
165
return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
162
166
}
163
167
168
+ /**
169
+ * Extract HTTP header field parameters until the first comma.
170
+ */
171
+ export function headerParams ( value : string ) : { params : Record < string , string > ; remainder : string } {
172
+ const params : Record < string , string > = { } ;
173
+
174
+ const sticky = { offset : 0 , value} ;
175
+ while ( value . length > sticky . offset ) {
176
+ const nameMatch = stickyMatch ( sticky , NAME_RE ) ;
177
+ if ( nameMatch === null ) break ;
178
+ const name = nameMatch [ 1 ] ;
179
+
180
+ const quotedMatch = stickyMatch ( sticky , QUOTED_RE ) ;
181
+ if ( quotedMatch !== null ) {
182
+ params [ name ] ??= headerUnquote ( quotedMatch [ 1 ] ) ;
183
+ continue ;
184
+ }
185
+
186
+ const unquotedMatch = stickyMatch ( sticky , UNQUOTED_RE ) ;
187
+ if ( unquotedMatch !== null ) params [ name ] ??= unquotedMatch [ 1 ] ;
188
+ }
189
+
190
+ return { params, remainder : value . slice ( sticky . offset ) } ;
191
+ }
192
+
193
+ /**
194
+ * Quote HTTP header field parameter.
195
+ */
196
+ export function headerQuote ( value : string ) : string {
197
+ return '"' + value . replaceAll ( '\\' , '\\\\' ) . replaceAll ( '"' , '\\"' ) + '"' ;
198
+ }
199
+
200
+ /**
201
+ * Unquote HTTP header field parameter.
202
+ */
203
+ export function headerUnquote ( value : string ) : string {
204
+ if ( value . startsWith ( '"' ) !== true || value . endsWith ( '"' ) !== true ) return value ;
205
+ return value . slice ( 1 , - 1 ) . replaceAll ( '\\\\' , '\\' ) . replaceAll ( '\\"' , '"' ) ;
206
+ }
207
+
164
208
/**
165
209
* JSON pointers.
166
210
*/
0 commit comments