]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
altsvc: clone setting in curl_easy_duphandle 5923/head
authorDaniel Stenberg <daniel@haxx.se>
Sat, 5 Sep 2020 19:21:26 +0000 (21:21 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Sun, 6 Sep 2020 07:54:54 +0000 (09:54 +0200)
The cache content is not duplicated, like other caches, but the setting
and specified file name are.

Test 1908 is extended to verify this somewhat. Since the duplicated
handle gets the same file name, the test unfortunately overwrites the
same file twice (with different contents) which makes it hard to check
automatically.

Closes #5923

lib/altsvc.c
lib/altsvc.h
lib/easy.c
lib/url.c
tests/data/test1908
tests/libtest/lib1908.c
tests/unit/unit1654.c

index fc56c251badfe985ac534e6f12509d804d86038a..d3deba3e8311b6fb3c3fdeefb04fa0a1beedf770 100644 (file)
@@ -302,11 +302,12 @@ CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
  * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
  * resources.
  */
-void Curl_altsvc_cleanup(struct altsvcinfo *altsvc)
+void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp)
 {
   struct Curl_llist_element *e;
   struct Curl_llist_element *n;
-  if(altsvc) {
+  if(*altsvcp) {
+    struct altsvcinfo *altsvc = *altsvcp;
     for(e = altsvc->list.head; e; e = n) {
       struct altsvc *as = e->ptr;
       n = e->next;
@@ -314,6 +315,7 @@ void Curl_altsvc_cleanup(struct altsvcinfo *altsvc)
     }
     free(altsvc->filename);
     free(altsvc);
+    *altsvcp = NULL; /* clear the pointer */
   }
 }
 
index df27504937443e2913aadc272e03d56296e063a6..1aeb625ab4c4b05760bdf7470022d5d95825ff3f 100644 (file)
@@ -61,7 +61,7 @@ CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file);
 CURLcode Curl_altsvc_save(struct Curl_easy *data,
                           struct altsvcinfo *asi, const char *file);
 CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl);
