Exemplo n.º 1
0
        internal static void SaveLibrary()
        {
#if !UNITY_WEBPLAYER
            if (library == null)
            {
                return;
            }

            try
            {
                lock (Library)
                {
                    using (var fs = new FileStream(LibraryPath, FileMode.Create))
                        using (var bw = new System.IO.BinaryWriter(fs))
                        {
                            bw.Write(LibraryVersion);
                            bw.Write(Library.Count);
                            foreach (var kvp in Library)
                            {
                                bw.Write(kvp.Key.ToString());

                                kvp.Value.SaveTo(bw);
                            }
                        }
                }
            }
            catch
            {}
#endif
        }
Exemplo n.º 2
0
        public static void WriteLine(this FileStream fs, string format, params object[] values)
        {
            var buff = string.Format(format, values).GetASCIIBytes();

            fs.Write(buff, 0, buff.Length);
            fs.WriteLine();
        }
Exemplo n.º 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
        }
Exemplo n.º 4
0
        public static void WriteLine(this FileStream fs, string line)
        {
            var buff = line.GetASCIIBytes();

            fs.Write(buff, 0, buff.Length);
            fs.WriteLine();
        }
Exemplo n.º 5
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
        }
        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));
        }
Exemplo n.º 7
0
        internal HTTPResponse ReadResponseTo(HTTPRequest request)
        {
            if (!IsExists())
            {
                return(null);
            }

            LastAccess = DateTime.UtcNow;

            using (FileStream stream = new FileStream(GetPath(), FileMode.Open))
            {
                var response = new HTTPResponse(request, stream, request.UseStreaming, true);
                response.Receive(BodyLength);
                return(response);
            }
        }
Exemplo n.º 8
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
        }
        public System.IO.Stream GetBodyStream(out int length)
        {
            if (!IsExists())
            {
                length = 0;
                return(null);
            }

            length = BodyLength;

            LastAccess = DateTime.UtcNow;

            FileStream stream = new FileStream(GetPath(), FileMode.Open, FileAccess.Read, FileShare.Read);

            stream.Seek(-length, System.IO.SeekOrigin.End);

            return(stream);
        }
Exemplo n.º 10
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
        }
        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);
        }
        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
        }
        internal static void SaveLibrary()
        {
            #if !UNITY_WEBPLAYER
            if (library == null)
                return;

            try
            {
                lock (Library)
                {
                    using (var fs = new FileStream(LibraryPath, FileMode.Create))
                    using (var bw = new System.IO.BinaryWriter(fs))
                    {
                        bw.Write(LibraryVersion);
                        bw.Write(Library.Count);
                        foreach (var kvp in Library)
                        {
                            bw.Write(kvp.Key.ToString());

                            kvp.Value.SaveTo(bw);
                        }
                    }
                }
            }
            catch
            {}
            #endif
        }
        /// <summary>
        /// Saves the Cookie Jar to a file.
        /// </summary>
        /// <remarks>Not implemented under Unity WebPlayer</remarks>
        internal static void Persist()
        {
            if (!IsSavingSupported)
                return;

            lock (Locker)
            {
                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
                { }
            }
        }
        /// <summary>
        /// Load previously persisted cooki library from the file.
        /// </summary>
        internal static void Load()
        {
            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;
                }
            }
        }
        private static void LoadLibrary()
        {
            // Already loaded?
            if (library != null)
                return;

            if (!IsSupported)
                return;

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

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

            try
            {
                int version;

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

                        if (version > 1)
                            NextNameIDX = br.ReadUInt64();

                        int statCount = br.ReadInt32();

                        for (int i = 0; i < statCount; ++i)
                        {
                            Uri uri = new Uri(br.ReadString());

                            var entity = new HTTPCacheFileInfo(uri, br, version);
                            if (entity.IsExists())
                            {
                                library.Add(uri, entity);

                                if (version > 1)
                                    UsedIndexes.Add(entity.MappedNameIDX, entity);
                            }
                        }
                    }
                }

                if (version == 1)
                    BeginClear();
                else
                    DeleteUnusedFiles();
            }
            catch
            {}
        }
        internal static void SaveLibrary()
        {
            if (library == null)
                return;

            if (!IsSupported)
                return;

            try
            {
                lock (Library)
                {
                    using (var fs = new FileStream(LibraryPath, FileMode.Create))
                    using (var bw = new System.IO.BinaryWriter(fs))
                    {
                        bw.Write(LibraryVersion);
                        bw.Write(NextNameIDX);

                        bw.Write(Library.Count);
                        foreach (var kvp in Library)
                        {
                            bw.Write(kvp.Key.ToString());

                            kvp.Value.SaveTo(bw);
                        }
                    }
                }
            }
            catch
            {}
        }
Exemplo n.º 18
0
 public static void WriteLine(this FileStream fs)
 {
     fs.Write(HTTPRequest.EOL, 0, 2);
 }