lib1915.c 3.8 KB

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