示例#1
0
        /// <summary>
        /// Returns all Cookies that corresponds to the given Uri.
        /// </summary>
        public static List <Cookie> Get(Uri uri)
        {
            Load();

            rwLock.EnterReadLock();
            try
            {
                List <Cookie> result = null;

                for (int i = 0; i < Cookies.Count; ++i)
                {
                    Cookie cookie = Cookies[i];
                    if (cookie.WillExpireInTheFuture() && (uri.Host.IndexOf(cookie.Domain) != -1 || string.Format("{0}:{1}", uri.Host, uri.Port).IndexOf(cookie.Domain) != -1) && uri.AbsolutePath.StartsWith(cookie.Path))
                    {
                        if (result == null)
                        {
                            result = new List <Cookie>();
                        }

                        result.Add(cookie);
                    }
                }

                return(result);
            }
            finally
            {
                rwLock.ExitReadLock();
            }
        }
示例#2
0
        /// <summary>
        /// Returns all Cookies that corresponds to the given Uri.
        /// </summary>
        public static List <Cookie> Get(Uri uri)
        {
            lock (Locker)
            {
                Load();

                List <Cookie> result = null;

                for (int i = 0; i < Cookies.Count; ++i)
                {
                    Cookie cookie = Cookies[i];
                    if (cookie.WillExpireInTheFuture() && uri.Host.IndexOf(cookie.Domain) != -1 && uri.AbsolutePath.StartsWith(cookie.Path))
                    {
                        if (result == null)
                        {
                            result = new List <Cookie>();
                        }

                        result.Add(cookie);
                    }
                }

                return(result);
            }
        }
示例#3
0
        /// <summary>
        /// Load previously persisted cookie library from the file.
        /// </summary>
        internal static void Load()
        {
#if !BESTHTTP_DISABLE_COOKIE_SAVE
            if (!IsSavingSupported)
            {
                return;
            }

            lock (Locker)
            {
                if (Loaded)
                {
                    return;
                }

                SetupFolder();

                try
                {
                    Cookies.Clear();

                    if (!Directory.Exists(CookieFolder))
                    {
                        Directory.CreateDirectory(CookieFolder);
                    }

                    if (!File.Exists(LibraryPath))
                    {
                        return;
                    }

                    using (var fs = new FileStream(LibraryPath, FileMode.Open))
                        using (var br = new System.IO.BinaryReader(fs))
                        {
                            /*int version = */ br.ReadInt32();
                            int cookieCount = br.ReadInt32();

                            for (int i = 0; i < cookieCount; ++i)
                            {
                                Cookie cookie = new Cookie();
                                cookie.LoadFrom(br);

                                if (cookie.WillExpireInTheFuture())
                                {
                                    Cookies.Add(cookie);
                                }
                            }
                        }
                }
                catch
                {
                    Cookies.Clear();
                }
                finally
                {
                    Loaded = true;
                }
            }
#endif
        }
示例#4
0
        /// <summary>
        /// Load previously persisted cookie library from the file.
        /// </summary>
        internal static void Load()
        {
#if !BESTHTTP_DISABLE_COOKIE_SAVE
            if (!IsSavingSupported)
            {
                return;
            }

            if (Loaded)
            {
                return;
            }

            SetupFolder();

            rwLock.EnterWriteLock();
            try
            {
                Cookies.Clear();

                if (!HTTPManager.IOService.DirectoryExists(CookieFolder))
                {
                    HTTPManager.IOService.DirectoryCreate(CookieFolder);
                }

                if (!HTTPManager.IOService.FileExists(LibraryPath))
                {
                    return;
                }

                using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.Open))
                    using (var br = new System.IO.BinaryReader(fs))
                    {
                        /*int version = */ br.ReadInt32();
                        int cookieCount = br.ReadInt32();

                        for (int i = 0; i < cookieCount; ++i)
                        {
                            Cookie cookie = new Cookie();
                            cookie.LoadFrom(br);

                            if (cookie.WillExpireInTheFuture())
                            {
                                Cookies.Add(cookie);
                            }
                        }
                    }
            }
            catch
            {
                Cookies.Clear();
            }
            finally
            {
                Loaded = true;
                rwLock.ExitWriteLock();
            }
#endif
        }
