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