2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2005-07-28 06:17:14 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-07-26 20:27:45 +08:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2015-06-18 16:17:02 +08:00
|
|
|
/* <DESC>
|
|
|
|
* Import and export cookies with COOKIELIST.
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2005-07-28 06:17:14 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-07-17 05:22:12 +08:00
|
|
|
#include <stdlib.h>
|
2005-07-28 06:17:14 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_cookies(CURL *curl)
|
|
|
|
{
|
|
|
|
CURLcode res;
|
|
|
|
struct curl_slist *cookies;
|
|
|
|
struct curl_slist *nc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("Cookies, curl knows:\n");
|
|
|
|
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
|
2016-02-11 16:42:38 +08:00
|
|
|
if(res != CURLE_OK) {
|
2016-02-11 15:44:59 +08:00
|
|
|
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
2005-07-28 06:17:14 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-11 15:59:56 +08:00
|
|
|
nc = cookies;
|
|
|
|
i = 1;
|
2016-02-11 16:42:38 +08:00
|
|
|
while(nc) {
|
2005-07-28 06:17:14 +08:00
|
|
|
printf("[%d]: %s\n", i, nc->data);
|
|
|
|
nc = nc->next;
|
|
|
|
i++;
|
|
|
|
}
|
2016-02-11 16:42:38 +08:00
|
|
|
if(i == 1) {
|
2005-07-28 06:17:14 +08:00
|
|
|
printf("(none)\n");
|
|
|
|
}
|
|
|
|
curl_slist_free_all(cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
curl = curl_easy_init();
|
2016-02-11 16:42:38 +08:00
|
|
|
if(curl) {
|
2021-07-26 20:27:45 +08:00
|
|
|
char nline[512];
|
2005-07-28 06:17:14 +08:00
|
|
|
|
2018-09-23 17:20:26 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
|
2008-05-23 05:20:07 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2016-02-11 15:44:59 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
|
2005-07-28 06:17:14 +08:00
|
|
|
res = curl_easy_perform(curl);
|
2016-02-11 16:42:38 +08:00
|
|
|
if(res != CURLE_OK) {
|
2005-07-28 06:17:14 +08:00
|
|
|
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_cookies(curl);
|
|
|
|
|
|
|
|
printf("Erasing curl's knowledge of cookies!\n");
|
2005-07-29 05:51:20 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
|
2005-07-28 06:17:14 +08:00
|
|
|
|
|
|
|
print_cookies(curl);
|
|
|
|
|
|
|
|
printf("-----------------------------------------------\n"
|
|
|
|
"Setting a cookie \"PREF\" via cookie interface:\n");
|
|
|
|
#ifdef WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
/* Netscape format cookie */
|
2021-07-26 20:27:45 +08:00
|
|
|
snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%.0f\t%s\t%s",
|
2017-04-27 06:27:59 +08:00
|
|
|
".example.com", "TRUE", "/", "FALSE",
|
2021-07-25 02:54:30 +08:00
|
|
|
difftime(time(NULL) + 31337, (time_t)0),
|
2017-04-27 06:27:59 +08:00
|
|
|
"PREF", "hello example, i like you very much!");
|
2005-07-28 06:17:14 +08:00
|
|
|
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
|
2016-02-11 16:42:38 +08:00
|
|
|
if(res != CURLE_OK) {
|
2016-02-11 15:44:59 +08:00
|
|
|
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
2010-02-15 03:40:18 +08:00
|
|
|
return 1;
|
2005-07-28 06:17:14 +08:00
|
|
|
}
|
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
/* HTTP-header style cookie. If you use the Set-Cookie format and do not
|
2015-06-04 09:48:47 +08:00
|
|
|
specify a domain then the cookie is sent for any domain and will not be
|
|
|
|
modified, likely not what you intended. Starting in 7.43.0 any-domain
|
|
|
|
cookies will not be exported either. For more information refer to the
|
|
|
|
CURLOPT_COOKIELIST documentation.
|
|
|
|
*/
|
2007-07-13 05:11:10 +08:00
|
|
|
snprintf(nline, sizeof(nline),
|
2005-07-28 06:17:14 +08:00
|
|
|
"Set-Cookie: OLD_PREF=3d141414bf4209321; "
|
2017-04-27 06:30:34 +08:00
|
|
|
"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
|
2005-07-28 06:17:14 +08:00
|
|
|
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
|
2016-02-11 16:42:38 +08:00
|
|
|
if(res != CURLE_OK) {
|
2016-02-11 15:44:59 +08:00
|
|
|
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
|
|
|
|
curl_easy_strerror(res));
|
2010-02-15 03:40:18 +08:00
|
|
|
return 1;
|
2005-07-28 06:17:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
print_cookies(curl);
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
2016-02-11 16:42:38 +08:00
|
|
|
if(res != CURLE_OK) {
|
2005-07-28 06:17:14 +08:00
|
|
|
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-11 15:44:59 +08:00
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
2005-07-28 06:17:14 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Curl init failed!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_global_cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|