xref: /curl/docs/examples/cookie_interface.c (revision 45202cbb)
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 #include <curl/mprintf.h>
36 
37 static void
print_cookies(CURL * curl)38 print_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 
65 int
main(void)66 main(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     /* Netscape format cookie */
95     curl_msnprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
96                    ".example.com", "TRUE", "/", "FALSE",
97                    difftime(time(NULL) + 31337, (time_t)0),
98                    "PREF", "hello example, i like you!");
99     res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
100     if(res != CURLE_OK) {
101       fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
102               curl_easy_strerror(res));
103       return 1;
104     }
105 
106     /* HTTP-header style cookie. If you use the Set-Cookie format and do not
107        specify a domain then the cookie is sent for any domain and is not
108        modified, likely not what you intended. For more information refer to
109        the CURLOPT_COOKIELIST documentation.
110     */
111     curl_msnprintf(nline, sizeof(nline),
112       "Set-Cookie: OLD_PREF=3d141414bf4209321; "
113       "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
114     res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
115     if(res != CURLE_OK) {
116       fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
117               curl_easy_strerror(res));
118       return 1;
119     }
120 
121     print_cookies(curl);
122 
123     res = curl_easy_perform(curl);
124     if(res != CURLE_OK) {
125       fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
126       return 1;
127     }
128 
129     curl_easy_cleanup(curl);
130   }
131   else {
132     fprintf(stderr, "Curl init failed!\n");
133     return 1;
134   }
135 
136   curl_global_cleanup();
137   return 0;
138 }
139