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