2
0

lib1554.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 "test.h"
  25. #include "memdebug.h"
  26. static const char *ldata_names[] = {
  27. "NONE",
  28. "SHARE",
  29. "COOKIE",
  30. "DNS",
  31. "SESSION",
  32. "CONNECT",
  33. "PSL",
  34. "HSTS",
  35. "NULL",
  36. };
  37. static void test_lock(CURL *handle, curl_lock_data data,
  38. curl_lock_access laccess, void *useptr)
  39. {
  40. (void)handle;
  41. (void)data;
  42. (void)laccess;
  43. (void)useptr;
  44. printf("-> Mutex lock %s\n", ldata_names[data]);
  45. }
  46. static void test_unlock(CURL *handle, curl_lock_data data, void *useptr)
  47. {
  48. (void)handle;
  49. (void)data;
  50. (void)useptr;
  51. printf("<- Mutex unlock %s\n", ldata_names[data]);
  52. }
  53. /* test function */
  54. CURLcode test(char *URL)
  55. {
  56. CURLcode res = CURLE_OK;
  57. CURLSH *share = NULL;
  58. int i;
  59. global_init(CURL_GLOBAL_ALL);
  60. share = curl_share_init();
  61. if(!share) {
  62. fprintf(stderr, "curl_share_init() failed\n");
  63. goto test_cleanup;
  64. }
  65. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
  66. curl_share_setopt(share, CURLSHOPT_LOCKFUNC, test_lock);
  67. curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, test_unlock);
  68. /* Loop the transfer and cleanup the handle properly every lap. This will
  69. still reuse connections since the pool is in the shared object! */
  70. for(i = 0; i < 3; i++) {
  71. CURL *curl = curl_easy_init();
  72. if(curl) {
  73. curl_easy_setopt(curl, CURLOPT_URL, URL);
  74. /* use the share object */
  75. curl_easy_setopt(curl, CURLOPT_SHARE, share);
  76. /* Perform the request, res will get the return code */
  77. res = curl_easy_perform(curl);
  78. /* always cleanup */
  79. curl_easy_cleanup(curl);
  80. /* Check for errors */
  81. if(res != CURLE_OK) {
  82. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  83. curl_easy_strerror(res));
  84. goto test_cleanup;
  85. }
  86. }
  87. }
  88. test_cleanup:
  89. curl_share_cleanup(share);
  90. curl_global_cleanup();
  91. return res;
  92. }