示例#5
0
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    CookieJar.Maintain();
                    List <Cookie> list         = new List <Cookie>();
                    List <string> headerValues = response.GetHeaderValues("set-cookie");
                    if (headerValues != null)
                    {
                        foreach (string current in headerValues)
                        {
                            try
                            {
                                Cookie cookie = Cookie.Parse(current, response.baseRequest.CurrentUri);
                                if (cookie != null)
                                {
                                    int    num;
                                    Cookie cookie2 = CookieJar.Find(cookie, out num);
                                    if (!string.IsNullOrEmpty(cookie.Value) && cookie.WillExpireInTheFuture())
                                    {
                                        if (cookie2 == null)
                                        {
                                            CookieJar.Cookies.Add(cookie);
                                            list.Add(cookie);
                                        }
                                        else
                                        {
                                            cookie.Date            = cookie2.Date;
                                            CookieJar.Cookies[num] = cookie;
                                            list.Add(cookie);
                                        }
                                    }
                                    else if (num != -1)
                                    {
                                        CookieJar.Cookies.RemoveAt(num);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                        response.Cookies = list;
                    }
                }
                catch
                {
                }
            }
        }
示例#6
0
        /// <summary>
        /// Load previously persisted cooki library from the file.
        /// </summary>
        internal static void Load()
        {
#if !UNITY_WEBPLAYER
            lock (Locker)
            {
                try
                {
                    Cookies.Clear();

                    if (!Directory.Exists(CookieFolder))
                    {
                        Directory.CreateDirectory(CookieFolder);
                    }

                    if (!File.Exists(LibraryPath))
                    {
                        return;
                    }

                    using (var fs = new FileStream(LibraryPath, FileMode.Open))
                        using (var br = new System.IO.BinaryReader(fs))
                        {
                            /*int version = */ br.ReadInt32();
                            int cookieCount = br.ReadInt32();

                            for (int i = 0; i < cookieCount; ++i)
                            {
                                Cookie cookie = new Cookie();
                                cookie.LoadFrom(br);

                                if (cookie.WillExpireInTheFuture())
                                {
                                    Cookies.Add(cookie);
                                }
                            }
                        }
                }
                catch
                {
                    Cookies.Clear();
                }
            }
#endif
        }
示例#7
0
        internal static void Maintain()
        {
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    uint     num = 0u;
                    TimeSpan t   = TimeSpan.FromDays(7.0);
                    int      i   = 0;
                    while (i < CookieJar.Cookies.Count)
                    {
                        Cookie cookie = CookieJar.Cookies[i];
                        if (!cookie.WillExpireInTheFuture() || cookie.LastAccess + t < DateTime.UtcNow)
                        {
                            CookieJar.Cookies.RemoveAt(i);
                        }
                        else
                        {
                            if (!cookie.IsSession)
                            {
                                num += cookie.GuessSize();
                            }
                            i++;
                        }
                    }
                    if (num > HTTPManager.CookieJarSize)
                    {
                        CookieJar.Cookies.Sort();
                        while (num > HTTPManager.CookieJarSize && CookieJar.Cookies.Count > 0)
                        {
                            Cookie cookie2 = CookieJar.Cookies[0];
                            CookieJar.Cookies.RemoveAt(0);
                            num -= cookie2.GuessSize();
                        }
                    }
                }
                catch
                {
                }
            }
        }
示例#8
0
        public static List <Cookie> Get(Uri uri)
        {
            object        locker = CookieJar.Locker;
            List <Cookie> result;

            lock (locker)
            {
                List <Cookie> list = new List <Cookie>();
                for (int i = 0; i < CookieJar.Cookies.Count; i++)
                {
                    Cookie cookie = CookieJar.Cookies[i];
                    if (cookie.WillExpireInTheFuture() && uri.Host.IndexOf(cookie.Domain) != -1 && uri.AbsolutePath.StartsWith(cookie.Path))
                    {
                        list.Add(cookie);
                    }
                }
                result = list;
            }
            return(result);
        }
