Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c22a81a

Browse files
ViktorHoferdanmoseley
authored andcommitted
Enabling CookieContainer serialization by exposing PathList type and adding a type forward
1 parent 6e8c70d commit c22a81a

File tree

3 files changed

+15
-40
lines changed

3 files changed

+15
-40
lines changed

src/System.Net.Primitives/src/System/Net/CookieContainer.cs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,10 @@ internal void Add(Cookie cookie, bool throwOnError)
282282
{
283283
lock (m_domainTable.SyncRoot)
284284
{
285-
object pathValue = m_domainTable[cookie.DomainKey];
286-
if (pathValue == null)
285+
pathList = (PathList)m_domainTable[cookie.DomainKey];
286+
if (pathList == null)
287287
{
288-
m_domainTable[cookie.DomainKey] = (pathList = PathList.Create());
289-
}
290-
else
291-
{
292-
pathList = (PathList)pathValue;
288+
m_domainTable[cookie.DomainKey] = (pathList = new PathList());
293289
}
294290
}
295291
int domain_count = pathList.GetCookiesCount();
@@ -815,12 +811,11 @@ private void BuildCookieCollectionFromDomainMatches(Uri uri, bool isSecure, int
815811
PathList pathList;
816812
lock (m_domainTable.SyncRoot)
817813
{
818-
object pathListValue = m_domainTable[domainAttribute[i]];
819-
if (pathListValue == null)
814+
pathList = (PathList)m_domainTable[domainAttribute[i]];
815+
if (pathList == null)
820816
{
821817
continue;
822818
}
823-
pathList = (PathList)pathListValue;
824819
}
825820

826821
lock (pathList.SyncRoot)
@@ -990,29 +985,17 @@ public void SetCookies(Uri uri, string cookieHeader)
990985
}
991986
}
992987

988+
// PathList needs to be public in order to maintain binary serialization compatibility as the System shim
989+
// needs to have access to type-forward it.
993990
[Serializable]
994991
[System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
995-
internal struct PathList
992+
public sealed class PathList
996993
{
997994
// Usage of PathList depends on it being shallowly immutable;
998995
// adding any mutable fields to it would result in breaks.
999-
private readonly SortedList m_list; // Do not rename (binary serialization)
996+
private readonly SortedList m_list = SortedList.Synchronized(new SortedList(PathListComparer.StaticInstance)); // Do not rename (binary serialization)
1000997

1001-
public static PathList Create() => new PathList(SortedList.Synchronized(new SortedList(PathListComparer.StaticInstance)));
1002-
1003-
private PathList(SortedList list)
1004-
{
1005-
Debug.Assert(list != null, $"{nameof(list)} must not be null.");
1006-
m_list = list;
1007-
}
1008-
1009-
public int Count
1010-
{
1011-
get
1012-
{
1013-
return m_list.Count;
1014-
}
1015-
}
998+
public int Count => m_list.Count;
1016999

10171000
public int GetCookiesCount()
10181001
{
@@ -1062,17 +1045,9 @@ public IEnumerator GetEnumerator()
10621045
}
10631046
}
10641047

1065-
public object SyncRoot
1066-
{
1067-
get
1068-
{
1069-
Debug.Assert(m_list != null, $"{nameof(PathList)} should never be default initialized and only ever created with {nameof(Create)}.");
1070-
return m_list;
1071-
}
1072-
}
1048+
public object SyncRoot => m_list.SyncRoot;
10731049

10741050
[Serializable]
1075-
[System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
10761051
private sealed class PathListComparer : IComparer
10771052
{
10781053
internal static readonly PathListComparer StaticInstance = new PathListComparer();

0 commit comments

Comments
 (0)