113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci/* <DESC> 2513498266Sopenharmony_ci * Import and export cookies with COOKIELIST. 2613498266Sopenharmony_ci * </DESC> 2713498266Sopenharmony_ci */ 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include <stdio.h> 3013498266Sopenharmony_ci#include <string.h> 3113498266Sopenharmony_ci#include <stdlib.h> 3213498266Sopenharmony_ci#include <errno.h> 3313498266Sopenharmony_ci#include <time.h> 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci#include <curl/curl.h> 3613498266Sopenharmony_ci 3713498266Sopenharmony_cistatic void 3813498266Sopenharmony_ciprint_cookies(CURL *curl) 3913498266Sopenharmony_ci{ 4013498266Sopenharmony_ci CURLcode res; 4113498266Sopenharmony_ci struct curl_slist *cookies; 4213498266Sopenharmony_ci struct curl_slist *nc; 4313498266Sopenharmony_ci int i; 4413498266Sopenharmony_ci 4513498266Sopenharmony_ci printf("Cookies, curl knows:\n"); 4613498266Sopenharmony_ci res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies); 4713498266Sopenharmony_ci if(res != CURLE_OK) { 4813498266Sopenharmony_ci fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n", 4913498266Sopenharmony_ci curl_easy_strerror(res)); 5013498266Sopenharmony_ci exit(1); 5113498266Sopenharmony_ci } 5213498266Sopenharmony_ci nc = cookies; 5313498266Sopenharmony_ci i = 1; 5413498266Sopenharmony_ci while(nc) { 5513498266Sopenharmony_ci printf("[%d]: %s\n", i, nc->data); 5613498266Sopenharmony_ci nc = nc->next; 5713498266Sopenharmony_ci i++; 5813498266Sopenharmony_ci } 5913498266Sopenharmony_ci if(i == 1) { 6013498266Sopenharmony_ci printf("(none)\n"); 6113498266Sopenharmony_ci } 6213498266Sopenharmony_ci curl_slist_free_all(cookies); 6313498266Sopenharmony_ci} 6413498266Sopenharmony_ci 6513498266Sopenharmony_ciint 6613498266Sopenharmony_cimain(void) 6713498266Sopenharmony_ci{ 6813498266Sopenharmony_ci CURL *curl; 6913498266Sopenharmony_ci CURLcode res; 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci curl_global_init(CURL_GLOBAL_ALL); 7213498266Sopenharmony_ci curl = curl_easy_init(); 7313498266Sopenharmony_ci if(curl) { 7413498266Sopenharmony_ci char nline[512]; 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/"); 7713498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 7813498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */ 7913498266Sopenharmony_ci res = curl_easy_perform(curl); 8013498266Sopenharmony_ci if(res != CURLE_OK) { 8113498266Sopenharmony_ci fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res)); 8213498266Sopenharmony_ci return 1; 8313498266Sopenharmony_ci } 8413498266Sopenharmony_ci 8513498266Sopenharmony_ci print_cookies(curl); 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci printf("Erasing curl's knowledge of cookies!\n"); 8813498266Sopenharmony_ci curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL"); 8913498266Sopenharmony_ci 9013498266Sopenharmony_ci print_cookies(curl); 9113498266Sopenharmony_ci 9213498266Sopenharmony_ci printf("-----------------------------------------------\n" 9313498266Sopenharmony_ci "Setting a cookie \"PREF\" via cookie interface:\n"); 9413498266Sopenharmony_ci#ifdef _WIN32 9513498266Sopenharmony_ci#define snprintf _snprintf 9613498266Sopenharmony_ci#endif 9713498266Sopenharmony_ci /* Netscape format cookie */ 9813498266Sopenharmony_ci snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s", 9913498266Sopenharmony_ci ".example.com", "TRUE", "/", "FALSE", 10013498266Sopenharmony_ci difftime(time(NULL) + 31337, (time_t)0), 10113498266Sopenharmony_ci "PREF", "hello example, i like you!"); 10213498266Sopenharmony_ci res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline); 10313498266Sopenharmony_ci if(res != CURLE_OK) { 10413498266Sopenharmony_ci fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", 10513498266Sopenharmony_ci curl_easy_strerror(res)); 10613498266Sopenharmony_ci return 1; 10713498266Sopenharmony_ci } 10813498266Sopenharmony_ci 10913498266Sopenharmony_ci /* HTTP-header style cookie. If you use the Set-Cookie format and do not 11013498266Sopenharmony_ci specify a domain then the cookie is sent for any domain and will not be 11113498266Sopenharmony_ci modified, likely not what you intended. Starting in 7.43.0 any-domain 11213498266Sopenharmony_ci cookies will not be exported either. For more information refer to the 11313498266Sopenharmony_ci CURLOPT_COOKIELIST documentation. 11413498266Sopenharmony_ci */ 11513498266Sopenharmony_ci snprintf(nline, sizeof(nline), 11613498266Sopenharmony_ci "Set-Cookie: OLD_PREF=3d141414bf4209321; " 11713498266Sopenharmony_ci "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com"); 11813498266Sopenharmony_ci res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline); 11913498266Sopenharmony_ci if(res != CURLE_OK) { 12013498266Sopenharmony_ci fprintf(stderr, "Curl curl_easy_setopt failed: %s\n", 12113498266Sopenharmony_ci curl_easy_strerror(res)); 12213498266Sopenharmony_ci return 1; 12313498266Sopenharmony_ci } 12413498266Sopenharmony_ci 12513498266Sopenharmony_ci print_cookies(curl); 12613498266Sopenharmony_ci 12713498266Sopenharmony_ci res = curl_easy_perform(curl); 12813498266Sopenharmony_ci if(res != CURLE_OK) { 12913498266Sopenharmony_ci fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res)); 13013498266Sopenharmony_ci return 1; 13113498266Sopenharmony_ci } 13213498266Sopenharmony_ci 13313498266Sopenharmony_ci curl_easy_cleanup(curl); 13413498266Sopenharmony_ci } 13513498266Sopenharmony_ci else { 13613498266Sopenharmony_ci fprintf(stderr, "Curl init failed!\n"); 13713498266Sopenharmony_ci return 1; 13813498266Sopenharmony_ci } 13913498266Sopenharmony_ci 14013498266Sopenharmony_ci curl_global_cleanup(); 14113498266Sopenharmony_ci return 0; 14213498266Sopenharmony_ci} 143