Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Cookie.cs
Go to the documentation of this file.
6using System.Text;
7
8namespace System.Net;
9
11[TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
12public sealed class Cookie
13{
14 internal static readonly char[] PortSplitDelimiters = new char[3] { ' ', ',', '"' };
15
16 internal static readonly char[] ReservedToName = new char[6] { '\t', '\r', '\n', '=', ';', ',' };
17
18 internal static readonly char[] ReservedToValue = new char[2] { ';', ',' };
19
20 private string m_comment = string.Empty;
21
23
25
26 private bool m_discard;
27
28 private string m_domain = string.Empty;
29
30 private bool m_domain_implicit = true;
31
33
34 private string m_name = string.Empty;
35
36 private string m_path = string.Empty;
37
38 private bool m_path_implicit = true;
39
40 private string m_port = string.Empty;
41
42 private bool m_port_implicit = true;
43
44 private int[] m_port_list;
45
46 private bool m_secure;
47
49 private bool m_httpOnly;
50
52
53 private string m_value = string.Empty;
54
55 private int m_version;
56
57 private string m_domainKey = string.Empty;
58
59 internal bool IsQuotedVersion;
60
61 internal bool IsQuotedDomain;
62
63 public string Comment
64 {
65 get
66 {
67 return m_comment;
68 }
70 set
71 {
72 m_comment = value ?? string.Empty;
73 }
74 }
75
77 {
78 get
79 {
80 return m_commentUri;
81 }
82 set
83 {
85 }
86 }
87
88 public bool HttpOnly
89 {
90 get
91 {
92 return m_httpOnly;
93 }
94 set
95 {
97 }
98 }
99
100 public bool Discard
101 {
102 get
103 {
104 return m_discard;
105 }
106 set
107 {
109 }
110 }
111
112 public string Domain
113 {
114 get
115 {
116 return m_domain;
117 }
119 set
120 {
121 m_domain = value ?? string.Empty;
122 m_domain_implicit = false;
123 m_domainKey = string.Empty;
124 }
125 }
126
127 internal bool DomainImplicit
128 {
129 get
130 {
131 return m_domain_implicit;
132 }
133 set
134 {
136 }
137 }
138
139 public bool Expired
140 {
141 get
142 {
144 {
145 return m_expires.ToLocalTime() <= DateTime.Now;
146 }
147 return false;
148 }
149 set
150 {
151 if (value)
152 {
154 }
155 }
156 }
157
159 {
160 get
161 {
162 return m_expires;
163 }
164 set
165 {
167 }
168 }
169
170 public string Name
171 {
172 get
173 {
174 return m_name;
175 }
176 set
177 {
178 if (string.IsNullOrEmpty(value) || !InternalSetName(value))
179 {
180 throw new CookieException(System.SR.Format(System.SR.net_cookie_attribute, "Name", (value == null) ? "<null>" : value));
181 }
182 }
183 }
184
185 public string Path
186 {
187 get
188 {
189 return m_path;
190 }
192 set
193 {
194 m_path = value ?? string.Empty;
195 m_path_implicit = false;
196 }
197 }
198
199 internal bool Plain => Variant == CookieVariant.Plain;
200
201 public string Port
202 {
203 get
204 {
205 return m_port;
206 }
208 set
209 {
210 m_port_implicit = false;
211 if (string.IsNullOrEmpty(value))
212 {
213 m_port = string.Empty;
214 return;
215 }
216 if (value[0] != '"' || value[value.Length - 1] != '"')
217 {
219 }
220 string[] array = value.Split(PortSplitDelimiters);
221 List<int> list = new List<int>();
222 for (int i = 0; i < array.Length; i++)
223 {
224 if (array[i] != string.Empty)
225 {
226 if (!int.TryParse(array[i], out var result))
227 {
229 }
230 if (result < 0 || result > 65535)
231 {
233 }
234 list.Add(result);
235 }
236 }
237 m_port_list = list.ToArray();
238 m_port = value;
239 m_version = 1;
241 }
242 }
243
244 internal int[]? PortList => m_port_list;
245
246 public bool Secure
247 {
248 get
249 {
250 return m_secure;
251 }
252 set
253 {
254 m_secure = value;
255 }
256 }
257
259
260 public string Value
261 {
262 get
263 {
264 return m_value;
265 }
267 set
268 {
269 m_value = value ?? string.Empty;
270 }
271 }
272
274
275 internal string DomainKey
276 {
277 get
278 {
280 {
281 return m_domainKey;
282 }
283 return Domain;
284 }
285 }
286
287 public int Version
288 {
289 get
290 {
291 return m_version;
292 }
293 set
294 {
295 if (value < 0)
296 {
297 throw new ArgumentOutOfRangeException("value");
298 }
300 if (value > 0 && m_cookieVariant < CookieVariant.Rfc2109)
301 {
303 }
304 }
305 }
306
307 [DynamicDependency("ToServerString")]
308 public Cookie()
309 {
310 }
311
312 [DynamicDependency("ToServerString")]
313 public Cookie(string name, string? value)
314 {
315 Name = name;
316 Value = value;
317 }
318
319 public Cookie(string name, string? value, string? path)
320 : this(name, value)
321 {
322 Path = path;
323 }
324
325 public Cookie(string name, string? value, string? path, string? domain)
326 : this(name, value, path)
327 {
328 Domain = domain;
329 }
330
331 internal bool InternalSetName(string value)
332 {
333 if (string.IsNullOrEmpty(value) || value[0] == '$' || value.IndexOfAny(ReservedToName) != -1 || value[0] == ' ' || value[value.Length - 1] == ' ')
334 {
335 m_name = string.Empty;
336 return false;
337 }
338 m_name = value;
339 return true;
340 }
341
366
367 private static bool IsDomainEqualToHost(string domain, string host)
368 {
369 if (host.Length + 1 == domain.Length)
370 {
371 return string.Compare(host, 0, domain, 1, host.Length, StringComparison.OrdinalIgnoreCase) == 0;
372 }
373 return false;
374 }
375
377 {
378 string host = uri.Host;
379 int port = uri.Port;
380 string absolutePath = uri.AbsolutePath;
381 bool flag = true;
382 if (setDefault)
383 {
384 if (Version == 0)
385 {
386 variant = CookieVariant.Plain;
387 }
388 else if (Version == 1 && variant == CookieVariant.Unknown)
389 {
390 variant = CookieVariant.Rfc2109;
391 }
393 }
394 if (string.IsNullOrEmpty(m_name) || m_name[0] == '$' || m_name.IndexOfAny(ReservedToName) != -1 || m_name[0] == ' ' || m_name[m_name.Length - 1] == ' ')
395 {
396 if (shouldThrow)
397 {
398 throw new CookieException(System.SR.Format(System.SR.net_cookie_attribute, "Name", (m_name == null) ? "<null>" : m_name));
399 }
400 return false;
401 }
402 if (m_value == null || ((m_value.Length <= 2 || m_value[0] != '"' || m_value[m_value.Length - 1] != '"') && m_value.IndexOfAny(ReservedToValue) != -1))
403 {
404 if (shouldThrow)
405 {
406 throw new CookieException(System.SR.Format(System.SR.net_cookie_attribute, "Value", (m_value == null) ? "<null>" : m_value));
407 }
408 return false;
409 }
410 if (Comment != null && (Comment.Length <= 2 || Comment[0] != '"' || Comment[Comment.Length - 1] != '"') && Comment.IndexOfAny(ReservedToValue) != -1)
411 {
412 if (shouldThrow)
413 {
415 }
416 return false;
417 }
418 if (Path != null && (Path.Length <= 2 || Path[0] != '"' || Path[Path.Length - 1] != '"') && Path.IndexOfAny(ReservedToValue) != -1)
419 {
420 if (shouldThrow)
421 {
423 }
424 return false;
425 }
427 {
428 m_domain = host;
429 }
430 else
431 {
433 {
434 string text = m_domain;
435 if (!DomainCharsTest(text))
436 {
437 if (shouldThrow)
438 {
439 throw new CookieException(System.SR.Format(System.SR.net_cookie_attribute, "Domain", (text == null) ? "<null>" : text));
440 }
441 return false;
442 }
443 if (text[0] != '.')
444 {
445 text = "." + text;
446 }
447 int num = host.IndexOf('.');
448 if (isLocalDomain && string.Equals(localDomain, text, StringComparison.OrdinalIgnoreCase))
449 {
450 flag = true;
451 }
452 else if (text.IndexOf('.', 1, text.Length - 2) == -1)
453 {
455 {
456 flag = false;
457 }
458 }
459 else if (variant == CookieVariant.Plain)
460 {
461 if (!IsDomainEqualToHost(text, host) && (host.Length <= text.Length || string.Compare(host, host.Length - text.Length, text, 0, text.Length, StringComparison.OrdinalIgnoreCase) != 0))
462 {
463 flag = false;
464 }
465 }
466 else if ((num == -1 || text.Length != host.Length - num || string.Compare(host, num, text, 0, text.Length, StringComparison.OrdinalIgnoreCase) != 0) && !IsDomainEqualToHost(text, host))
467 {
468 flag = false;
469 }
470 if (flag)
471 {
472 m_domainKey = text.ToLowerInvariant();
473 }
474 }
475 else if (!string.Equals(host, m_domain, StringComparison.OrdinalIgnoreCase))
476 {
477 flag = false;
478 }
479 if (!flag)
480 {
481 if (shouldThrow)
482 {
484 }
485 return false;
486 }
487 }
489 {
490 switch (m_cookieVariant)
491 {
492 case CookieVariant.Plain:
493 {
494 int length;
495 if (absolutePath.Length == 0 || absolutePath[0] != '/' || (length = absolutePath.LastIndexOf('/')) == 0)
496 {
497 m_path = "/";
498 }
499 else
500 {
501 m_path = absolutePath.Substring(0, length);
502 }
503 break;
504 }
505 case CookieVariant.Rfc2109:
506 m_path = absolutePath.Substring(0, absolutePath.LastIndexOf('/'));
507 break;
508 default:
509 m_path = absolutePath.Substring(0, absolutePath.LastIndexOf('/') + 1);
510 break;
511 }
512 }
513 if (setDefault && !m_port_implicit && m_port.Length == 0)
514 {
515 m_port_list = new int[1] { port };
516 }
517 if (!m_port_implicit)
518 {
519 flag = false;
520 int[] port_list = m_port_list;
521 foreach (int num2 in port_list)
522 {
523 if (num2 == port)
524 {
525 flag = true;
526 break;
527 }
528 }
529 if (!flag)
530 {
531 if (shouldThrow)
532 {
534 }
535 return false;
536 }
537 }
538 return true;
539 }
540
541 private static bool DomainCharsTest(string name)
542 {
543 if (name == null || name.Length == 0)
544 {
545 return false;
546 }
547 foreach (char c in name)
548 {
549 if (c >= '0' && c <= '9')
550 {
551 continue;
552 }
553 switch (c)
554 {
555 case '-':
556 case '.':
557 case 'a':
558 case 'b':
559 case 'c':
560 case 'd':
561 case 'e':
562 case 'f':
563 case 'g':
564 case 'h':
565 case 'i':
566 case 'j':
567 case 'k':
568 case 'l':
569 case 'm':
570 case 'n':
571 case 'o':
572 case 'p':
573 case 'q':
574 case 'r':
575 case 's':
576 case 't':
577 case 'u':
578 case 'v':
579 case 'w':
580 case 'x':
581 case 'y':
582 case 'z':
583 continue;
584 }
585 if ((c < 'A' || c > 'Z') && c != '_')
586 {
587 return false;
588 }
589 }
590 return true;
591 }
592
593 public override bool Equals([NotNullWhen(true)] object? comparand)
594 {
595 if (comparand is Cookie cookie && string.Equals(Name, cookie.Name, StringComparison.OrdinalIgnoreCase) && string.Equals(Value, cookie.Value, StringComparison.Ordinal) && string.Equals(Path, cookie.Path, StringComparison.Ordinal) && string.Equals(Domain, cookie.Domain, StringComparison.OrdinalIgnoreCase))
596 {
597 return Version == cookie.Version;
598 }
599 return false;
600 }
601
602 public override int GetHashCode()
603 {
604 return (Name + "=" + Value + ";" + Path + "; " + Domain + "; " + Version).GetHashCode();
605 }
606
613
614 internal void ToString(StringBuilder sb)
615 {
616 int length = sb.Length;
617 if (Version != 0)
618 {
619 sb.Append("$Version=");
620 if (IsQuotedVersion)
621 {
622 sb.Append('"');
623 }
626 handler.AppendFormatted(m_version);
627 sb.Append(invariantInfo, ref handler);
628 if (IsQuotedVersion)
629 {
630 sb.Append('"');
631 }
632 sb.Append("; ");
633 }
634 sb.Append(Name).Append('=').Append(Value);
635 if (!Plain)
636 {
637 if (!m_path_implicit && m_path.Length > 0)
638 {
639 sb.Append("; $Path=");
640 sb.Append(m_path);
641 }
642 if (!m_domain_implicit && m_domain.Length > 0)
643 {
644 sb.Append("; $Domain=");
645 if (IsQuotedDomain)
646 {
647 sb.Append('"');
648 }
649 sb.Append(m_domain);
650 if (IsQuotedDomain)
651 {
652 sb.Append('"');
653 }
654 }
655 }
656 if (!m_port_implicit)
657 {
658 sb.Append("; $Port");
659 if (m_port.Length > 0)
660 {
661 sb.Append('=');
662 sb.Append(m_port);
663 }
664 }
665 int length2 = sb.Length;
666 if (length2 == 1 + length && sb[length] == '=')
667 {
669 }
670 }
671
672 internal string ToServerString()
673 {
674 string text = Name + "=" + Value;
675 if (m_comment != null && m_comment.Length > 0)
676 {
677 text = text + "; Comment=" + m_comment;
678 }
679 if (m_commentUri != null)
680 {
681 text = text + "; CommentURL=\"" + m_commentUri.ToString() + "\"";
682 }
683 if (m_discard)
684 {
685 text += "; Discard";
686 }
687 if (!m_domain_implicit && m_domain != null && m_domain.Length > 0)
688 {
689 text = text + "; Domain=" + m_domain;
690 }
692 {
693 int num = (int)(Expires.ToLocalTime() - DateTime.Now).TotalSeconds;
694 if (num < 0)
695 {
696 num = 0;
697 }
698 text = text + "; Max-Age=" + num.ToString(NumberFormatInfo.InvariantInfo);
699 }
700 if (!m_path_implicit && m_path != null && m_path.Length > 0)
701 {
702 text = text + "; Path=" + m_path;
703 }
704 if (!Plain && !m_port_implicit && m_port != null && m_port.Length > 0)
705 {
706 text = text + "; Port=" + m_port;
707 }
708 if (m_version > 0)
709 {
710 text = text + "; Version=" + m_version.ToString(NumberFormatInfo.InvariantInfo);
711 }
712 if (!(text == "="))
713 {
714 return text;
715 }
716 return null;
717 }
718}
string Comment
Definition Cookie.cs:64
override int GetHashCode()
Definition Cookie.cs:602
bool VerifySetDefaults(CookieVariant variant, Uri uri, bool isLocalDomain, string localDomain, bool setDefault, bool shouldThrow)
Definition Cookie.cs:376
Cookie(string name, string? value, string? path)
Definition Cookie.cs:319
static bool DomainCharsTest(string name)
Definition Cookie.cs:541
DateTime Expires
Definition Cookie.cs:159
string m_name
Definition Cookie.cs:34
string m_path
Definition Cookie.cs:36
bool m_path_implicit
Definition Cookie.cs:38
Cookie Clone()
Definition Cookie.cs:342
DateTime TimeStamp
Definition Cookie.cs:258
bool IsQuotedDomain
Definition Cookie.cs:61
static readonly char[] ReservedToValue
Definition Cookie.cs:18
static readonly char[] PortSplitDelimiters
Definition Cookie.cs:14
string DomainKey
Definition Cookie.cs:276
Cookie(string name, string? value, string? path, string? domain)
Definition Cookie.cs:325
bool m_domain_implicit
Definition Cookie.cs:30
override bool Equals([NotNullWhen(true)] object? comparand)
Definition Cookie.cs:593
bool IsQuotedVersion
Definition Cookie.cs:59
string m_value
Definition Cookie.cs:53
bool m_port_implicit
Definition Cookie.cs:42
CookieVariant m_cookieVariant
Definition Cookie.cs:24
string ToServerString()
Definition Cookie.cs:672
int[] m_port_list
Definition Cookie.cs:44
bool DomainImplicit
Definition Cookie.cs:128
DateTime m_timeStamp
Definition Cookie.cs:51
string m_domainKey
Definition Cookie.cs:57
override string ToString()
Definition Cookie.cs:607
int?[] PortList
Definition Cookie.cs:244
void ToString(StringBuilder sb)
Definition Cookie.cs:614
static bool IsDomainEqualToHost(string domain, string host)
Definition Cookie.cs:367
static readonly char[] ReservedToName
Definition Cookie.cs:16
string m_domain
Definition Cookie.cs:28
Cookie(string name, string? value)
Definition Cookie.cs:313
string m_comment
Definition Cookie.cs:20
DateTime m_expires
Definition Cookie.cs:32
bool InternalSetName(string value)
Definition Cookie.cs:331
string m_port
Definition Cookie.cs:40
static string net_cookie_attribute
Definition SR.cs:36
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)
StringBuilder Append(char value, int repeatCount)
string Host
Definition Uri.cs:441
string AbsolutePath
Definition Uri.cs:239
override string ToString()
Definition Uri.cs:1119
int Port
Definition Uri.cs:453
static readonly DateTime MinValue
Definition DateTime.cs:35
static DateTime Now
Definition DateTime.cs:103
DateTime ToLocalTime()
Definition DateTime.cs:1068