private void MergeUpdateCollections(CookieCollection destination, CookieCollection source, int port, bool isSecure, bool isPlainOnly)
{
lock (source)
{
// Cannot use foreach as we going update 'source'
for (int idx = 0; idx < source.Count; ++idx)
{
bool to_add = false;
Cookie cookie = source[idx];
if (cookie.Expired)
{
// If expired, remove from container and don't add to the destination
source.RemoveAt(idx);
--_count;
--idx;
}
else
{
// Add only if port does match to this request URI
// or was not present in the original response.
if (isPlainOnly && cookie.Variant != CookieVariant.Plain)
{
; // Don't add
}
else if (cookie.PortList != null)
{
foreach (int p in cookie.PortList)
{
if (p == port)
{
to_add = true;
break;
}
}
}
else
{
// It was implicit Port, always OK to add.
to_add = true;
}
// Refuse to add a secure cookie into an 'unsecure' destination
if (cookie.Secure && !isSecure)
{
to_add = false;
}
if (to_add)
{
// In 'source' are already orederd.
// If two same cookies come from different 'source' then they
// will follow (not replace) each other.
destination.InternalAdd(cookie, false);
}
}
}
}
}