コード例 #1
0
ファイル: HTTPCacheService.cs プロジェクト: Ivan12581/SDKDome
        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;
            }
        }
コード例 #2
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
        }
コード例 #3
0
        internal bool IsExists()
        {
#if !UNITY_WEBPLAYER
            return(File.Exists(GetPath()));
#else
            return(false);
#endif
        }
コード例 #4
0
        public bool IsExists()
        {
            if (!HTTPCacheService.IsSupported)
            {
                return(false);
            }

            return(File.Exists(GetPath()));
        }
コード例 #5
0
        internal System.IO.Stream GetSaveStream(HTTPResponse response)
        {
            if (!HTTPCacheService.IsSupported)
            {
                return(null);
            }

            LastAccess = DateTime.UtcNow;

            string path = GetPath();

            if (File.Exists(path))
            {
                Delete();
            }

            // Path name too long, we don't want to get exceptions
            if (path.Length > HTTPManager.MaxPathLength)
            {
                return(null);
            }

            // First write out the headers
            using (FileStream writer = new FileStream(path, FileMode.Create))
            {
                writer.WriteLine("HTTP/1.1 {0} {1}", response.StatusCode, response.Message);
                foreach (var kvp in response.Headers)
                {
                    for (int i = 0; i < kvp.Value.Count; ++i)
                    {
                        writer.WriteLine("{0}: {1}", kvp.Key, kvp.Value[i]);
                    }
                }

                writer.WriteLine();
            }

            // If caching is enabled and the response is from cache, and no content-length header set, then we set one to the response.
            if (response.IsFromCache && !response.Headers.ContainsKey("content-length"))
            {
                response.Headers.Add("content-length", new List <string> {
                    BodyLength.ToString()
                });
            }

            SetUpCachingValues(response);

            // then create the stream with Append FileMode
            return(new FileStream(GetPath(), FileMode.Append));
        }
コード例 #6
0
        internal void Delete()
        {
#if !UNITY_WEBPLAYER
            string path = GetPath();
            try
            {
                File.Delete(path);
            }
            catch
            { }
            finally
            {
                Reset();
            }
#endif
        }
コード例 #7
0
        private static void LoadLibrary()
        {
            // Already loaded?
            if (library != null)
            {
                return;
            }

            library = new Dictionary <Uri, HTTPCacheFileInfo>();

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

            try
            {
                lock (library)
                {
                    using (var fs = new FileStream(LibraryPath, FileMode.Open))
                        using (var br = new System.IO.BinaryReader(fs))
                        {
                            int version   = br.ReadInt32();
                            int statCount = br.ReadInt32();

                            for (int i = 0; i < statCount; ++i)
                            {
                                Uri  uri          = new Uri(br.ReadString());
                                bool onFileSystem = File.Exists(System.IO.Path.Combine(CacheFolder, GetFileNameFromUri(uri)));

                                if (onFileSystem)
                                {
                                    library.Add(uri, new HTTPCacheFileInfo(uri, br, version));
                                }
                            }
                        }
                }

                DeleteUnusedFiles();
            }
            catch
            {}
#endif
        }
コード例 #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
        }
コード例 #9
0
        internal void Delete()
        {
            if (!HTTPCacheService.IsSupported)
            {
                return;
            }

            string path = GetPath();

            try
            {
                File.Delete(path);
            }
            catch
            { }
            finally
            {
                Reset();
            }
        }
コード例 #10
0
        internal void Store(HTTPResponse response)
        {
            if (!HTTPCacheService.IsSupported)
            {
                return;
            }

            string path = GetPath();

            // Path name too long, we don't want to get exceptions
            if (path.Length > HTTPManager.MaxPathLength)
            {
                return;
            }

            if (File.Exists(path))
            {
                Delete();
            }

            using (FileStream writer = new FileStream(path, FileMode.Create))
            {
                writer.WriteLine("HTTP/1.1 {0} {1}", response.StatusCode, response.Message);
                foreach (var kvp in response.Headers)
                {
                    for (int i = 0; i < kvp.Value.Count; ++i)
                    {
                        writer.WriteLine("{0}: {1}", kvp.Key, kvp.Value[i]);
                    }
                }

                writer.WriteLine();

                writer.Write(response.Data, 0, response.Data.Length);
            }

            BodyLength = response.Data.Length;
            LastAccess = DateTime.UtcNow;

            SetUpCachingValues(response);
        }