• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  
32  static void
print_cookies(CURL * curl)33  print_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  
58  int
main(void)59  main(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. If you use the Set-Cookie format and don't
100      specify a domain then the cookie is sent for any domain and will not be
101      modified, likely not what you intended. Starting in 7.43.0 any-domain
102      cookies will not be exported either. For more information refer to the
103      CURLOPT_COOKIELIST documentation.
104      */
105      snprintf(nline, sizeof(nline),
106        "Set-Cookie: OLD_PREF=3d141414bf4209321; "
107        "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
108      res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
109      if (res != CURLE_OK) {
110        fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", curl_easy_strerror(res));
111        return 1;
112      }
113  
114      print_cookies(curl);
115  
116      res = curl_easy_perform(curl);
117      if (res != CURLE_OK) {
118        fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
119        return 1;
120      }
121    }
122    else {
123      fprintf(stderr, "Curl init failed!\n");
124      return 1;
125    }
126  
127    curl_global_cleanup();
128    return 0;
129  }
130