Skip to content

Commit 831523d

Browse files
authored
Expand set of known header names and values (#117989)
* Collection expressions for known values * Simplify new() * Expand set of known header names and values * Also add text/plain without a space
1 parent 3c94a47 commit 831523d

File tree

5 files changed

+169
-112
lines changed

5 files changed

+169
-112
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,25 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
207207
break;
208208

209209
case 10:
210-
switch (contentTypeValue[0])
210+
switch (contentTypeValue[6])
211211
{
212-
case (byte)'t': candidate = "text/plain"; break; // [t]ext/plain
213-
case (byte)'i': candidate = "image/jpeg"; break; // [i]mage/jpeg
212+
case (byte)'l': candidate = "text/plain"; break; // text/p[l]ain
213+
case (byte)'j': candidate = "image/jpeg"; break; // image/[j]peg
214+
case (byte)'w': candidate = "image/webp"; break; // image/[w]ebp
214215
}
215216
break;
216217

218+
case 13:
219+
candidate = "image/svg+xml"; // image/svg+xml
220+
break;
221+
217222
case 15:
218223
switch (contentTypeValue[12])
219224
{
220225
case (byte)'p': candidate = "application/pdf"; break; // application/[p]df
221226
case (byte)'x': candidate = "application/xml"; break; // application/[x]ml
222227
case (byte)'z': candidate = "application/zip"; break; // application/[z]ip
228+
case (byte)'i': candidate = "text/javascript"; break; // text/javascr[i]pt
223229
}
224230
break;
225231

@@ -231,6 +237,10 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
231237
}
232238
break;
233239

240+
case 17:
241+
candidate = "text/event-stream"; // text/event-stream
242+
break;
243+
234244
case 19:
235245
candidate = "multipart/form-data"; // multipart/form-data
236246
break;
@@ -239,17 +249,47 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
239249
candidate = "application/javascript"; // application/javascript
240250
break;
241251

252+
case 23:
253+
switch (contentTypeValue[18])
254+
{
255+
case (byte)'u': candidate = "text/html;charset=utf-8"; break; // text/html;charset=[u]tf-8
256+
case (byte)'U': candidate = "text/html;charset=UTF-8"; break; // text/html;charset=[U]TF-8
257+
}
258+
break;
259+
242260
case 24:
243-
switch (contentTypeValue[19])
261+
switch (contentTypeValue[10] ^ contentTypeValue[19])
244262
{
245-
case (byte)'t': candidate = "application/octet-stream"; break; // application/octet-s[t]ream
246-
case (byte)'u': candidate = "text/html; charset=utf-8"; break; // text/html; charset=[u]tf-8
247-
case (byte)'U': candidate = "text/html; charset=UTF-8"; break; // text/html; charset=[U]TF-8
263+
case 'n' ^ 't': candidate = "application/octet-stream"; break; // applicatio[n]/octet-s[t]ream
264+
case ' ' ^ 'u': candidate = "text/html; charset=utf-8"; break; // text/html;[ ]charset=[u]tf-8
265+
case ' ' ^ 'U': candidate = "text/html; charset=UTF-8"; break; // text/html;[ ]charset=[U]TF-8
266+
case ';' ^ 'u': candidate = "text/plain;charset=utf-8"; break; // text/plain[;]charset=[u]tf-8
267+
case ';' ^ 'U': candidate = "text/plain;charset=UTF-8"; break; // text/plain[;]charset=[U]TF-8
248268
}
249269
break;
250270

251271
case 25:
252-
candidate = "text/plain; charset=utf-8"; // text/plain; charset=utf-8
272+
switch (contentTypeValue[20])
273+
{
274+
case (byte)'u': candidate = "text/plain; charset=utf-8"; break; // text/plain; charset=[u]tf-8
275+
case (byte)'U': candidate = "text/plain; charset=UTF-8"; break; // text/plain; charset=[U]TF-8
276+
}
277+
break;
278+
279+
case 29:
280+
switch (contentTypeValue[19])
281+
{
282+
case (byte)'I': candidate = "text/html; charset=ISO-8859-1"; break; // text/html; charset=[I]SO-8859-1
283+
case (byte)'i': candidate = "text/html; charset=iso-8859-1"; break; // text/html; charset=[i]so-8859-1
284+
}
285+
break;
286+
287+
case 30:
288+
switch (contentTypeValue[25])
289+
{
290+
case (byte)'u': candidate = "text/javascript; charset=utf-8"; break; // text/javascript; charset=[u]tf-8
291+
case (byte)'U': candidate = "text/javascript; charset=UTF-8"; break; // text/javascript; charset=[U]TF-8
292+
}
253293
break;
254294

255295
case 31:

src/libraries/System.Net.Http/src/System/Net/Http/Headers/KnownHeader.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ namespace System.Net.Http.Headers
88
{
99
internal sealed partial class KnownHeader
1010
{
11-
public KnownHeader(string name, int? http2StaticTableIndex = null, int? http3StaticTableIndex = null) :
12-
this(name, HttpHeaderType.Custom, parser: null, knownValues: null, http2StaticTableIndex, http3StaticTableIndex)
13-
{
14-
Debug.Assert(!string.IsNullOrEmpty(name));
15-
Debug.Assert(name[0] == ':' || HttpRuleParser.IsToken(name));
16-
}
11+
public KnownHeader(string name, string[]? knownValues = null, int? http2StaticTableIndex = null, int? http3StaticTableIndex = null)
12+
: this(name, HttpHeaderType.Custom, parser: null, knownValues, http2StaticTableIndex, http3StaticTableIndex)
13+
{ }
1714

1815
public KnownHeader(string name, HttpHeaderType headerType, HttpHeaderParser? parser, string[]? knownValues = null, int? http2StaticTableIndex = null, int? http3StaticTableIndex = null)
1916
{

0 commit comments

Comments
 (0)