cookie_interface.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Import and export cookies with COOKIELIST.
00024  * </DESC>
00025  */
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <errno.h>
00031 #include <time.h>
00032 
00033 #include <curl/curl.h>
00034 
00035 static void
00036 print_cookies(CURL *curl)
00037 {
00038   CURLcode res;
00039   struct curl_slist *cookies;
00040   struct curl_slist *nc;
00041   int i;
00042 
00043   printf("Cookies, curl knows:\n");
00044   res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
00045   if(res != CURLE_OK) {
00046     fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
00047             curl_easy_strerror(res));
00048     exit(1);
00049   }
00050   nc = cookies, i = 1;
00051   while(nc) {
00052     printf("[%d]: %s\n", i, nc->data);
00053     nc = nc->next;
00054     i++;
00055   }
00056   if(i == 1) {
00057     printf("(none)\n");
00058   }
00059   curl_slist_free_all(cookies);
00060 }
00061 
00062 int
00063 main(void)
00064 {
00065   CURL *curl;
00066   CURLcode res;
00067 
00068   curl_global_init(CURL_GLOBAL_ALL);
00069   curl = curl_easy_init();
00070   if(curl) {
00071     char nline[256];
00072 
00073     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/");
00074     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00075     curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
00076     res = curl_easy_perform(curl);
00077     if(res != CURLE_OK) {
00078       fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
00079       return 1;
00080     }
00081 
00082     print_cookies(curl);
00083 
00084     printf("Erasing curl's knowledge of cookies!\n");
00085     curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
00086 
00087     print_cookies(curl);
00088 
00089     printf("-----------------------------------------------\n"
00090            "Setting a cookie \"PREF\" via cookie interface:\n");
00091 #ifdef WIN32
00092 #define snprintf _snprintf
00093 #endif
00094     /* Netscape format cookie */
00095     snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
00096              ".google.com", "TRUE", "/", "FALSE",
00097              (unsigned long)time(NULL) + 31337UL,
00098              "PREF", "hello google, i like you very much!");
00099     res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
00100     if(res != CURLE_OK) {
00101       fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
00102               curl_easy_strerror(res));
00103       return 1;
00104     }
00105 
00106     /* HTTP-header style cookie. If you use the Set-Cookie format and don't
00107     specify a domain then the cookie is sent for any domain and will not be
00108     modified, likely not what you intended. Starting in 7.43.0 any-domain
00109     cookies will not be exported either. For more information refer to the
00110     CURLOPT_COOKIELIST documentation.
00111     */
00112     snprintf(nline, sizeof(nline),
00113       "Set-Cookie: OLD_PREF=3d141414bf4209321; "
00114       "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
00115     res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
00116     if(res != CURLE_OK) {
00117       fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
00118               curl_easy_strerror(res));
00119       return 1;
00120     }
00121 
00122     print_cookies(curl);
00123 
00124     res = curl_easy_perform(curl);
00125     if(res != CURLE_OK) {
00126       fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
00127       return 1;
00128     }
00129 
00130     curl_easy_cleanup(curl);
00131   }
00132   else {
00133     fprintf(stderr, "Curl init failed!\n");
00134     return 1;
00135   }
00136 
00137   curl_global_cleanup();
00138   return 0;
00139 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02