Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CookieContainer.cs
Go to the documentation of this file.
5using System.Text;
6
7namespace System.Net;
8
10[TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
11public class CookieContainer
12{
13 public const int DefaultCookieLimit = 300;
14
15 public const int DefaultPerDomainCookieLimit = 20;
16
17 public const int DefaultCookieLengthLimit = 4096;
18
19 private static readonly string s_fqdnMyDomain = CreateFqdnMyDomain();
20
21 private static readonly HeaderVariantInfo[] s_headerInfo = new HeaderVariantInfo[2]
22 {
23 new HeaderVariantInfo("Set-Cookie", CookieVariant.Rfc2109),
24 new HeaderVariantInfo("Set-Cookie2", CookieVariant.Rfc2965)
25 };
26
27 private readonly Hashtable m_domainTable = new Hashtable();
28
29 private int m_maxCookieSize = 4096;
30
31 private int m_maxCookies = 300;
32
33 private int m_maxCookiesPerDomain = 20;
34
35 private int m_count;
36
37 private readonly string m_fqdnMyDomain = s_fqdnMyDomain;
38
39 public int Capacity
40 {
41 get
42 {
43 return m_maxCookies;
44 }
45 set
46 {
47 if (value <= 0 || (value < m_maxCookiesPerDomain && m_maxCookiesPerDomain != int.MaxValue))
48 {
50 }
51 if (value < m_maxCookies)
52 {
54 AgeCookies(null);
55 }
57 }
58 }
59
60 public int Count => m_count;
61
62 public int MaxCookieSize
63 {
64 get
65 {
66 return m_maxCookieSize;
67 }
68 set
69 {
70 if (value <= 0)
71 {
72 throw new ArgumentOutOfRangeException("value");
73 }
75 }
76 }
77
79 {
80 get
81 {
83 }
84 set
85 {
86 if (value <= 0 || (value > m_maxCookies && value != int.MaxValue))
87 {
88 throw new ArgumentOutOfRangeException("value");
89 }
91 {
93 AgeCookies(null);
94 }
96 }
97 }
98
100 {
101 }
102
104 {
105 if (capacity <= 0)
106 {
107 throw new ArgumentException(System.SR.net_toosmall, "capacity");
108 }
110 }
111
113 : this(capacity)
114 {
116 {
117 throw new ArgumentOutOfRangeException("perDomainCapacity", System.SR.Format(System.SR.net_cookie_capacity_range, "PerDomainCapacity", 0, capacity));
118 }
120 if (maxCookieSize <= 0)
121 {
122 throw new ArgumentException(System.SR.net_toosmall, "maxCookieSize");
123 }
125 }
126
127 private static string CreateFqdnMyDomain()
128 {
129 string domainName = HostInformation.DomainName;
130 if (domainName == null || domainName.Length <= 1)
131 {
132 return string.Empty;
133 }
134 return "." + domainName;
135 }
136
137 public void Add(Cookie cookie)
138 {
139 if (cookie == null)
140 {
141 throw new ArgumentNullException("cookie");
142 }
143 if (cookie.Domain.Length == 0)
144 {
145 throw new ArgumentException(System.SR.Format(System.SR.net_emptystringcall, "cookie.Domain"), "cookie");
146 }
148 stringBuilder.Append(cookie.Secure ? "https" : "http").Append("://");
149 if (!cookie.DomainImplicit && cookie.Domain[0] == '.')
150 {
151 stringBuilder.Append('0');
152 }
153 stringBuilder.Append(cookie.Domain);
154 if (cookie.PortList != null)
155 {
156 stringBuilder.Append(':').Append(cookie.PortList[0]);
157 }
158 stringBuilder.Append(cookie.Path);
159 if (!Uri.TryCreate(stringBuilder.ToString(), UriKind.Absolute, out Uri result))
160 {
161 throw new CookieException(System.SR.Format(System.SR.net_cookie_attribute, "Domain", cookie.Domain));
162 }
163 Cookie cookie2 = cookie.Clone();
164 cookie2.VerifySetDefaults(cookie2.Variant, result, IsLocalDomain(result.Host), m_fqdnMyDomain, setDefault: true, shouldThrow: true);
165 Add(cookie2, throwOnError: true);
166 }
167
168 internal void Add(Cookie cookie, bool throwOnError)
169 {
170 if (cookie.Value.Length > m_maxCookieSize)
171 {
172 if (throwOnError)
173 {
175 }
176 return;
177 }
178 try
179 {
182 {
184 if (pathList == null)
185 {
186 pathList = (PathList)(m_domainTable[cookie.DomainKey] = new PathList());
187 }
188 }
189 int cookiesCount = pathList.GetCookiesCount();
192 {
194 if (cookieCollection == null)
195 {
198 }
199 }
200 if (cookie.Expired)
201 {
203 {
204 int num = cookieCollection.IndexOf(cookie);
205 if (num != -1)
206 {
207 cookieCollection.RemoveAt(num);
208 m_count--;
209 }
210 }
211 }
212 else
213 {
214 if ((cookiesCount >= m_maxCookiesPerDomain && !AgeCookies(cookie.DomainKey)) || (m_count >= m_maxCookies && !AgeCookies(null)))
215 {
216 return;
217 }
219 {
220 m_count += cookieCollection.InternalAdd(cookie, isStrict: true);
221 }
222 }
224 {
226 }
227 }
229 {
230 throw;
231 }
232 catch (Exception inner)
233 {
234 if (throwOnError)
235 {
237 }
238 }
239 }
240
241 private bool AgeCookies(string domain)
242 {
243 int num = 0;
246 string text = null;
247 int num2 = 0;
248 int num3 = 0;
249 float num4 = 1f;
250 if (m_count > m_maxCookies)
251 {
252 num4 = (float)m_maxCookies / (float)m_count;
253 }
255 {
256 foreach (object item in m_domainTable)
257 {
259 string text2;
261 if (domain == null)
262 {
265 }
266 else
267 {
268 text2 = domain;
270 }
271 num2 = 0;
273 {
275 {
277 num += num3;
278 m_count -= num3;
279 num2 += value.Count;
281 if (value.Count > 0 && (dateTime2 = value.TimeStamp(CookieCollection.Stamp.Check)) < dateTime)
282 {
283 text = text2;
286 }
287 }
288 }
289 int num5 = Math.Min((int)((float)num2 * num4), Math.Min(m_maxCookiesPerDomain, m_maxCookies) - 1);
290 if (num2 <= num5)
291 {
292 continue;
293 }
297 {
301 {
302 array2[num3] = value2.TimeStamp(CookieCollection.Stamp.Check);
303 array[num3] = value2;
304 num3++;
305 }
306 }
308 num3 = 0;
310 {
312 {
313 while (num2 > num5 && cookieCollection4.Count > 0)
314 {
315 cookieCollection4.RemoveAt(0);
316 num2--;
317 m_count--;
318 num++;
319 }
320 }
321 if (num2 <= num5)
322 {
323 break;
324 }
325 }
326 if (num2 > num5 && domain != null)
327 {
328 return false;
329 }
330 }
331 }
332 if (domain != null)
333 {
334 return true;
335 }
336 if (num != 0)
337 {
338 return true;
339 }
341 {
342 return false;
343 }
345 {
347 {
348 cookieCollection.RemoveAt(0);
349 m_count--;
350 }
351 }
352 return true;
353 }
354
355 private void DomainTableCleanup()
356 {
360 {
362 while (enumerator.MoveNext())
363 {
364 string item = (string)enumerator.Key;
367 {
369 while (enumerator2.MoveNext())
370 {
372 if (cookieCollection.Count == 0)
373 {
374 list.Add(enumerator2.Key);
375 }
376 }
377 foreach (object item2 in list)
378 {
380 }
381 list.Clear();
382 if (pathList.Count == 0)
383 {
384 list2.Add(item);
385 }
386 }
387 }
388 foreach (string item3 in list2)
389 {
391 }
392 }
393 }
394
396 {
397 lock (cc)
398 {
399 int count = cc.Count;
400 for (int num = count - 1; num >= 0; num--)
401 {
402 Cookie cookie = cc[num];
403 if (cookie.Expired)
404 {
405 cc.RemoveAt(num);
406 }
407 }
408 return count - cc.Count;
409 }
410 }
411
413 {
414 if (cookies == null)
415 {
416 throw new ArgumentNullException("cookies");
417 }
419 {
420 Add(item);
421 }
422 }
423
424 internal bool IsLocalDomain(string host)
425 {
426 int num = host.IndexOf('.');
427 if (num == -1)
428 {
429 return true;
430 }
431 switch (host)
432 {
433 case "127.0.0.1":
434 case "::1":
435 case "0:0:0:0:0:0:0:1":
436 return true;
437 default:
438 {
439 if (string.Compare(m_fqdnMyDomain, 0, host, num, m_fqdnMyDomain.Length, StringComparison.OrdinalIgnoreCase) == 0)
440 {
441 return true;
442 }
443 string[] array = host.Split('.');
444 if (array != null && array.Length == 4 && array[0] == "127")
445 {
446 int i;
447 for (i = 1; i < array.Length; i++)
448 {
449 string text = array[i];
450 switch (text.Length)
451 {
452 case 3:
453 if (text[2] < '0' || text[2] > '9')
454 {
455 break;
456 }
457 goto case 2;
458 case 2:
459 if (text[1] < '0' || text[1] > '9')
460 {
461 break;
462 }
463 goto case 1;
464 case 1:
465 if (text[0] >= '0' && text[0] <= '9')
466 {
467 continue;
468 }
469 break;
470 }
471 break;
472 }
473 if (i == 4)
474 {
475 return true;
476 }
477 }
478 return false;
479 }
480 }
481 }
482
483 public void Add(Uri uri, Cookie cookie)
484 {
485 if (uri == null)
486 {
487 throw new ArgumentNullException("uri");
488 }
489 if (cookie == null)
490 {
491 throw new ArgumentNullException("cookie");
492 }
493 Cookie cookie2 = cookie.Clone();
494 cookie2.VerifySetDefaults(cookie2.Variant, uri, IsLocalDomain(uri.Host), m_fqdnMyDomain, setDefault: true, shouldThrow: true);
495 Add(cookie2, throwOnError: true);
496 }
497
498 public void Add(Uri uri, CookieCollection cookies)
499 {
500 if (uri == null)
501 {
502 throw new ArgumentNullException("uri");
503 }
504 if (cookies == null)
505 {
506 throw new ArgumentNullException("cookies");
507 }
508 bool isLocalDomain = IsLocalDomain(uri.Host);
509 foreach (Cookie cookie3 in cookies)
510 {
511 Cookie cookie2 = cookie3.Clone();
512 cookie2.VerifySetDefaults(cookie2.Variant, uri, isLocalDomain, m_fqdnMyDomain, setDefault: true, shouldThrow: true);
513 Add(cookie2, throwOnError: true);
514 }
515 }
516
518 {
519 if (NetEventSource.Log.IsEnabled())
520 {
521 NetEventSource.Info(this, $"uri:{uri} headerName:{headerName} setCookieHeader:{setCookieHeader} isThrow:{isThrow}", "CookieCutter");
522 }
525 if (headerName == null)
526 {
527 variant = CookieVariant.Rfc2109;
528 }
529 else
530 {
531 for (int i = 0; i < s_headerInfo.Length; i++)
532 {
533 if (string.Equals(headerName, s_headerInfo[i].Name, StringComparison.OrdinalIgnoreCase))
534 {
535 variant = s_headerInfo[i].Variant;
536 }
537 }
538 }
539 bool isLocalDomain = IsLocalDomain(uri.Host);
540 try
541 {
543 while (true)
544 {
545 Cookie cookie = cookieParser.Get();
546 if (NetEventSource.Log.IsEnabled())
547 {
548 NetEventSource.Info(this, $"CookieParser returned cookie:{cookie}", "CookieCutter");
549 }
550 if (cookie == null)
551 {
552 if (cookieParser.EndofHeader())
553 {
554 break;
555 }
556 }
557 else if (string.IsNullOrEmpty(cookie.Name))
558 {
559 if (isThrow)
560 {
562 }
563 }
564 else if (cookie.VerifySetDefaults(variant, uri, isLocalDomain, m_fqdnMyDomain, setDefault: true, isThrow))
565 {
566 cookieCollection.InternalAdd(cookie, isStrict: true);
567 }
568 }
569 }
571 {
572 throw;
573 }
574 catch (Exception inner)
575 {
576 if (isThrow)
577 {
579 }
580 }
582 for (int j = 0; j < count; j++)
583 {
585 }
586 return cookieCollection;
587 }
588
590 {
591 if (uri == null)
592 {
593 throw new ArgumentNullException("uri");
594 }
595 return InternalGetCookies(uri) ?? new CookieCollection();
596 }
597
619
621 {
622 if (m_count == 0)
623 {
624 return null;
625 }
626 bool isSecure = uri.Scheme == "https" || uri.Scheme == "wss";
627 int port = uri.Port;
630 List<string> list2 = null;
631 string host = uri.Host;
632 list.Add(host);
633 list.Add("." + host);
634 int num = host.IndexOf('.');
635 if (num == -1)
636 {
637 if (m_fqdnMyDomain != null && m_fqdnMyDomain.Length != 0)
638 {
641 }
642 }
643 else
644 {
645 list.Add(host.Substring(num));
646 if (host.Length > 2)
647 {
648 int num2 = host.LastIndexOf('.', host.Length - 2);
649 if (num2 > 0)
650 {
651 num2 = host.LastIndexOf('.', num2 - 1);
652 }
653 if (num2 != -1)
654 {
655 while (num < num2 && (num = host.IndexOf('.', num + 1)) != -1)
656 {
657 if (list2 == null)
658 {
659 list2 = new List<string>();
660 }
661 list2.Add(host.Substring(num));
662 }
663 }
664 }
665 }
667 if (list2 != null)
668 {
670 }
671 return cookies;
672 }
673
675 {
676 for (int i = 0; i < domainAttribute.Count; i++)
677 {
680 {
682 if (pathList == null)
683 {
684 continue;
685 }
686 }
688 {
689 SortedList list = pathList.List;
690 int count = list.Count;
691 for (int j = 0; j < count; j++)
692 {
693 string cookiePath = (string)list.GetKey(j);
695 {
699 }
700 }
701 }
702 if (pathList.Count == 0)
703 {
705 {
707 }
708 }
709 }
710 }
711
712 private static bool PathMatch(string requestPath, string cookiePath)
713 {
715 if (!requestPath.StartsWith(cookiePath, StringComparison.Ordinal))
716 {
717 return false;
718 }
719 if (requestPath.Length != cookiePath.Length && (cookiePath.Length <= 0 || cookiePath[^1] != '/'))
720 {
721 return requestPath[cookiePath.Length] == '/';
722 }
723 return true;
724 }
725
727 {
728 lock (source)
729 {
730 for (int i = 0; i < source.Count; i++)
731 {
732 bool flag = false;
733 Cookie cookie = source[i];
734 if (cookie.Expired)
735 {
736 source.RemoveAt(i);
737 m_count--;
738 i--;
739 continue;
740 }
741 if (!isPlainOnly || cookie.Variant == CookieVariant.Plain)
742 {
743 if (cookie.PortList != null)
744 {
745 int[] portList = cookie.PortList;
746 foreach (int num in portList)
747 {
748 if (num == port)
749 {
750 flag = true;
751 break;
752 }
753 }
754 }
755 else
756 {
757 flag = true;
758 }
759 }
760 if (cookie.Secure && !isSecure)
761 {
762 flag = false;
763 }
764 if (flag)
765 {
766 if (destination == null)
767 {
769 }
770 destination.InternalAdd(cookie, isStrict: false);
771 }
772 }
773 }
774 }
775
776 public string GetCookieHeader(Uri uri)
777 {
778 if (uri == null)
779 {
780 throw new ArgumentNullException("uri");
781 }
782 string optCookie;
783 return GetCookieHeader(uri, out optCookie);
784 }
785
786 internal string GetCookieHeader(Uri uri, out string optCookie2)
787 {
789 if (cookieCollection == null)
790 {
791 optCookie2 = string.Empty;
792 return string.Empty;
793 }
794 string value = string.Empty;
796 for (int i = 0; i < cookieCollection.Count; i++)
797 {
798 stringBuilder.Append(value);
799 cookieCollection[i].ToString(stringBuilder);
800 value = "; ";
801 }
802 optCookie2 = (cookieCollection.IsOtherVersionSeen ? "$Version=1" : string.Empty);
804 }
805
806 public void SetCookies(Uri uri, string cookieHeader)
807 {
808 if (uri == null)
809 {
810 throw new ArgumentNullException("uri");
811 }
812 if (cookieHeader == null)
813 {
814 throw new ArgumentNullException("cookieHeader");
815 }
816 CookieCutter(uri, null, cookieHeader, isThrow: true);
817 }
818}
static void Sort(Array array)
Definition Array.cs:2329
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
IEnumerator IEnumerable. GetEnumerator()
Definition Hashtable.cs:899
virtual void Remove(object key)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
int ExpireCollection(CookieCollection cc)
CookieCollection GetCookies(Uri uri)
bool IsLocalDomain(string host)
void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int port, ref CookieCollection cookies, List< string > domainAttribute, bool matchOnlyPlainCookie)
static readonly HeaderVariantInfo[] s_headerInfo
static bool PathMatch(string requestPath, string cookiePath)
CookieContainer(int capacity, int perDomainCapacity, int maxCookieSize)
void Add(Cookie cookie, bool throwOnError)
static readonly string s_fqdnMyDomain
CookieCollection CookieCutter(Uri uri, string headerName, string setCookieHeader, bool isThrow)
readonly Hashtable m_domainTable
static string CreateFqdnMyDomain()
CookieCollection InternalGetCookies(Uri uri)
void SetCookies(Uri uri, string cookieHeader)
void MergeUpdateCollections(ref CookieCollection destination, CookieCollection source, int port, bool isSecure, bool isPlainOnly)
void Add(Uri uri, Cookie cookie)
string GetCookieHeader(Uri uri, out string optCookie2)
void Add(Uri uri, CookieCollection cookies)
CookieCollection GetAllCookies()
void Add(CookieCollection cookies)
bool AgeCookies(string domain)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static string net_cookie_size
Definition SR.cs:32
static string net_cookie_attribute
Definition SR.cs:36
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_cookie_capacity_range
Definition SR.cs:40
static string net_toosmall
Definition SR.cs:14
static string net_container_add_cookie
Definition SR.cs:30
static string net_cookie_parse_header
Definition SR.cs:34
static string net_emptystringcall
Definition SR.cs:14
static string net_cookie_format
Definition SR.cs:38
Definition SR.cs:7
static string GetStringAndRelease(StringBuilder sb)
static StringBuilder Acquire(int capacity=16)
string Host
Definition Uri.cs:441
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
string AbsolutePath
Definition Uri.cs:239
string AbsoluteUri
Definition Uri.cs:266
int Port
Definition Uri.cs:453
UriKind
Definition UriKind.cs:4
static readonly DateTime MaxValue
Definition DateTime.cs:37
static string CheckQuoted(string value)