1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24/* <DESC>
25 * Import and export cookies with COOKIELIST.
26 * </DESC>
27 */
28
29#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
32#include <errno.h>
33#include <time.h>
34
35#include <curl/curl.h>
36
37static void
38print_cookies(CURL *curl)
39{
40  CURLcode res;
41  struct curl_slist *cookies;
42  struct curl_slist *nc;
43  int i;
44
45  printf("Cookies, curl knows:\n");
46  res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
47  if(res != CURLE_OK) {
48    fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
49            curl_easy_strerror(res));
50    exit(1);
51  }
52  nc = cookies;
53  i = 1;
54  while(nc) {
55    printf("[%d]: %s\n", i, nc->data);
56    nc = nc->next;
57    i++;
58  }
59  if(i == 1) {
60    printf("(none)\n");
61  }
62  curl_slist_free_all(cookies);
63}
64
65int
66main(void)
67{
68  CURL *curl;
69  CURLcode res;
70
71  curl_global_init(CURL_GLOBAL_ALL);
72  curl = curl_easy_init();
73  if(curl) {
74    char nline[512];
75
76    curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
77    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
78    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
79    res = curl_easy_perform(curl);
80    if(res != CURLE_OK) {
81      fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
82      return 1;
83    }
84
85    print_cookies(curl);
86
87    printf("Erasing curl's knowledge of cookies!\n");
88    curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
89
90    print_cookies(curl);
91
92    printf("-----------------------------------------------\n"
93           "Setting a cookie \"PREF\" via cookie interface:\n");
94#ifdef _WIN32
95#define snprintf _snprintf
96#endif
97    /* Netscape format cookie */
98    snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
99             ".example.com", "TRUE", "/", "FALSE",
100             difftime(time(NULL) + 31337, (time_t)0),
101             "PREF", "hello example, i like you!");
102    res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
103    if(res != CURLE_OK) {
104      fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
105              curl_easy_strerror(res));
106      return 1;
107    }
108
109    /* HTTP-header style cookie. If you use the Set-Cookie format and do not
110    specify a domain then the cookie is sent for any domain and will not be
111    modified, likely not what you intended. Starting in 7.43.0 any-domain
112    cookies will not be exported either. For more information refer to the
113    CURLOPT_COOKIELIST documentation.
114    */
115    snprintf(nline, sizeof(nline),
116      "Set-Cookie: OLD_PREF=3d141414bf4209321; "
117      "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
118    res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
119    if(res != CURLE_OK) {
120      fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
121              curl_easy_strerror(res));
122      return 1;
123    }
124
125    print_cookies(curl);
126
127    res = curl_easy_perform(curl);
128    if(res != CURLE_OK) {
129      fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
130      return 1;
131    }
132
133    curl_easy_cleanup(curl);
134  }
135  else {
136    fprintf(stderr, "Curl init failed!\n");
137    return 1;
138  }
139
140  curl_global_cleanup();
141  return 0;
142}
143