2
0

cookie_interface.c 4.0 KB

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