]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cookies: Support multiple -b parameters
authorDaniel Gustafsson <daniel@yesql.se>
Thu, 25 Feb 2021 17:12:28 +0000 (18:12 +0100)
committerDaniel Gustafsson <daniel@yesql.se>
Thu, 25 Feb 2021 17:12:28 +0000 (18:12 +0100)
Previously only a single -b cookie parameter was supported with the last
one winning.  This adds support for supplying multiple -b params to have
them serialized semicolon separated.  Both cookiefiles and cookies can be
entered multiple times.

Closes #6649
Reviewed-by: Daniel Stenberg <daniel@haxx.se>
docs/cmdline-opts/cookie.d
src/tool_cfgable.c
src/tool_cfgable.h
src/tool_getparam.c
src/tool_operate.c
tests/data/test329
tests/data/test6

index 5d2c9d7838609f959e96c0d3d5b44a46bd50f2e3..d5e63e427bd8f87103aa5268bfebc2331043e7f3 100644 (file)
@@ -31,7 +31,7 @@ name then both will be sent on a future transfer to that server, likely not
 what you intended.  To address these issues set a domain in Set-Cookie (doing
 that will include sub domains) or use the Netscape format.
 
-If this option is used several times, the last one will be used.
+This option can be used multiple times.
 
 Users very often want to both read cookies from a file and write updated
 cookies back to a file, so using both --cookie and --cookie-jar in the same
index ea186a7c38609f9dbf56a790d8446b5a55300ab7..c3f7cecb14b0d6614131ef29268889a1aa471b64 100644 (file)
@@ -56,9 +56,9 @@ static void free_config_fields(struct OperationConfig *config)
   Curl_safefree(config->useragent);
   Curl_safefree(config->altsvc);
   Curl_safefree(config->hsts);
-  Curl_safefree(config->cookie);
+  curl_slist_free_all(config->cookies);
   Curl_safefree(config->cookiejar);
-  Curl_safefree(config->cookiefile);
+  curl_slist_free_all(config->cookiefiles);
 
   Curl_safefree(config->postfields);
   Curl_safefree(config->referer);
index bd84315f57daf91d1866ad36c5edc115c8ef2231..95c66d0817e5fb829e511cdbd61eb09d22d97e4c 100644 (file)
@@ -54,9 +54,9 @@ struct OperationConfig {
   char *random_file;
   char *egd_file;
   char *useragent;
-  char *cookie;             /* single line with specified cookies */
+  struct curl_slist *cookies;  /* cookies to serialize into a single line */
   char *cookiejar;          /* write to this file */
-  char *cookiefile;         /* read from this file */
+  struct curl_slist *cookiefiles;  /* file(s) to load cookies from */
   char *altsvc;             /* alt-svc cache file name */
   char *hsts;               /* HSTS cache file name */
   bool cookiesession;       /* new session? */
index 5b155cf00f4e1865e490b49ad40153901d4bb363..f1393c37389c369a0287f8dd9a86159aad7a464f 100644 (file)
@@ -1320,11 +1320,15 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
         }
         else if(strchr(nextarg, '=')) {
           /* A cookie string must have a =-letter */
-          GetStr(&config->cookie, nextarg);
+          err = add2list(&config->cookies, nextarg);
+          if(err)
+            return err;
           break;
         }
         /* We have a cookie file to read from! */
-        GetStr(&config->cookiefile, nextarg);
+        err = add2list(&config->cookiefiles, nextarg);
+        if(err)
+          return err;
       }
       break;
     case 'B':
index 2605a4cdf74faa59e9dc1049943900f37217cd86..e2b287269a6ae5e2136072dee58da8564dd67927 100644 (file)
@@ -82,6 +82,7 @@
 #include "tool_help.h"
 #include "tool_hugehelp.h"
 #include "tool_progress.h"
+#include "dynbuf.h"
 
 #include "memdebug.h" /* keep this as LAST include */
 
