aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2005-07-27 22:17:14 +0000
committerDaniel Stenberg <daniel@haxx.se>2005-07-27 22:17:14 +0000
commit2236ba0d206fe9fef5d93889ee652feaa03fe089 (patch)
tree2651d5300c11cf99f0abd3d73b9d7ddb14efa748 /lib
parent463c0f7096f7a0e56929a8e1b4fb3c38e164ce13 (diff)
Peteris Krumins added CURLOPT_COOKIELIST and CURLINFO_COOKIELIST, which is a
simple interface to extracting and setting cookies in libcurl's internal "cookie jar". See the new cookie_interface.c example code.
Diffstat (limited to 'lib')
-rw-r--r--lib/cookie.c90
-rw-r--r--lib/cookie.h6
-rw-r--r--lib/getinfo.c3
-rw-r--r--lib/url.c31
4 files changed, 110 insertions, 20 deletions
diff --git a/lib/cookie.c b/lib/cookie.c
index 019c00b71..00ea0d635 100644
--- a/lib/cookie.c
+++ b/lib/cookie.c
@@ -85,6 +85,9 @@ Example set of cookies:
#include <stdlib.h>
#include <string.h>
+#define _MPRINTF_REPLACE /* without this on windows OS we get undefined reference to snprintf */
+#include <curl/mprintf.h>
+
#include "urldata.h"
#include "cookie.h"
#include "strequal.h"
@@ -816,6 +819,34 @@ void Curl_cookie_cleanup(struct CookieInfo *c)
}
}
+/* get_netscape_format()
+ *
+ * Formats a string for Netscape output file, w/o a newline at the end.
+ *
+ * Function returns a char * to a formatted line. Has to be free()d
+*/
+static char *get_netscape_format(const struct Cookie *co)
+{
+ return aprintf(
+ "%s%s\t" /* domain */
+ "%s\t" /* tailmatch */
+ "%s\t" /* path */
+ "%s\t" /* secure */
+ "%u\t" /* expires */
+ "%s\t" /* name */
+ "%s", /* value */
+ /* Make sure all domains are prefixed with a dot if they allow
+ tailmatching. This is Mozilla-style. */
+ (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
+ co->domain?co->domain:"unknown",
+ co->tailmatch?"TRUE":"FALSE",
+ co->path?co->path:"/",
+ co->secure?"TRUE":"FALSE",
+ (unsigned int)co->expires,
+ co->name,
+ co->value?co->value:"");
+}
+
/*
* Curl_cookie_output()
*
@@ -847,6 +878,8 @@ int Curl_cookie_output(struct CookieInfo *c, char *dumphere)
}
if(c) {
+ char *format_ptr;
+
fputs("# Netscape HTTP Cookie File\n"
"# http://www.netscape.com/newsref/std/cookie_spec.html\n"
"# This file was generated by libcurl! Edit at your own risk.\n\n",
@@ -854,26 +887,13 @@ int Curl_cookie_output(struct CookieInfo *c, char *dumphere)
co = c->cookies;
while(co) {
- fprintf(out,
- "%s%s\t" /* domain */
- "%s\t" /* tailmatch */
- "%s\t" /* path */
- "%s\t" /* secure */
- "%u\t" /* expires */
- "%s\t" /* name */
- "%s\n", /* value */
-
- /* Make sure all domains are prefixed with a dot if they allow
- tailmatching. This is Mozilla-style. */
- (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
- co->domain?co->domain:"unknown",
- co->tailmatch?"TRUE":"FALSE",
- co->path?co->path:"/",
- co->secure?"TRUE":"FALSE",
- (unsigned int)co->expires,
- co->name,
- co->value?co->value:"");
-
+ format_ptr = get_netscape_format(co);
+ if (format_ptr == NULL) {
+ fprintf(out, "#\n# Fatal libcurl error\n");
+ return 1;
+ }
+ fprintf(out, "%s\n", format_ptr);
+ free(format_ptr);
co=co->next;
}
}
@@ -884,4 +904,34 @@ int Curl_cookie_output(struct CookieInfo *c, char *dumphere)
return 0;
}
+struct curl_slist *Curl_cookie_list(struct SessionHandle *data)
+{
+ struct curl_slist *list = NULL;
+ struct curl_slist *beg;
+ struct Cookie *c;
+ char *line;
+
+ if (data->cookies == NULL) return NULL;
+ if (data->cookies->numcookies == 0) return NULL;
+
+ c = data->cookies->cookies;
+
+ beg = list;
+ while (c) {
+ /* fill the list with _all_ the cookies we know */
+ line = get_netscape_format(c);
+ if (line == NULL) {
+ /* get_netscape_format returns null only if we run out of memory */
+
+ curl_slist_free_all(beg); /* free some memory */
+ return NULL;
+ }
+ list = curl_slist_append(list, line);
+ free(line);
+ c = c->next;
+ }
+
+ return list;
+}
+
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
diff --git a/lib/cookie.h b/lib/cookie.h
index 6f8e8e5fc..aed9f73f7 100644
--- a/lib/cookie.h
+++ b/lib/cookie.h
@@ -92,4 +92,10 @@ void Curl_cookie_freelist(struct Cookie *);
void Curl_cookie_cleanup(struct CookieInfo *);
int Curl_cookie_output(struct CookieInfo *, char *);
+#if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_COOKIES)
+#define Curl_cookie_list(x) NULL
+#else
+struct curl_slist *Curl_cookie_list(struct SessionHandle *data);
+#endif
+
#endif
diff --git a/lib/getinfo.c b/lib/getinfo.c
index 77945bbfc..47828212b 100644
--- a/lib/getinfo.c
+++ b/lib/getinfo.c
@@ -184,6 +184,9 @@ CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
case CURLINFO_SSL_ENGINES:
*param_slistp = Curl_ssl_engines_list(data);
break;
+ case CURLINFO_COOKIELIST:
+ *param_slistp = Curl_cookie_list(data);
+ break;
default:
return CURLE_BAD_FUNCTION_ARGUMENT;
}
diff --git a/lib/url.c b/lib/url.c
index 98662ceb9..07a34a94e 100644
--- a/lib/url.c
+++ b/lib/url.c
@@ -773,6 +773,37 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
*/
data->set.cookiesession = (bool)va_arg(param, long);
break;
+
+ case CURLOPT_COOKIELIST:
+ argptr = va_arg(param, char *);
+
+ if (argptr == NULL)
+ break;
+
+ if (strequal(argptr, "ALL")) {
+ if (data->cookies == NULL) {
+ break;
+ }
+ else {
+ /* clear all cookies */
+ Curl_cookie_freelist(data->cookies->cookies);
+ data->cookies->cookies = NULL;
+ break;
+ }
+ }
+
+ if (!data->cookies)
+ /* if cookie engine was not running, activate it */
+ data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
+
+ if (checkprefix("Set-Cookie:", argptr))
+ /* HTTP Header format line */
+ Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
+
+ else
+ /* Netscape format line */
+ Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
+ break;
#endif /* CURL_DISABLE_COOKIES */
case CURLOPT_HTTPGET: