| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Net.Http.Headers |
| | | 7 | | { |
| | | 8 | | public sealed class HttpRequestHeaders : HttpHeaders |
| | | 9 | | { |
| | | 10 | | private const int AcceptSlot = 0; |
| | | 11 | | private const int AcceptCharsetSlot = 1; |
| | | 12 | | private const int AcceptEncodingSlot = 2; |
| | | 13 | | private const int AcceptLanguageSlot = 3; |
| | | 14 | | private const int IfMatchSlot = 4; |
| | | 15 | | private const int IfNoneMatchSlot = 5; |
| | | 16 | | private const int TransferEncodingSlot = 6; |
| | | 17 | | private const int UserAgentSlot = 7; |
| | | 18 | | private const int ExpectSlot = 8; |
| | | 19 | | private const int ProtocolSlot = 9; |
| | | 20 | | private const int NumCollectionsSlots = 10; |
| | | 21 | | |
| | | 22 | | private object?[]? _specialCollectionsSlots; |
| | | 23 | | private HttpGeneralHeaders? _generalHeaders; |
| | | 24 | | private bool _expectContinueSet; |
| | | 25 | | |
| | | 26 | | #region Request Headers |
| | | 27 | | |
| | | 28 | | private T GetSpecializedCollection<T>(int slot, Func<HttpRequestHeaders, T> creationFunc) |
| | 0 | 29 | | { |
| | | 30 | | // 8 properties each lazily allocate a collection to store the value(s) for that property. |
| | | 31 | | // Rather than having a field for each of these, store them untyped in an array that's lazily |
| | | 32 | | // allocated. Then we only pay for the 64 bytes for those fields when any is actually accessed. |
| | 0 | 33 | | _specialCollectionsSlots ??= new object[NumCollectionsSlots]; |
| | 0 | 34 | | return (T)(_specialCollectionsSlots[slot] ??= creationFunc(this)!); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public HttpHeaderValueCollection<MediaTypeWithQualityHeaderValue> Accept => |
| | 0 | 38 | | GetSpecializedCollection(AcceptSlot, static thisRef => new HttpHeaderValueCollection<MediaTypeWithQualityHea |
| | | 39 | | |
| | | 40 | | public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptCharset => |
| | 0 | 41 | | GetSpecializedCollection(AcceptCharsetSlot, static thisRef => new HttpHeaderValueCollection<StringWithQualit |
| | | 42 | | |
| | | 43 | | public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptEncoding => |
| | 0 | 44 | | GetSpecializedCollection(AcceptEncodingSlot, static thisRef => new HttpHeaderValueCollection<StringWithQuali |
| | | 45 | | |
| | | 46 | | public HttpHeaderValueCollection<StringWithQualityHeaderValue> AcceptLanguage => |
| | 0 | 47 | | GetSpecializedCollection(AcceptLanguageSlot, static thisRef => new HttpHeaderValueCollection<StringWithQuali |
| | | 48 | | |
| | | 49 | | public AuthenticationHeaderValue? Authorization |
| | | 50 | | { |
| | 0 | 51 | | get { return (AuthenticationHeaderValue?)GetSingleParsedValue(KnownHeaders.Authorization.Descriptor); } |
| | 0 | 52 | | set { SetOrRemoveParsedValue(KnownHeaders.Authorization.Descriptor, value); } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public bool? ExpectContinue |
| | | 56 | | { |
| | | 57 | | get |
| | 0 | 58 | | { |
| | 0 | 59 | | if (ContainsParsedValue(KnownHeaders.Expect.Descriptor, HeaderUtilities.ExpectContinue)) |
| | 0 | 60 | | { |
| | 0 | 61 | | return true; |
| | | 62 | | } |
| | 0 | 63 | | if (_expectContinueSet) |
| | 0 | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return null; |
| | 0 | 69 | | } |
| | | 70 | | set |
| | 0 | 71 | | { |
| | 0 | 72 | | if (value == true) |
| | 0 | 73 | | { |
| | 0 | 74 | | _expectContinueSet = true; |
| | 0 | 75 | | if (!ContainsParsedValue(KnownHeaders.Expect.Descriptor, HeaderUtilities.ExpectContinue)) |
| | 0 | 76 | | { |
| | 0 | 77 | | AddParsedValue(KnownHeaders.Expect.Descriptor, HeaderUtilities.ExpectContinue); |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | else |
| | 0 | 81 | | { |
| | 0 | 82 | | _expectContinueSet = value != null; |
| | | 83 | | // We intentionally ignore the return value. It's OK if "100-continue" wasn't in the store. |
| | 0 | 84 | | RemoveParsedValue(KnownHeaders.Expect.Descriptor, HeaderUtilities.ExpectContinue); |
| | 0 | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public string? From |
| | | 90 | | { |
| | 0 | 91 | | get { return (string?)GetSingleParsedValue(KnownHeaders.From.Descriptor); } |
| | | 92 | | set |
| | 0 | 93 | | { |
| | | 94 | | // Null and empty string are equivalent. In this case it means, remove the From header value (if any). |
| | 0 | 95 | | if (value == string.Empty) |
| | 0 | 96 | | { |
| | 0 | 97 | | value = null; |
| | 0 | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | CheckContainsNewLineOrNull(value); |
| | | 101 | | |
| | 0 | 102 | | SetOrRemoveParsedValue(KnownHeaders.From.Descriptor, value); |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public string? Host |
| | | 107 | | { |
| | 0 | 108 | | get { return (string?)GetSingleParsedValue(KnownHeaders.Host.Descriptor); } |
| | | 109 | | set |
| | 0 | 110 | | { |
| | | 111 | | // Null and empty string are equivalent. In this case it means, remove the Host header value (if any). |
| | 0 | 112 | | if (value == string.Empty) |
| | 0 | 113 | | { |
| | 0 | 114 | | value = null; |
| | 0 | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | if ((value != null) && (HttpRuleParser.GetHostLength(value, 0, false) != value.Length)) |
| | 0 | 118 | | { |
| | 0 | 119 | | throw new FormatException(SR.net_http_headers_invalid_host_header); |
| | | 120 | | } |
| | 0 | 121 | | SetOrRemoveParsedValue(KnownHeaders.Host.Descriptor, value); |
| | 0 | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public HttpHeaderValueCollection<EntityTagHeaderValue> IfMatch => |
| | 0 | 126 | | GetSpecializedCollection(IfMatchSlot, static thisRef => new HttpHeaderValueCollection<EntityTagHeaderValue>( |
| | | 127 | | |
| | | 128 | | public DateTimeOffset? IfModifiedSince |
| | | 129 | | { |
| | 0 | 130 | | get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.IfModifiedSince.Descriptor, this); } |
| | 0 | 131 | | set { SetOrRemoveParsedValue(KnownHeaders.IfModifiedSince.Descriptor, value); } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public HttpHeaderValueCollection<EntityTagHeaderValue> IfNoneMatch => |
| | 0 | 135 | | GetSpecializedCollection(IfNoneMatchSlot, static thisRef => new HttpHeaderValueCollection<EntityTagHeaderVal |
| | | 136 | | |
| | | 137 | | public RangeConditionHeaderValue? IfRange |
| | | 138 | | { |
| | 0 | 139 | | get { return (RangeConditionHeaderValue?)GetSingleParsedValue(KnownHeaders.IfRange.Descriptor); } |
| | 0 | 140 | | set { SetOrRemoveParsedValue(KnownHeaders.IfRange.Descriptor, value); } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public DateTimeOffset? IfUnmodifiedSince |
| | | 144 | | { |
| | 0 | 145 | | get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.IfUnmodifiedSince.Descriptor, this); } |
| | 0 | 146 | | set { SetOrRemoveParsedValue(KnownHeaders.IfUnmodifiedSince.Descriptor, value); } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public int? MaxForwards |
| | | 150 | | { |
| | | 151 | | get |
| | 0 | 152 | | { |
| | 0 | 153 | | object? storedValue = GetSingleParsedValue(KnownHeaders.MaxForwards.Descriptor); |
| | 0 | 154 | | if (storedValue != null) |
| | 0 | 155 | | { |
| | 0 | 156 | | return (int)storedValue; |
| | | 157 | | } |
| | 0 | 158 | | return null; |
| | 0 | 159 | | } |
| | 0 | 160 | | set { SetOrRemoveParsedValue(KnownHeaders.MaxForwards.Descriptor, value); } |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary>Gets or sets the value of the <see langword=":protocol" /> pseudo-header for an HTTP request.</summ |
| | | 164 | | /// <value>The value of the <see langword=":protocol" /> pseudo-header for an HTTP request.</value> |
| | | 165 | | public string? Protocol |
| | | 166 | | { |
| | 0 | 167 | | get => _specialCollectionsSlots is null ? null : (string?)_specialCollectionsSlots[ProtocolSlot]; |
| | | 168 | | set |
| | 0 | 169 | | { |
| | 0 | 170 | | CheckContainsNewLineOrNull(value); |
| | 0 | 171 | | _specialCollectionsSlots ??= new object[NumCollectionsSlots]; |
| | 0 | 172 | | _specialCollectionsSlots[ProtocolSlot] = value; |
| | 0 | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public AuthenticationHeaderValue? ProxyAuthorization |
| | | 177 | | { |
| | 0 | 178 | | get { return (AuthenticationHeaderValue?)GetSingleParsedValue(KnownHeaders.ProxyAuthorization.Descriptor); } |
| | 0 | 179 | | set { SetOrRemoveParsedValue(KnownHeaders.ProxyAuthorization.Descriptor, value); } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | public RangeHeaderValue? Range |
| | | 183 | | { |
| | 0 | 184 | | get { return (RangeHeaderValue?)GetSingleParsedValue(KnownHeaders.Range.Descriptor); } |
| | 0 | 185 | | set { SetOrRemoveParsedValue(KnownHeaders.Range.Descriptor, value); } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public Uri? Referrer |
| | | 189 | | { |
| | 0 | 190 | | get { return (Uri?)GetSingleParsedValue(KnownHeaders.Referer.Descriptor); } |
| | 0 | 191 | | set { SetOrRemoveParsedValue(KnownHeaders.Referer.Descriptor, value); } |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public HttpHeaderValueCollection<TransferCodingWithQualityHeaderValue> TE => |
| | 0 | 195 | | GetSpecializedCollection(TransferEncodingSlot, static thisRef => new HttpHeaderValueCollection<TransferCodin |
| | | 196 | | |
| | | 197 | | public HttpHeaderValueCollection<ProductInfoHeaderValue> UserAgent => |
| | 0 | 198 | | GetSpecializedCollection(UserAgentSlot, static thisRef => new HttpHeaderValueCollection<ProductInfoHeaderVal |
| | | 199 | | |
| | | 200 | | public HttpHeaderValueCollection<NameValueWithParametersHeaderValue> Expect => |
| | 0 | 201 | | GetSpecializedCollection(ExpectSlot, static thisRef => new HttpHeaderValueCollection<NameValueWithParameters |
| | | 202 | | |
| | | 203 | | #endregion |
| | | 204 | | |
| | | 205 | | #region General Headers |
| | | 206 | | |
| | | 207 | | public CacheControlHeaderValue? CacheControl |
| | | 208 | | { |
| | 0 | 209 | | get { return GeneralHeaders.CacheControl; } |
| | 0 | 210 | | set { GeneralHeaders.CacheControl = value; } |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | public HttpHeaderValueCollection<string> Connection |
| | | 214 | | { |
| | 0 | 215 | | get { return GeneralHeaders.Connection; } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public bool? ConnectionClose |
| | | 219 | | { |
| | 0 | 220 | | get { return HttpGeneralHeaders.GetConnectionClose(this, _generalHeaders); } // special-cased to avoid forci |
| | 0 | 221 | | set { GeneralHeaders.ConnectionClose = value; } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public DateTimeOffset? Date |
| | | 225 | | { |
| | 0 | 226 | | get { return GeneralHeaders.Date; } |
| | 0 | 227 | | set { GeneralHeaders.Date = value; } |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | public HttpHeaderValueCollection<NameValueHeaderValue> Pragma |
| | | 231 | | { |
| | 0 | 232 | | get { return GeneralHeaders.Pragma; } |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | public HttpHeaderValueCollection<string> Trailer |
| | | 236 | | { |
| | 0 | 237 | | get { return GeneralHeaders.Trailer; } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding |
| | | 241 | | { |
| | 0 | 242 | | get { return GeneralHeaders.TransferEncoding; } |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | public bool? TransferEncodingChunked |
| | | 246 | | { |
| | 0 | 247 | | get { return HttpGeneralHeaders.GetTransferEncodingChunked(this, _generalHeaders); } // special-cased to avo |
| | 0 | 248 | | set { GeneralHeaders.TransferEncodingChunked = value; } |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | public HttpHeaderValueCollection<ProductHeaderValue> Upgrade |
| | | 252 | | { |
| | 0 | 253 | | get { return GeneralHeaders.Upgrade; } |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | public HttpHeaderValueCollection<ViaHeaderValue> Via |
| | | 257 | | { |
| | 0 | 258 | | get { return GeneralHeaders.Via; } |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public HttpHeaderValueCollection<WarningHeaderValue> Warning |
| | | 262 | | { |
| | 0 | 263 | | get { return GeneralHeaders.Warning; } |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | #endregion |
| | | 267 | | |
| | | 268 | | internal HttpRequestHeaders() |
| | 1 | 269 | | : base(HttpHeaderType.General | HttpHeaderType.Request | HttpHeaderType.Custom, HttpHeaderType.Response) |
| | 1 | 270 | | { |
| | 1 | 271 | | } |
| | | 272 | | |
| | | 273 | | internal override void AddHeaders(HttpHeaders sourceHeaders) |
| | 0 | 274 | | { |
| | 0 | 275 | | base.AddHeaders(sourceHeaders); |
| | 0 | 276 | | HttpRequestHeaders? sourceRequestHeaders = sourceHeaders as HttpRequestHeaders; |
| | 0 | 277 | | Debug.Assert(sourceRequestHeaders != null); |
| | | 278 | | |
| | | 279 | | // Copy special values but do not overwrite. |
| | 0 | 280 | | if (sourceRequestHeaders._generalHeaders != null) |
| | 0 | 281 | | { |
| | 0 | 282 | | GeneralHeaders.AddSpecialsFrom(sourceRequestHeaders._generalHeaders); |
| | 0 | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | bool? expectContinue = ExpectContinue; |
| | 0 | 286 | | if (!expectContinue.HasValue) |
| | 0 | 287 | | { |
| | 0 | 288 | | ExpectContinue = sourceRequestHeaders.ExpectContinue; |
| | 0 | 289 | | } |
| | 0 | 290 | | } |
| | | 291 | | |
| | 0 | 292 | | private HttpGeneralHeaders GeneralHeaders => _generalHeaders ??= new HttpGeneralHeaders(this); |
| | | 293 | | } |
| | | 294 | | } |