]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
urlapi: make curl_url_set(url, CURLUPART_URL, NULL, 0) clear all parts
authorPierrick Charron <pierrick@webstart.fr>
Sun, 19 Jun 2022 14:54:04 +0000 (10:54 -0400)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 20 Jun 2022 06:15:51 +0000 (08:15 +0200)
As per the documentation :

> Setting a part to a NULL pointer will effectively remove that
> part's contents from the CURLU handle.

But currently clearing CURLUPART_URL does nothing and returns
CURLUE_OK. This change will clear all parts of the URL at once.

Closes #9028

lib/urlapi.c
tests/libtest/lib1560.c

index a58b329063e23ae2fe51386251bc0c555e07791c..dee4b5aa0983bba3a19028b941c360a8d31d9b7d 100644 (file)
@@ -1518,6 +1518,10 @@ CURLUcode curl_url_set(CURLU *u, CURLUPart what,
     if(storep && *storep) {
       Curl_safefree(*storep);
     }
+    else if(!storep) {
+      free_urlhandle(u);
+      memset(u, 0, sizeof(struct Curl_URL));
+    }
     return CURLUE_OK;
   }
 
index 2dc3837e35481cb255cbc4019d53fdad36a1e396..e5dd6889663c86ea2424eb066015052cfc7f4edd 100644 (file)
@@ -130,6 +130,13 @@ struct querycase {
   CURLUcode ucode;
 };
 
+struct clearurlcase {
+  CURLUPart part;
+  const char *in;
+  const char *out;
+  CURLUcode ucode;
+};
+
 static const struct testcase get_parts_list[] ={
   {"https://user:password@example.net/get?this=and#but frag then", "",
    CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_FRAGMENT},
@@ -1235,6 +1242,54 @@ static int get_nothing(void)
   return 0;
 }
 
+static const struct clearurlcase clear_url_list[] ={
+  {CURLUPART_SCHEME, "http", NULL, CURLUE_NO_SCHEME},
+  {CURLUPART_USER, "user", NULL, CURLUE_NO_USER},
+  {CURLUPART_PASSWORD, "password", NULL, CURLUE_NO_PASSWORD},
+  {CURLUPART_OPTIONS, "options", NULL, CURLUE_NO_OPTIONS},
+  {CURLUPART_HOST, "host", NULL, CURLUE_NO_HOST},
+  {CURLUPART_ZONEID, "eth0", NULL, CURLUE_NO_ZONEID},
+  {CURLUPART_PORT, "1234", NULL, CURLUE_NO_PORT},
+  {CURLUPART_PATH, "/hello", "/", CURLUE_OK},
+  {CURLUPART_QUERY, "a=b", NULL, CURLUE_NO_QUERY},
+  {CURLUPART_FRAGMENT, "anchor", NULL, CURLUE_NO_FRAGMENT},
+  {0, NULL, NULL, CURLUE_OK},
+};
+
+static int clear_url(void)
+{
+  CURLU *u = curl_url();
+  int i, error = 0;
+  if(u) {
+    char *p = NULL;
+    CURLUcode rc;
+
+    for(i = 0; clear_url_list[i].in && !error; i++) {
+      rc = curl_url_set(u, clear_url_list[i].part, clear_url_list[i].in, 0);
+      if(rc != CURLUE_OK)
+        fprintf(stderr, "unexpected return code line %u\n", __LINE__);
+
+      rc = curl_url_set(u, CURLUPART_URL, NULL, 0);
+      if(rc != CURLUE_OK)
+        fprintf(stderr, "unexpected return code line %u\n", __LINE__);
+
+      rc = curl_url_get(u, clear_url_list[i].part, &p, 0);
+      if(rc != clear_url_list[i].ucode || (clear_url_list[i].out &&
+         0 != strcmp(p, clear_url_list[i].out))) {
+
+        fprintf(stderr, "unexpected return code line %u\n", __LINE__);
+        error++;
+      }
+      if(rc == CURLUE_OK)
+        curl_free(p);
+    }
+  }
+
+  curl_url_cleanup(u);
+
+  return error;
+}
+
 int test(char *URL)
 {
   (void)URL; /* not used */
@@ -1260,6 +1315,9 @@ int test(char *URL)
   if(get_parts())
     return 4;
 
+  if(clear_url())
+    return 8;
+
   printf("success\n");
   return 0;
 }