Skip to content

Commit

Permalink
trurl: run --trim query before --append query
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emanuele6 authored and bagder committed Aug 7, 2023
1 parent 9cac0f5 commit 1e83ccc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
33 changes: 33 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
}
}
]
6 changes: 3 additions & 3 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 1e83ccc

Please sign in to comment.