From a52e46f3900fb02fd45423c35d970703978fe6ff Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 22 Nov 2018 13:24:02 +0100 Subject: cookies: create the cookiejar even if no cookies to save Important for when the file is going to be read again and thus must not contain old contents! Adds test 327 to verify. Reported-by: daboul on github Fixes #3299 Closes #3300 --- lib/cookie.c | 62 +++++++++++++++++++---------------------- tests/data/Makefile.inc | 2 +- tests/data/test1160 | 6 +++- tests/data/test327 | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 36 deletions(-) create mode 100644 tests/data/test327 diff --git a/lib/cookie.c b/lib/cookie.c index 5b7ab6633..d8655f790 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -1456,22 +1456,9 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere) unsigned int j; struct Cookie **array; - if((NULL == c) || (0 == c->numcookies)) - /* If there are no known cookies, we don't write or even create any - destination file */ - return 0; - /* at first, remove expired cookies */ remove_expired(c); - /* make sure we still have cookies after expiration */ - if(0 == c->numcookies) - return 0; - - array = malloc(sizeof(struct Cookie *) * c->numcookies); - if(!array) - return 1; - if(!strcmp("-", dumphere)) { /* use stdout */ out = stdout; @@ -1480,7 +1467,6 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere) else { out = fopen(dumphere, FOPEN_WRITETEXT); if(!out) { - free(array); return 1; /* failure */ } } @@ -1490,32 +1476,40 @@ static int cookie_output(struct CookieInfo *c, const char *dumphere) "# This file was generated by libcurl! Edit at your own risk.\n\n", out); - j = 0; - for(i = 0; i < COOKIE_HASH_SIZE; i++) { - for(co = c->cookies[i]; co; co = co->next) { - if(!co->domain) - continue; - array[j++] = co; - } - } - - qsort(array, c->numcookies, sizeof(struct Cookie *), cookie_sort_ct); - - for(i = 0; i < j; i++) { - format_ptr = get_netscape_format(array[i]); - if(format_ptr == NULL) { - fprintf(out, "#\n# Fatal libcurl error\n"); - free(array); + if(c->numcookies) { + array = malloc(sizeof(struct Cookie *) * c->numcookies); + if(!array) { if(!use_stdout) fclose(out); return 1; } - fprintf(out, "%s\n", format_ptr); - free(format_ptr); - } - free(array); + j = 0; + for(i = 0; i < COOKIE_HASH_SIZE; i++) { + for(co = c->cookies[i]; co; co = co->next) { + if(!co->domain) + continue; + array[j++] = co; + } + } + qsort(array, c->numcookies, sizeof(struct Cookie *), cookie_sort_ct); + + for(i = 0; i < j; i++) { + format_ptr = get_netscape_format(array[i]); + if(format_ptr == NULL) { + fprintf(out, "#\n# Fatal libcurl error\n"); + free(array); + if(!use_stdout) + fclose(out); + return 1; + } + fprintf(out, "%s\n", format_ptr); + free(format_ptr); + } + + free(array); + } if(!use_stdout) fclose(out); diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc index e58fb27fb..d100eafb5 100644 --- a/tests/data/Makefile.inc +++ b/tests/data/Makefile.inc @@ -56,7 +56,7 @@ test289 test290 test291 test292 test293 test294 test295 test296 test297 \ test298 test299 test300 test301 test302 test303 test304 test305 test306 \ test307 test308 test309 test310 test311 test312 test313 test314 test315 \ test316 test317 test318 test319 test320 test321 test322 test323 test324 \ -test325 test326 \ +test325 test326 test327 \ \ test340 \ \ diff --git a/tests/data/test1160 b/tests/data/test1160 index 26a758c4e..3fe689e96 100644 --- a/tests/data/test1160 +++ b/tests/data/test1160 @@ -43,7 +43,11 @@ Host: %HOSTIP:%HTTPPORT Accept: */* - + +# Netscape HTTP Cookie File +# https://curl.haxx.se/docs/http-cookies.html +# This file was generated by libcurl! Edit at your own risk. + diff --git a/tests/data/test327 b/tests/data/test327 new file mode 100644 index 000000000..fcb98990f --- /dev/null +++ b/tests/data/test327 @@ -0,0 +1,73 @@ + + + +HTTP +HTTP GET +cookies +header dump + + + +# This test verifies that the cookie file gets created even if the last cookie +# has been removed - particularly important when the file is going to be read +# again and thus must not contain old contents. + +# Server-side + + +HTTP/1.1 200 OK +Date: Thu, 09 Nov 2010 14:49:00 GMT +Server: test-server/fake +Content-Type: text/html +Funny-head: yesyes swsclose +Set-Cookie: foobar=name; expires=Thu, 22 Nov 2028 10:54:11 GMT; + + + +HTTP/1.1 200 OK +Date: Thu, 09 Nov 2010 14:49:00 GMT +Server: test-server/fake +Content-Type: text/html +Funny-head: yesyes swsclose +Set-Cookie: foobar=name; expires=Thu, 01 Jan 1970 00:00:00 GMT; + + + + +# Client-side + + +http + + +HTTP with cookiejar without cookies left + + +http://%HOSTIP:%HTTPPORT/we/want/327 -b none -c log/cookies327 http://%HOSTIP:%HTTPPORT/we/want/3270002 + + + +# Verify data after the test has been "shot" + + +^User-Agent:.* + + +GET /we/want/327 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* + +GET /we/want/3270002 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* +Cookie: foobar=name + + + +# Netscape HTTP Cookie File +# https://curl.haxx.se/docs/http-cookies.html +# This file was generated by libcurl! Edit at your own risk. + + + + -- cgit v1.2.3