lib1915.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2020 - 2022, 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. /* curl turns 39 that day just before 31-bit time_t overflow */
  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. { "4.example.com", "" },
  38. { NULL, NULL } /* end of list marker */
  39. };
  40. struct state {
  41. int index;
  42. };
  43. /* "read" is from the point of the library, it wants data from us */
  44. static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
  45. void *userp)
  46. {
  47. const char *host;
  48. const char *expire;
  49. struct state *s = (struct state *)userp;
  50. (void)easy;
  51. host = preload_hosts[s->index].name;
  52. expire = preload_hosts[s->index++].exp;
  53. if(host && (strlen(host) < e->namelen)) {
  54. strcpy(e->name, host);
  55. e->includeSubDomains = FALSE;
  56. strcpy(e->expire, expire);
  57. fprintf(stderr, "add '%s'\n", host);
  58. }
  59. else
  60. return CURLSTS_DONE;
  61. return CURLSTS_OK;
  62. }
  63. /* verify error from callback */
  64. static CURLSTScode hstsreadfail(CURL *easy, struct curl_hstsentry *e,
  65. void *userp)
  66. {
  67. (void)easy;
  68. (void)e;
  69. (void)userp;
  70. return CURLSTS_FAIL;
  71. }
  72. /* check that we get the hosts back in the save */
  73. static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
  74. struct curl_index *i, void *userp)
  75. {
  76. (void)easy;
  77. (void)userp;
  78. printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
  79. return CURLSTS_OK;
  80. }
  81. /*
  82. * Read/write HSTS cache entries via callback.
  83. */
  84. int test(char *URL)
  85. {
  86. CURLcode ret = CURLE_OK;
  87. CURL *hnd;
  88. struct state st = {0};
  89. curl_global_init(CURL_GLOBAL_ALL);
  90. hnd = curl_easy_init();
  91. if(hnd) {
  92. curl_easy_setopt(hnd, CURLOPT_URL, URL);
  93. curl_easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
  94. curl_easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  95. curl_easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  96. curl_easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  97. curl_easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  98. ret = curl_easy_perform(hnd);
  99. curl_easy_cleanup(hnd);
  100. printf("First request returned %d\n", (int)ret);
  101. }
  102. hnd = curl_easy_init();
  103. if(hnd) {
  104. curl_easy_setopt(hnd, CURLOPT_URL, URL);
  105. curl_easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsreadfail);
  106. curl_easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  107. curl_easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  108. curl_easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  109. curl_easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  110. ret = curl_easy_perform(hnd);
  111. curl_easy_cleanup(hnd);
  112. printf("Second request returned %d\n", (int)ret);
  113. }
  114. curl_global_cleanup();
  115. return (int)ret;
  116. }