cookie_interface.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <time.h>
  32. #include <curl/curl.h>
  33. #include <curl/mprintf.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. /* Netscape format cookie */
  84. curl_msnprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
  85. ".example.com", "TRUE", "/", "FALSE",
  86. difftime(time(NULL) + 31337, (time_t)0),
  87. "PREF", "hello example, i like you!");
  88. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  89. if(res != CURLE_OK) {
  90. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  91. curl_easy_strerror(res));
  92. return 1;
  93. }
  94. /* HTTP-header style cookie. If you use the Set-Cookie format and do not
  95. specify a domain then the cookie is sent for any domain and is not
  96. modified, likely not what you intended. For more information refer to
  97. the CURLOPT_COOKIELIST documentation.
  98. */
  99. curl_msnprintf(nline, sizeof(nline),
  100. "Set-Cookie: OLD_PREF=3d141414bf4209321; "
  101. "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
  102. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  103. if(res != CURLE_OK) {
  104. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  105. curl_easy_strerror(res));
  106. return 1;
  107. }
  108. print_cookies(curl);
  109. res = curl_easy_perform(curl);
  110. if(res != CURLE_OK) {
  111. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  112. return 1;
  113. }
  114. curl_easy_cleanup(curl);
  115. }
  116. else {
  117. fprintf(stderr, "Curl init failed!\n");
  118. return 1;
  119. }
  120. curl_global_cleanup();
  121. return 0;
  122. }