cookie_interface.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.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
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",
47  curl_easy_strerror(res));
48  exit(1);
49  }
50  nc = cookies;
51  i = 1;
52  while(nc) {
53  printf("[%d]: %s\n", i, nc->data);
54  nc = nc->next;
55  i++;
56  }
57  if(i == 1) {
58  printf("(none)\n");
59  }
60  curl_slist_free_all(cookies);
61 }
62 
63 int
64 main(void)
65 {
66  CURL *curl;
67  CURLcode res;
68 
70  curl = curl_easy_init();
71  if(curl) {
72  char nline[256];
73 
74  curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
75  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
76  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
77  res = curl_easy_perform(curl);
78  if(res != CURLE_OK) {
79  fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
80  return 1;
81  }
82 
83  print_cookies(curl);
84 
85  printf("Erasing curl's knowledge of cookies!\n");
86  curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
87 
88  print_cookies(curl);
89 
90  printf("-----------------------------------------------\n"
91  "Setting a cookie \"PREF\" via cookie interface:\n");
92 #ifdef WIN32
93 #define snprintf _snprintf
94 #endif
95  /* Netscape format cookie */
96  snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
97  ".example.com", "TRUE", "/", "FALSE",
98  (unsigned long)time(NULL) + 31337UL,
99  "PREF", "hello example, i like you very much!");
100  res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
101  if(res != CURLE_OK) {
102  fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
103  curl_easy_strerror(res));
104  return 1;
105  }
106 
107  /* HTTP-header style cookie. If you use the Set-Cookie format and don't
108  specify a domain then the cookie is sent for any domain and will not be
109  modified, likely not what you intended. Starting in 7.43.0 any-domain
110  cookies will not be exported either. For more information refer to the
111  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 
139  return 0;
140 }
char * data
Definition: curl.h:2336
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
unsigned int i
Definition: unit1303.c:79
#define curl_easy_getinfo(handle, info, arg)
struct curl_slist * next
Definition: curl.h:2337
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
#define fprintf
Definition: curl_printf.h:41
#define snprintf
Definition: curl_printf.h:42
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08