From 445699c1a3f673222093d86f5783378d2c3cd5a4 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 26 Aug 2024 16:11:20 +0200 Subject: [PATCH] trurl: make --replace URL encode the provided data argument Makes it work like --append query= already does. - update man page - update test cases Fixes #321 --- tests.json | 6 +++--- trurl.1 | 3 +++ trurl.c | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/tests.json b/tests.json index a7480f17..b2afb15f 100644 --- a/tests.json +++ b/tests.json @@ -2366,17 +2366,17 @@ }, "expected": { "stdout": [{ - "url": "http://example.org/?quest=%00", + "url": "http://example.org/?quest=%2500", "parts": { "scheme": "http", "host": "example.org", "path": "/", - "query": "quest=%00" + "query": "quest=%2500" }, "params": [ { "key": "quest", - "value": "\u0000" + "value": "%00" } ] }], diff --git a/trurl.1 b/trurl.1 index d3309ccb..ff49b2cf 100644 --- a/trurl.1 +++ b/trurl.1 @@ -211,6 +211,9 @@ Replaces a URL query. data can either take the form of a single value, or as a key/value pair in the shape \fIfoo=bar\fP. If replace is called on an item that isn't in the list of queries trurl ignores that item. + +trurl URL encodes both sides of the '=' character in the given input data +argument. .IP "--force-replace [data]" Works the same as \fI--replace\fP, but trurl appends a missing query string if it is not in the query list already. diff --git a/trurl.c b/trurl.c index e1ff9111..516f5e5e 100644 --- a/trurl.c +++ b/trurl.c @@ -472,9 +472,8 @@ static void pathadd(struct option *o, const char *path) } } -static void queryadd(struct option *o, const char *query) +static char *encodeassign(const char *query) { - struct curl_slist *n; char *p = strchr(query, '='); char *urle; if(p) { @@ -487,11 +486,16 @@ static void queryadd(struct option *o, const char *query) } else urle = curl_easy_escape(NULL, query, 0); + return urle; +} + +static void queryadd(struct option *o, const char *query) +{ + char *urle = encodeassign(query); if(urle) { - n = curl_slist_append(o->append_query, urle); - if(n) { + struct curl_slist *n = curl_slist_append(o->append_query, urle); + if(n) o->append_query = n; - } curl_free(urle); } } @@ -537,14 +541,17 @@ static void trimadd(struct option *o, static void replaceadd(struct option *o, const char *replace_list) /* [component]=[data] */ { - struct curl_slist *n = NULL; - if(replace_list) - n = curl_slist_append(o->replace_list, replace_list); + if(replace_list) { + char *urle = encodeassign(replace_list); + if(urle) { + struct curl_slist *n = curl_slist_append(o->replace_list, urle); + if(n) + o->replace_list = n; + curl_free(urle); + } + } else errorf(o, ERROR_REPL, "No data passed to replace component"); - - if(n) - o->replace_list = n; } static bool longarg(const char *flag, const char *check)