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