Beispiel #1
0
        internal static bool IsCachedEntityExpiresInTheFuture(HTTPRequest request)
        {
            if (!IsSupported)
            {
                return(false);
            }

            CheckSetup();

            HTTPCacheFileInfo info = null;

            rwLock.EnterReadLock();
            try
            {
                if (!library.TryGetValue(request.CurrentUri, out info))
                {
                    return(false);
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }

            return(info.WillExpireInTheFuture());
        }
Beispiel #2
0
        internal static HTTPCacheFileInfo Store(Uri uri, HTTPMethods method, HTTPResponse response)
        {
            if (response == null || response.Data == null || response.Data.Length == 0)
            {
                return(null);
            }
            HTTPCacheFileInfo hTTPCacheFileInfo     = null;
            Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library;

            lock (obj)
            {
                if (!HTTPCacheService.Library.TryGetValue(uri, out hTTPCacheFileInfo))
                {
                    HTTPCacheService.Library.Add(uri, hTTPCacheFileInfo = new HTTPCacheFileInfo(uri));
                }
                try
                {
                    hTTPCacheFileInfo.Store(response);
                }
                catch
                {
                    HTTPCacheService.DeleteEntity(uri, true);
                    throw;
                }
            }
            return(hTTPCacheFileInfo);
        }
Beispiel #3
0
        internal static HTTPResponse GetFullResponse(HTTPRequest request)
        {
            if (!IsSupported)
            {
                return(null);
            }

            CheckSetup();

            HTTPCacheFileInfo info = null;

            rwLock.EnterReadLock();
            try
            {
                if (!library.TryGetValue(request.CurrentUri, out info))
                {
                    return(null);
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }

            return(info.ReadResponseTo(request));
        }
Beispiel #4
0
        internal static bool IsCachedEntityExpiresInTheFuture(HTTPRequest request)
        {
            if (!IsSupported || request.DisableCache)
            {
                return(false);
            }

            CheckSetup();

            HTTPCacheFileInfo info = null;

            rwLock.EnterReadLock();
            try
            {
                if (!library.TryGetValue(request.CurrentUri, out info))
                {
                    return(false);
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }

            return(info.WillExpireInTheFuture(request.State == HTTPRequestStates.ConnectionTimedOut ||
                                              request.State == HTTPRequestStates.TimedOut ||
                                              request.State == HTTPRequestStates.Error ||
                                              (request.State == HTTPRequestStates.Finished && request.Response != null && request.Response.StatusCode >= 500)));
        }
Beispiel #5
0
        internal static System.IO.Stream PrepareStreamed(Uri uri, HTTPResponse response)
        {
            if (!IsSupported)
            {
                return(null);
            }

            HTTPCacheFileInfo info;

            lock (Library)
            {
                if (!Library.TryGetValue(uri, out info))
                {
                    Library.Add(uri, info = new HTTPCacheFileInfo(uri));
                    UsedIndexes.Add(info.MappedNameIDX, info);
                }

                try
                {
                    return(info.GetSaveStream(response));
                }
                catch
                {
                    // If something happens while we write out the response, than we will delete it because it might be in an invalid state.
                    DeleteEntity(uri);

                    throw;
                }
            }
        }
        internal static Stream PrepareStreamed(Uri uri, HTTPResponse response)
        {
            lock (Library)
            {
                if (!Library.TryGetValue(uri, out HTTPCacheFileInfo value))
                {
                    Library.Add(uri, value = new HTTPCacheFileInfo(uri));
                }
                try
                {
                    return(value.GetSaveStream(response));

IL_003e:
                    Stream result;
                    return(result);
                }
                catch (Exception ex)
                {
                    DeleteEntity(uri);
                    throw ex;
IL_004c:
                    Stream result;
                    return(result);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Utility function to set the cache control headers according to the spec.: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4
        /// </summary>
        /// <param name="request"></param>
        internal static void SetHeaders(HTTPRequest request)
        {
            if (!IsSupported)
            {
                return;
            }

            CheckSetup();

            request.RemoveHeader("If-None-Match");
            request.RemoveHeader("If-Modified-Since");

            HTTPCacheFileInfo info = null;

            rwLock.EnterReadLock();
            try
            {
                if (!library.TryGetValue(request.CurrentUri, out info))
                {
                    return;
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }

            info.SetUpRevalidationHeaders(request);
        }
Beispiel #8
0
        internal static HTTPCacheFileInfo Store(Uri uri, HTTPMethods method, HTTPResponse response)
        {
            if (response == null || response.Data == null || response.Data.Length == 0)
            {
                return(null);
            }

            HTTPCacheFileInfo info = null;

            lock (Library)
            {
                if (!Library.TryGetValue(uri, out info))
                {
                    Library.Add(uri, info = new HTTPCacheFileInfo(uri));
                }

                try
                {
                    info.Store(response);
                }
                catch
                {
                    // If something happens while we write out the response, than we will delete it becouse it might be in an invalid state.
                    DeleteEntity(uri);

                    throw;
                }
            }

            return(info);
        }
Beispiel #9
0
        internal static HTTPCacheFileInfo GetEntity(Uri uri)
        {
            if (!IsSupported)
            {
                return(null);
            }
            HTTPCacheFileInfo info = null;

            lock (Library)
                Library.TryGetValue(uri, out info);
            return(info);
        }
 internal static void SetBodyLength(Uri uri, int bodyLength)
 {
     lock (Library)
     {
         if (Library.TryGetValue(uri, out HTTPCacheFileInfo value))
         {
             value.BodyLength = bodyLength;
         }
         else
         {
             Library.Add(uri, value = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength));
         }
     }
 }
        internal static HTTPCacheFileInfo Store(HTTPRequest request, HTTPMethods method, HTTPResponse response)
        {
            if (response == null || response.Data == null || response.Data.Length == 0)
            {
                return(null);
            }

            if (!IsSupported)
            {
                return(null);
            }

            Uri cacheUri;

            if (request.CacheUriBuilder != null)
            {
                cacheUri = request.CacheUriBuilder.BuildCacheUri(request.CurrentUri);
            }
            else
            {
                cacheUri = request.CurrentUri;
            }

            HTTPCacheFileInfo info = null;

            lock (Library)
            {
                if (!Library.TryGetValue(cacheUri, out info))
                {
                    Library.Add(cacheUri, info = new HTTPCacheFileInfo(cacheUri));
                    UsedIndexes.Add(info.MappedNameIDX, info);
                }

                try
                {
                    info.Store(response);
                }
                catch
                {
                    // If something happens while we write out the response, than we will delete it becouse it might be in an invalid state.
                    DeleteEntity(cacheUri);

                    throw;
                }
            }

            return(info);
        }
Beispiel #12
0
        internal static HTTPCacheFileInfo Store(Uri uri, HTTPMethods method, HTTPResponse response)
        {
            if (response == null || response.Data == null || response.Data.Length == 0)
            {
                return(null);
            }

            if (!IsSupported)
            {
                return(null);
            }

            CheckSetup();

            HTTPCacheFileInfo info = null;

            rwLock.EnterWriteLock();
            try
            {
                if (!library.TryGetValue(uri, out info))
                {
                    library.Add(uri, info = new HTTPCacheFileInfo(uri));
                    UsedIndexes.Add(info.MappedNameIDX, info);
                }
            }
            finally
            {
                rwLock.ExitWriteLock();
            }

            try
            {
                info.Store(response);
                if (HTTPManager.Logger.Level == Logger.Loglevels.All)
                {
                    HTTPManager.Logger.Verbose("HTTPCacheService", string.Format("{0} - Saved to cache", uri.ToString()));
                }
            }
            catch
            {
                // If something happens while we write out the response, than we will delete it because it might be in an invalid state.
                DeleteEntity(uri);

                throw;
            }

            return(info);
        }
Beispiel #13
0
        internal static void SetBodyLength(Uri uri, int bodyLength)
        {
            lock (Library)
            {
                HTTPCacheFileInfo fileInfo;

                if (Library.TryGetValue(uri, out fileInfo))
                {
                    fileInfo.BodyLength = bodyLength;
                }
                else
                {
                    Library.Add(uri, fileInfo = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength));
                }
            }
        }
Beispiel #14
0
        internal static void SetBodyLength(Uri uri, int bodyLength)
        {
            Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library;

            lock (obj)
            {
                HTTPCacheFileInfo hTTPCacheFileInfo;
                if (HTTPCacheService.Library.TryGetValue(uri, out hTTPCacheFileInfo))
                {
                    hTTPCacheFileInfo.BodyLength = bodyLength;
                }
                else
                {
                    HTTPCacheService.Library.Add(uri, hTTPCacheFileInfo = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength));
                }
            }
        }
Beispiel #15
0
    static int get_CacheFileInfo(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            BestHTTP.HTTPResponse obj = (BestHTTP.HTTPResponse)o;
            BestHTTP.Caching.HTTPCacheFileInfo ret = obj.CacheFileInfo;
            ToLua.PushObject(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index CacheFileInfo on a nil value"));
        }
    }
Beispiel #16
0
        internal static void SetUpCachingValues(Uri uri, HTTPResponse response)
        {
            if (!IsSupported)
            {
                return;
            }

            CheckSetup();

            rwLock.EnterWriteLock();
            try
            {
                HTTPCacheFileInfo info = null;
                if (!library.TryGetValue(uri, out info))
                {
                    library.Add(uri, info = new HTTPCacheFileInfo(uri));
                    UsedIndexes.Add(info.MappedNameIDX, info);
                }

                try
                {
                    info.SetUpCachingValues(response);
                    if (HTTPManager.Logger.Level == Logger.Loglevels.All)
                    {
                        HTTPManager.Logger.Verbose("HTTPCacheService", string.Format("{0} - SetUpCachingValues done!", uri.ToString()));
                    }
                }
                catch
                {
                    // If something happens while we write out the response, than we will delete it because it might be in an invalid state.
                    DeleteEntityImpl(uri);

                    throw;
                }
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
        }
 internal static void Store(Uri uri, HTTPMethods method, HTTPResponse response)
 {
     if (response != null && response.Data != null && response.Data.Length != 0)
     {
         lock (Library)
         {
             if (!Library.TryGetValue(uri, out HTTPCacheFileInfo value))
             {
                 Library.Add(uri, value = new HTTPCacheFileInfo(uri));
             }
             try
             {
                 value.Store(response);
             }
             catch (Exception ex)
             {
                 DeleteEntity(uri);
                 throw ex;
                 IL_0065 :;
             }
         }
     }
 }
Beispiel #18
0
        internal static HTTPCacheFileInfo GetEntity(Uri uri)
        {
            if (!IsSupported)
            {
                return(null);
            }

            CheckSetup();

            HTTPCacheFileInfo info = null;

            rwLock.EnterReadLock();
            try
            {
                library.TryGetValue(uri, out info);
            }
            finally
            {
                rwLock.ExitReadLock();
            }

            return(info);
        }
Beispiel #19
0
        internal static Stream PrepareStreamed(Uri uri, HTTPResponse response)
        {
            Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library;
            Stream saveStream;

            lock (obj)
            {
                HTTPCacheFileInfo hTTPCacheFileInfo;
                if (!HTTPCacheService.Library.TryGetValue(uri, out hTTPCacheFileInfo))
                {
                    HTTPCacheService.Library.Add(uri, hTTPCacheFileInfo = new HTTPCacheFileInfo(uri));
                }
                try
                {
                    saveStream = hTTPCacheFileInfo.GetSaveStream(response);
                }
                catch
                {
                    HTTPCacheService.DeleteEntity(uri, true);
                    throw;
                }
            }
            return(saveStream);
        }
Beispiel #20
0
        internal static void SetBodyLength(Uri uri, int bodyLength)
        {
            if (!IsSupported)
            {
                return;
            }

            CheckSetup();

            rwLock.EnterUpgradeableReadLock();
            try
            {
                HTTPCacheFileInfo fileInfo;
                if (library.TryGetValue(uri, out fileInfo))
                {
                    fileInfo.BodyLength = bodyLength;
                }
                else
                {
                    rwLock.EnterWriteLock();
                    try
                    {
                        library.Add(uri, fileInfo = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength));
                        UsedIndexes.Add(fileInfo.MappedNameIDX, fileInfo);
                    }
                    finally
                    {
                        rwLock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                rwLock.ExitUpgradeableReadLock();
            }
        }
Beispiel #21
0
 public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam)
 {
     if (maintananceParam == null)
     {
         throw new ArgumentNullException("maintananceParams == null");
     }
     if (HTTPCacheService.InMaintainenceThread)
     {
         return;
     }
     HTTPCacheService.InMaintainenceThread = true;
     HTTPCacheService.SetupCacheFolder();
     new Thread(delegate(object param)
     {
         try
         {
             Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library;
             lock (obj)
             {
                 DateTime t      = DateTime.UtcNow - maintananceParam.DeleteOlder;
                 List <Uri> list = new List <Uri>();
                 foreach (KeyValuePair <Uri, HTTPCacheFileInfo> current in HTTPCacheService.Library)
                 {
                     if (current.Value.LastAccess < t && HTTPCacheService.DeleteEntity(current.Key, false))
                     {
                         list.Add(current.Key);
                     }
                 }
                 for (int i = 0; i < list.Count; i++)
                 {
                     HTTPCacheService.Library.Remove(list[i]);
                 }
                 list.Clear();
                 ulong num = HTTPCacheService.GetCacheSize();
                 if (num > maintananceParam.MaxCacheSize)
                 {
                     List <HTTPCacheFileInfo> list2 = new List <HTTPCacheFileInfo>(HTTPCacheService.library.Count);
                     foreach (KeyValuePair <Uri, HTTPCacheFileInfo> current2 in HTTPCacheService.library)
                     {
                         list2.Add(current2.Value);
                     }
                     list2.Sort();
                     int num2 = 0;
                     while (num >= maintananceParam.MaxCacheSize && num2 < list2.Count)
                     {
                         try
                         {
                             HTTPCacheFileInfo hTTPCacheFileInfo = list2[num2];
                             ulong num3 = (ulong)((long)hTTPCacheFileInfo.BodyLength);
                             HTTPCacheService.DeleteEntity(hTTPCacheFileInfo.Uri, true);
                             num -= num3;
                         }
                         catch
                         {
                         }
                         finally
                         {
                             num2++;
                         }
                     }
                 }
             }
         }
         finally
         {
             HTTPCacheService.SaveLibrary();
             HTTPCacheService.InMaintainenceThread = false;
         }
     }).Start();
 }
Beispiel #22
0
        private static void LoadLibrary()
        {
            // Already loaded?
            if (library != null)
            {
                return;
            }

            if (!IsSupported)
            {
                return;
            }

            int version = 1;

            rwLock.EnterWriteLock();

            library = new Dictionary <Uri, HTTPCacheFileInfo>(new UriComparer());
            try
            {
                using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.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);
                                }
                            }
                        }
                    }
            }
            catch
            { }
            finally
            {
                rwLock.ExitWriteLock();
            }

            if (version == 1)
            {
                BeginClear();
            }
            else
            {
                DeleteUnusedFiles();
            }
        }
 public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParams)
 {
     if (maintananceParams == null)
     {
         throw new ArgumentNullException("maintananceParams == null");
     }
     if (!InMaintainenceThread)
     {
         InMaintainenceThread = true;
         SetupCacheFolder();
         ThreadPool.QueueUserWorkItem(delegate(object maintananceParam)
         {
             HTTPCacheMaintananceParams hTTPCacheMaintananceParams = maintananceParam as HTTPCacheMaintananceParams;
             try
             {
                 lock (Library)
                 {
                     DateTime t = DateTime.UtcNow - hTTPCacheMaintananceParams.DeleteOlder;
                     foreach (KeyValuePair <Uri, HTTPCacheFileInfo> item in Library)
                     {
                         if (item.Value.LastAccess < t)
                         {
                             DeleteEntity(item.Key);
                         }
                     }
                     ulong num = GetCacheSize();
                     if (num > hTTPCacheMaintananceParams.MaxCacheSize)
                     {
                         List <HTTPCacheFileInfo> list = new List <HTTPCacheFileInfo>(library.Count);
                         foreach (KeyValuePair <Uri, HTTPCacheFileInfo> item2 in library)
                         {
                             list.Add(item2.Value);
                         }
                         list.Sort();
                         int num2 = 0;
                         while (num >= hTTPCacheMaintananceParams.MaxCacheSize && num2 < list.Count)
                         {
                             try
                             {
                                 HTTPCacheFileInfo hTTPCacheFileInfo = list[num2];
                                 ulong num3 = (ulong)hTTPCacheFileInfo.BodyLength;
                                 DeleteEntity(hTTPCacheFileInfo.Uri);
                                 num -= num3;
                             }
                             catch
                             {
                             }
                             finally
                             {
                                 num2++;
                             }
                         }
                     }
                 }
             }
             finally
             {
                 SaveLibrary();
                 InMaintainenceThread = false;
             }
         }, maintananceParams);
     }
 }