Skip to content

Commit

Permalink
trurl: normalize the fragment
Browse files Browse the repository at this point in the history
Update a test, add a test
  • Loading branch information
bagder committed Sep 12, 2024
1 parent bb9b7db commit 63ee805
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 63ee805

Please sign in to comment.