@@ -1765,11 +1766,35 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
         my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
 
-        if(config->cookie)
-          my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
+        if(config->cookies) {
+          struct curlx_dynbuf cookies;
+          struct curl_slist *cl;
+          CURLcode ret;
 
-        if(config->cookiefile)
-          my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
+          /* The maximum size needs to match MAX_NAME in cookie.h */
+          curlx_dyn_init(&cookies, 4096);
+          for(cl = config->cookies; cl; cl = cl->next) {
+            if(cl == config->cookies)
+              ret = curlx_dyn_addf(&cookies, "%s", cl->data);
+            else
+              ret = curlx_dyn_addf(&cookies, ";%s", cl->data);
+
+            if(ret) {
+              result = CURLE_OUT_OF_MEMORY;
+              break;
+            }
+          }
+
+          my_setopt_str(curl, CURLOPT_COOKIE, curlx_dyn_ptr(&cookies));
+          curlx_dyn_free(&cookies);
+        }
+
+        if(config->cookiefiles) {
+          struct curl_slist *cfl;
+
+          for(cfl = config->cookiefiles; cfl; cfl = cfl->next)
+            my_setopt_str(curl, CURLOPT_COOKIEFILE, cfl->data);
+        }
 
         /* new in libcurl 7.9 */
         if(config->cookiejar)
index 81b8383c1d553ab85a549bc25b04afd5724ec7df..67aac4baa85f67cb5270838bc2cdc717237a778c 100644 (file)
@@ -33,6 +33,9 @@ moo
 <file name="log/jar329.txt" mode="text">
 .host.foo.com  TRUE    /we/want/       FALSE   2147483647      test    no
 </file>
+<file name="log/jar329-2.txt" mode="text">
+.host.foo.com  TRUE    /we/want/       FALSE   2147483647      tester  yes
+</file>
 <server>
 http
 </server>
@@ -46,7 +49,7 @@ HTTP cookie with Max-Age=0
 TZ=GMT
 </setenv>
  <command>
-http://%HOSTIP:%HTTPPORT/we/want/329 -b log/jar329.txt -H "Host: host.foo.com" http://%HOSTIP:%HTTPPORT/we/want/3290002
+http://%HOSTIP:%HTTPPORT/we/want/329 -b log/jar329.txt -b log/jar329-2.txt -H "Host: host.foo.com" http://%HOSTIP:%HTTPPORT/we/want/3290002
 </command>
 </client>
 
@@ -57,12 +60,13 @@ GET /we/want/329 HTTP/1.1
 Host: host.foo.com\r
 User-Agent: curl/%VERSION\r
 Accept: */*\r
-Cookie: test=no\r
+Cookie: tester=yes; test=no\r
 \r
 GET /we/want/3290002 HTTP/1.1\r
 Host: host.foo.com\r
 User-Agent: curl/%VERSION\r
 Accept: */*\r
+Cookie: tester=yes\r
 \r
 </protocol>
 </verify>
index efd15a9019788845b87535bb3e2eae3ceb687912..bbd67cbf89d04401020ba7ab1fcb4428984d672e 100644 (file)
@@ -29,7 +29,7 @@ http
 HTTP with simple cookie send
  </name>
  <command>
-http://%HOSTIP:%HTTPPORT/we/want/that/page/6 -b "name=contents;name2=content2"
+http://%HOSTIP:%HTTPPORT/we/want/that/page/6 -b "name=contents;name2=content2" -b name3=content3
 </command>
 </client>
 
@@ -40,7 +40,7 @@ GET /we/want/that/page/6 HTTP/1.1
 Host: %HOSTIP:%HTTPPORT\r
 User-Agent: curl/%VERSION\r
 Accept: */*\r
-Cookie: name=contents;name2=content2\r
+Cookie: name=contents;name2=content2;name3=content3\r
 \r
 </protocol>
 </verify>