psl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #ifdef USE_LIBPSL
  27. #include "psl.h"
  28. #include "share.h"
  29. /* The last 2 #include files should be in this order */
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. void Curl_psl_destroy(struct PslCache *pslcache)
  33. {
  34. if(pslcache->psl) {
  35. if(pslcache->dynamic)
  36. psl_free((psl_ctx_t *)CURL_UNCONST(pslcache->psl));
  37. pslcache->psl = NULL;
  38. pslcache->dynamic = FALSE;
  39. }
  40. }
  41. static time_t now_seconds(void)
  42. {
  43. struct curltime now = curlx_now();
  44. return now.tv_sec;
  45. }
  46. const psl_ctx_t *Curl_psl_use(struct Curl_easy *easy)
  47. {
  48. struct PslCache *pslcache = easy->psl;
  49. const psl_ctx_t *psl;
  50. time_t now;
  51. if(!pslcache)
  52. return NULL;
  53. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
  54. now = now_seconds();
  55. if(!pslcache->psl || pslcache->expires <= now) {
  56. /* Let a chance to other threads to do the job: avoids deadlock. */
  57. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  58. /* Update cache: this needs an exclusive lock. */
  59. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE);
  60. /* Recheck in case another thread did the job. */
  61. now = now_seconds();
  62. if(!pslcache->psl || pslcache->expires <= now) {
  63. bool dynamic = FALSE;
  64. time_t expires = TIME_T_MAX;
  65. #if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000
  66. psl = psl_latest(NULL);
  67. dynamic = psl != NULL;
  68. /* Take care of possible time computation overflow. */
  69. expires = now < TIME_T_MAX - PSL_TTL ? now + PSL_TTL : TIME_T_MAX;
  70. /* Only get the built-in PSL if we do not already have the "latest". */
  71. if(!psl && !pslcache->dynamic)
  72. #endif
  73. psl = psl_builtin();
  74. if(psl) {
  75. Curl_psl_destroy(pslcache);
  76. pslcache->psl = psl;
  77. pslcache->dynamic = dynamic;
  78. pslcache->expires = expires;
  79. }
  80. }
  81. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); /* Release exclusive lock. */
  82. Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED);
  83. }
  84. psl = pslcache->psl;
  85. if(!psl)
  86. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  87. return psl;
  88. }
  89. void Curl_psl_release(struct Curl_easy *easy)
  90. {
  91. Curl_share_unlock(easy, CURL_LOCK_DATA_PSL);
  92. }
  93. #endif /* USE_LIBPSL */