etlegacy-libs/curl/docs/examples/cookie_interface.c

141 lines
4 KiB
C
Raw Normal View History

2012-06-01 15:23:17 +00:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2019-01-03 15:09:10 +00:00
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
2012-06-01 15:23:17 +00:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2016-09-16 04:29:17 +00:00
* are also available at https://curl.haxx.se/docs/copyright.html.
2012-06-01 15:23:17 +00: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.
*
***************************************************************************/
2016-09-16 04:29:17 +00:00
/* <DESC>
* Import and export cookies with COOKIELIST.
* </DESC>
*/
2012-06-01 15:23:17 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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-09-16 04:29:17 +00:00
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
curl_easy_strerror(res));
2012-06-01 15:23:17 +00:00
exit(1);
}
2017-07-01 14:06:24 +00:00
nc = cookies;
i = 1;
2016-09-16 04:29:17 +00:00
while(nc) {
2012-06-01 15:23:17 +00:00
printf("[%d]: %s\n", i, nc->data);
nc = nc->next;
i++;
}
2016-09-16 04:29:17 +00:00
if(i == 1) {
2012-06-01 15:23:17 +00: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-09-16 04:29:17 +00:00
if(curl) {
2012-06-01 15:23:17 +00:00
char nline[256];
2019-01-03 15:09:10 +00:00
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
2012-06-01 15:23:17 +00:00
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
2016-09-16 04:29:17 +00:00
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); /* start cookie engine */
2012-06-01 15:23:17 +00:00
res = curl_easy_perform(curl);
2016-09-16 04:29:17 +00:00
if(res != CURLE_OK) {
2012-06-01 15:23:17 +00: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");
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "ALL");
print_cookies(curl);
printf("-----------------------------------------------\n"
"Setting a cookie \"PREF\" via cookie interface:\n");
#ifdef WIN32
#define snprintf _snprintf
#endif
/* Netscape format cookie */
snprintf(nline, sizeof(nline), "%s\t%s\t%s\t%s\t%lu\t%s\t%s",
2017-07-01 14:06:24 +00:00
".example.com", "TRUE", "/", "FALSE",
2016-09-16 04:29:17 +00:00
(unsigned long)time(NULL) + 31337UL,
2017-07-01 14:06:24 +00:00
"PREF", "hello example, i like you very much!");
2012-06-01 15:23:17 +00:00
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
2016-09-16 04:29:17 +00:00
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
curl_easy_strerror(res));
2012-06-01 15:23:17 +00:00
return 1;
}
2016-09-16 04:29:17 +00:00
/* HTTP-header style cookie. If you use the Set-Cookie format and don't
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.
*/
2012-06-01 15:23:17 +00:00
snprintf(nline, sizeof(nline),
"Set-Cookie: OLD_PREF=3d141414bf4209321; "
2017-07-01 14:06:24 +00:00
"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.example.com");
2012-06-01 15:23:17 +00:00
res = curl_easy_setopt(curl, CURLOPT_COOKIELIST, nline);
2016-09-16 04:29:17 +00:00
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_setopt failed: %s\n",
curl_easy_strerror(res));
2012-06-01 15:23:17 +00:00
return 1;
}
print_cookies(curl);
res = curl_easy_perform(curl);
2016-09-16 04:29:17 +00:00
if(res != CURLE_OK) {
2012-06-01 15:23:17 +00:00
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(res));
return 1;
}
2016-09-16 04:29:17 +00:00
curl_easy_cleanup(curl);
2012-06-01 15:23:17 +00:00
}
else {
fprintf(stderr, "Curl init failed!\n");
return 1;
}
curl_global_cleanup();
return 0;
}