// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // ReSharper disable UnusedMember.Local #pragma warning disable CS8602 using System; using System.Diagnostics; using System.Reflection; using System.Net; namespace SpaceWizards.HttpListener { // TODO https://github.com/dotnet/runtime/issues/19348 internal static class CookieExtensions { private static Func? s_toServerStringFunc; public static string ToServerString(this Cookie cookie) { s_toServerStringFunc ??= (Func)typeof(Cookie).GetMethod("ToServerString", BindingFlags.Instance | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func)); Debug.Assert(s_toServerStringFunc != null, "Reflection failed for Cookie.ToServerString()."); return s_toServerStringFunc(cookie); } private static Func? s_cloneFunc; public static Cookie Clone(this Cookie cookie) { s_cloneFunc ??= (Func)typeof(Cookie).GetMethod("Clone", BindingFlags.Instance | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func)); Debug.Assert(s_cloneFunc != null, "Reflection failed for Cookie.Clone()."); return s_cloneFunc(cookie); } private enum CookieVariant { Unknown, Plain, Rfc2109, Rfc2965, Default = Rfc2109 } private static Func? s_getVariantFunc; public static bool IsRfc2965Variant(this Cookie cookie) { s_getVariantFunc ??= (Func)typeof(Cookie).GetProperty("Variant", BindingFlags.Instance | BindingFlags.NonPublic)!.GetGetMethod(true)!.CreateDelegate(typeof(Func)); Debug.Assert(s_getVariantFunc != null, "Reflection failed for Cookie.Variant."); return s_getVariantFunc(cookie) == CookieVariant.Rfc2965; } } internal static class CookieCollectionExtensions { private static Func? s_internalAddFunc; public static int InternalAdd(this CookieCollection cookieCollection, Cookie cookie, bool isStrict) { s_internalAddFunc ??= (Func)typeof(CookieCollection).GetMethod("InternalAdd", BindingFlags.Instance | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func)); Debug.Assert(s_internalAddFunc != null, "Reflection failed for CookieCollection.InternalAdd()."); return s_internalAddFunc(cookieCollection, cookie, isStrict); } } }