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