Example #1
0
        private static void ClearImpl(object param)
        {
            if (!IsSupported)
            {
                return;
            }

            try
            {
                // GetFiles will return a string array that contains the files in the folder with the full path
                string[] cacheEntries = Directory.GetFiles(CacheFolder);

                for (int i = 0; i < cacheEntries.Length; ++i)
                {
                    // We need a try-catch block because between the Directory.GetFiles call and the File.Delete calls a maintenance job, or other file operations can delete any file from the cache folder.
                    // So while there might be some problem with any file, we don't want to abort the whole for loop
                    try
                    {
                        File.Delete(cacheEntries[i]);
                    }
                    catch
                    { }
                }
            }
            finally
            {
                UsedIndexes.Clear();
                library.Clear();
                NextNameIDX = 0x0001;

                SaveLibrary();
                InClearThread = false;
            }
        }
Example #2
0
        private static void ClearImpl(object param)
        {
            try
            {
                string[] cacheEntries = Directory.GetFiles(CacheFolder);

                for (int i = 0; i < cacheEntries.Length; ++i)
                {
                    // We need a try-catch block becouse between the Directory.GetFiles call and the File.Delete calls a maintainance job, or other file operations can delelete any file from the cache folder.
                    // So while there might be some problem with any file, we don't want to abort the whole for loop
                    try
                    {
                        string fileName = System.IO.Path.GetFileName(cacheEntries[i]);
                        DeleteEntity(GetUriFromFileName(fileName));
                    }
                    catch
                    { }
                }
            }
            finally
            {
                SaveLibrary();
                InClearThread = false;
            }
        }
Example #3
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!");
            }
        }
Example #4
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
        }
Example #5
0
        /// <summary>
        /// Deletes all files from the cache folder that isn't in the Library.
        /// </summary>
        private static void DeleteUnusedFiles()
        {
#if !UNITY_WEBPLAYER
            CheckSetup();

            string[] cacheEntries = Directory.GetFiles(CacheFolder);

                                                       for (int i = 0; i < cacheEntries.Length; ++i)
            {
                // We need a try-catch block becouse between the Directory.GetFiles call and the File.Delete calls a maintainance job, or other file operations can delelete any file from the cache folder.
                // So while there might be some problem with any file, we don't want to abort the whole for loop
                try
                {
                    string fileName = System.IO.Path.GetFileName(cacheEntries[i]);
                    Uri    uri = GetUriFromFileName(fileName);

                    lock (Library)
                        if (!Library.ContainsKey(uri))
                        {
                            DeleteEntity(uri);
                        }
                }
                catch
                {}
            }
#endif
        }
Example #6
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
        }
Example #7
0
        /// <summary>
        /// Deletes all cache entity. Non blocking.
        /// <remarks>Call it only if there no requests currently processed, becouse cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
        /// </summary>
        public static void BeginClear()
        {
#if !UNITY_WEBPLAYER
            if (InClearThread)
            {
                return;
            }
            InClearThread = true;

            SetupCacheFolder();

#if !NETFX_CORE
            ThreadPool.QueueUserWorkItem(new WaitCallback((param) =>
#else
            Windows.System.Threading.ThreadPool.RunAsync((param) =>
#endif
            {
                try
                {
                    string[] cacheEntries = Directory.GetFiles(CacheFolder);

                    for (int i = 0; i < cacheEntries.Length; ++i)
                    {
                        // We need a try-catch block becouse between the Directory.GetFiles call and the File.Delete calls a maintainance job, or other file operations can delelete any file from the cache folder.
                        // So while there might be some problem with any file, we don't want to abort the whole for loop
                        try
                        {
                            string fileName = System.IO.Path.GetFileName(cacheEntries[i]);
                            DeleteEntity(GetUriFromFileName(fileName));
                        }
                        catch
                        {}
                    }
                }
                finally
                {
                    SaveLibrary();
                    InClearThread = false;
                }
            }
#if !NETFX_CORE
                                                          )
#endif
                                                          );
#endif
        }
Example #8
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
        }
Example #9
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
        }