HTTPCacheService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #if !BESTHTTP_DISABLE_CACHING
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. //
  6. // Version 1: Initial release
  7. // Version 2: Filenames are generated from an index.
  8. //
  9. namespace BestHTTP.Caching
  10. {
  11. using BestHTTP.Extensions;
  12. using BestHTTP.PlatformSupport.FileSystem;
  13. public sealed class UriComparer : IEqualityComparer<Uri>
  14. {
  15. public bool Equals(Uri x, Uri y)
  16. {
  17. return Uri.Compare(x, y, UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped, StringComparison.Ordinal) == 0;
  18. }
  19. public int GetHashCode(Uri uri)
  20. {
  21. return uri.ToString().GetHashCode();
  22. }
  23. }
  24. public static class HTTPCacheService
  25. {
  26. #region Properties & Fields
  27. /// <summary>
  28. /// Library file-format versioning support
  29. /// </summary>
  30. private const int LibraryVersion = 2;
  31. public static bool IsSupported
  32. {
  33. get
  34. {
  35. if (IsSupportCheckDone)
  36. return isSupported;
  37. try
  38. {
  39. // If DirectoryExists throws an exception we will set IsSupprted to false
  40. HTTPManager.IOService.DirectoryExists(HTTPManager.GetRootCacheFolder());
  41. isSupported = true;
  42. }
  43. catch
  44. {
  45. isSupported = false;
  46. HTTPManager.Logger.Warning("HTTPCacheService", "Cache Service Disabled!");
  47. }
  48. finally
  49. {
  50. IsSupportCheckDone = true;
  51. }
  52. return isSupported;
  53. }
  54. }
  55. private static bool isSupported;
  56. private static bool IsSupportCheckDone;
  57. private static Dictionary<Uri, HTTPCacheFileInfo> library;
  58. private static Dictionary<Uri, HTTPCacheFileInfo> Library { get { LoadLibrary(); return library; } }
  59. private static Dictionary<UInt64, HTTPCacheFileInfo> UsedIndexes = new Dictionary<ulong, HTTPCacheFileInfo>();
  60. internal static string CacheFolder { get; private set; }
  61. private static string LibraryPath { get; set; }
  62. private static bool InClearThread;
  63. private static bool InMaintainenceThread;
  64. /// <summary>
  65. /// Stores the index of the next stored entity. The entity's file name is generated from this index.
  66. /// </summary>
  67. private static UInt64 NextNameIDX;
  68. #endregion
  69. static HTTPCacheService()
  70. {
  71. NextNameIDX = 0x0001;
  72. }
  73. #region Common Functions
  74. internal static void CheckSetup()
  75. {
  76. if (!HTTPCacheService.IsSupported)
  77. return;
  78. try
  79. {
  80. SetupCacheFolder();
  81. LoadLibrary();
  82. }
  83. catch
  84. { }
  85. }
  86. internal static void SetupCacheFolder()
  87. {
  88. if (!HTTPCacheService.IsSupported)
  89. return;
  90. try
  91. {
  92. if (string.IsNullOrEmpty(CacheFolder) || string.IsNullOrEmpty(LibraryPath))
  93. {
  94. CacheFolder = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "HTTPCache");
  95. if (!HTTPManager.IOService.DirectoryExists(CacheFolder))
  96. HTTPManager.IOService.DirectoryCreate(CacheFolder);
  97. LibraryPath = System.IO.Path.Combine(HTTPManager.GetRootCacheFolder(), "Library");
  98. }
  99. }
  100. catch
  101. {
  102. isSupported = false;
  103. HTTPManager.Logger.Warning("HTTPCacheService", "Cache Service Disabled!");
  104. }
  105. }
  106. internal static UInt64 GetNameIdx()
  107. {
  108. lock(Library)
  109. {
  110. UInt64 result = NextNameIDX;
  111. do
  112. {
  113. NextNameIDX = ++NextNameIDX % UInt64.MaxValue;
  114. } while (UsedIndexes.ContainsKey(NextNameIDX));
  115. return result;
  116. }
  117. }
  118. internal static bool HasEntity(Uri uri)
  119. {
  120. if (!IsSupported)
  121. return false;
  122. lock (Library)
  123. return Library.ContainsKey(uri);
  124. }
  125. public static bool DeleteEntity(Uri uri, bool removeFromLibrary = true)
  126. {
  127. if (!IsSupported)
  128. return false;
  129. object uriLocker = HTTPCacheFileLock.Acquire(uri);
  130. // Just use lock now: http://forum.unity3d.com/threads/4-6-ios-64-bit-beta.290551/page-6#post-1937033
  131. // To avoid a dead-lock we try acquire the lock on this uri only for a little time.
  132. // If we can't acquire it, its better to just return without risking a deadlock.
  133. //if (Monitor.TryEnter(uriLocker, TimeSpan.FromSeconds(0.5f)))
  134. lock(uriLocker)
  135. {
  136. try
  137. {
  138. lock (Library)
  139. {
  140. HTTPCacheFileInfo info;
  141. bool inStats = Library.TryGetValue(uri, out info);
  142. if (inStats)
  143. info.Delete();
  144. if (inStats && removeFromLibrary)
  145. {
  146. Library.Remove(uri);
  147. UsedIndexes.Remove(info.MappedNameIDX);
  148. }
  149. return true;
  150. }
  151. }
  152. finally
  153. {
  154. //Monitor.Exit(uriLocker);
  155. }
  156. }
  157. //return false;
  158. }
  159. internal static bool IsCachedEntityExpiresInTheFuture(HTTPRequest request)
  160. {
  161. if (!IsSupported)
  162. return false;
  163. HTTPCacheFileInfo info;
  164. lock (Library)
  165. if (Library.TryGetValue(request.CurrentUri, out info))
  166. return info.WillExpireInTheFuture();
  167. return false;
  168. }
  169. /// <summary>
  170. /// Utility function to set the cache control headers according to the spec.: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4
  171. /// </summary>
  172. /// <param name="request"></param>
  173. internal static void SetHeaders(HTTPRequest request)
  174. {
  175. if (!IsSupported)
  176. return;
  177. request.RemoveHeader("If-None-Match");
  178. request.RemoveHeader("If-Modified-Since");
  179. HTTPCacheFileInfo info;
  180. lock (Library)
  181. if (Library.TryGetValue(request.CurrentUri, out info))
  182. info.SetUpRevalidationHeaders(request);
  183. }
  184. #endregion
  185. #region Get Functions
  186. internal static HTTPCacheFileInfo GetEntity(Uri uri)
  187. {
  188. if (!IsSupported)
  189. return null;
  190. HTTPCacheFileInfo info = null;
  191. lock (Library)
  192. Library.TryGetValue(uri, out info);
  193. return info;
  194. }
  195. internal static HTTPResponse GetFullResponse(HTTPRequest request)
  196. {
  197. if (!IsSupported)
  198. return null;
  199. HTTPCacheFileInfo info;
  200. lock (Library)
  201. if (Library.TryGetValue(request.CurrentUri, out info))
  202. return info.ReadResponseTo(request);
  203. return null;
  204. }
  205. #endregion
  206. #region Storing
  207. /// <summary>
  208. /// Checks if the given response can be cached. http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
  209. /// </summary>
  210. /// <returns>Returns true if cacheable, false otherwise.</returns>
  211. internal static bool IsCacheble(Uri uri, HTTPMethods method, HTTPResponse response)
  212. {
  213. if (!IsSupported)
  214. return false;
  215. if (method != HTTPMethods.Get)
  216. return false;
  217. if (response == null)
  218. return false;
  219. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.12 - Cache Replacement
  220. // It MAY insert it into cache storage and MAY, if it meets all other requirements, use it to respond to any future requests that would previously have caused the old response to be returned.
  221. //if (response.StatusCode == 304)
  222. // return false;
  223. if (response.StatusCode < 200 || response.StatusCode >= 400)
  224. return false;
  225. //http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2
  226. var cacheControls = response.GetHeaderValues("cache-control");
  227. if (cacheControls != null)
  228. {
  229. if (cacheControls.Exists(headerValue => {
  230. string value = headerValue.ToLower();
  231. return value.Contains("no-store") || value.Contains("no-cache");
  232. }))
  233. return false;
  234. }
  235. var pragmas = response.GetHeaderValues("pragma");
  236. if (pragmas != null)
  237. {
  238. if (pragmas.Exists(headerValue => {
  239. string value = headerValue.ToLower();
  240. return value.Contains("no-store") || value.Contains("no-cache");
  241. }))
  242. return false;
  243. }
  244. // Responses with byte ranges not supported yet.
  245. var byteRanges = response.GetHeaderValues("content-range");
  246. if (byteRanges != null)
  247. return false;
  248. return true;
  249. }
  250. internal static HTTPCacheFileInfo Store(Uri uri, HTTPMethods method, HTTPResponse response)
  251. {
  252. if (response == null || response.Data == null || response.Data.Length == 0)
  253. return null;
  254. if (!IsSupported)
  255. return null;
  256. HTTPCacheFileInfo info = null;
  257. lock (Library)
  258. {
  259. if (!Library.TryGetValue(uri, out info))
  260. {
  261. Library.Add(uri, info = new HTTPCacheFileInfo(uri));
  262. UsedIndexes.Add(info.MappedNameIDX, info);
  263. }
  264. try
  265. {
  266. info.Store(response);
  267. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  268. HTTPManager.Logger.Verbose("HTTPCacheService", string.Format("{0} - Saved to cache", uri.ToString()));
  269. }
  270. catch
  271. {
  272. // If something happens while we write out the response, than we will delete it because it might be in an invalid state.
  273. DeleteEntity(uri);
  274. throw;
  275. }
  276. }
  277. return info;
  278. }
  279. internal static System.IO.Stream PrepareStreamed(Uri uri, HTTPResponse response)
  280. {
  281. if (!IsSupported)
  282. return null;
  283. HTTPCacheFileInfo info;
  284. lock (Library)
  285. {
  286. if (!Library.TryGetValue(uri, out info))
  287. {
  288. Library.Add(uri, info = new HTTPCacheFileInfo(uri));
  289. UsedIndexes.Add(info.MappedNameIDX, info);
  290. }
  291. try
  292. {
  293. return info.GetSaveStream(response);
  294. }
  295. catch
  296. {
  297. // If something happens while we write out the response, than we will delete it because it might be in an invalid state.
  298. DeleteEntity(uri);
  299. throw;
  300. }
  301. }
  302. }
  303. #endregion
  304. #region Public Maintenance Functions
  305. /// <summary>
  306. /// Deletes all cache entity. Non blocking.
  307. /// <remarks>Call it only if there no requests currently processed, because cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
  308. /// </summary>
  309. public static void BeginClear()
  310. {
  311. if (!IsSupported)
  312. return;
  313. if (InClearThread)
  314. return;
  315. InClearThread = true;
  316. SetupCacheFolder();
  317. #if !NETFX_CORE
  318. ThreadPool.QueueUserWorkItem(new WaitCallback((param) => ClearImpl(param)));
  319. //new Thread(ClearImpl).Start();
  320. #else
  321. #pragma warning disable 4014
  322. Windows.System.Threading.ThreadPool.RunAsync(ClearImpl);
  323. #pragma warning restore 4014
  324. #endif
  325. }
  326. private static void ClearImpl(object param)
  327. {
  328. if (!IsSupported)
  329. return;
  330. try
  331. {
  332. // GetFiles will return a string array that contains the files in the folder with the full path
  333. string[] cacheEntries = HTTPManager.IOService.GetFiles(CacheFolder);
  334. for (int i = 0; i < cacheEntries.Length; ++i)
  335. {
  336. // 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.
  337. // So while there might be some problem with any file, we don't want to abort the whole for loop
  338. try
  339. {
  340. HTTPManager.IOService.FileDelete(cacheEntries[i]);
  341. }
  342. catch
  343. { }
  344. }
  345. }
  346. finally
  347. {
  348. UsedIndexes.Clear();
  349. library.Clear();
  350. NextNameIDX = 0x0001;
  351. SaveLibrary();
  352. InClearThread = false;
  353. }
  354. }
  355. /// <summary>
  356. /// Deletes all expired cache entity.
  357. /// <remarks>Call it only if there no requests currently processed, because cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
  358. /// </summary>
  359. public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam)
  360. {
  361. if (maintananceParam == null)
  362. throw new ArgumentNullException("maintananceParams == null");
  363. if (!HTTPCacheService.IsSupported)
  364. return;
  365. if (InMaintainenceThread)
  366. return;
  367. InMaintainenceThread = true;
  368. SetupCacheFolder();
  369. #if !NETFX_CORE
  370. ThreadPool.QueueUserWorkItem(new WaitCallback((param) =>
  371. //new Thread((param) =>
  372. #else
  373. #pragma warning disable 4014
  374. Windows.System.Threading.ThreadPool.RunAsync((param) =>
  375. #pragma warning restore 4014
  376. #endif
  377. {
  378. try
  379. {
  380. lock (Library)
  381. {
  382. // Delete cache entries older than the given time.
  383. DateTime deleteOlderAccessed = DateTime.UtcNow - maintananceParam.DeleteOlder;
  384. List<HTTPCacheFileInfo> removedEntities = new List<HTTPCacheFileInfo>();
  385. foreach (var kvp in Library)
  386. if (kvp.Value.LastAccess < deleteOlderAccessed)
  387. {
  388. if (DeleteEntity(kvp.Key, false))
  389. removedEntities.Add(kvp.Value);
  390. }
  391. for (int i = 0; i < removedEntities.Count; ++i)
  392. {
  393. Library.Remove(removedEntities[i].Uri);
  394. UsedIndexes.Remove(removedEntities[i].MappedNameIDX);
  395. }
  396. removedEntities.Clear();
  397. ulong cacheSize = GetCacheSize();
  398. // This step will delete all entries starting with the oldest LastAccess property while the cache size greater then the MaxCacheSize in the given param.
  399. if (cacheSize > maintananceParam.MaxCacheSize)
  400. {
  401. List<HTTPCacheFileInfo> fileInfos = new List<HTTPCacheFileInfo>(library.Count);
  402. foreach(var kvp in library)
  403. fileInfos.Add(kvp.Value);
  404. fileInfos.Sort();
  405. int idx = 0;
  406. while (cacheSize >= maintananceParam.MaxCacheSize && idx < fileInfos.Count)
  407. {
  408. try
  409. {
  410. var fi = fileInfos[idx];
  411. ulong length = (ulong)fi.BodyLength;
  412. DeleteEntity(fi.Uri);
  413. cacheSize -= length;
  414. }
  415. catch
  416. {}
  417. finally
  418. {
  419. ++idx;
  420. }
  421. }
  422. }
  423. }
  424. }
  425. finally
  426. {
  427. SaveLibrary();
  428. InMaintainenceThread = false;
  429. }
  430. }
  431. #if !NETFX_CORE
  432. ));
  433. #else
  434. );
  435. #endif
  436. }
  437. public static int GetCacheEntityCount()
  438. {
  439. if (!HTTPCacheService.IsSupported)
  440. return 0;
  441. CheckSetup();
  442. lock(Library)
  443. return Library.Count;
  444. }
  445. public static ulong GetCacheSize()
  446. {
  447. ulong size = 0;
  448. if (!IsSupported)
  449. return size;
  450. CheckSetup();
  451. lock (Library)
  452. foreach (var kvp in Library)
  453. if (kvp.Value.BodyLength > 0)
  454. size += (ulong)kvp.Value.BodyLength;
  455. return size;
  456. }
  457. #endregion
  458. #region Cache Library Management
  459. private static void LoadLibrary()
  460. {
  461. // Already loaded?
  462. if (library != null)
  463. return;
  464. if (!IsSupported)
  465. return;
  466. library = new Dictionary<Uri, HTTPCacheFileInfo>(new UriComparer());
  467. if (!HTTPManager.IOService.FileExists(LibraryPath))
  468. {
  469. DeleteUnusedFiles();
  470. return;
  471. }
  472. try
  473. {
  474. int version;
  475. lock (library)
  476. {
  477. using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.Open))
  478. using (var br = new System.IO.BinaryReader(fs))
  479. {
  480. version = br.ReadInt32();
  481. if (version > 1)
  482. NextNameIDX = br.ReadUInt64();
  483. int statCount = br.ReadInt32();
  484. for (int i = 0; i < statCount; ++i)
  485. {
  486. Uri uri = new Uri(br.ReadString());
  487. var entity = new HTTPCacheFileInfo(uri, br, version);
  488. if (entity.IsExists())
  489. {
  490. library.Add(uri, entity);
  491. if (version > 1)
  492. UsedIndexes.Add(entity.MappedNameIDX, entity);
  493. }
  494. }
  495. }
  496. }
  497. if (version == 1)
  498. BeginClear();
  499. else
  500. DeleteUnusedFiles();
  501. }
  502. catch
  503. {}
  504. }
  505. internal static void SaveLibrary()
  506. {
  507. if (library == null)
  508. return;
  509. if (!IsSupported)
  510. return;
  511. try
  512. {
  513. lock (Library)
  514. {
  515. using (var fs = HTTPManager.IOService.CreateFileStream(LibraryPath, FileStreamModes.Create))
  516. using (var bw = new System.IO.BinaryWriter(fs))
  517. {
  518. bw.Write(LibraryVersion);
  519. bw.Write(NextNameIDX);
  520. bw.Write(Library.Count);
  521. foreach (var kvp in Library)
  522. {
  523. bw.Write(kvp.Key.ToString());
  524. kvp.Value.SaveTo(bw);
  525. }
  526. }
  527. }
  528. }
  529. catch
  530. {}
  531. }
  532. internal static void SetBodyLength(Uri uri, int bodyLength)
  533. {
  534. if (!IsSupported)
  535. return;
  536. lock (Library)
  537. {
  538. HTTPCacheFileInfo fileInfo;
  539. if (Library.TryGetValue(uri, out fileInfo))
  540. fileInfo.BodyLength = bodyLength;
  541. else
  542. {
  543. Library.Add(uri, fileInfo = new HTTPCacheFileInfo(uri, DateTime.UtcNow, bodyLength));
  544. UsedIndexes.Add(fileInfo.MappedNameIDX, fileInfo);
  545. }
  546. }
  547. }
  548. /// <summary>
  549. /// Deletes all files from the cache folder that isn't in the Library.
  550. /// </summary>
  551. private static void DeleteUnusedFiles()
  552. {
  553. if (!IsSupported)
  554. return;
  555. CheckSetup();
  556. // GetFiles will return a string array that contains the files in the folder with the full path
  557. string[] cacheEntries = HTTPManager.IOService.GetFiles(CacheFolder);
  558. for (int i = 0; i < cacheEntries.Length; ++i)
  559. {
  560. // 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.
  561. // So while there might be some problem with any file, we don't want to abort the whole for loop
  562. try
  563. {
  564. string filename = System.IO.Path.GetFileName(cacheEntries[i]);
  565. UInt64 idx = 0;
  566. bool deleteFile = false;
  567. if (UInt64.TryParse(filename, System.Globalization.NumberStyles.AllowHexSpecifier, null, out idx))
  568. lock (Library)
  569. deleteFile = !UsedIndexes.ContainsKey(idx);
  570. else
  571. deleteFile = true;
  572. if (deleteFile)
  573. HTTPManager.IOService.FileDelete(cacheEntries[i]);
  574. }
  575. catch
  576. {}
  577. }
  578. }
  579. #endregion
  580. }
  581. }
  582. #endif