Ejemplo n.º 1
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
        }
Ejemplo n.º 2
0
        internal static void SetupCacheFolder()
        {
            if (!HTTPCacheService.IsSupported)
            {
                return;
            }

            try
            {
                if (string.IsNullOrEmpty(CacheFolder) || string.IsNullOrEmpty(LibraryPath))
                {
                    CacheFolder = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "HTTPCache");
                    if (!Directory.Exists(CacheFolder))
                    {
                        Directory.CreateDirectory(CacheFolder);
                    }

                    LibraryPath = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "Library");
                }
            }
            catch
            {
                isSupported = false;

                HTTPManager.Logger.Warning("HTTPCacheService", "Cache Service Disabled!");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves the Cookie Jar to a file.
        /// </summary>
        /// <remarks>Not implemented under Unity WebPlayer</remarks>
        internal static void Persist()
        {
#if !BESTHTTP_DISABLE_COOKIE_SAVE
            if (!IsSavingSupported)
            {
                return;
            }

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

                try
                {
                    // Delete any expired cookie
                    Maintain();

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

                    using (var fs = new FileStream(LibraryPath, FileMode.Create))
                        using (var bw = new System.IO.BinaryWriter(fs))
                        {
                            bw.Write(Version);

                            // Count how many non-session cookies we have
                            int count = 0;
                            foreach (var cookie in Cookies)
                            {
                                if (!cookie.IsSession)
                                {
                                    count++;
                                }
                            }

                            bw.Write(count);

                            // Save only the persistable cookies
                            foreach (var cookie in Cookies)
                            {
                                if (!cookie.IsSession)
                                {
                                    cookie.SaveTo(bw);
                                }
                            }
                        }
                }
                catch
                { }
            }
#endif
        }
Ejemplo n.º 4
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
        }
Ejemplo n.º 5
0
        internal static void SetupCacheFolder()
        {
#if !UNITY_WEBPLAYER
            try
            {
                if (string.IsNullOrEmpty(CacheFolder) || string.IsNullOrEmpty(LibraryPath))
                {
                    CacheFolder = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "HTTPCache");
                    if (!Directory.Exists(CacheFolder))
                    {
                        Directory.CreateDirectory(CacheFolder);
                    }

                    LibraryPath = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "Library");
                }
            }
            catch
            { }
#endif
        }