cookie_interface.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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!");
  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 is not
  99. modified, likely not what you intended. For more information refer to
  100. the CURLOPT_COOKIELIST documentation.
  101. */
  102. snprintf(nline, sizeof(nline),
  103. "Set-Cookie: OLD_PREF=3d141414bf4209321; "
  104. "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
  105. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  106. if(res != CURLE_OK) {
  107. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  108. curl_easy_strerror(res));
  109. return 1;
  110. }
  111. print_cookies(curl);
  112. res = curl_easy_perform(curl);
  113. if(res != CURLE_OK) {
  114. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  115. return 1;
  116. }
  117. curl_easy_cleanup(curl);
  118. }
  119. else {
  120. fprintf(stderr, "Curl init failed!\n");
  121. return 1;
  122. }
  123. curl_global_cleanup();
  124. return 0;
  125. }