/
HttpResponseHeaders.cs
172 lines (136 loc) · 6.82 KB
/
HttpResponseHeaders.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
namespace System.Net.Http.Headers
{
public sealed class HttpResponseHeaders : HttpHeaders
{
private const int AcceptRangesSlot = 0;
private const int ProxyAuthenticateSlot = 1;
private const int ServerSlot = 2;
private const int VarySlot = 3;
private const int WwwAuthenticateSlot = 4;
private const int NumCollectionsSlots = 5;
private object[]? _specialCollectionsSlots;
private HttpGeneralHeaders? _generalHeaders;
private readonly bool _containsTrailingHeaders;
#region Response Headers
private T GetSpecializedCollection<T>(int slot, Func<HttpResponseHeaders, T> creationFunc)
{
// 5 properties each lazily allocate a collection to store the value(s) for that property.
// Rather than having a field for each of these, store them untyped in an array that's lazily
// allocated. Then we only pay for the 45 bytes for those fields when any is actually accessed.
object[] collections = _specialCollectionsSlots ??= new object[NumCollectionsSlots];
return (T)(collections[slot] ??= creationFunc(this)!);
}
public HttpHeaderValueCollection<string> AcceptRanges =>
GetSpecializedCollection(AcceptRangesSlot, static thisRef => new HttpHeaderValueCollection<string>(KnownHeaders.AcceptRanges.Descriptor, thisRef));
public TimeSpan? Age
{
get { return HeaderUtilities.GetTimeSpanValue(KnownHeaders.Age.Descriptor, this); }
set { SetOrRemoveParsedValue(KnownHeaders.Age.Descriptor, value); }
}
public EntityTagHeaderValue? ETag
{
get { return (EntityTagHeaderValue?)GetSingleParsedValue(KnownHeaders.ETag.Descriptor); }
set { SetOrRemoveParsedValue(KnownHeaders.ETag.Descriptor, value); }
}
public Uri? Location
{
get { return (Uri?)GetSingleParsedValue(KnownHeaders.Location.Descriptor); }
set { SetOrRemoveParsedValue(KnownHeaders.Location.Descriptor, value); }
}
public HttpHeaderValueCollection<AuthenticationHeaderValue> ProxyAuthenticate =>
GetSpecializedCollection(ProxyAuthenticateSlot, static thisRef => new HttpHeaderValueCollection<AuthenticationHeaderValue>(KnownHeaders.ProxyAuthenticate.Descriptor, thisRef));
public RetryConditionHeaderValue? RetryAfter
{
get { return (RetryConditionHeaderValue?)GetSingleParsedValue(KnownHeaders.RetryAfter.Descriptor); }
set { SetOrRemoveParsedValue(KnownHeaders.RetryAfter.Descriptor, value); }
}
public HttpHeaderValueCollection<ProductInfoHeaderValue> Server =>
GetSpecializedCollection(ServerSlot, static thisRef => new HttpHeaderValueCollection<ProductInfoHeaderValue>(KnownHeaders.Server.Descriptor, thisRef));
public HttpHeaderValueCollection<string> Vary =>
GetSpecializedCollection(VarySlot, static thisRef => new HttpHeaderValueCollection<string>(KnownHeaders.Vary.Descriptor, thisRef));
public HttpHeaderValueCollection<AuthenticationHeaderValue> WwwAuthenticate =>
GetSpecializedCollection(WwwAuthenticateSlot, static thisRef => new HttpHeaderValueCollection<AuthenticationHeaderValue>(KnownHeaders.WWWAuthenticate.Descriptor, thisRef));
#endregion
#region General Headers
public CacheControlHeaderValue? CacheControl
{
get { return GeneralHeaders.CacheControl; }
set { GeneralHeaders.CacheControl = value; }
}
public HttpHeaderValueCollection<string> Connection
{
get { return GeneralHeaders.Connection; }
}
public bool? ConnectionClose
{
get { return HttpGeneralHeaders.GetConnectionClose(this, _generalHeaders); } // special-cased to avoid forcing _generalHeaders initialization
set { GeneralHeaders.ConnectionClose = value; }
}
public DateTimeOffset? Date
{
get { return GeneralHeaders.Date; }
set { GeneralHeaders.Date = value; }
}
public HttpHeaderValueCollection<NameValueHeaderValue> Pragma
{
get { return GeneralHeaders.Pragma; }
}
public HttpHeaderValueCollection<string> Trailer
{
get { return GeneralHeaders.Trailer; }
}
public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding
{
get { return GeneralHeaders.TransferEncoding; }
}
public bool? TransferEncodingChunked
{
get { return HttpGeneralHeaders.GetTransferEncodingChunked(this, _generalHeaders); } // special-cased to avoid forcing _generalHeaders initialization
set { GeneralHeaders.TransferEncodingChunked = value; }
}
public HttpHeaderValueCollection<ProductHeaderValue> Upgrade
{
get { return GeneralHeaders.Upgrade; }
}
public HttpHeaderValueCollection<ViaHeaderValue> Via
{
get { return GeneralHeaders.Via; }
}
public HttpHeaderValueCollection<WarningHeaderValue> Warning
{
get { return GeneralHeaders.Warning; }
}
#endregion
internal HttpResponseHeaders(bool containsTrailingHeaders = false)
: base(containsTrailingHeaders ? HttpHeaderType.All ^ HttpHeaderType.Request : HttpHeaderType.General | HttpHeaderType.Response | HttpHeaderType.Custom,
HttpHeaderType.Request)
{
_containsTrailingHeaders = containsTrailingHeaders;
}
internal bool ContainsTrailingHeaders => _containsTrailingHeaders;
internal override void AddHeaders(HttpHeaders sourceHeaders)
{
base.AddHeaders(sourceHeaders);
HttpResponseHeaders? sourceResponseHeaders = sourceHeaders as HttpResponseHeaders;
Debug.Assert(sourceResponseHeaders != null);
// Copy special values, but do not overwrite
if (sourceResponseHeaders._generalHeaders != null)
{
GeneralHeaders.AddSpecialsFrom(sourceResponseHeaders._generalHeaders);
}
}
internal override bool IsAllowedHeaderName(HeaderDescriptor descriptor)
{
if (!_containsTrailingHeaders)
return true;
KnownHeader? knownHeader = KnownHeaders.TryGetKnownHeader(descriptor.Name);
if (knownHeader == null)
return true;
return (knownHeader.HeaderType & HttpHeaderType.NonTrailing) == 0;
}
private HttpGeneralHeaders GeneralHeaders => _generalHeaders ??= new HttpGeneralHeaders(this);
}
}