cookie_interface.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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. /* <DESC>
  23. * Import and export cookies with COOKIELIST.
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <time.h>
  31. #include <curl/curl.h>
  32. static void
  33. print_cookies(CURL *curl)
  34. {
  35. CURLcode res;
  36. struct curl_slist *cookies;
  37. struct curl_slist *nc;
  38. int i;
  39. printf("Cookies, curl knows:\n");
  40. res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  41. if(res != CURLE_OK) {
  42. fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
  43. curl_easy_strerror(res));
  44. exit(1);
  45. }
  46. nc = cookies;
  47. i = 1;
  48. while(nc) {
  49. printf("[%d]: %s\n", i, nc->data);
  50. nc = nc->next;
  51. i++;
  52. }
  53. if(i == 1) {
  54. printf("(none)\n");
  55. }
  56. curl_slist_free_all(cookies);
  57. }
  58. int
  59. main(void)
  60. {
  61. CURL *curl;
  62. CURLcode res;
  63. curl_global_init(CURL_GLOBAL_ALL);
  64. curl = curl_easy_init();
  65. if(curl) {
  66. char nline[256];
  67. curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
  68. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  69. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
  70. res = curl_easy_perform(curl);
  71. if(res != CURLE_OK) {
  72. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  73. return 1;
  74. }
  75. print_cookies(curl);
  76. printf("Erasing curl's knowledge of cookies!\n");
  77. curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  78. print_cookies(curl);
  79. printf("-----------------------------------------------\n"
  80. "Setting a cookie \"PREF\" via cookie interface:\n");
  81. #ifdef WIN32
  82. #define snprintf _snprintf
  83. #endif
  84. /* Netscape format cookie */
  85. snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
  86. ".example.com", "TRUE", "/", "FALSE",
  87. (unsigned long)time(NULL) + 31337UL,
  88. "PREF", "hello example, i like you very much!");
  89. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  90. if(res != CURLE_OK) {
  91. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
  92. curl_easy_strerror(res));
  93. return 1;
  94. }
  95. /* HTTP-header style cookie. If you use the Set-Cookie format and don't
  96. specify a domain then the cookie is sent for any domain and will not be
  97. modified, likely not what you intended. Starting in 7.43.0 any-domain
  98. cookies will not be exported either. For more information refer to the
  99. 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. }