2
0

cookie_interface.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /* <DESC>
  25. * Import and export cookies with COOKIELIST.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include <time.h>
  33. #include <curl/curl.h>
  34. static void
  35. print_cookies(CURL *curl)
  36. {
  37. CURLcode res;
  38. struct curl_slist *cookies;
  39. struct curl_slist *nc;
  40. int i;
  41. printf("Cookies, curl knows:\n");
  42. res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  43. if(res != CURLE_OK) {
  44. fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
  45. curl_easy_strerror(res));
  46. exit(1);
  47. }
  48. nc = cookies;
  49. i = 1;
  50. while(nc) {
  51. printf("[%d]: %s\n", i, nc->data);
  52. nc = nc->next;
  53. i++;
  54. }
  55. if(i == 1) {
  56. printf("(none)\n");
  57. }
  58. curl_slist_free_all(cookies);
  59. }
  60. int
  61. main(void)
  62. {
  63. CURL *curl;
  64. CURLcode res;
  65. curl_global_init(CURL_GLOBAL_ALL);
  66. curl = curl_easy_init();
  67. if(curl) {
  68. char nline[512];
  69. curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
  70. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  71. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
  72. res = curl_easy_perform(curl);
  73. if(res != CURLE_OK) {
  74. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  75. return 1;
  76. }
  77. print_cookies(curl);
  78. printf("Erasing curl's knowledge of cookies!\n");
  79. curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  80. print_cookies(curl);
  81. printf("-----------------------------------------------\n"
  82. "Setting a cookie \"PREF\" via cookie interface:\n");
  83. #ifdef WIN32
  84. #define snprintf _snprintf
  85. #endif
  86. /* Netscape format cookie */
  87. snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
  88. ".example.com", "TRUE", "/", "FALSE",
  89. difftime(time(NULL) + 31337, (time_t)0),
  90. "PREF", "hello example, i like you very much!");
  91. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  92. if(res != CURLE_OK) {
  93. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  94. curl_easy_strerror(res));
  95. return 1;
  96. }
  97. /* HTTP-header style cookie. If you use the Set-Cookie format and do not
  98. specify a domain then the cookie is sent for any domain and will not be
  99. modified, likely not what you intended. Starting in 7.43.0 any-domain
  100. cookies will not be exported either. For more information refer to the
  101. CURLOPT_COOKIELIST documentation.
  102. */
  103. snprintf(nline, sizeof(nline),
  104. "Set-Cookie: OLD_PREF=3d141414bf4209321; "
  105. "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
  106. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  107. if(res != CURLE_OK) {
  108. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  109. curl_easy_strerror(res));
  110. return 1;
  111. }
  112. print_cookies(curl);
  113. res = curl_easy_perform(curl);
  114. if(res != CURLE_OK) {
  115. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  116. return 1;
  117. }
  118. curl_easy_cleanup(curl);
  119. }
  120. else {
  121. fprintf(stderr, "Curl init failed!\n");
  122. return 1;
  123. }
  124. curl_global_cleanup();
  125. return 0;
  126. }