cookie_interface.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://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. /* This example shows usage of simple cookie interface. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <time.h>
  28. #include <curl/curl.h>
  29. static void
  30. print_cookies(CURL *curl)
  31. {
  32. CURLcode res;
  33. struct curl_slist *cookies;
  34. struct curl_slist *nc;
  35. int i;
  36. printf("Cookies, curl knows:\n");
  37. res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  38. if (res != CURLE_OK) {
  39. fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", curl_easy_strerror(res));
  40. exit(1);
  41. }
  42. nc = cookies, i = 1;
  43. while (nc) {
  44. printf("[%d]: %s\n", i, nc->data);
  45. nc = nc->next;
  46. i++;
  47. }
  48. if (i == 1) {
  49. printf("(none)\n");
  50. }
  51. curl_slist_free_all(cookies);
  52. }
  53. int
  54. main(void)
  55. {
  56. CURL *curl;
  57. CURLcode res;
  58. curl_global_init(CURL_GLOBAL_ALL);
  59. curl = curl_easy_init();
  60. if (curl) {
  61. char nline[256];
  62. curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
  63. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  64. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* just to start the cookie engine */
  65. res = curl_easy_perform(curl);
  66. if (res != CURLE_OK) {
  67. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  68. return 1;
  69. }
  70. print_cookies(curl);
  71. printf("Erasing curl's knowledge of cookies!\n");
  72. curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  73. print_cookies(curl);
  74. printf("-----------------------------------------------\n"
  75. "Setting a cookie \"PREF\" via cookie interface:\n");
  76. #ifdef WIN32
  77. #define snprintf _snprintf
  78. #endif
  79. /* Netscape format cookie */
  80. snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
  81. ".google.com", "TRUE", "/", "FALSE", (unsigned long)time(NULL) + 31337UL, "PREF", "hello google, i like you very much!");
  82. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  83. if (res != CURLE_OK) {
  84. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
  85. return 1;
  86. }
  87. /* HTTP-header style cookie */
  88. snprintf(nline, sizeof(nline),
  89. "Set-Cookie: OLD_PREF=3d141414bf4209321; "
  90. "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
  91. res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
  92. if (res != CURLE_OK) {
  93. fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
  94. return 1;
  95. }
  96. print_cookies(curl);
  97. res = curl_easy_perform(curl);
  98. if (res != CURLE_OK) {
  99. fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
  100. return 1;
  101. }
  102. }
  103. else {
  104. fprintf(stderr, "Curl init failed!\n");
  105. return 1;
  106. }
  107. curl_global_cleanup();
  108. return 0;
  109. }