From 1e83ccc488f5d8556f30ca7aa68a69a761b502a4 Mon Sep 17 00:00:00 2001 From: Emanuele Torre Date: Thu, 22 Jun 2023 09:41:08 +0200 Subject: [PATCH] trurl: run --trim query before --append query Before this patch there was not a way to add a query parameter to a URL, removing the previous value if the parameter was already present in the base URL. I tried to use: x () { trurl --verify --trim query=foo --append "query=foo=$2" -- "$1" } But that didn't work since --trim query runs after --append query, so the new value value added with --append also gets trimmed: $ x 'https://example.org/?foo=hello&baz=boo' 'howdy' https://example.org/?baz=boo This patch makes trurl run --trim before --append so that using --trim query=foo --append query=foo=bar works to set foo to bar overwriting previous values of foo. $ x 'https://example.org/?foo=hello&baz=boo' 'howdy' https://example.org/?baz=boo&foo=howdy I added a test for this, and I also added a test that tests that --sort-query and --append query work together, since I noticed it was missing. --- tests.json | 33 +++++++++++++++++++++++++++++++++ trurl.c | 6 +++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests.json b/tests.json index cc1339e1..ed17cefa 100644 --- a/tests.json +++ b/tests.json @@ -1981,5 +1981,38 @@ "returncode": 0, "stderr": "" } + }, + { + "input": { + "arguments": [ + "-a", + "query=c=moo", + "--sort-query", + "https://example.org/foo?x=hi#rye" + ] + }, + "expected": { + "stdout": "https://example.org/foo?c=moo&x=hi#rye\n", + "returncode": 0, + "stderr": "" + } + }, + { + "input": { + "arguments": [ + "--trim", + "query=a", + "-a", + "query=a=ciao", + "-a", + "query=b=salve", + "https://example.org/foo?a=hi&b=hello&x=y" + ] + }, + "expected": { + "stdout": "https://example.org/foo?b=hello&x=y&a=ciao&b=salve\n", + "returncode": 0, + "stderr": "" + } } ] diff --git a/trurl.c b/trurl.c index ec5cbc87..2712c598 100644 --- a/trurl.c +++ b/trurl.c @@ -1167,14 +1167,14 @@ static void singleurl(struct option *o, extractqpairs(uh, o); + /* trim parts */ + trim(o); + /* append query segments */ for(p = o->append_query; p; p = p->next) { addqpair(p->data, strlen(p->data)); } - /* trim parts */ - trim(o); - sortquery(o); /* put the query back */