| | | 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.Buffers; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace System.Net |
| | | 12 | | { |
| | | 13 | | [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 |
| | | 14 | | public enum CookieVariant |
| | | 15 | | { |
| | | 16 | | Unknown, |
| | | 17 | | Plain, |
| | | 18 | | Rfc2109, |
| | | 19 | | Rfc2965, |
| | | 20 | | Default = Rfc2109 |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | // Cookie class |
| | | 24 | | // |
| | | 25 | | // Adheres to RFC 2965 |
| | | 26 | | // |
| | | 27 | | // Currently, only represents client-side cookies. The cookie classes know |
| | | 28 | | // how to parse a set-cookie format string, but not a cookie format string |
| | | 29 | | // (e.g. "Cookie: $Version=1; name=value; $Path=/foo; $Secure") |
| | | 30 | | [Serializable] |
| | | 31 | | [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5 |
| | | 32 | | public sealed class Cookie |
| | | 33 | | { |
| | | 34 | | // NOTE: these two constants must change together. |
| | | 35 | | internal const int MaxSupportedVersion = 1; |
| | | 36 | | internal const string MaxSupportedVersionString = "1"; |
| | | 37 | | |
| | | 38 | | internal const string SeparatorLiteral = "; "; |
| | | 39 | | internal const char EqualsLiteral = '='; |
| | | 40 | | internal const string QuotesLiteral = "\""; |
| | | 41 | | internal const string SpecialAttributeLiteral = "$"; |
| | | 42 | | |
| | 0 | 43 | | internal static readonly char[] PortSplitDelimiters = new char[] { ' ', ',', '\"' }; |
| | | 44 | | // Space (' ') should be reserved as well per RFCs, but major web browsers support it and some web sites use it |
| | 0 | 45 | | private static readonly SearchValues<char> s_reservedToNameChars = SearchValues.Create("\t\0\r\n=;,"); |
| | | 46 | | |
| | 0 | 47 | | private static readonly SearchValues<char> s_domainChars = |
| | 0 | 48 | | SearchValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"); |
| | | 49 | | |
| | 0 | 50 | | private string m_comment = string.Empty; // Do not rename (binary serialization) |
| | | 51 | | private Uri? m_commentUri; // Do not rename (binary serialization) |
| | 0 | 52 | | private CookieVariant m_cookieVariant = CookieVariant.Plain; // Do not rename (binary serialization) |
| | | 53 | | private bool m_discard; // Do not rename (binary serialization) |
| | 0 | 54 | | private string m_domain = string.Empty; // Do not rename (binary serialization) |
| | 0 | 55 | | private bool m_domain_implicit = true; // Do not rename (binary serialization) |
| | 0 | 56 | | private DateTime m_expires = DateTime.MinValue; // Do not rename (binary serialization) |
| | 0 | 57 | | private string m_name = string.Empty; // Do not rename (binary serialization) |
| | 0 | 58 | | private string m_path = string.Empty; // Do not rename (binary serialization) |
| | 0 | 59 | | private bool m_path_implicit = true; // Do not rename (binary serialization) |
| | 0 | 60 | | private string m_port = string.Empty; // Do not rename (binary serialization) |
| | 0 | 61 | | private bool m_port_implicit = true; // Do not rename (binary serialization) |
| | | 62 | | private int[]? m_port_list; // Do not rename (binary serialization) |
| | | 63 | | private bool m_secure; // Do not rename (binary serialization) |
| | | 64 | | [System.Runtime.Serialization.OptionalField] |
| | 0 | 65 | | private bool m_httpOnly = false; // Do not rename (binary serialization) |
| | 0 | 66 | | private DateTime m_timeStamp = DateTime.UtcNow; // Do not rename (binary serialization) |
| | 0 | 67 | | private string m_value = string.Empty; // Do not rename (binary serialization) |
| | | 68 | | private int m_version; // Do not rename (binary serialization) |
| | | 69 | | |
| | | 70 | | private string? m_domainKey; // Do not rename (binary serialization) |
| | | 71 | | |
| | | 72 | | #pragma warning disable 0649 // set via reflection by CookieParser: https://github.com/dotnet/runtime/issues/19348 |
| | | 73 | | internal bool IsQuotedVersion; // Do not rename (binary serialization) |
| | | 74 | | internal bool IsQuotedDomain; // Do not rename (binary serialization) |
| | | 75 | | #pragma warning restore 0649 |
| | | 76 | | |
| | | 77 | | #if DEBUG |
| | | 78 | | static Cookie() |
| | 0 | 79 | | { |
| | 0 | 80 | | Debug.Assert(MaxSupportedVersion.ToString(NumberFormatInfo.InvariantInfo).Equals(MaxSupportedVersionString, |
| | 0 | 81 | | } |
| | | 82 | | #endif |
| | | 83 | | |
| | | 84 | | // These DynamicDependency attributes are a workaround for https://github.com/dotnet/runtime/issues/19348. |
| | | 85 | | // HttpListener uses the non-public ToServerString, which isn't used by anything else in this assembly, |
| | | 86 | | // and which accesses other internals and can't be moved to HttpListener (at least not without incurring |
| | | 87 | | // functional differences). However, once we do our initial System.Net.Primitives build and ToServerString |
| | | 88 | | // survives to it, we no longer want the DynamicDependencyAttribute to remain around, so that ToServerString |
| | | 89 | | // can be trimmed out if the relevant functionality from HttpListener isn't used when performing whole-app |
| | | 90 | | // analysis. As such, when trimming System.Net.Primitives, we build the assembly with ILLinkKeepDepAttributes=f |
| | | 91 | | // such that when this assembly is compiled, ToServerString will remain but the DynamicDependency attributes |
| | | 92 | | // will be removed. This hack will need to be revisited if anything else in the assembly starts using |
| | | 93 | | // DynamicDependencyAttribute. |
| | | 94 | | // https://github.com/mono/linker/issues/802 |
| | | 95 | | |
| | | 96 | | [DynamicDependency("ToServerString")] |
| | 0 | 97 | | public Cookie() |
| | 0 | 98 | | { |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | [DynamicDependency("ToServerString")] // Workaround for https://github.com/dotnet/runtime/issues/19348 |
| | 0 | 102 | | public Cookie(string name, string? value) |
| | 0 | 103 | | { |
| | 0 | 104 | | Name = name; |
| | 0 | 105 | | Value = value; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public Cookie(string name, string? value, string? path) |
| | 0 | 109 | | : this(name, value) |
| | 0 | 110 | | { |
| | 0 | 111 | | Path = path; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public Cookie(string name, string? value, string? path, string? domain) |
| | 0 | 115 | | : this(name, value, path) |
| | 0 | 116 | | { |
| | 0 | 117 | | Domain = domain; |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | [AllowNull] |
| | | 121 | | public string Comment |
| | | 122 | | { |
| | | 123 | | get |
| | 0 | 124 | | { |
| | 0 | 125 | | return m_comment; |
| | 0 | 126 | | } |
| | | 127 | | set |
| | 0 | 128 | | { |
| | 0 | 129 | | m_comment = value ?? string.Empty; |
| | 0 | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | public Uri? CommentUri |
| | | 134 | | { |
| | | 135 | | get |
| | 0 | 136 | | { |
| | 0 | 137 | | return m_commentUri; |
| | 0 | 138 | | } |
| | | 139 | | set |
| | 0 | 140 | | { |
| | 0 | 141 | | m_commentUri = value; |
| | 0 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | |
| | | 146 | | public bool HttpOnly |
| | | 147 | | { |
| | | 148 | | get |
| | 0 | 149 | | { |
| | 0 | 150 | | return m_httpOnly; |
| | 0 | 151 | | } |
| | | 152 | | set |
| | 0 | 153 | | { |
| | 0 | 154 | | m_httpOnly = value; |
| | 0 | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | |
| | | 159 | | public bool Discard |
| | | 160 | | { |
| | | 161 | | get |
| | 0 | 162 | | { |
| | 0 | 163 | | return m_discard; |
| | 0 | 164 | | } |
| | | 165 | | set |
| | 0 | 166 | | { |
| | 0 | 167 | | m_discard = value; |
| | 0 | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | [AllowNull] |
| | | 172 | | public string Domain |
| | | 173 | | { |
| | | 174 | | get |
| | 0 | 175 | | { |
| | 0 | 176 | | return m_domain; |
| | 0 | 177 | | } |
| | | 178 | | set |
| | 0 | 179 | | { |
| | 0 | 180 | | SetDomainAndKey(value ?? string.Empty); |
| | 0 | 181 | | m_domain_implicit = false; |
| | 0 | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | internal bool DomainImplicit |
| | | 186 | | { |
| | | 187 | | get |
| | 0 | 188 | | { |
| | 0 | 189 | | return m_domain_implicit; |
| | 0 | 190 | | } |
| | | 191 | | set |
| | 0 | 192 | | { |
| | 0 | 193 | | m_domain_implicit = value; |
| | 0 | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public bool Expired |
| | | 198 | | { |
| | | 199 | | get |
| | 0 | 200 | | { |
| | 0 | 201 | | return (m_expires != DateTime.MinValue) && (m_expires.ToUniversalTime() <= DateTime.UtcNow); |
| | 0 | 202 | | } |
| | | 203 | | set |
| | 0 | 204 | | { |
| | 0 | 205 | | if (value) |
| | 0 | 206 | | { |
| | 0 | 207 | | m_expires = DateTime.UtcNow; |
| | 0 | 208 | | } |
| | 0 | 209 | | } |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public DateTime Expires |
| | | 213 | | { |
| | | 214 | | get |
| | 0 | 215 | | { |
| | 0 | 216 | | return m_expires; |
| | 0 | 217 | | } |
| | | 218 | | set |
| | 0 | 219 | | { |
| | 0 | 220 | | m_expires = value; |
| | 0 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public string Name |
| | | 225 | | { |
| | | 226 | | get |
| | 0 | 227 | | { |
| | 0 | 228 | | return m_name; |
| | 0 | 229 | | } |
| | | 230 | | set |
| | 0 | 231 | | { |
| | 0 | 232 | | if (string.IsNullOrEmpty(value) || !InternalSetName(value)) |
| | 0 | 233 | | { |
| | 0 | 234 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, "Name", value ?? "<null>")); |
| | | 235 | | } |
| | 0 | 236 | | } |
| | | 237 | | } |
| | | 238 | | internal bool InternalSetName(string? value) |
| | 0 | 239 | | { |
| | 0 | 240 | | if (string.IsNullOrEmpty(value) |
| | 0 | 241 | | || value.StartsWith('$') |
| | 0 | 242 | | || value.StartsWith(' ') |
| | 0 | 243 | | || value.EndsWith(' ') |
| | 0 | 244 | | || value.AsSpan().ContainsAny(s_reservedToNameChars)) |
| | 0 | 245 | | { |
| | 0 | 246 | | m_name = string.Empty; |
| | 0 | 247 | | return false; |
| | | 248 | | } |
| | 0 | 249 | | m_name = value; |
| | 0 | 250 | | return true; |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | [AllowNull] |
| | | 254 | | public string Path |
| | | 255 | | { |
| | | 256 | | get |
| | 0 | 257 | | { |
| | 0 | 258 | | return m_path; |
| | 0 | 259 | | } |
| | | 260 | | set |
| | 0 | 261 | | { |
| | 0 | 262 | | m_path = value ?? string.Empty; |
| | 0 | 263 | | m_path_implicit = false; |
| | 0 | 264 | | } |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | internal bool Plain |
| | | 268 | | { |
| | | 269 | | get |
| | 0 | 270 | | { |
| | 0 | 271 | | return Variant == CookieVariant.Plain; |
| | 0 | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | internal Cookie Clone() |
| | 0 | 276 | | { |
| | 0 | 277 | | Cookie clonedCookie = new Cookie(m_name, m_value); |
| | | 278 | | |
| | | 279 | | // Copy over all the properties from the original cookie |
| | 0 | 280 | | if (!m_port_implicit) |
| | 0 | 281 | | { |
| | 0 | 282 | | clonedCookie.Port = m_port; |
| | 0 | 283 | | } |
| | 0 | 284 | | if (!m_path_implicit) |
| | 0 | 285 | | { |
| | 0 | 286 | | clonedCookie.Path = m_path; |
| | 0 | 287 | | } |
| | 0 | 288 | | clonedCookie.m_domain = m_domain; |
| | | 289 | | |
| | | 290 | | // If the domain in the original cookie was implicit, we should preserve that property |
| | 0 | 291 | | clonedCookie.DomainImplicit = m_domain_implicit; |
| | 0 | 292 | | clonedCookie.m_domainKey = m_domainKey; |
| | 0 | 293 | | clonedCookie.m_timeStamp = m_timeStamp; |
| | 0 | 294 | | clonedCookie.Comment = m_comment; |
| | 0 | 295 | | clonedCookie.CommentUri = m_commentUri; |
| | 0 | 296 | | clonedCookie.HttpOnly = m_httpOnly; |
| | 0 | 297 | | clonedCookie.Discard = m_discard; |
| | 0 | 298 | | clonedCookie.Expires = m_expires; |
| | 0 | 299 | | clonedCookie.Version = m_version; |
| | 0 | 300 | | clonedCookie.Secure = m_secure; |
| | | 301 | | |
| | | 302 | | // The variant is set when we set properties like port/version. So, |
| | | 303 | | // we should copy over the variant from the original cookie after |
| | | 304 | | // we set all other properties |
| | 0 | 305 | | clonedCookie.m_cookieVariant = m_cookieVariant; |
| | | 306 | | |
| | 0 | 307 | | return clonedCookie; |
| | 0 | 308 | | } |
| | | 309 | | |
| | | 310 | | private void SetDomainAndKey(string domain) |
| | 0 | 311 | | { |
| | 0 | 312 | | m_domain = domain; |
| | 0 | 313 | | m_domainKey = CookieComparer.StripLeadingDot(m_domain).ToString().ToLowerInvariant(); |
| | 0 | 314 | | } |
| | | 315 | | |
| | | 316 | | // Implements Domain Matching following RFC 6265 rules with a special handling for single-label domains. |
| | | 317 | | // The method assumes that 'domain' has been stripped of its optional leading dot and converted to lower case. |
| | | 318 | | // It checks if the condition defined in https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 is met: |
| | | 319 | | // [rephrased] 'host' domain-matches 'domain' if at least one of the following conditions hold: |
| | | 320 | | // - 'domain' and 'host' are identical. |
| | | 321 | | // - All of the following conditions hold: |
| | | 322 | | // * 'domain' is a suffix of 'host' |
| | | 323 | | // * The last character of 'host' that is not included in 'domain' is a "." |
| | | 324 | | // * 'host' is a host name (i.e., not an IP address). |
| | | 325 | | // Beside the RFC 6265 rules, an extra condition is included for compatibility: |
| | | 326 | | // in case 'domain' is a single-label domain, an exact match is required. |
| | | 327 | | // This is to avoid matching top-level domains, for example "test.com" should not match "com", |
| | | 328 | | // however this does not prevent matching multi-label public suffixes, eg. "co.uk". |
| | | 329 | | // Note that browsers handle this by validating against the Public Suffix List (https://publicsuffix.org/) |
| | | 330 | | // which is a behavior under standardization in the latest RFC drafts: https://datatracker.ietf.org/doc/draft-ie |
| | | 331 | | private static bool HostMatchesDomain(ReadOnlySpan<char> host, ReadOnlySpan<char> domain) |
| | 0 | 332 | | { |
| | 0 | 333 | | if (!host.EndsWith(domain, StringComparison.Ordinal)) |
| | 0 | 334 | | { |
| | 0 | 335 | | return false; |
| | | 336 | | } |
| | | 337 | | |
| | | 338 | | // The last character of the 'host' that is not included in the domain |
| | 0 | 339 | | int idxOfSeparator = host.Length - domain.Length - 1; |
| | 0 | 340 | | if (idxOfSeparator < 0) |
| | 0 | 341 | | { |
| | | 342 | | // 'host' and 'domain' are equal |
| | 0 | 343 | | Debug.Assert(idxOfSeparator == -1); |
| | 0 | 344 | | return true; |
| | | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | return host[idxOfSeparator] is '.' // The last character of 'host' that is not included in 'domain' is a "." |
| | 0 | 348 | | && domain.Contains('.') // In case of single-label domains, there should be an exact match. |
| | 0 | 349 | | && !IPAddress.IsValid(host); // If host is an IP address, there should be an exact match. |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | // According to spec we must assume default values for attributes but still |
| | | 353 | | // keep in mind that we must not include them into the requests. |
| | | 354 | | // We also check the validity of all attributes based on the version and variant (read RFC) |
| | | 355 | | // |
| | | 356 | | // To work properly this function must be called after cookie construction with |
| | | 357 | | // default (response) URI. |
| | | 358 | | internal void VerifyAndSetDefaults(CookieVariant variant, Uri uri) |
| | 0 | 359 | | { |
| | 0 | 360 | | string host = uri.Host; |
| | 0 | 361 | | int port = uri.Port; |
| | 0 | 362 | | string path = uri.AbsolutePath; |
| | | 363 | | |
| | | 364 | | // Set Variant. If version is zero => reset cookie to Version0 style |
| | 0 | 365 | | if (Version == 0) |
| | 0 | 366 | | { |
| | 0 | 367 | | variant = CookieVariant.Plain; |
| | 0 | 368 | | } |
| | 0 | 369 | | else if (Version == 1 && variant == CookieVariant.Unknown) |
| | 0 | 370 | | { |
| | | 371 | | // Since we don't expose Variant to an app, set it to Default |
| | 0 | 372 | | variant = CookieVariant.Default; |
| | 0 | 373 | | } |
| | 0 | 374 | | m_cookieVariant = variant; |
| | | 375 | | |
| | | 376 | | // Check the name |
| | 0 | 377 | | if (string.IsNullOrEmpty(m_name) || |
| | 0 | 378 | | m_name.StartsWith('$') || |
| | 0 | 379 | | m_name.StartsWith(' ') || |
| | 0 | 380 | | m_name.EndsWith(' ') || |
| | 0 | 381 | | m_name.AsSpan().ContainsAny(s_reservedToNameChars)) |
| | 0 | 382 | | { |
| | 0 | 383 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, "Name", m_name ?? "<null>")); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | // Check the value |
| | 0 | 387 | | if (m_value == null || m_value.ContainsAny('\r', '\n', '\0') || |
| | 0 | 388 | | (!(m_value.Length > 2 && m_value.StartsWith('\"') && m_value.EndsWith('\"')) && m_value.AsSpan().Contain |
| | 0 | 389 | | { |
| | 0 | 390 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, "Value", m_value ?? "<null>")); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | // Check Comment syntax |
| | 0 | 394 | | if (Comment != null && !(Comment.Length > 2 && Comment.StartsWith('\"') && Comment.EndsWith('\"')) |
| | 0 | 395 | | && (Comment.AsSpan().ContainsAny(';', ','))) |
| | 0 | 396 | | { |
| | 0 | 397 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.CommentAttributeName, Comment) |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | // Check Path syntax |
| | 0 | 401 | | if (Path != null && !(Path.Length > 2 && Path.StartsWith('\"') && Path.EndsWith('\"')) |
| | 0 | 402 | | && (Path.AsSpan().ContainsAny(';', ','))) |
| | 0 | 403 | | { |
| | 0 | 404 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PathAttributeName, Path)); |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | // Check/set domain |
| | | 408 | | // |
| | | 409 | | // If domain is implicit => assume a) uri is valid, b) just set domain to uri hostname. |
| | 0 | 410 | | if (m_domain_implicit) |
| | 0 | 411 | | { |
| | 0 | 412 | | SetDomainAndKey(host); |
| | 0 | 413 | | } |
| | | 414 | | else |
| | 0 | 415 | | { |
| | 0 | 416 | | Debug.Assert(m_domainKey is not null); |
| | | 417 | | |
| | 0 | 418 | | if (!IsValidDomainName(m_domainKey) || !HostMatchesDomain(host, m_domainKey)) |
| | 0 | 419 | | { |
| | 0 | 420 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.DomainAttributeName, m_dom |
| | | 421 | | } |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | // Check/Set Path |
| | 0 | 425 | | if (m_path_implicit) |
| | 0 | 426 | | { |
| | | 427 | | // This code assumes that the URI path is always valid and contains at least one '/'. |
| | 0 | 428 | | switch (m_cookieVariant) |
| | | 429 | | { |
| | | 430 | | case CookieVariant.Plain: |
| | | 431 | | // As per RFC6265 5.1.4. (https://tools.ietf.org/html/rfc6265#section-5.1.4): |
| | | 432 | | // | 2. If the uri-path is empty or if the first character of the uri- |
| | | 433 | | // | path is not a %x2F ("/") character, output %x2F ("/") and skip |
| | | 434 | | // | the remaining steps. |
| | | 435 | | // | 3. If the uri-path contains no more than one %x2F ("/") character, |
| | | 436 | | // | output %x2F ("/") and skip the remaining step. |
| | | 437 | | // Note: Normally Uri.AbsolutePath contains at least one "/" after parsing, |
| | | 438 | | // but it's possible construct Uri with an empty path using a custom UriParser |
| | | 439 | | int lastSlash; |
| | 0 | 440 | | if (!path.StartsWith('/') || (lastSlash = path.LastIndexOf('/')) == 0) |
| | 0 | 441 | | { |
| | 0 | 442 | | m_path = "/"; |
| | 0 | 443 | | break; |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | // | 4. Output the characters of the uri-path from the first character up |
| | | 447 | | // | to, but not including, the right-most %x2F ("/"). |
| | 0 | 448 | | m_path = path.Substring(0, lastSlash); |
| | 0 | 449 | | break; |
| | | 450 | | case CookieVariant.Rfc2109: |
| | 0 | 451 | | m_path = path.Substring(0, path.LastIndexOf('/')); // May be empty |
| | 0 | 452 | | break; |
| | | 453 | | |
| | | 454 | | case CookieVariant.Rfc2965: |
| | | 455 | | default: |
| | | 456 | | // NOTE: this code is not resilient against future versions with different 'Path' semantics. |
| | 0 | 457 | | m_path = path.Substring(0, path.LastIndexOf('/') + 1); |
| | 0 | 458 | | break; |
| | | 459 | | } |
| | 0 | 460 | | } |
| | | 461 | | |
| | | 462 | | // Set the default port if Port attribute was present but had no value. |
| | 0 | 463 | | if (!m_port_implicit && m_port.Length == 0) |
| | 0 | 464 | | { |
| | 0 | 465 | | m_port_list = new int[1] { port }; |
| | 0 | 466 | | } |
| | | 467 | | |
| | 0 | 468 | | if (!m_port_implicit) |
| | 0 | 469 | | { |
| | | 470 | | // Port must match against the one from the uri. |
| | 0 | 471 | | bool valid = false; |
| | 0 | 472 | | foreach (int p in m_port_list!) |
| | 0 | 473 | | { |
| | 0 | 474 | | if (p == port) |
| | 0 | 475 | | { |
| | 0 | 476 | | valid = true; |
| | 0 | 477 | | break; |
| | | 478 | | } |
| | 0 | 479 | | } |
| | 0 | 480 | | if (!valid) |
| | 0 | 481 | | { |
| | 0 | 482 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, m_port) |
| | | 483 | | } |
| | 0 | 484 | | } |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | // Very primitive test to make sure that the name does not have illegal characters |
| | | 488 | | // as per RFC 952 (relaxed on first char could be a digit and string can have '_'). |
| | | 489 | | private static bool IsValidDomainName(ReadOnlySpan<char> name) => |
| | 0 | 490 | | !name.IsEmpty && |
| | 0 | 491 | | !name.ContainsAnyExcept(s_domainChars); |
| | | 492 | | |
| | | 493 | | [AllowNull] |
| | | 494 | | public string Port |
| | | 495 | | { |
| | | 496 | | get |
| | 0 | 497 | | { |
| | 0 | 498 | | return m_port; |
| | 0 | 499 | | } |
| | | 500 | | set |
| | 0 | 501 | | { |
| | 0 | 502 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 503 | | { |
| | | 504 | | // "Port" is present but has no value. |
| | | 505 | | // Therefore; the effective port value is implicit. |
| | 0 | 506 | | m_port_implicit = true; |
| | 0 | 507 | | m_port = string.Empty; |
| | 0 | 508 | | } |
| | | 509 | | else |
| | 0 | 510 | | { |
| | | 511 | | // "Port" value is present, so we use the provided value rather than an implicit one. |
| | 0 | 512 | | m_port_implicit = false; |
| | | 513 | | // Parse port list |
| | 0 | 514 | | if (!value.StartsWith('\"') || !value.EndsWith('\"')) |
| | 0 | 515 | | { |
| | 0 | 516 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, val |
| | | 517 | | } |
| | 0 | 518 | | string[] ports = value.Split(PortSplitDelimiters, StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 519 | | int[] parsedPorts = new int[ports.Length]; |
| | | 520 | | |
| | 0 | 521 | | for (int i = 0; i < ports.Length; ++i) |
| | 0 | 522 | | { |
| | 0 | 523 | | if (!int.TryParse(ports[i], out int port)) |
| | 0 | 524 | | { |
| | 0 | 525 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | // valid values for port 0 - 0xFFFF |
| | 0 | 529 | | if ((port < 0) || (port > 0xFFFF)) |
| | 0 | 530 | | { |
| | 0 | 531 | | throw new CookieException(SR.Format(SR.net_cookie_attribute, CookieFields.PortAttributeName, |
| | | 532 | | } |
| | | 533 | | |
| | 0 | 534 | | parsedPorts[i] = port; |
| | 0 | 535 | | } |
| | 0 | 536 | | m_port_list = parsedPorts; |
| | 0 | 537 | | m_port = value; |
| | 0 | 538 | | m_version = MaxSupportedVersion; |
| | 0 | 539 | | m_cookieVariant = CookieVariant.Rfc2965; |
| | 0 | 540 | | } |
| | 0 | 541 | | } |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | |
| | | 545 | | internal int[]? PortList |
| | | 546 | | { |
| | | 547 | | get |
| | 0 | 548 | | { |
| | | 549 | | // PortList will be null if Port Attribute was omitted in the response. |
| | 0 | 550 | | return m_port_list; |
| | 0 | 551 | | } |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | public bool Secure |
| | | 555 | | { |
| | | 556 | | get |
| | 0 | 557 | | { |
| | 0 | 558 | | return m_secure; |
| | 0 | 559 | | } |
| | | 560 | | set |
| | 0 | 561 | | { |
| | 0 | 562 | | m_secure = value; |
| | 0 | 563 | | } |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | public DateTime TimeStamp |
| | | 567 | | { |
| | | 568 | | get |
| | 0 | 569 | | { |
| | 0 | 570 | | return m_timeStamp; |
| | 0 | 571 | | } |
| | | 572 | | } |
| | | 573 | | |
| | | 574 | | [AllowNull] |
| | | 575 | | public string Value |
| | | 576 | | { |
| | | 577 | | get |
| | 0 | 578 | | { |
| | 0 | 579 | | return m_value; |
| | 0 | 580 | | } |
| | | 581 | | set |
| | 0 | 582 | | { |
| | 0 | 583 | | m_value = value ?? string.Empty; |
| | 0 | 584 | | } |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | internal CookieVariant Variant |
| | | 588 | | { |
| | | 589 | | get |
| | 0 | 590 | | { |
| | 0 | 591 | | return m_cookieVariant; |
| | 0 | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | // _domainKey member is set internally in VerifySetDefaults(). |
| | | 596 | | // If it is not set then verification function was not called; |
| | | 597 | | // this should never happen. |
| | | 598 | | internal string DomainKey |
| | | 599 | | { |
| | | 600 | | get |
| | 0 | 601 | | { |
| | 0 | 602 | | return m_domain_implicit ? Domain : m_domainKey!; |
| | 0 | 603 | | } |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | public int Version |
| | | 607 | | { |
| | | 608 | | get |
| | 0 | 609 | | { |
| | 0 | 610 | | return m_version; |
| | 0 | 611 | | } |
| | | 612 | | set |
| | 0 | 613 | | { |
| | 0 | 614 | | ArgumentOutOfRangeException.ThrowIfNegative(value); |
| | 0 | 615 | | m_version = value; |
| | 0 | 616 | | if (value > 0 && m_cookieVariant < CookieVariant.Rfc2109) |
| | 0 | 617 | | { |
| | 0 | 618 | | m_cookieVariant = CookieVariant.Rfc2109; |
| | 0 | 619 | | } |
| | 0 | 620 | | } |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | public override bool Equals([NotNullWhen(true)] object? comparand) |
| | 0 | 624 | | { |
| | 0 | 625 | | return comparand is Cookie other |
| | 0 | 626 | | && string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase) |
| | 0 | 627 | | && string.Equals(Value, other.Value, StringComparison.Ordinal) |
| | 0 | 628 | | && string.Equals(Path, other.Path, StringComparison.Ordinal) |
| | 0 | 629 | | && CookieComparer.EqualDomains(Domain, other.Domain) |
| | 0 | 630 | | && (Version == other.Version); |
| | 0 | 631 | | } |
| | | 632 | | |
| | | 633 | | public override int GetHashCode() |
| | 0 | 634 | | { |
| | 0 | 635 | | return HashCode.Combine( |
| | 0 | 636 | | StringComparer.OrdinalIgnoreCase.GetHashCode(Name), |
| | 0 | 637 | | StringComparer.Ordinal.GetHashCode(Value), |
| | 0 | 638 | | StringComparer.Ordinal.GetHashCode(Path), |
| | 0 | 639 | | StringComparer.OrdinalIgnoreCase.GetHashCode(DomainKey), |
| | 0 | 640 | | Version); |
| | 0 | 641 | | } |
| | | 642 | | |
| | | 643 | | public override string ToString() |
| | 0 | 644 | | { |
| | 0 | 645 | | StringBuilder sb = StringBuilderCache.Acquire(); |
| | 0 | 646 | | ToString(sb); |
| | 0 | 647 | | return StringBuilderCache.GetStringAndRelease(sb); |
| | 0 | 648 | | } |
| | | 649 | | |
| | | 650 | | internal void ToString(StringBuilder sb) |
| | 0 | 651 | | { |
| | 0 | 652 | | int beforeLength = sb.Length; |
| | | 653 | | |
| | | 654 | | // Add the Cookie version if necessary. |
| | 0 | 655 | | if (Version != 0) |
| | 0 | 656 | | { |
| | 0 | 657 | | sb.Append(SpecialAttributeLiteral + CookieFields.VersionAttributeName + EqualsLiteral); // const strings |
| | 0 | 658 | | if (IsQuotedVersion) sb.Append('"'); |
| | 0 | 659 | | sb.Append(NumberFormatInfo.InvariantInfo, $"{m_version}"); |
| | 0 | 660 | | if (IsQuotedVersion) sb.Append('"'); |
| | 0 | 661 | | sb.Append(SeparatorLiteral); |
| | 0 | 662 | | } |
| | | 663 | | |
| | | 664 | | // Add the Cookie Name=Value pair. |
| | 0 | 665 | | sb.Append(Name).Append(EqualsLiteral).Append(Value); |
| | | 666 | | |
| | 0 | 667 | | if (!Plain) |
| | 0 | 668 | | { |
| | | 669 | | // Add the Path if necessary. |
| | 0 | 670 | | if (!m_path_implicit && m_path.Length > 0) |
| | 0 | 671 | | { |
| | 0 | 672 | | sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.PathAttributeName + EqualsLitera |
| | 0 | 673 | | sb.Append(m_path); |
| | 0 | 674 | | } |
| | | 675 | | |
| | | 676 | | // Add the Domain if necessary. |
| | 0 | 677 | | if (!m_domain_implicit && m_domain.Length > 0) |
| | 0 | 678 | | { |
| | 0 | 679 | | sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.DomainAttributeName + EqualsLite |
| | 0 | 680 | | if (IsQuotedDomain) sb.Append('"'); |
| | 0 | 681 | | sb.Append(m_domain); |
| | 0 | 682 | | if (IsQuotedDomain) sb.Append('"'); |
| | 0 | 683 | | } |
| | 0 | 684 | | } |
| | | 685 | | |
| | | 686 | | // Add the Port if necessary. |
| | 0 | 687 | | if (!m_port_implicit) |
| | 0 | 688 | | { |
| | 0 | 689 | | sb.Append(SeparatorLiteral + SpecialAttributeLiteral + CookieFields.PortAttributeName); // const strings |
| | 0 | 690 | | if (m_port.Length > 0) |
| | 0 | 691 | | { |
| | 0 | 692 | | sb.Append(EqualsLiteral); |
| | 0 | 693 | | sb.Append(m_port); |
| | 0 | 694 | | } |
| | 0 | 695 | | } |
| | | 696 | | |
| | | 697 | | // Check to see whether the only thing we added was "=", and if so, |
| | | 698 | | // remove it so that we leave the StringBuilder unchanged in contents. |
| | 0 | 699 | | int afterLength = sb.Length; |
| | 0 | 700 | | if (afterLength == (1 + beforeLength) && sb[beforeLength] == '=') |
| | 0 | 701 | | { |
| | 0 | 702 | | sb.Length = beforeLength; |
| | 0 | 703 | | } |
| | 0 | 704 | | } |
| | | 705 | | |
| | | 706 | | internal string? ToServerString() |
| | 0 | 707 | | { |
| | 0 | 708 | | string result = Name + EqualsLiteral + Value; |
| | 0 | 709 | | if (m_comment != null && m_comment.Length > 0) |
| | 0 | 710 | | { |
| | 0 | 711 | | result += SeparatorLiteral + CookieFields.CommentAttributeName + EqualsLiteral + m_comment; |
| | 0 | 712 | | } |
| | 0 | 713 | | if (m_commentUri != null) |
| | 0 | 714 | | { |
| | 0 | 715 | | result += SeparatorLiteral + CookieFields.CommentUrlAttributeName + EqualsLiteral + QuotesLiteral + m_co |
| | 0 | 716 | | } |
| | 0 | 717 | | if (m_discard) |
| | 0 | 718 | | { |
| | 0 | 719 | | result += SeparatorLiteral + CookieFields.DiscardAttributeName; |
| | 0 | 720 | | } |
| | 0 | 721 | | if (!m_domain_implicit && m_domain != null && m_domain.Length > 0) |
| | 0 | 722 | | { |
| | 0 | 723 | | result += SeparatorLiteral + CookieFields.DomainAttributeName + EqualsLiteral + m_domain; |
| | 0 | 724 | | } |
| | 0 | 725 | | if (Expires != DateTime.MinValue) |
| | 0 | 726 | | { |
| | 0 | 727 | | int seconds = (int)(Expires.ToUniversalTime() - DateTime.UtcNow).TotalSeconds; |
| | 0 | 728 | | if (seconds < 0) |
| | 0 | 729 | | { |
| | | 730 | | // This means that the cookie has already expired. Set Max-Age to 0 |
| | | 731 | | // so that the client will discard the cookie immediately. |
| | 0 | 732 | | seconds = 0; |
| | 0 | 733 | | } |
| | 0 | 734 | | result += SeparatorLiteral + CookieFields.MaxAgeAttributeName + EqualsLiteral + seconds.ToString(NumberF |
| | 0 | 735 | | } |
| | 0 | 736 | | if (!m_path_implicit && m_path != null && m_path.Length > 0) |
| | 0 | 737 | | { |
| | 0 | 738 | | result += SeparatorLiteral + CookieFields.PathAttributeName + EqualsLiteral + m_path; |
| | 0 | 739 | | } |
| | 0 | 740 | | if (!Plain && !m_port_implicit && m_port != null && m_port.Length > 0) |
| | 0 | 741 | | { |
| | | 742 | | // QuotesLiteral are included in _port. |
| | 0 | 743 | | result += SeparatorLiteral + CookieFields.PortAttributeName + EqualsLiteral + m_port; |
| | 0 | 744 | | } |
| | 0 | 745 | | if (m_version > 0) |
| | 0 | 746 | | { |
| | 0 | 747 | | result += SeparatorLiteral + CookieFields.VersionAttributeName + EqualsLiteral + m_version.ToString(Numb |
| | 0 | 748 | | } |
| | 0 | 749 | | return result == "=" ? null : result; |
| | 0 | 750 | | } |
| | | 751 | | } |
| | | 752 | | } |
| | | 753 | | |