quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

cookie_interface.c (4077B)


      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 int 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     return 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   return 0;
     64 }
     65 
     66 int
     67 main(void)
     68 {
     69   CURL *curl;
     70   CURLcode res;
     71 
     72   curl_global_init(CURL_GLOBAL_ALL);
     73   curl = curl_easy_init();
     74   if(curl) {
     75     char nline[512];
     76 
     77     curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
     78     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
     79     curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
     80     res = curl_easy_perform(curl);
     81     if(res != CURLE_OK) {
     82       fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
     83       return 1;
     84     }
     85 
     86     print_cookies(curl);
     87 
     88     printf("Erasing curl's knowledge of cookies!\n");
     89     curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
     90 
     91     print_cookies(curl);
     92 
     93     printf("-----------------------------------------------\n"
     94            "Setting a cookie \"PREF\" via cookie interface:\n");
     95     /* Netscape format cookie */
     96     curl_msnprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
     97                    ".example.com", "TRUE", "/", "FALSE",
     98                    difftime(time(NULL) + 31337, (time_t)0),
     99                    "PREF", "hello example, i like you!");
    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 do not
    108        specify a domain then the cookie is sent for any domain and is not
    109        modified, likely not what you intended. For more information refer to
    110        the CURLOPT_COOKIELIST documentation.
    111     */
    112     curl_msnprintf(nline, sizeof(nline),
    113       "Set-Cookie: OLD_PREF=3d141414bf4209321; "
    114       "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
    115     res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
    116     if(res != CURLE_OK) {
    117       fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
    118               curl_easy_strerror(res));
    119       return 1;
    120     }
    121 
    122     print_cookies(curl);
    123 
    124     res = curl_easy_perform(curl);
    125     if(res != CURLE_OK) {
    126       fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
    127       return 1;
    128     }
    129 
    130     curl_easy_cleanup(curl);
    131   }
    132   else {
    133     fprintf(stderr, "Curl init failed!\n");
    134     return 1;
    135   }
    136 
    137   curl_global_cleanup();
    138   return 0;
    139 }