-void Curl_altsvc_cleanup(struct altsvcinfo *altsvc);
+void Curl_altsvc_cleanup(struct altsvcinfo **altsvc);
 CURLcode Curl_altsvc_parse(struct Curl_easy *data,
                            struct altsvcinfo *altsvc, const char *value,
                            enum alpnid srcalpn, const char *srchost,
@@ -74,5 +74,6 @@ bool Curl_altsvc_lookup(struct altsvcinfo *asi,
 #else
 /* disabled */
 #define Curl_altsvc_save(a,b,c)
+#define Curl_altsvc_cleanup(x)
 #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */
 #endif /* HEADER_CURL_ALTSVC_H */
index a69eb9e567596590c26cca664fc9af9a1e8ce74b..4da26f914b486ad51adb0c1df345db0f6a5782d5 100644 (file)
@@ -78,6 +78,7 @@
 #include "system_win32.h"
 #include "http2.h"
 #include "dynbuf.h"
+#include "altsvc.h"
 
 /* The last 3 #include files should be in this order */
 #include "curl_printf.h"
@@ -883,6 +884,15 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
       goto fail;
   }
 
+#ifdef USE_ALTSVC
+  if(data->asi) {
+    outcurl->asi = Curl_altsvc_init();
+    if(!outcurl->asi)
+      goto fail;
+    if(outcurl->set.str[STRING_ALTSVC])
+      (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]);
+  }
+#endif
   /* Clone the resolver handle, if present, for the new handle */
   if(Curl_resolver_duphandle(outcurl,
                              &outcurl->state.resolver,
@@ -930,6 +940,7 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
     Curl_dyn_free(&outcurl->state.headerb);
     Curl_safefree(outcurl->change.url);
     Curl_safefree(outcurl->change.referer);
+    Curl_altsvc_cleanup(&outcurl->asi);
     Curl_freeset(outcurl);
     free(outcurl);
   }
index 2f02a0a16a3cd15a50660640a291f583f2e3a4a8..b3884572d624cf7c059f74df06e600049dbf54da 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -391,11 +391,8 @@ CURLcode Curl_close(struct Curl_easy **datap)
   Curl_dyn_free(&data->state.headerb);
   Curl_safefree(data->state.ulbuf);
   Curl_flush_cookies(data, TRUE);
-#ifdef USE_ALTSVC
   Curl_altsvc_save(data, data->asi, data->set.str[STRING_ALTSVC]);
-  Curl_altsvc_cleanup(data->asi);
-  data->asi = NULL;
-#endif
+  Curl_altsvc_cleanup(&data->asi);
 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
   Curl_http_auth_cleanup_digest(data);
 #endif
index bf23ef9fcec8f463778caa994e1a37025674c3ee..00ce98a6aa144cbba6dbb19b34889f61e1e6827f 100644 (file)
@@ -8,7 +8,7 @@ CURLINFO_EFFECTIVE_URL
 # Server-side
 <reply>
 <data nocheck="yes">
-HTTP/1.1 200 OK
+HTTP/1.1 200 OK swsbounce
 Date: Thu, 09 Nov 2010 14:49:00 GMT
 Server: test-server/fake
 Content-Type: text/html
@@ -17,6 +17,15 @@ Content-Length: 0
 alt-svc: h2="3dbbdetxoyw4nsp6c3cc456oj2ays6s43ezxzsfxxri3h5xqd.example:443"; ma=315360000; persist=1
 
 </data>
+
+# This is the second response
+<data1 nocheck="yes">
+HTTP/1.1 200 OK
+Connection: close
+Content-Length: 0
+alt-svc: h2="second.example:443"; ma=315360000;
+
+</data1>
 </reply>
 
 # Client-side
@@ -56,6 +65,10 @@ GET /1908 HTTP/1.1
 Host: %HOSTIP:%HTTPPORT\r
 Accept: */*\r
 \r
+GET /1908 HTTP/1.1\r
+Host: %HOSTIP:%HTTPPORT\r
+Accept: */*\r
+\r
 </protocol>
 <stripfile>
 # strip out the (dynamic) expire date from the file so that the rest
index bacbc97107e2fd802002fbe7b20cc0d5d5a4313e..30e7d80190edf7a34f62544b5fec36b7ecfcbe85 100644 (file)
@@ -39,7 +39,22 @@ int test(char *URL)
     curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
     curl_easy_setopt(hnd, CURLOPT_ALTSVC, "log/altsvc-1908");
     ret = curl_easy_perform(hnd);
+
+    if(!ret) {
+      /* make a copy and check that this also has alt-svc activated */
+      CURL *also = curl_easy_duphandle(hnd);
+      if(also) {
+        ret = curl_easy_perform(also);
+        /* we close the second handle first, which makes it store the alt-svc
+           file only to get overwritten when the next handle is closed! */
+        curl_easy_cleanup(also);
+      }
+    }
+
     curl_easy_reset(hnd);
+
+    /* using the same file name for the alt-svc cache, this clobbers the
+       content just written from the 'also' handle */
     curl_easy_cleanup(hnd);
   }
   curl_global_cleanup();
index 79e97631cfd7d265ec77c4ef677d85ea93ca6102..6274d0af855c36b9054aa4d5912a17268649e9b7 100644 (file)
@@ -54,7 +54,7 @@ UNITTEST_START
     return 1;
   result = Curl_altsvc_load(asi, arg);
   if(result) {
-    Curl_altsvc_cleanup(asi);
+    Curl_altsvc_cleanup(&asi);
     return result;
   }
   curl = curl_easy_init();
@@ -131,7 +131,7 @@ UNITTEST_START
 
   curl_easy_cleanup(curl);
   fail:
-  Curl_altsvc_cleanup(asi);
+  Curl_altsvc_cleanup(&asi);
   return unitfail;
 }
 UNITTEST_STOP