private bool LoadFromCache(Uri uri) { if (HTTPManager.Logger.Level == Loglevels.All) { HTTPManager.Logger.Verbose("HTTPConnection", $"{base.CurrentRequest.CurrentUri.ToString()} - LoadFromCache for Uri: {uri.ToString()}"); } HTTPCacheFileInfo entity = HTTPCacheService.GetEntity(uri); if (entity == null) { HTTPManager.Logger.Warning("HTTPConnection", $"{base.CurrentRequest.CurrentUri.ToString()} - LoadFromCache for Uri: {uri.ToString()} - Cached entity not found!"); return(false); } base.CurrentRequest.Response.CacheFileInfo = entity; using (System.IO.Stream stream = entity.GetBodyStream(out int num)) { if (stream == null) { return(false); } if (!base.CurrentRequest.Response.HasHeader("content-length")) { List <string> list = new List <string>(1) { num.ToString() }; base.CurrentRequest.Response.Headers.Add("content-length", list); } base.CurrentRequest.Response.IsFromCache = true; if (!base.CurrentRequest.CacheOnly) { base.CurrentRequest.Response.ReadRaw(stream, (long)num); } } return(true); }
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; }
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)); } }
internal static System.IO.Stream PrepareStreamed(Uri uri, HTTPResponse response) { HTTPCacheFileInfo info; lock (Library) { if (!Library.TryGetValue(uri, out info)) Library.Add(uri, info = new HTTPCacheFileInfo(uri)); try { return info.GetSaveStream(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; } } }
internal static void SetBodyLength(Uri uri, int bodyLength) { if (!IsSupported) return; lock (Library) { HTTPCacheFileInfo fileInfo; if (Library.TryGetValue(uri, out fileInfo)) fileInfo.BodyLength = bodyLength; else { Library.Add(uri, fileInfo = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength)); UsedIndexes.Add(fileInfo.MappedNameIDX, fileInfo); } } }
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 {} }