lib1915.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "testutil.h"
  26. #include "warnless.h"
  27. #include "memdebug.h"
  28. struct entry {
  29. const char *name;
  30. const char *exp;
  31. };
  32. static const struct entry preload_hosts[] = {
  33. #if (SIZEOF_TIME_T < 5)
  34. { "1.example.com", "20370320 01:02:03" },
  35. { "2.example.com", "20370320 03:02:01" },
  36. { "3.example.com", "20370319 01:02:03" },
  37. #else
  38. { "1.example.com", "25250320 01:02:03" },
  39. { "2.example.com", "25250320 03:02:01" },
  40. { "3.example.com", "25250319 01:02:03" },
  41. #endif
  42. { "4.example.com", "" },
  43. { NULL, NULL } /* end of list marker */
  44. };
  45. struct state {
  46. int index;
  47. };
  48. /* "read" is from the point of the library, it wants data from us */
  49. static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
  50. void *userp)
  51. {
  52. const char *host;
  53. const char *expire;
  54. struct state *s = (struct state *)userp;
  55. (void)easy;
  56. host = preload_hosts[s->index].name;
  57. expire = preload_hosts[s->index++].exp;
  58. if(host && (strlen(host) < e->namelen)) {
  59. strcpy(e->name, host);
  60. e->includeSubDomains = FALSE;
  61. strcpy(e->expire, expire);
  62. fprintf(stderr, "add '%s'\n", host);
  63. }
  64. else
  65. return CURLSTS_DONE;
  66. return CURLSTS_OK;
  67. }
  68. /* verify error from callback */
  69. static CURLSTScode hstsreadfail(CURL *easy, struct curl_hstsentry *e,
  70. void *userp)
  71. {
  72. (void)easy;
  73. (void)e;
  74. (void)userp;
  75. return CURLSTS_FAIL;
  76. }
  77. /* check that we get the hosts back in the save */
  78. static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
  79. struct curl_index *i, void *userp)
  80. {
  81. (void)easy;
  82. (void)userp;
  83. printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
  84. return CURLSTS_OK;
  85. }
  86. /*
  87. * Read/write HSTS cache entries via callback.
  88. */
  89. int test(char *URL)
  90. {
  91. CURLcode res = CURLE_OK;
  92. CURL *hnd;
  93. struct state st = {0};
  94. global_init(CURL_GLOBAL_ALL);
  95. easy_init(hnd);
  96. easy_setopt(hnd, CURLOPT_URL, URL);
  97. easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
  98. easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  99. easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  100. easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  101. easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  102. res = curl_easy_perform(hnd);
  103. curl_easy_cleanup(hnd);
  104. hnd = NULL;
  105. printf("First request returned %d\n", (int)res);
  106. res = CURLE_OK;
  107. easy_init(hnd);
  108. easy_setopt(hnd, CURLOPT_URL, URL);
  109. easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsreadfail);
  110. easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  111. easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  112. easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  113. easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  114. res = curl_easy_perform(hnd);
  115. curl_easy_cleanup(hnd);
  116. hnd = NULL;
  117. printf("Second request returned %d\n", (int)res);
  118. test_cleanup:
  119. curl_easy_cleanup(hnd);
  120. curl_global_cleanup();
  121. return (int)res;
  122. }