From 63ee8058ff5f9cacae3fb60ca026ea2d752c4251 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 12 Sep 2024 17:28:05 +0200 Subject: [PATCH] trurl: normalize the fragment Update a test, add a test --- tests.json | 15 ++++++++++++++- trurl.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests.json b/tests.json index 83ea422a..d871d178 100644 --- a/tests.json +++ b/tests.json @@ -461,7 +461,20 @@ ] }, "expected": { - "stdout": "%2e%61%13\n", + "stdout": ".a%13\n", + "stderr": "", + "returncode": 0 + } + }, + { + "input": { + "arguments": [ + "--url", + "https://example.com/#%2e%61%13%Fa" + ] + }, + "expected": { + "stdout": "https://example.com/#.a%13%fa\n", "stderr": "", "returncode": 0 } diff --git a/trurl.c b/trurl.c index d13e405e..314374dc 100644 --- a/trurl.c +++ b/trurl.c @@ -1890,6 +1890,37 @@ static void singleurl(struct option *o, errorf(o, ERROR_ITER, "out of memory"); } curl_free(opath); + + { + char *frag; + size_t fraglen = 0; + (void)curl_url_get(uh, CURLUPART_FRAGMENT, &frag, 0); + + if(frag) + fraglen = strlen(frag); + + if(fraglen) { + int olen; + char *ufrag; + /* First URL decode the fragment */ + char *rawfrag = curl_easy_unescape(NULL, frag, (int)fraglen, &olen); + if(!rawfrag) + errorf(o, ERROR_ITER, "out of memory"); + + /* Then URL encode it again */ + ufrag = curl_easy_escape(NULL, rawfrag, olen); + curl_free(rawfrag); + if(!ufrag) + errorf(o, ERROR_ITER, "out of memory"); + + if(strcmp(frag, ufrag)) { + /* changed, store the new one */ + (void)curl_url_set(uh, CURLUPART_FRAGMENT, ufrag, 0); + } + curl_free(ufrag); + } + curl_free(frag); + } } query_is_modified |= extractqpairs(uh, o);