示例#9
0
        public static void Clear(string domain)
        {
            object locker = CookieJar.Locker;

            lock (locker)
            {
                int i = 0;
                while (i < CookieJar.Cookies.Count)
                {
                    Cookie cookie = CookieJar.Cookies[i];
                    if (!cookie.WillExpireInTheFuture() || cookie.Domain.IndexOf(domain) != -1)
                    {
                        CookieJar.Cookies.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
        }
示例#10
0
        public static void Clear(TimeSpan olderThan)
        {
            object locker = CookieJar.Locker;

            lock (locker)
            {
                int i = 0;
                while (i < CookieJar.Cookies.Count)
                {
                    Cookie cookie = CookieJar.Cookies[i];
                    if (!cookie.WillExpireInTheFuture() || cookie.Date + olderThan < DateTime.UtcNow)
                    {
                        CookieJar.Cookies.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
        }
示例#11
0
        internal static void Load()
        {
            object locker = CookieJar.Locker;

            lock (locker)
            {
                try
                {
                    CookieJar.Cookies.Clear();
                    if (!Directory.Exists(CookieJar.CookieFolder))
                    {
                        Directory.CreateDirectory(CookieJar.CookieFolder);
                    }
                    if (File.Exists(CookieJar.LibraryPath))
                    {
                        using (FileStream fileStream = new FileStream(CookieJar.LibraryPath, FileMode.Open))
                        {
                            using (BinaryReader binaryReader = new BinaryReader(fileStream))
                            {
                                binaryReader.ReadInt32();
                                int num = binaryReader.ReadInt32();
                                for (int i = 0; i < num; i++)
                                {
                                    Cookie cookie = new Cookie();
                                    cookie.LoadFrom(binaryReader);
                                    if (cookie.WillExpireInTheFuture())
                                    {
                                        CookieJar.Cookies.Add(cookie);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    CookieJar.Cookies.Clear();
                }
            }
        }
示例#12
0
        internal static void Set(HTTPResponse response)
        {
            if (response == null)
            {
                return;
            }

            lock (Locker)
            {
                try
                {
                    Maintain();

                    List <Cookie> newCookies       = new List <Cookie>();
                    var           setCookieHeaders = response.GetHeaderValues("set-cookie");

                    // No cookies. :'(
                    if (setCookieHeaders == null)
                    {
                        return;
                    }

                    foreach (var cookieHeader in setCookieHeaders)
                    {
                        try
                        {
                            Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri);

                            if (cookie != null)
                            {
                                int idx;
                                var old = Find(cookie, out idx);

                                // if no value for the cookie or already expired then the server asked us to delete the cookie
                                bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();

                                if (!expired)
                                {
                                    // no old cookie, add it straith to the list
                                    if (old == null)
                                    {
                                        Cookies.Add(cookie);

                                        newCookies.Add(cookie);
                                    }
                                    else
                                    {
                                        // Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
                                        cookie.Date  = old.Date;
                                        Cookies[idx] = cookie;

                                        newCookies.Add(cookie);
                                    }
                                }
                                else if (idx != -1) // delete the cookie
                                {
                                    Cookies.RemoveAt(idx);
                                }
                            }
                        }
                        catch
                        {
                            // Ignore cookie on error
                        }
                    }

                    response.Cookies = newCookies;
                }
                catch
                {}
            }
        }
示例#13
0
        /// <summary>
        /// Will set or update all cookies from the response object.
        /// </summary>
        internal static bool Set(HTTPResponse response)
        {
            if (response == null)
            {
                return(false);
            }

            List <Cookie> newCookies       = new List <Cookie>();
            var           setCookieHeaders = response.GetHeaderValues("set-cookie");

            // No cookies. :'(
            if (setCookieHeaders == null)
            {
                return(false);
            }

            foreach (var cookieHeader in setCookieHeaders)
            {
                Cookie cookie = Cookie.Parse(cookieHeader, response.baseRequest.CurrentUri, response.baseRequest.Context);
                if (cookie != null)
                {
                    rwLock.EnterWriteLock();
                    try
                    {
                        int idx;
                        var old = Find(cookie, out idx);

                        // if no value for the cookie or already expired then the server asked us to delete the cookie
                        bool expired = string.IsNullOrEmpty(cookie.Value) || !cookie.WillExpireInTheFuture();

                        if (!expired)
                        {
                            // no old cookie, add it straight to the list
                            if (old == null)
                            {
                                Cookies.Add(cookie);

                                newCookies.Add(cookie);
                            }
                            else
                            {
                                // Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
                                cookie.Date  = old.Date;
                                Cookies[idx] = cookie;

                                newCookies.Add(cookie);
                            }
                        }
                        else if (idx != -1) // delete the cookie
                        {
                            Cookies.RemoveAt(idx);
                        }
                    }
                    catch
                    {
                        // Ignore cookie on error
                    }
                    finally
                    {
                        rwLock.ExitWriteLock();
                    }
                }
            }

            response.Cookies = newCookies;

            PluginEventHelper.EnqueuePluginEvent(new PluginEventInfo(PluginEvents.SaveCookieLibrary));

            return(true);
        }