private bool LoadFromCache(Uri uri) { if (HTTPManager.Logger.Level == Logger.Loglevels.All) { HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - LoadFromCache for Uri: {1}", this.CurrentRequest.CurrentUri.ToString(), uri.ToString())); } var cacheEntity = HTTPCacheService.GetEntity(uri); if (cacheEntity == null) { HTTPManager.Logger.Warning("HTTPConnection", string.Format("{0} - LoadFromCache for Uri: {1} - Cached entity not found!", this.CurrentRequest.CurrentUri.ToString(), uri.ToString())); return(false); } try { int bodyLength; using (var cacheStream = cacheEntity.GetBodyStream(out bodyLength)) { if (cacheStream == null) { return(false); } if (!CurrentRequest.Response.HasHeader("content-length")) { CurrentRequest.Response.AddHeader("content-length", bodyLength.ToString()); } CurrentRequest.Response.IsFromCache = true; if (!CurrentRequest.CacheOnly) { CurrentRequest.Response.ReadRaw(cacheStream, bodyLength); } } CurrentRequest.Response.CacheFileInfo = cacheEntity; } catch { cacheEntity.Delete(); return(false); } return(true); }
private static bool LoadFromCache(string context, HTTPRequest request, Uri uri, LoggingContext loggingContext1 = null, LoggingContext loggingContext2 = null, LoggingContext loggingContext3 = null) { if (HTTPManager.Logger.Level == Logger.Loglevels.All) { HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - LoadFromCache for Uri: {1}", context, uri.ToString()), loggingContext1, loggingContext2, loggingContext3); } var cacheEntity = HTTPCacheService.GetEntity(uri); if (cacheEntity == null) { HTTPManager.Logger.Warning("HTTPConnection", string.Format("[{0}] - LoadFromCache for Uri: {1} - Cached entity not found!", context, uri.ToString()), loggingContext1, loggingContext2, loggingContext3); return(false); } request.Response.CacheFileInfo = cacheEntity; try { int bodyLength; using (var cacheStream = cacheEntity.GetBodyStream(out bodyLength)) { if (cacheStream == null) { return(false); } if (!request.Response.HasHeader("content-length")) { request.Response.AddHeader("content-length", bodyLength.ToString()); } request.Response.IsFromCache = true; if (!request.CacheOnly) { request.Response.ReadRaw(cacheStream, bodyLength); } } } catch { return(false); } return(true); }
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); }