Skip to content

Commit 38a718b

Browse files
committed
Preencoded header values
1 parent faff20c commit 38a718b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/StaticWebAssetsSdk/Tasks/Data/StaticWebAssetEndpointResponseHeader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,15 @@ internal static string ToMetadataValue(
142142
var header = headers[i];
143143
writer.WriteStartObject();
144144
writer.WritePropertyName(NamePropertyName);
145-
writer.WriteStringValue(header.Name);
145+
var preEncoded = WellKnownResponseHeaders.TryGetPreEncodedHeaderName(header.Name);
146+
if (preEncoded.HasValue)
147+
{
148+
writer.WriteStringValue(preEncoded.Value);
149+
}
150+
else
151+
{
152+
writer.WriteStringValue(header.Name);
153+
}
146154
writer.WritePropertyName(ValuePropertyName);
147155
writer.WriteStringValue(header.Value);
148156
writer.WriteEndObject();

src/StaticWebAssetsSdk/Tasks/Data/WellKnownResponseHeaders.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#nullable disable
55

6+
using System.Text.Json;
7+
68
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
79

810
internal static class WellKnownResponseHeaders
@@ -17,6 +19,16 @@ internal static class WellKnownResponseHeaders
1719
public const string LastModified = "Last-Modified";
1820
public const string Vary = "Vary";
1921

22+
// Pre-encoded property names for high-performance JSON serialization
23+
public static readonly JsonEncodedText AcceptRangesPropertyName = JsonEncodedText.Encode(AcceptRanges);
24+
public static readonly JsonEncodedText CacheControlPropertyName = JsonEncodedText.Encode(CacheControl);
25+
public static readonly JsonEncodedText ContentEncodingPropertyName = JsonEncodedText.Encode(ContentEncoding);
26+
public static readonly JsonEncodedText ContentLengthPropertyName = JsonEncodedText.Encode(ContentLength);
27+
public static readonly JsonEncodedText ContentTypePropertyName = JsonEncodedText.Encode(ContentType);
28+
public static readonly JsonEncodedText ETagPropertyName = JsonEncodedText.Encode(ETag);
29+
public static readonly JsonEncodedText LastModifiedPropertyName = JsonEncodedText.Encode(LastModified);
30+
public static readonly JsonEncodedText VaryPropertyName = JsonEncodedText.Encode(Vary);
31+
2032
public static string TryGetInternedHeaderName(ReadOnlySpan<byte> headerNameSpan)
2133
{
2234
return headerNameSpan.Length switch
@@ -40,4 +52,26 @@ public static string TryGetInternedHeaderName(ReadOnlySpan<byte> headerNameSpan)
4052
_ => null
4153
};
4254
}
55+
56+
public static JsonEncodedText? TryGetPreEncodedHeaderName(string name) =>
57+
name?.Length switch
58+
{
59+
4 => name[0] switch
60+
{
61+
'E' when string.Equals(name, ETag, StringComparison.Ordinal) => ETagPropertyName,
62+
'V' when string.Equals(name, Vary, StringComparison.Ordinal) => VaryPropertyName,
63+
_ => null
64+
},
65+
12 when string.Equals(name, ContentType, StringComparison.Ordinal) => ContentTypePropertyName,
66+
13 => name[0] switch
67+
{
68+
'A' when string.Equals(name, AcceptRanges, StringComparison.Ordinal) => AcceptRangesPropertyName,
69+
'C' when string.Equals(name, CacheControl, StringComparison.Ordinal) => CacheControlPropertyName,
70+
'L' when string.Equals(name, LastModified, StringComparison.Ordinal) => LastModifiedPropertyName,
71+
_ => null
72+
},
73+
14 when string.Equals(name, ContentLength, StringComparison.Ordinal) => ContentLengthPropertyName,
74+
16 when string.Equals(name, ContentEncoding, StringComparison.Ordinal) => ContentEncodingPropertyName,
75+
_ => null
76+
};
4377
}

0 commit comments

Comments
 (0)